译(六十)-PyTorch得到整型列表类型的tensor形状

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

如何在PyTorch中得到整型列表类型的tensor形状

  • patapouf_ai asked:

    • Numpy 里,V.shape 得到整型元组类型的 V 的维度。
    • TensorFlow 里,V.get_shape().as_list() 得到整型列表类型的 V 的维度。
    • 而 PyTorch 里,V.size() 得到对象,我怎么转换成整型?
  • Answers:

    • alvas – vote: 88

    • PyTorch 1.0 (也许以上版本也行):

    • >>> import torch
      >>> var = torch.tensor([[1,0], [0,1]])
      #
      # Using .size function, returns a torch.Size object.
      >>> var.size()
      torch.Size([2, 2])
      >>> type(var.size())
      
      #
      # Similarly, using .shape
      >>> var.shape
      torch.Size([2, 2])
      >>> type(var.shape)
      
    • 可以直接类型转换 torch.Size 对象为 Python 列表:

    • >>> list(var.size())
      [2, 2]
      >>> type(list(var.size()))
      
    • PyTorch 0.3 及 0.4:

    • 只需 list(var.size()),如:

    • >>> import torch
      >>> from torch.autograd import Variable
      >>> from torch import IntTensor
      >>> var = Variable(IntTensor([[1,0],[0,1]]))
      #
      >>> var
      Variable containing:
      1  0
      0  1
      [torch.IntTensor of size 2x2]
      #
      >>> var.size()
      torch.Size([2, 2])
      #
      >>> list(var.size())
      [2, 2]
    • kmario23 – vote: 13

    • 如果你更喜欢 NumPy 系的语法,用 tensor.shape

    • In [3]: ar = torch.rand(3, 3)
      #
      In [4]: ar.shape
      Out[4]: torch.Size([3, 3])
      #
      # method-1
      In [7]: list(ar.shape)
      Out[7]: [3, 3]
      #
      # method-2
      In [8]: [*ar.shape]
      Out[8]: [3, 3]
      #
      # method-3
      In [9]: [*ar.size()]
      Out[9]: [3, 3]
    • tensor.shapetensor.size() 的别名,但 tensor.shape 是属性而 tensor.size() 是方法。

    • iacob – vote: 2

    • torch.Size 对象是 tuple 的子类,它继承了一些属性,如索引:

    • v = torch.tensor([[1,2], [3,4]])
      v.shape[0]
      >>> 2
    • 注意,这就是 int 类型。

    • 如果想要列表的话,就像其它可迭代对象一样使用 list

    • list(v.shape)

PyTorch: How to get the shape of a Tensor as a list of int

  • patapouf_ai asked:

    • In numpy, V.shape gives a tuple of ints of dimensions of V.
      Numpy 里,V.shape 得到整型元组类型的 V 的维度。
    • In tensorflow V.get_shape().as_list() gives a list of integers of the dimensions of V.
      TensorFlow 里,V.get_shape().as_list() 得到整型列表类型的 V 的维度。
    • In pytorch, V.size() gives a size object, but how do I convert it to ints?
      而 PyTorch 里,V.size() 得到对象,我怎么转换成整型?
  • Answers:

    • alvas – vote: 88

    • For PyTorch v1.0 and possibly above:
      PyTorch 1.0 (也许以上版本也行):

    • >>> import torch
      >>> var = torch.tensor([[1,0], [0,1]])
      #
      # Using .size function, returns a torch.Size object.
      >>> var.size()
      torch.Size([2, 2])
      >>> type(var.size())
      
      #
      # Similarly, using .shape
      >>> var.shape
      torch.Size([2, 2])
      >>> type(var.shape)
      
    • You can cast any torch.Size object to a native Python list:
      可以直接类型转换 torch.Size 对象为 Python 列表:

    • >>> list(var.size())
      [2, 2]
      >>> type(list(var.size()))
      
    • In PyTorch v0.3 and 0.4:
      PyTorch 0.3 及 0.4:

    • Simply list(var.size()), e.g.:
      只需 list(var.size()),如:

    • >>> import torch
      >>> from torch.autograd import Variable
      >>> from torch import IntTensor
      >>> var = Variable(IntTensor([[1,0],[0,1]]))
      #
      >>> var
      Variable containing:
      1  0
      0  1
      [torch.IntTensor of size 2x2]
      #
      >>> var.size()
      torch.Size([2, 2])
      #
      >>> list(var.size())
      [2, 2]
    • kmario23 – vote: 13

    • If you\’re a fan of NumPyish syntax, then there\’s tensor.shape.
      如果你更喜欢 NumPy 系的语法,用 tensor.shape

You may also like...

1 Response

  1. Aegean College说道:

    Fabulous, what a web site it is! This weblog presents helpful facts to us, keep it up.

发表评论

您的电子邮箱地址不会被公开。

11 − = 10