复制Pytorch中某些索引的张量元素

发布于 2025-02-02 12:38:08 字数 420 浏览 2 评论 0原文

所需的操作在精神上与torch.tensor.index_copy相似,但有些不同。

最好用一个例子来解释。

张量a具有我们将复制的原始值:

[10,20,30]

张量b具有a :

[0、1、0、1、2、1]

张量c的长度与b,包含<索引值<代码> a :

[10、20、10、20、30、20]

是从a c的好方法>和b在pytorch中,不使用循环?

The desired operation is similar in spirit to torch.Tensor.index_copy, but a little different.

It's best explained with an example.

Tensor A has original values that we will copy:

[10, 20, 30]

Tensor B has indices of A:

[0, 1, 0, 1, 2, 1]

Tensor C has same length as B, containing the indexed values of A:

[10, 20, 10, 20, 30, 20]

What's a good way to make C from A and B in PyTorch, without using loops?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坐在坟头思考人生 2025-02-09 12:38:08

您是否尝试过仅通过A索引?

In [1]: import torch
  
In [2]: a = torch.tensor([20,30,40])

In [3]: b = torch.tensor([0,1,2,1,1,2,0,0,1,2])

In [4]: a[b]
Out[4]: tensor([20, 30, 40, 30, 30, 40, 20, 20, 30, 40])

Have you tried just indexing by A?

In [1]: import torch
  
In [2]: a = torch.tensor([20,30,40])

In [3]: b = torch.tensor([0,1,2,1,1,2,0,0,1,2])

In [4]: a[b]
Out[4]: tensor([20, 30, 40, 30, 30, 40, 20, 20, 30, 40])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文