torch.nn.functional.cosine_similarity¶
-
torch.nn.functional.
cosine_similarity
(x1, x2, dim=1, eps=1e-08) → Tensor¶ Returns cosine similarity between x1 and x2, computed along dim.
Supports type promotion.
- Parameters
x1 (Tensor) – First input.
x2 (Tensor) – Second input (with the same number of dimensions as x1, matching x1 size at dimension dim, and broadcastable with x1 at other dimensions).
dim (int, optional) – Dimension of vectors. Default: 1
eps (float, optional) – Small value to avoid division by zero. Default: 1e-8
- Shape:
Input: where D is at position dim.
Output:
Example:
>>> input1 = torch.randn(100, 128) >>> input2 = torch.randn(100, 128) >>> output = F.cosine_similarity(input1, input2) >>> print(output)