假设我有一个ashape 的张量[B,D],并且有一个I包含 shape 索引的列表[B]。[M,D]现在我想使用M > B列表中的索引将张量扩展到形状。请注意,索引属于范围[0,M]。具体来说,I是将行从张量映射a到另一个具有较大维度值的张量0。此功能与功能相反tf.gather()。有人可以提出解决方案吗?谢谢
tf.scatter_nd是 的相反tf.gather_nd。让我们看看一个往返示例:
((5,4,1,2,3))的元素外,所有元素均为零,它们分别为和。[1,2,0,0][3,0,0,1][16, 12, 11][3,0,0,1]tf.scatter_ndtf.gather_nd,以获得两个原始向量,即[16, 12, 11]和[3,0,0,1]。updates = [[16, 12, 11],[18, 40, 37]]
indices = [[1,2,0,0], [3,0,0,1]]
shape = (5,4,1,2,3)
# first step - scatter
scat_tensor = tf.scatter_nd(indices=indices, updates=updates, shape=shape)
print(f"verification: expected {updates[0]}, got {scat_tensor[1,2,0,0]}")
# now the reverse step
reconstructed_updates = tf.gather_nd(scat_tensor, indices)
print(f"verification: expected {updates }, got {reconstructed_updates }")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1457 次 |
| 最近记录: |