Skip to main content

ClikaRT::ops::cosine_similarity

function

cosine_similarity()​

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

Cosine similarity along dim.

out=a⋅bmax⁡(∥a∥2,ε) max⁡(∥b∥2,ε)\mathrm{out} = \frac{a \cdot b} {\max(\lVert a \rVert_2, \varepsilon)\, \max(\lVert b \rVert_2, \varepsilon)}

Parameters

  • x1: left operand.
  • x2: right operand; shapes broadcast against x1.
  • 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 2682