use*_*665 3 numpy flatten theano conv-neural-network
在[ http://deeplearning.net/tutorial/lenet.html#lenet]中它说:
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)
Run Code Online (Sandbox Code Playgroud)
当我在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])
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
例如,如果我们用展平(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()
Run Code Online (Sandbox Code Playgroud)
版画
(2L, 3L, 4L) (2L, 12L)
Run Code Online (Sandbox Code Playgroud)