---
title: "ClikaRT::ops::kl_div"
sidebar_label: "kl_div"
description: "Kullback–Leibler divergence loss. input is given in LOG space; target is probabilities unless log_target is set (then both are logs): \\[ \\ell_i = y_i\\,(\\log y_i - x_i) \\] input log-probabilities."
---

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

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

## kl\_div() {#kl_div}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`kl_div`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `target`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Reduction`](./index.md#Reduction) `reduction` `=` [`Reduction::Mean`](./index.md#Reduction),<br />
&nbsp;&nbsp;&nbsp;&nbsp;`bool` `log_target` `=` `false`<br />
)

</div>

Kullback–Leibler divergence loss.

`input` is given in LOG space; `target` is probabilities unless `log_target` is set (then both are logs):

$$
\ell_i = y_i\,(\log y_i - x_i)
$$

**Parameters**

- `input`: log-probabilities.
- `target`: probabilities (or log-probabilities under `log_target`); same shape as `input`.
- `reduction`: `None` / `Mean` / `Sum`; default `Mean`.
- `log_target`: `target` is already in log space; default false.

**Returns:** the divergence (0-D under `Mean`/`Sum`).

**Throws**

- `ClikaRT::Error`: (`INVALID_ARGUMENT`) on an input/target shape mismatch.

<div class="api-declared">

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

</div>
