pytorch中的shape属性

torch.shape 和 torch.size() 输出结果相同。

In:

import torch

x = torch.tensor([[0, 2], [3, 4], [9, 8]])
print('xx1.shape', x.shape)
#xx1.shape torch.Size([3, 2])
#数组x有两个维度,第1个维度长度为3,代表x有三个子数组
#				 第2个维度长度为2,代表每个子数组有2个元素

x = torch.tensor([[[0, 2], [0, 8], [2, 7]], [[2, 5], [9, 3], [7, 3]]])
print('tensor', x)
print('xx2.shape', x.shape)
'''
tensor tensor([[[0, 2],
         [0, 8],
         [2, 7]],

        [[2, 5],
         [9, 3],
         [7, 3]]])
xx2.shape torch.Size([2, 3, 2])
'''
#数组x有3个维度,第1个维度长度为2,代表x有2个子数组
#				 第2个维度长度为3,代表每个子数组又有3个子数组
#				 第3个维度长度为2,代表每个子数组有2个元素

x = torch.tensor([0, 1, 2, 4, 5, 6, 2])
print('xx3.shape', x.shape)
#xx3.shape torch.Size([7])

综上所述,shape即为数组的每个维度的长度

你可能感兴趣的:(pytorch)