【发布时间】:2017-11-21 21:02:34
【问题描述】:
我有一个 3d 张量,我需要在第二维中的某些位置保留向量,并将剩余向量归零。这些位置被指定为一维数组。我认为最好的方法是将张量与二进制掩码相乘。
这是一个简单的 Numpy 版本:
A.shape: (b, n, m)
indices.shape: (b)
mask = np.zeros(A.shape)
for i in range(b):
mask[i][indices[i]] = 1
result = A*mask
因此,对于 A 中的每个 nxm 矩阵,我需要保留由索引指定的行,并将其余行归零。
我正在尝试在 TensorFlow 中使用 tf.scatter_nd op 执行此操作,但我无法确定索引的正确形状:
shape = tf.constant([3,5,4])
A = tf.random_normal(shape)
indices = tf.constant([2,1,4]) #???
updates = tf.ones((3,4))
mask = tf.scatter_nd(indices, updates, shape)
result = A*mask
【问题讨论】:
标签: tensorflow