Regularization¶
Penalties on a single tensor: no target, any shape.
Regularizer
¶
Bases: Metric
Single-tensor magnitude penalty.
Takes one tensor field, applies a pointwise penalty function, and
reduces the result to a per-sample scalar via :meth:Metric.reduce.
Useful whenever you want to penalise the magnitude (or some
function of the magnitude) of an auxiliary model output as part
of the total loss.
Built-in penalties:
============== ============================ ==================================
penalty= Function applied to x Common name
============== ============================ ==================================
"identity" x Mean of the field
"abs" |x| L1 norm / sparsity
"square" x² L2 norm / magnitude
"exp" exp(x) Penalises positive log-quantities
"entropy" -x·log(x) Element-wise entropy
"huber" smooth-L1 with huber_delta Quadratic near 0, linear elsewhere
callable any Tensor → Tensor Custom — same-shape output required
============== ============================ ==================================
Multiple :class:Regularizer instances can be combined in a
:class:LossCombiner with different penalties and/or fields —
each is a regular Metric with its own weight and IO binding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
penalty
|
Union[str, Callable]
|
Either a built-in name (see table) or a callable
|
'identity'
|
huber_delta
|
float
|
Transition point for |
1.0
|
Example::
# L1 sparsity penalty on an attention map
Regularizer(penalty="abs").set_io({"inputs": {"x": "attention"}})
# L2 magnitude penalty (with mask)
Regularizer(penalty="square").set_io(
{"inputs": {"x": "noise_pred", "y_mask": "valid_mask"}}
)
# Huber with custom delta
Regularizer(penalty="huber", huber_delta=0.5)
# Custom — log-L1 (robust soft-L1)
Regularizer(penalty=lambda x: torch.log1p(x.abs()))