如何根据2-D张量的值将3-D张量设置为0
假设我有一个3-D张量 P
形状(B,N,D)
和2-D Tensor q
Q 形状(( b,n)
,其中 q
中的值小于 d
。我想在 p
中将一些值设置为 0
使用 Q
的索引:
例如,
P = torch.randn(2,3,6)
Q = torch.tensor([[0,3,4], [2,1,3]])
我如何设置 p [0 ,0,0] = 0; p [0,1,3] = 0; p [0,2,4] = 0; p [1,0,2] = 0; p [1,1,1] = 0; p [1,2,3] = 0
使用 q
并将其他值保留在 p
不变的中?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要做的是:
这是
torch.tensor.scatter
,其效果是在指定的位置上放置任意价值。我们首先需要输入和索引器具有匹配形状:
并将散点功能应用于
dim = 2
,并使用 hiddenvalue
参数。 。What you are looking to do is:
This is the perfect use case for
torch.Tensor.scatter
, which has the effect of placing an arbitrary value at designated positions.We first need the input and indexer to have matching shapes:
And apply the scatter function on
dim=2
, with the hiddenvalue
argument...