【问题标题】:how to get specific elements in tensor (pytorch)如何获取张量中的特定元素(pytorch)
【发布时间】:2021-05-25 23:14:45
【问题描述】:

我有一个张量,例如: 输入:

a = torch.rand(2,3,5)

输出:

tensor([[[0.2764, 0.2209, 0.8007, 0.1246, 0.4302],
     [0.9716, 0.8063, 0.3904, 0.7574, 0.2392],
     [0.3366, 0.4209, 0.0527, 0.1328, 0.0441]],

    [[0.8166, 0.6519, 0.5450, 0.3072, 0.2716],
     [0.0583, 0.0613, 0.8984, 0.0110, 0.4744],
     [0.2269, 0.2693, 0.6447, 0.6078, 0.6148]]])

我怎样才能得到:

tensor([[[0.2764, 0.2209, 0.8007, 0.1246, 0.4302],
 [0.9716, 0.8063, 0.3904, 0.7574, 0.2392]],

[[0.8166, 0.6519, 0.5450, 0.3072, 0.2716],
 [0.2269, 0.2693, 0.6447, 0.6078, 0.6148]]])

我试过gather(),但是没用。

【问题讨论】:

  • 你能澄清输出的预期形状吗?为什么你认为gather() 是你想要的功能?

标签: numpy pytorch tensor


【解决方案1】:

试试这个。这应该可以工作

a[:,:-1, :]

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 2019-01-13
    • 1970-01-01
    • 2020-01-03
    • 2021-09-14
    • 2020-06-10
    • 2021-04-10
    • 2019-04-18
    • 2021-11-12
    相关资源
    最近更新 更多