Term#
- class braintools.fit.Term(fn, weight=1.0, key=None, name=None)#
A weighted loss term of an
Objective.- Parameters:
fn (
Callable[[Any,Any],Any]) –fn(prediction, target) -> scalar, the loss.weight (
Any) – Multiplier of the loss.key (
Hashable|None) – When given, the term evaluatesfn(prediction[key], target[key]), e.g. to pick one output of a dict-valued prediction.name (
str|None) – Label used byObjective.breakdown(). Defaults tokeywhen given, otherwise to the name offn.
- Raises:
TypeError – If
fnis not callable orweightis not a scalar.
Notes
A term is a plain callable: it holds no JAX state, so it can be used inside
jax.jit,jax.vmapandjax.gradas long asfncan.2.0 * termreturns a new term with the weight doubled;term + otherreturns anObjective.Examples
>>> import jax.numpy as jnp >>> from braintools.fit import Term >>> mse = lambda p, t: jnp.mean((p - t) ** 2) >>> term = 2.0 * Term(mse, name='mse') >>> term Term(name='mse', weight=2.0, key=None) >>> print(term(jnp.array([1., 2.]), jnp.array([1., 4.]))) 4.0