torch.nn.functional.cosine_similarity¶
- torch.nn.functional.cosine_similarity(x1, x2, dim=1, eps=1e-8) Tensor ¶
Returns cosine similarity between
x1
andx2
, computed along dim.x1
andx2
must be broadcastable to a common shape.dim
refers to the dimension in this common shape. Dimensiondim
of the output is squeezed (seetorch.squeeze()
), resulting in the output tensor having 1 fewer dimension.\[\text{similarity} = \dfrac{x_1 \cdot x_2}{\max(\Vert x_1 \Vert _2, \epsilon) \cdot \max(\Vert x_2 \Vert _2, \epsilon)} \]Supports type promotion.
- Parameters:
Example:
>>> input1 = torch.randn(100, 128) >>> input2 = torch.randn(100, 128) >>> output = F.cosine_similarity(input1, input2) >>> print(output)