Numpy 关于Theano中展平函数的澄清

Numpy 关于Theano中展平函数的澄清,numpy,theano,flatten,conv-neural-network,Numpy,Theano,Flatten,Conv Neural Network,在[它说: 当我在numpy 3d阵列上使用展平函数时,我得到了一个一维阵列。但这里它说我得到了一个矩阵。展平(2)在theano中是如何工作的 numpy上的类似示例生成1D阵列: a= array([[[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24]

在[它说:

当我在numpy 3d阵列上使用展平函数时,我得到了一个一维阵列。但这里它说我得到了一个矩阵。展平(2)在theano中是如何工作的

numpy上的类似示例生成1D阵列:

     a= array([[[ 1,  2,  3],
    [ 4,  5,  6],
    [ 7,  8,  9]],

   [[10, 11, 12],
    [13, 14, 15],
    [16, 17, 18]],

   [[19, 20, 21],
    [22, 23, 24],
    [25, 26, 27]]])

   a.flatten(2)=array([ 1, 10, 19,  4, 13, 22,  7, 16, 25,  2, 11, 20,  5, 14, 23,  8, 17,
   26,  3, 12, 21,  6, 15, 24,  9, 18, 27])

numpy不支持仅展平某些维度,但Theano支持

因此,如果
a
是一个numpy数组,
a.flatten(2)
没有任何意义。它运行时没有错误,只是因为
2
作为
order
参数传递,这似乎导致numpy坚持默认的
C
顺序

Theano的
flatten
确实支持axis规范。解释了它的工作原理

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.
例如,如果我们将形状为(2,3,4,5)的张量用 展平(x,outdim=2),然后我们将有相同的(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)

代码给出了错误。我的ur代码版本(对于像我这样的新手):
import numpy as np import theano import theano.tensor as tt from numpy import dtype x=tt.tensor4()f=theano.function([x],x.flatte(2))a=np.asarray(np.arange(2*3*4*5)。重塑((2,3,4,5)),dtype=theano.config.floatX)打印f(a)
错误是什么?如果您已经搜索过但无法找到现有答案,则最好开始新问题。
(2L, 3L, 4L) (2L, 12L)