2014-10-31 48 views
2

我要动态地创建面具矩阵,例如,在numpy更改元素的值的子集在theano矩阵

mask = numpy.zeros((5,5)) 
row = numpy.arange(5) 
col = [0, 2, 3, 0, 1] 
mask[row, col] += 1 # that is setting some values to `1` 

这是我在theano试过,

mask = tensor.zeros((5,5)) 
row = tensor.ivector('row') 
col = tensor.ivector('col') 
mask = tensor.set_subtensor(mask[row, col], 1) 

以上theano代码失败,并显示错误消息:not supported。 还有其他方法吗?

+0

对我的作品在'0.6.0'(做'进口theano.tensor为tensor'和复制和粘贴代码)。你到底什么时候会收到错误信息? – eickenberg 2014-10-31 10:14:20

+0

通常'set_subtensor'实际上用作更新。也许你在构建函数时遇到错误?在这种情况下,您能否发布导致此问题的确切代码(包括导入)以及确切的错误消息? – eickenberg 2014-10-31 10:15:52

回答

4

这对我的作品0.6.0。我使用你的代码并从它创建一个函数来检查输出。尝试复制和粘贴这样的:

import theano 
from theano import tensor 

mask = tensor.zeros((5,5)) 
row = tensor.ivector('row') 
col = tensor.ivector('col') 
mask = tensor.set_subtensor(mask[row, col], 1) 

f = theano.function([row, col], mask) 

print f(np.array([0, 1, 2]).astype(np.int32), np.array([1, 2, 3]).astype(np.int32)) 

这就产生

array([[ 0., 1., 0., 0., 0.], 
     [ 0., 0., 1., 0., 0.], 
     [ 0., 0., 0., 1., 0.], 
     [ 0., 0., 0., 0., 0.], 
     [ 0., 0., 0., 0., 0.]])