pytorch中的tensor的shape如何理解
首先,对于整数来说,如果要写一个大小为[4,3,2]的3D tensor,那么首先tensor需要一个括号,并在里面再写4个括号
torch = torch.tensor([ [ ], [ ], [ ], [ ] ])
再在四个括号里面,分别加上三个括号
torch = torch.tensor([ [],[],[] ],[ [],[],[] ],[ [],[],[] ],[ [],[],[] ])
然后三个括号内分别有两个数字
torch = torch.tensor([ [ [1,2],[1,3],[1,4] ],
[ [2,3],[3,4],[2,4] ],
[ [1,3],[2,4],[3,3] ],
[ [2,3],[2,4],[4,4] ] ])
print(torch.shape)
输出的结果即为[4,3,2]。
同理,如果要写一个1D tenser,shape为[4]
torch = torch.tensor([1,2,3,4])
写一个2D tensor, shape为[4,2]
torch = torch.tensor([1,2],[1,3],[1,4],[2,3])
本文详细解释了PyTorch中Tensor的shape属性,通过实例展示了如何创建不同维度的tensor,包括1D、2D和3D tensor,并阐述了如何通过torch.shape获取其尺寸信息。通过对形状的理解,读者能更好地掌握PyTorch中数据的操作和处理。
1043

被折叠的 条评论
为什么被折叠?



