【发布时间】:2017-12-04 22:51:59
【问题描述】:
我想根据位置(索引)列表修改 3D 张量中的特定向量:
#indices: 1D vector of positions, indices.shape: (k)
mask = np.zeros(k, n, m)
for i in range(k):
mask[i][indices[i]] = 1
此蒙版将应用于另一个 3D 张量(相同形状),我想在其中保留特定向量,并将其余向量归零。
在 TensorFlow 中构建这种掩码的最佳方法是什么?我可以使用分配操作通过循环来做到这一点,但我想找到一个更优雅的解决方案。也许使用tf.scatter_nd?
编辑:示例:
>>> mask_before
array([[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]])
>>> indices
array([2, 1, 4])
>>> mask_after
array([[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 1., 1., 1., 1.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 1., 1., 1., 1.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]],
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 1., 1., 1., 1.]]])
【问题讨论】:
-
请提供样本数据集和您想要的数据集
-
@MaxU,请看一下
标签: python numpy tensorflow