记录pytorch的shape是怎么变化的,主要是unsquezze函数
代码如下:
a = torch.tensor([[[1,2,3,4],[0,0,0,0],[7,8,9,10]],[[1,4,9,16],[16,25,36,49],[49,64,81,100]]],dtype=torch.float)
len_q = a.size(1)
padding_mask = a.eq(0)
print('1,',padding_mask)
print('2,',padding_mask[:,:,0])
print('3,',padding_mask[:,:,0].unsqueeze(1))
print('4,',padding_mask[:,:,0].unsqueeze(1).expand(-1, len_q, -1))
结果
1, tensor([[[False, False, False, False],
[ True, True, True, True],
[False, False, False, False]],
[[False, False, False, False],
[False, False, False, False],
[False, False, False, False]]])
2, tensor([[False, True, False],
[False, False, False]])
3, tensor([[[False, True, False]],
[[False, False, False]]])
4, tensor([[[False, True, False],
[False, True, False],
[False, True, False]],
[[False, False, False],
[False, False, False],
[False, False, False]]])
查看维度变化
print(a.shape)
print(a.unsqueeze(0).shape)
print(a.unsqueeze(1).shape)
print(a.unsqueeze(2).shape)
print(a.unsqueeze(3).shape)
结果
torch.Size([2, 3, 4])
torch.Size([1, 2, 3, 4])
torch.Size([2, 1, 3, 4])
torch.Size([2, 3, 1, 4])
torch.Size([2, 3, 4, 1])