最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - When using numpy advanced indexing with a torch tensor, it sometimes will squeeze dimensions - Stack Overflow

programmeradmin0浏览0评论

In this code:

a_numpy = np.array([1,2,3])
a_torch = torch.tensor([1,2,3])
index_torch = torch.tensor([[0]])
index_np = np.array([[0]])
print(f"{a_numpy[index_torch]=}")
print(f"{a_numpy[index_np]=}")
print(f"{a_torch[index_torch]=}")
print(f"{a_torch[index_np]=}")

it prints:

a_numpy[index_torch]=np.int64(1)
a_numpy[index_np]=array([[1]])
a_torch[index_torch]=tensor([[1]])
a_torch[index_np]=tensor([[1]])

notice the odd one out a_numpy[index_torch]=np.int64(1). Why does this happen? Is this a bug?

numpy version: 2.0.2 torch version: 2.5.1

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论