发布时间:2023-02-27 18:00
其次自己补充一下,请看如下代码:
>>>import torch
>>>a = torch.tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
>>> print(a.size())
torch.Size([2, 2, 2])
>>> print(a)
tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
>>> b = a[:,:,1]
>>> print(b)
tensor([[2, 4],
[6, 8]])
>>> b = a[:,:,0:1]
>>> print(b)
tensor([[[1],
[3]],
[[5],
[7]]])
>>> b = a[:,1,:]
>>> print(b)
tensor([[3, 4],
[7, 8]])
如果a的维度是(2,2,2), 如果b=a[:, :, 1],此时b的维度是(2, 2);如果b=a[:, :, 0:1],此时b的维度是(2, 2, 1);可以看出来如果某一个维度是一个确切的一维数字,那么该维度就会消失。