class

CosineSimilarity

extendsModule
CosineSimilarity(dim: int = 1, eps: float = 1e-08)
source

Cosine similarity between two tensors along a specified dimension.

Computes element-wise cosine similarity:

CosineSimilarity(x1,x2)=x1x2max ⁣(x12x22,  ε)\text{CosineSimilarity}(x_1, x_2) = \frac{x_1 \cdot x_2}{\max\!\bigl(\|x_1\|_2 \cdot \|x_2\|_2,\; \varepsilon\bigr)}

The result is a scalar (or tensor with dim reduced) in the range [1,1][-1, 1], where 11 indicates identical direction, 00 orthogonality, and 1-1 opposite direction. The eps floor on the denominator prevents division by zero for zero-norm vectors.

Parameters

dimint= 1
Dimension along which cosine similarity is computed. Default: 1.
epsfloat= 1e-08
Small value ε\varepsilon added to the denominator for numerical stability. Default: 1e-8.

Notes

  • Input x1: (N,D)(N, D) or any shape broadcastable to x2.
  • Input x2: (N,D)(N, D) or any shape broadcastable to x1.
  • Output: (N,)(N,)dim is reduced; same batch dimensions as input.

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.CosineSimilarity(dim=1)
>>> x1 = lucid.tensor([[1.0, 0.0], [0.0, 1.0]])
>>> x2 = lucid.tensor([[1.0, 0.0], [1.0, 0.0]])
>>> m(x1, x2)
tensor([1., 0.])
>>> # Comparing sentence embeddings along the feature dimension
>>> emb1 = lucid.randn(32, 768)
>>> emb2 = lucid.randn(32, 768)
>>> sim = nn.CosineSimilarity(dim=1)(emb1, emb2)
>>> sim.shape
(32,)

Methods (3)

dunder

__init__

None
__init__(dim: int = 1, eps: float = 1e-08)
source

Initialise the CosineSimilarity module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x1: Tensor, x2: Tensor)
source

Apply the activation function element-wise.

Parameters

inputTensor
Input tensor of arbitrary shape.

Returns

Tensor

Output tensor of the same shape as input.

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.