base
base
¶
Base classes for loss computation and metrics tracking.
LossOutput
dataclass
¶
Output from loss computation containing total loss and component losses.
Attributes:
| Name | Type | Description |
|---|---|---|
total_loss |
Tensor
|
Scalar tensor representing the total weighted loss |
component_losses |
dict[str, Tensor]
|
Dictionary mapping loss component names to their values |
metadata |
dict[str, Any]
|
Optional dictionary for additional information (e.g., predictions for metrics) |
to_dict
¶
Convert loss output to dictionary of scalar values.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with MetricKey.TOTAL_LOSS and all component losses as floats |
Source code in src/versatil/metrics/base.py
BaseLoss
¶
Bases: Module, ABC
Abstract base class for loss computation modules.
Subclasses override weights and set_weights to expose their
tunable scalars.
requires_action_space_targets
property
¶
Whether this loss expects targets in the action space.
When True, the loss uses classification-style objectives (e.g. BCE) that only make sense when targets are actual action labels. Algorithms that predict outside the action space (velocity, noise) are incompatible with such losses.
Default is False (regression losses like MSE/L1 work in any space).
set_weights
¶
update_weights
¶
Apply a partial override by merging onto the current structure.
Source code in src/versatil/metrics/base.py
forward
abstractmethod
¶
Compute loss given predictions and targets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Dictionary of model predictions |
required |
targets
|
dict[str, Tensor]
|
Dictionary of ground truth values |
required |
is_pad
|
Tensor | None
|
Optional boolean tensor indicating padded positions (B, horizon) |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput containing total loss and component losses |
Source code in src/versatil/metrics/base.py
get_required_keys
abstractmethod
¶
Get the set of output keys this loss consumes.
Used at experiment validation to check every key the loss reads is
produced by the decoder, the algorithm, or a non-predicted action-space
entry (see experiment.validate_loss_keys). Losses that read their
targets from the predictions dictionary report those keys here. For
composite losses, this should recursively collect keys from all
sub-losses.
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of output keys required by this loss |
Source code in src/versatil/metrics/base.py
ScalarWeightedLoss
¶
reduce_loss_with_padding
¶
Apply padding-aware reduction to a loss tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_tensor
|
Tensor
|
Loss values of shape (B, horizon, ...) |
required |
is_pad
|
Tensor | None
|
Boolean mask of shape (B, horizon) where True indicates padding |
required |
reduction
|
str
|
Reduction mode ('mean', 'sum', or 'none') |
'mean'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Reduced loss tensor |