在Theano矩阵中改变子集元素的值
我想动态创建一个掩码矩阵,比如在numpy
里。
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
代码出错了,提示信息是:不支持
。有没有其他的方法呢?
1 个回答
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.]])