This will generate a matrix of shape (batch_size, nkerns[1] * 4 * 4),
# or (500, 50 * 4 * 4) = (500, 800) with the default values.
layer2_input = layer1.output.flatten(2)
当我在numpy 3d数组上使用flatten函数时,我得到了一维数组。但在这里它说我得到一个矩阵。 flatten(2)如何在theano工作?
关于numpy的类似示例生成一维数组:
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])
答案 0 :(得分:5)
numpy不支持只展平某些尺寸,但Theano确实如此。
因此,如果a
是一个numpy数组,a.flatten(2)
没有任何意义。它运行时没有错误,但只是因为2
作为order
参数传递,这似乎导致numpy坚持默认顺序C
。
Theano的flatten
支持轴规格。 The documentation解释了它的工作原理。
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)压平张量 flatten(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)