fn

cosine_similarity

Tensor
cosine_similarity(x1: Tensor, x2: Tensor, dim: int = 1, eps: float = 1e-08)
source

Cosine similarity between two tensors along a dimension.

Measures the angle between two vectors irrespective of their magnitudes — the natural similarity metric for unit-norm embedding spaces (contrastive learning, retrieval, face verification).

Parameters

x1Tensor
Tensors of the same shape.
x2Tensor
Tensors of the same shape.
dimint= 1
Dimension along which the dot product / norms are taken. Default 1.
epsfloat= 1e-08
Lower bound on the denominator to avoid division by zero. Default 1e-8.

Returns

Tensor

Similarity tensor whose shape matches the inputs with dim reduced, values in [1,1][-1, 1].

Notes

cosine_sim(x1,x2)=x1x2max(x12x22,ϵ)\text{cosine\_sim}(x_1, x_2) = \frac{x_1 \cdot x_2}{\max(\|x_1\|_2 \, \|x_2\|_2,\, \epsilon)}

Internally each input is L2L_2-normalised along dim via normalize, after which the dot product is exactly the cosine. For pairwise similarity matrices, use unsqueeze to broadcast the inputs into rank-3 tensors before calling this function.

Examples

>>> import lucid
>>> from lucid.nn.functional import cosine_similarity
>>> a = lucid.tensor([[1.0, 0.0]])
>>> b = lucid.tensor([[1.0, 1.0]])
>>> cosine_similarity(a, b, dim=1)
Tensor([0.7071])