# pytorch计算余弦相似度

在pytorch中，可以使用torch.cosine_similarity函数对两个向量或者张量计算余弦相似度。先看一下pytorch源码对该函数的定义：

class CosineSimilarity(Module):
r"""Returns cosine similarity between :math:x_1 and :math:x_2, computed along dim.

.. math ::
\text{similarity} = \dfrac{x_1 \cdot x_2}{\max(\Vert x_1 \Vert _2 \cdot \Vert x_2 \Vert _2, \epsilon)}.

Args:
dim (int, optional): Dimension where cosine similarity is computed. Default: 1
eps (float, optional): Small value to avoid division by zero.
Default: 1e-8
Shape:
- Input1: :math:(\ast_1, D, \ast_2) where D is at position dim
- Input2: :math:(\ast_1, D, \ast_2), same shape as the Input1
- Output: :math:(\ast_1, \ast_2)
Examples::
>>> input1 = torch.randn(100, 128)
>>> input2 = torch.randn(100, 128)
>>> cos = nn.CosineSimilarity(dim=1, eps=1e-6)
>>> output = cos(input1, input2)
"""
__constants__ = ['dim', 'eps']

def __init__(self, dim=1, eps=1e-8):
super(CosineSimilarity, self).__init__()
self.dim = dim
self.eps = eps

def forward(self, x1, x2):
return F.cosine_similarity(x1, x2, self.dim, self.eps)

可以看到该函数一共有四个参数：

• x1和x2为待计算余弦相似度的张量；
• dim为在哪个维度上计算余弦相似度；
• eps是为了避免被零除而设置的一个小数值。

看一下例子：

import torch

x = torch.FloatTensor(torch.rand([10]))
print('x', x)
y = torch.FloatTensor(torch.rand([10]))
print('y', y)

similarity = torch.cosine_similarity(x, y, dim=0)
print('similarity', similarity)
x tensor([0.2817, 0.6858, 0.1820, 0.7357, 0.7625, 0.3569, 0.4781, 0.8485, 0.1385,
0.5654])
y tensor([0.3366, 0.8959, 0.7776, 0.2475, 0.9202, 0.2845, 0.7284, 0.8150, 0.2577,
0.0085])
similarity tensor(0.8502)

再看一个例子，给定一个张量，计算多个张量与它的余弦相似度，并将计算得到的余弦相似度标准化

import torch

def get_att_dis(target, behaviored):

attention_distribution = []

for i in range(behaviored.size(0)):
attention_score = torch.cosine_similarity(target, behaviored[i].view(1, -1))  # 计算每一个元素与给定元素的余弦相似度
attention_distribution.append(attention_score)
attention_distribution = torch.Tensor(attention_distribution)

return attention_distribution / torch.sum(attention_distribution, 0)		# 标准化

a = torch.FloatTensor(torch.rand(1, 10))
print('a', a)
b = torch.FloatTensor(torch.rand(3, 10))
print('b', b)

similarity = get_att_dis(target=a, behaviored=b)
print('similarity', similarity)
a tensor([[0.9255, 0.2194, 0.8370, 0.5346, 0.5152, 0.4645, 0.4926, 0.9882, 0.2783,
0.9258]])
b tensor([[0.6874, 0.4054, 0.5739, 0.8017, 0.9861, 0.0154, 0.8513, 0.8427, 0.6669,
0.0694],
[0.1720, 0.6793, 0.7764, 0.4583, 0.8167, 0.2718, 0.9686, 0.9301, 0.2421,
0.0811],
[0.2336, 0.4783, 0.5576, 0.6518, 0.9943, 0.6766, 0.0044, 0.7935, 0.2098,
0.0719]])
similarity tensor([0.3448, 0.3318, 0.3234])

07-20 2万+
10-12 1119
05-06 2538
11-01 1013
07-11 3597
01-20 1263