numpy不支持只展平一些尺寸,但Theano确实如此.
所以如果a
是一个numpy数组,a.flatten(2)
没有任何意义.它运行没有错误,但只是因为2
它作为order
参数传递,似乎导致numpy坚持默认顺序C
.
Theano flatten
确实支持轴规格.文档解释了它的工作原理.
Parameters: x (any TensorVariable (or compatible)) – variable to be flattened outdim (int) – the number of dimensions in the returned variable Return type: variable with same dtype as x and outdim dimensions Returns: variable with the same shape as x in the leading outdim-1 dimensions, but with all remaining dimensions of x collapsed into the last dimension.
例如,如果我们用展平(x,outdim = 2)展平形状(2,3,4,5)的张量,那么我们将具有相同的(2-1 = 1)前导尺寸(2,),其余尺寸已折叠.因此,此示例中的输出将具有形状(2,60).
一个简单的Theano演示:
import numpy import theano import theano.tensor as tt def compile(): x = tt.tensor3() return theano.function([x], x.flatten(2)) def main(): a = numpy.arange(2 * 3 * 4).reshape((2, 3, 4)) f = compile() print a.shape, f(a).shape main()
版画
(2L, 3L, 4L) (2L, 12L)