nmt中注意力解码器的余弦相似性



我正在实现一个神经机器翻译模型,对于解码器部分(具有注意力机制(,我想计算余弦相似性以找到分数。以下是功能:

得分(a,b(=/||a|||b||

就我而言:

a = htilde_t (N, H)
b = h (S, N, H)
the output should be (S, N)

我对它们的尺寸感到困惑,我不知道如何在pytorch中解决这个问题。

请参阅此处:https://pytorch.org/docs/master/nn.html?highlight=cosine#torch.nn.CosineSimilarity

cos = nn.CosineSimilarity(dim=2, eps=1e-6)
output = cos(a.unsqueeze(0),b)

你需要取消缩放来添加重影维度,以使两个输入都具有相同的亮度:

Input1: (∗1,D,∗2) where D is at position dim
Input2: (∗1,D,∗2) , same shape as the Input1
Output: (∗1,∗2)

最新更新