classification
classification
¶
Cross-entropy losses for phase and action-token classification.
PhaseClassificationLoss
¶
Bases: BaseLoss
Loss for phase classification in PhaseACT models.
Includes cross-entropy loss and optional entropy regularization.
Initialize phase classification loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Key for phase labels |
required |
cross_entropy_weight
|
float
|
Weight for cross-entropy loss |
0.1
|
entropy_weight
|
float
|
Weight for entropy regularization (Entropy maximization avoids experts collapse) |
0.01
|
label_smoothing
|
float
|
Label smoothing factor for cross-entropy |
0.2
|
Source code in src/versatil/metrics/losses/classification.py
set_weights
¶
Setter that updates the weight scalar coefficients.
Source code in src/versatil/metrics/losses/classification.py
get_required_keys
¶
Get required target keys for phase classification loss.
Returns:
| Type | Description |
|---|---|
set[str]
|
Set containing the phase label key |
forward
¶
Compute phase classification loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Dictionary with 'phase_label' logits (B, horizon, n_phases) |
required |
targets
|
dict[str, Tensor]
|
Dictionary with 'phase_label' ground truth (B, horizon) or (B, horizon, 1) |
required |
is_pad
|
Tensor | None
|
Optional padding mask |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with cross-entropy and optional entropy loss |
Source code in src/versatil/metrics/losses/classification.py
ActionTokenLoss
¶
Bases: ScalarWeightedLoss
Cross-entropy loss for tokenized actions.
Initialize action token loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weight
|
float
|
Scalar multiplier applied to the cross-entropy term. |
1.0
|
label_smoothing
|
float
|
Label smoothing factor [0, 1] |
0.2
|
Source code in src/versatil/metrics/losses/classification.py
get_required_keys
¶
Get required keys from predictions.
Returns:
| Type | Description |
|---|---|
set[str]
|
Empty set since target ground-truth tokens are in predictions |
forward
¶
Compute cross-entropy loss for tokenized actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predictions
|
dict[str, Tensor]
|
Dictionary containing: - '{DecoderOutputKey.ACTION_LOGITS.value}': logits (B, horizon, vocab_size) - '{SampleKey.TOKENIZED_ACTIONS.value}': ground truth token IDs (B, horizon) - 'is_pad': optional padding mask (B, horizon) |
required |
targets
|
dict[str, Tensor]
|
Dictionary containing ground truth tokens |
required |
is_pad
|
Tensor | None
|
Optional padding mask |
None
|
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with per-key cross-entropy losses |