scalarization

A Scalarizer reduces a tensor of values of any shape into a single scalar value. This is the simple baseline against which Aggregators are compared: instead of combining the per-loss gradients via the Jacobian or its Gramian, a Scalarizer combines the losses directly, and a standard call to backward() produces the gradient.

The following example shows how to use Mean to combine a vector of losses into a single scalar loss.

>>> from torch import tensor
>>> from torchjd.scalarization import Mean
>>>
>>> scalarizer = Mean()
>>> losses = tensor([1.0, 2.0, 3.0])
>>> loss = scalarizer(losses)
>>> loss
tensor(2.)

Abstract base class

class torchjd.scalarization.Scalarizer[source]

Abstract base class for all scalarizers. Reduces a tensor of values of any shape into a single scalar value.

__call__(values, /)[source]

Computes the scalar value from the input tensor of values and applies all registered hooks.

Parameters:

values (Tensor) – The tensor of values to scalarize. May be of any shape.

Return type:

Tensor