Python 更改元素的子集';无矩阵中的值
我想动态创建一个掩码矩阵,例如,在Python 更改元素的子集';无矩阵中的值,python,theano,Python,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('co
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
代码失败,错误消息为:不受支持
。
还有其他方法吗?这在
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.]])
在
0.6.0
上为我工作(是否将ano.tensor导入为tensor
并复制和粘贴了代码)。您什么时候会收到错误消息?通常set\u subsensor
实际用作更新。也许你在构造函数时遇到了错误?在这种情况下,您能否发布导致此问题的确切代码(包括导入),以及确切的错误消息?