---
title: "ClikaRT::ops::cosine_similarity"
sidebar_label: "cosine_similarity"
description: "Cosine similarity along dim. \\[ \\mathrm{out} = \\frac{a \\cdot b} {\\max(\\lVert a \\rVert_2, \\varepsilon)\\, \\max(\\lVert b \\rVert_2, \\varepsilon)} \\] x1 left operand."
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--function">function</span>

## cosine\_similarity() {#cosine_similarity}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`cosine_similarity`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `x1`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `x2`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `dim` `=` `1`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::optional<double>` `eps` `=` `std::nullopt`<br />
)

</div>

Cosine similarity along `dim`.

$$
\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.

<div class="api-declared">

Declared in `ClikaRT/compute/ops.h`, line 2749

</div>
