# oneflow.nn.CosineSimilarity¶

class oneflow.nn.CosineSimilarity(dim: Optional[int] = 1, eps: Optional[float] = 1e-08)

Returns cosine similarity between $$x_1$$ and $$x_2$$, computed along dim.

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

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/1.10/generated/torch.nn.CosineSimilarity.html#torch.nn.CosineSimilarity

Parameters
• 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: $$(\ast_1, D, \ast_2)$$ where D is at position dim.

• Input2: $$(\ast_1, D, \ast_2)$$, same number of dimensions as x1, matching x1 size at dimension dim,

and broadcastable with x1 at other dimensions.

• Output: $$(\ast_1, \ast_2)$$

For example:

>>> import oneflow as flow
>>> from oneflow import nn
>>> input1 = flow.randn(100, 128)
>>> input2 = flow.randn(100, 128)
>>> cos = nn.CosineSimilarity(dim=1, eps=1e-6)
>>> output = cos(input1, input2)

__init__(dim: Optional[int] = 1, eps: Optional[float] = 1e-08)None

Initialize self. See help(type(self)) for accurate signature.

Methods

 __call__(*args, **kwargs) Call self as a function. __delattr__(name, /) Implement delattr(self, name). __dir__() Default dir() implementation. __eq__(value, /) Return self==value. __format__(format_spec, /) Default object formatter. __ge__(value, /) Return self>=value. __getattr__(name) __getattribute__(name, /) Return getattr(self, name). __gt__(value, /) Return self>value. __hash__() Return hash(self). __init__([dim, eps]) Initialize self. __init_subclass__ This method is called when a class is subclassed. __le__(value, /) Return self<=value. __lt__(value, /) Return self