Skip to main content

ClikaRT::ops::cosine_similarity

function

cosine_similarity()

Tensor cosine_similarity(
    Tensor a,
    Tensor b,
    int64_t dim = 1,
    std::optional<double> eps = std::nullopt
)

Cosine similarity along dim.

out=abmax(a2,ε)max(b2,ε)\mathrm{out} = \frac{a \cdot b} {\max(\lVert a \rVert_2, \varepsilon)\, \max(\lVert b \rVert_2, \varepsilon)}

Parameters

  • a: left operand.
  • b: right operand; shapes broadcast against a.
  • dim: the axis reduced by the dot/norms; default 1.
  • eps: divide-by-zero floor; std::nullopt selects 1e-8.

Returns: the similarities; the broadcast shape minus dim.

Throws

  • ClikaRT::Error: (INVALID_ARGUMENT) when dim is out of range or the shapes cannot broadcast.

Declared in ClikaRT/compute/ops.h, line 2662