---
title: "ClikaRT::ops::cross_entropy"
sidebar_label: "cross_entropy"
description: "Cross-entropy loss over class LOGITS; the class dim is LAST. Equivalent to log_softmax over the class axis followed by negative-log-likelihood: \\[ \\ell = -\\log\\mathrm{softmax}(x)_{y} \\] input logits […"
---

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

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

## cross\_entropy() {#cross_entropy}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`cross_entropy`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `target`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`OptionalTensor`](../../index.md#OptionalTensor) `weight` `=` `{}`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::optional<int64_t>` `ignore_index` `=` `std::nullopt`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Reduction`](./index.md#Reduction) `reduction` `=` [`Reduction::Mean`](./index.md#Reduction)<br />
)

</div>

Cross-entropy loss over class LOGITS; the class dim is LAST.

Equivalent to [`log_softmax`](./log_softmax.md#log_softmax) over the class axis followed by negative-log-likelihood:

$$
\ell = -\log\mathrm{softmax}(x)_{y}
$$

**Parameters**

- `input`: logits `[.., C]` (channels-last class axis).
- `target`: class indices `[..]` (integer dtype).
- `weight`: optional per-class weight `[C]`.
- `ignore_index`: optional target value whose positions contribute no loss; absent = no masking.
- `reduction`: `None` / `Mean` / `Sum`; default `Mean`.

**Returns:** the loss (0-D under `Mean`/`Sum`; `[..]` under `None`).

**Throws**

- `ClikaRT::Error`: (`INVALID_ARGUMENT`) when `target`'s shape is not `input`'s minus the class axis, or a target index is out of range.

```cpp
auto loss = ClikaRT::ops::cross_entropy(logits, labels);   // 0-D mean
```

<div class="api-declared">

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

</div>
