2014-10-26 44 views
9

考慮numpy的廣播的以下示例:Theano廣播不同到numpy的的

import numpy as np 
import theano 
from theano import tensor as T 

xval = np.array([[1, 2, 3], [4, 5, 6]]) 
bval = np.array([[10, 20, 30]]) 
print xval + bval 

如所預期的,所述載體bval被添加到基質xval的各行並且輸出是:

[[11 22 33] 
[14 25 36]] 

試圖在gano版本的theano中複製相同的行爲:

x = T.dmatrix('x') 
b = theano.shared(bval) 
z = x + b 
f = theano.function([x], z) 

print f(xval) 

我得到以下錯誤:

ValueError: Input dimension mis-match. (input[0].shape[0] = 2, input[1].shape[0] = 1) 
Apply node that caused the error: Elemwise{add,no_inplace}(x, <TensorType(int64, matrix)>) 
Inputs types: [TensorType(float64, matrix), TensorType(int64, matrix)] 
Inputs shapes: [(2, 3), (1, 3)] 
Inputs strides: [(24, 8), (24, 8)] 
Inputs scalar values: ['not scalar', 'not scalar'] 

我明白Tensor對象,如xbroadcastable屬性,但我不能找到一種方法來1)正確設置此爲shared對象或2)正確地擁有它推斷。如何在theano中重新實現numpy的行爲?

回答

10

Theano需要在編譯之前在圖形中聲明所有可廣播的維度。 NumPy使用運行時間形狀信息。

默認情況下,所有共享變量dimsions不可廣播,因爲它們的形狀可能會更改。

要與您在您的示例需要broadcastable維上創建共享變量:

b = theano.shared(bval, broadcastable=(True,False)) 

我會將此信息添加到文檔。

+0

只是提醒:不在文檔中:) – 2015-01-14 19:23:41

+1

它在那裏:http://deeplearning.net/software/theano/library/compile/shared.html#theano.compile.sharedvalue.shared您在哪裏尋找它?也許我們需要在另一個地方添加它。 – nouiz 2015-01-18 16:05:23

+0

啊對不起,它在克瓦格斯那裏......沒有看到它。無論如何,我認爲這是一個重要的功能,可能會保證更多的可見性? – 2015-01-18 16:58:27