【问题标题】:How to correctly access elements in a 3D-Pytorch-Tensor?如何正确访问 3D-Pytorch-Tensor 中的元素?
【发布时间】:2019-09-22 17:22:30
【问题描述】:

我正在尝试访问 3D-Pytorch-Tensor 中的多个元素,但返回的元素数量是错误的。

这是我的代码:

import torch

a = torch.FloatTensor(4,3,2)
print("a = {}".format(a))
print("a[:][:][0] = {}".format(a[:][:][0]))

这是输出:

a = tensor([[[-4.8569e+36,  3.0760e-41],
         [ 2.7953e+20,  1.6928e+22],
         [ 3.1692e-40,  7.2945e-15]],

        [[ 2.5011e+24,  1.3173e-39],
         [ 1.7229e-07,  4.1262e-08],
         [ 4.1490e-08,  6.4103e-10]],

        [[ 3.1728e-40,  5.8258e-40],
         [ 2.8776e+32,  6.7805e-10],
         [ 3.1764e-40,  5.4229e+08]],

        [[ 7.2424e-37,  1.3697e+07],
         [-2.0362e-33,  1.8146e+11],
         [ 3.1836e-40,  1.9670e+34]]])
a[:][:][0] = tensor([[-4.8569e+36,  3.0760e-41],
        [ 2.7953e+20,  1.6928e+22],
        [ 3.1692e-40,  7.2945e-15]])

我希望是这样的:

a[:][:][0] = tensor([[-4.8569e+36,  2.7953e+20, 3.1692e-40, 
          2.5011e+24, 1.7229e-07, 4.1490e-08, 
          3.1728e-40, 2.8776e+32, 3.1764e-40, 
          7.2424e-37, -2.0362e-33, 3.1836e-40]])

谁能向我解释我是如何得出这个结果的? 非常感谢您!

我得到了完全预期的执行结果:

for i in range(4):
   for j in range(3):
      print("a[{}][{}][0] = {}".format(i,j, a[i][j][0]))

【问题讨论】:

  • 谢谢,但这不起作用。无论出于何种原因a[0][:][:]a[:][0][:]a[:][:][0] 甚至a[:][:][:][:][0] 输出相同?!?!这些东西是如何实现的?!?!
  • 我已经添加了详细的解释和注释。那应该澄清:)

标签: python-3.x vectorization pytorch tensor tensor-indexing


【解决方案1】:

简答,你需要使用a[:, :, 0]

更多解释: 当您执行 a[:] 时,它会返回 a 本身。所以a[:][:][0]a[0] 相同,它将为您提供位于第一个轴的第 0 位的元素(因此大小为 (3,2))。您想要的是来自最后一个轴的第 0 个位置的元素,您需要对其执行 a[:, :, 0]

【讨论】:

    【解决方案2】:

    以下是索引您要查找的元素的一些解释和正确方法:

    # input tensor to work with
    In [11]: a = torch.arange(4*3*2).reshape(4,3,2)
    
    # check its shape
    In [12]: a.shape
    Out[12]: torch.Size([4, 3, 2])
    
    # inspect/annotate the tensor
    In [13]: a
    Out[13]:            # (    4      ,  3    ,    2      ) <= shape
    tensor([[[ 0,  1],    | # block-0 | row-0 | col-0 col-1
             [ 2,  3],    | # block-0 | row-1 | col-0 col-1
             [ 4,  5]],   | # block-0 | row-2 | col-0 col-1
    
            [[ 6,  7],    | # block-1 | row-0 | col-0 col-1
             [ 8,  9],    | # block-1 | row-1 | col-0 col-1
             [10, 11]],   | # block-1 | row-2 | col-0 col-1
    
            [[12, 13],    | # block-2 | row-0 | col-0 col-1
             [14, 15],    | # block-2 | row-1 | col-0 col-1
             [16, 17]],   | # block-2 | row-2 | col-0 col-1
    
            [[18, 19],    | # block-3 | row-0 | col-0 col-1
             [20, 21],    | # block-3 | row-1 | col-0 col-1
             [22, 23]]])  | # block-3 | row-2 | col-0 col-1
    
    
    # slice out what we need; (in all blocks, all rows, column-0)
    In [14]: a[:, :, 0]
    Out[14]: 
    tensor([[ 0,  2,  4],
            [ 6,  8, 10],
            [12, 14, 16],
            [18, 20, 22]])
    

    解释/澄清

    张量的形状为[4, 3, 2],其中4 表示(块0,...块3)的数量。接下来,我们有3,它表示每个块中的行数。最后,我们有2,它代表每行中的列数。我们使用切片符号 a[:, :, 0] 对其进行切片。

    要访问 ,我们只需要一个索引(即 a[0],... a[3])。要访问特定 block 中的特定 row,我们需要两个索引(即 a[0, 1], ... a[3,2]) .要从特定 block 访问特定 row 的特定 column,我们需要三个索引(即 a[0, 1, 1]等,)


    我推测您的案例由于使用了torch.FloatTensor() 而引起了混淆。使用torch.FloatTensor() 的问题在于它会分配垃圾值或使用这些内存块的先前程序留下的值。这有时可能会令人费解,因为我们可能会在后续运行之间得到不一致的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-15
      • 2012-08-08
      • 2019-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多