fn
cosine_similarity
→Tensorcosine_similarity(x1: Tensor, x2: Tensor, dim: int = 1, eps: float = 1e-08)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
x1TensorTensors of the same shape.
x2TensorTensors of the same shape.
dimint= 1Dimension along which the dot product / norms are taken.
Default
1.epsfloat= 1e-08Lower bound on the denominator to avoid division by zero.
Default
1e-8.Returns
TensorSimilarity tensor whose shape matches the inputs with dim
reduced, values in .
Notes
Internally each input is -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])