mse

Contents

mse#

class braintools.fit.losses.mse(prediction, target)#

Compute the mean squared error between two time series.

\[\mathrm{MSE} = \frac{1}{M} \sum_{i=1}^{M} (\hat y_i - y_i)^2,\]

averaged over all M elements (time on axis 0, any trailing shape).

Parameters:
  • prediction (Any) – Model output, e.g. of shape (n_time, n_channels).

  • target (Any) – Data of the same shape. If one input carries units, both must, with the same dimension.

Returns:

Scalar MSE. For Quantity inputs it is the plain magnitude in the prediction’s unit squared (the target is converted first).

Return type:

Array

Raises:
  • ValueError – If the shapes differ.

  • TypeError – If only one input carries units.

  • brainunit.UnitMismatchError – If the units have different dimensions.

See also

rmse, mae

Notes

Removing an initial transient is left to the caller (Fitter does it). Works under jax.jit and jax.vmap and is differentiable.

Examples

>>> import brainunit as u
>>> import jax.numpy as jnp
>>> from braintools.fit import losses
>>> float(losses.mse(jnp.array([1., 2., 3.]), jnp.array([1., 2., 5.])))
1.3333333730697632
>>>
>>> float(losses.mse(jnp.ones(3) * u.mV, jnp.zeros(3) * u.volt))
1.0