CosineSimilarity

class dragon.vm.torch.nn.CosineSimilarity(
  dim=1,
  eps=1e-08
)[source]

Compute the cosine similarity.

The CosineSimilarity function is defined as:

\[\text{CosineSimilarity}(x1, x2) = \frac{x_1 \cdot x_2}{\max(\Vert x_1 \Vert _2 \cdot \Vert x_2 \Vert _2, \epsilon)} \]

Examples:

m = torch.nn.CosineSimilarity()
x1 = torch.randn(10, 10)
x2 = torch.randn(10, 10)
distance = m(x1, x2)

__init__

CosineSimilarity.__init__(
  dim=1,
  eps=1e-08
)[source]

Create CosineSimilarity module.

Parameters:
  • dim (int, optional, default=1) – The vector dimension.
  • eps (float, optional, default=1e-8) – The epsilon value.