mlvbench.tasks¶
Tasks.
Tasks allow reusing the trainer for different probing experiments by specifying
- How to obtain groung truth for a batch.
- The loss function.
- How to evaluate a probe.
Task interface.
Evaluator
¶
Bases: ABC
Base class for evaluators.
An evaluator is used to compute and aggregate performance metrics over a dataset (subset). The following simplified code shows how the evaluator is used by the trainer:
evaluator = task.get_evaluator()
for batch in dataset:
...
evaluator.update(batch, prediction, target, weight)
summary, results, predictions = evaluator.finalize()
finalize()
abstractmethod
¶
Finalize the evaluation and return the aggregated results.
Returns:
| Type | Description |
|---|---|
tuple[dict[str, float], DataFrame, list[dict[str, Any]]]
|
A 3-tuple of (summary, results, predictions). summary holds the aggregated
metrics for the whole subset, results holds one row per sample, and
predictions holds one record per stored sample as returned by
|
update(batch, prediction, target, weight=None)
abstractmethod
¶
Update the evaluator with data from a single batch.
Task
¶
Bases: ABC
Base class for tasks.
A task specifies the prediction target, the loss function and the evaluator.
configure(model)
¶
Configure the task for the given model.
Called by the trainer before any data preparation or probe fitting. Override to store model attributes (e.g., patch size) that are needed during target preparation or evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model whose features will be probed. |
required |
criterion(prediction, target, weight=None)
abstractmethod
¶
Loss function for the task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction
|
Tensor
|
The prediction from the probe, a |
required |
target
|
Tensor
|
The target for the task, a |
required |
weight
|
Tensor | None
|
Optional per-element |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
The scalar loss for the task, a |
get_evaluator(save_predictions=8)
abstractmethod
¶
Return an evaluator for the task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
save_predictions
|
int
|
The maximum number of samples for which the evaluator keeps raw predictions. Set to 0 to disable. |
8
|
prepare_target(batch)
abstractmethod
¶
Prepare the target for the probe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, Tensor]
|
A batch from the dataset. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor | None]
|
A 2-tuple of (target, weight). target is a |