---
title: "ClikaRT::ops::softmax"
sidebar_label: "softmax"
description: "Softmax along dim, computed stably (max-subtracted). \\[ \\mathrm{softmax}(x)_i = \\frac{e^{\\,x_i - \\max_j x_j}}{\\sum_j e^{\\,x_j - \\max_k x_k}} \\quad \\text{along } \\mathit{dim} \\] input the input tensor.…"
---

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

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

## softmax() {#softmax}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`softmax`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`int64_t` `dim` `=` `-1`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`DataType`](../../index.md#DataType) `dtype` `=` [`DataType::Undefined`](../../index.md#DataType)<br />
)

</div>

Softmax along `dim`, computed stably (max-subtracted).

$$
\mathrm{softmax}(x)_i = \frac{e^{\,x_i - \max_j x_j}}{\sum_j e^{\,x_j - \max_k x_k}} \quad \text{along } \mathit{dim}
$$

**Parameters**

- `input`: the input tensor.
- `dim`: the reduction dimension. Negative counts from the end. Default `-1`.
- `dtype`: output dtype; [`DataType::Undefined`](../../index.md#DataType) (default) keeps `input`'s.

**Throws**

- `ClikaRT::Error`: when `dim` is out of range, or when the dtype pair is not served (`code_name()` carries the reason).

**Returns:** same shape as `input`; dtype = [`dtype`](../dtype/index.md) when set, else `input`'s.

```cpp
auto p = ClikaRT::ops::softmax(logits, -1);
```

<div class="api-declared">

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

</div>
