l1_loss

Contents

l1_loss#

class braintools.metric.l1_loss(logits, targets, reduction='mean')#

Measure the mean absolute error (MAE) between each element in the logits \(x\) and the targets \(y\). It is useful in regression problems.

The per-sample mean absolute error (i.e. with reduction set to 'none') can be described as:

\[\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = \frac{1}{D} \sum_{d=1}^{D} \left| x_{n,d} - y_{n,d} \right|,\]

where \(N\) is the batch size and \(D\) is the number of features per sample (i.e. the product of all non-batch dimensions). If reduction is not 'none' (default 'mean'), then:

\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}\end{split}\]

\(x\) and \(y\) are tensors of arbitrary shapes with a total of \(n\) elements each.

Supports real-valued and complex-valued inputs.

Parameters:
  • logits (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – \((N, *)\) where \(*\) means any number of additional dimensions.

  • targets (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – \((N, *)\), same shape as logits.

  • reduction (str) –

    Specifies the reduction to apply to the per-sample losses: 'none' | 'mean' | 'sum'. Default: 'mean'.

    • 'none': no reduction will be applied; returns the per-sample MAE of shape \((N,)\),

    • 'mean': the per-sample losses are averaged,

    • 'sum': the per-sample losses are summed.

Returns:

output – Scalar if reduction is 'mean' or 'sum'. If reduction is 'none', an array of shape \((N,)\) holding the per-sample mean absolute errors.

Return type:

brainstate.typing.ArrayLike

Notes

This computes a true mean absolute error: the absolute differences are averaged over the feature axis (all non-batch dimensions) for each sample, rather than summed. The previous implementation returned a per-row L1 sum (jnp.linalg.norm(diff, ord=1, axis=1)); this corrected version divides by the number of features so the result matches the documented MAE semantics. With reduction='mean' over a single feature this equals jnp.mean(jnp.abs(logits - targets)).

Examples

>>> import jax.numpy as jnp
>>> import braintools
>>> logits = jnp.array([[1.0, 2.0], [3.0, 4.0]])
>>> targets = jnp.array([[1.5, 2.5], [2.0, 5.0]])
>>> braintools.metric.l1_loss(logits, targets)
Array(0.75, dtype=float32)
>>> braintools.metric.l1_loss(logits, targets, reduction='none')
Array([0.5, 1. ], dtype=float32)