我正在实现一个神经机器翻译模型,对于解码器部分(带有注意力机制),我想计算余弦相似度来找到分数。下面是函数:
score(a,b) =/||a|b||
在我的例子中:
a = htilde_t (N, H)
b = h (S, N, H)
the output should be (S, N)
我的困惑是关于它们的尺寸,我不知道如何在pytorch中解决这个问题。
发布于 2020-03-02 17:37:12
查看此处: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)
https://stackoverflow.com/questions/60481045
复制相似问题