2019-06-28晚
cs224n-winter2019-assignment4
用pytorch实现seq2seq-attention NMT Model(神经机器翻译模型)
unsqueeze
a = torch.randn(5,4)
for i in range(-3,3):
b = torch.unsqueeze(a,dim=i)
print('{} _th size:{}'.format(i,b.size()))
---------------------
输出
-3 _th size:torch.Size([1, 5, 4])
-2 _th size:torch.Size([5, 1, 4])
-1 _th size:torch.Size([5, 4, 1])
0 _th size:torch.Size([1, 5, 4])
1 _th size:torch.Size([5, 1, 4])
2 _th size:torch.Size([5, 4, 1])
这就是一个维度扩充的意思【只能扩充一维,此函数的参数意为在第i维扩充】。如上若原来为2维,则参数i的范围可在[-3,2]之间,并且是对称的–如下
b1 = torch.unsqueeze(a,dim= 1)
b2 = torch.unsqueeze(a,dim= -2)
b1 == b2
----------------
输出
tensor([[[1, 1, 1, 1]],
[[1, 1, 1, 1]],
[[1, 1, 1, 1]],
[[1, 1, 1, 1]],
[[1, 1, 1, 1]]], dtype=torch.uint8)
bmm
全称为 batch matrix multiplication,具体如下
a = torch.ra