2016-02-12 88 views
-1

您好我是theano的新手,我需要[N x M * 4]点[M x P],所以我想要第二个矩阵重复4次。如何复制theano中的矩阵

喜欢的东西

import numpy as np 
import theano 
import theano.tensor as T 

x = T.fmatrix("x") 

z = x.repeat(3, axis=0) 

foo = theano.function([x], z) 

a = np.array([[1, 2], [3, 4]]).astype("float32") 

c = foo(a) 

print c 

[[ 1. 2.] 
[ 1. 2.] 
[ 1. 2.] 
[ 3. 4.] 
[ 3. 4.] 
[ 3. 4.]] 

但在我的情况下,我想

[[ 1. 2.] 
[ 3. 4.] 
[ 1. 2.] 
[ 3. 4.] 
[ 1. 2.] 
[ 3. 4.]] 

我怎么能这样做呢?

回答

1

你想theano.tensor.tile

z2 = T.tile(x, (3, 1)) # repeat 3x in the first dimension, 1x in the second 
bar = theano.function([x], z2) 

print(bar(a)) 
# [[ 1. 2.] 
# [ 3. 4.] 
# [ 1. 2.] 
# [ 3. 4.] 
# [ 1. 2.] 
# [ 3. 4.]] 
+0

感谢的人的工作!你是天才! –

-3

您可能做到这一点使用numpy tile功能:

>>> np.tile(np.array([[1, 2], [3, 4]]), (2, 1)) 
array([[1, 2], 
     [3, 4], 
     [1, 2], 
     [3, 4]]) 

其中(2,1)是沿每个轴的重复次数。它应该是(4,1)你的情况

+2

这将不是一个符号变量 –