维度计算
import torch
x = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(f"x shape: {x.shape}, shape 0: {x.shape[0]}, shape 1: {x.shape[1]}")
print(f"x size: {x.size()}, size 0: {x.size(0)}, size 1: {x.size(1)}")
print(f"x dim: {x.dim()}, len(size): {len(x.size())}, len(shape): {len(x.shape)}")
x shape: torch.Size([2, 3]), shape 0: 2, shape 1: 3
x size: torch.Size([2, 3]), size 0: 2, size 1: 3
x dim: 2, len(size): 2, len(shape): 2
关于 shape 和 size() 的区别, 二者是一样的,区别在于 shape 是为了和numpy对应的,另外 在获取不同的维度的时候不太一样. dim 是获取实际的维度,相当于 len(x.shape)
或者 len(x.size())
网友评论