oneflow.nn.functional.cosine_similarity¶
-
oneflow.nn.functional.
cosine_similarity
(x1: Tensor, x2: Tensor, dim: int = 1, eps: float = 1e-08) → 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 (seeoneflow.squeeze()
), resulting in the output tensor having 1 fewer dimension.The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.nn.functional.cosine_similarity.html
\[\text{similarity} = \dfrac{x_1 \cdot x_2}{\max(\Vert x_1 \Vert _2 \cdot \Vert x_2 \Vert _2, \epsilon)}\]- Parameters
For examples:
>>> import oneflow as flow >>> import oneflow.nn.functional as F >>> input1 = flow.randn(100, 128) >>> input2 = flow.randn(100, 128) >>> output = F.cosine_similarity(input1, input2)