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

后端 未结 3 1054
囚心锁ツ
囚心锁ツ 2021-02-01 13:51

In numpy, V.shape gives a tuple of ints of dimensions of V.

In tensorflow V.get_shape().as_list() gives a list of integers of the dimensions of

3条回答
  •  逝去的感伤
    2021-02-01 14:39

    If you're a fan of NumPyish syntax, then there's 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]
    

    P.S.: Note that tensor.shape is an alias to tensor.size(), though tensor.shape is an attribute of the tensor in question whereas tensor.size() is a function.

提交回复
热议问题