accumulators
accumulators
¶
Metrics accumulator for tracking training and validation metrics.
LatentVisualizationData
dataclass
¶
Latent arrays and optional categorical labels for plotting.
MetricsAccumulator
dataclass
¶
MetricsAccumulator(total_loss=0.0, component_metrics=dict(), component_sample_counts=dict(), num_batches=0, num_samples=0, metadata=dict())
Generic metrics accumulator for tracking losses and computing metrics across batches.
This accumulator automatically tracks all component losses and metadata from LossOutput objects. It computes confusion matrices and accuracies for phase classification when phase metadata is available.
add_loss_output
¶
Add a loss output to the accumulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_output
|
LossOutput
|
LossOutput from a single batch. |
required |
batch_size
|
int
|
Samples in the batch, so epoch averages weight every sample equally instead of over-weighting partial batches. |
1
|
Source code in src/versatil/metrics/accumulators.py
average
¶
Compute average metrics over all batches.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary of averaged metric values |
Source code in src/versatil/metrics/accumulators.py
compute_phase_metrics
¶
Compute phase classification metrics from metadata if available.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with phase accuracy and per-phase accuracies, or empty dict |
Source code in src/versatil/metrics/accumulators.py
compute_confusion_matrix
¶
Compute confusion matrix from phase predictions if available.
Returns:
| Type | Description |
|---|---|
ndarray | None
|
Confusion matrix as numpy array, or None if no phase data |
Source code in src/versatil/metrics/accumulators.py
compute_expert_usage
¶
Compute average expert usage from metadata if available.
Returns:
| Type | Description |
|---|---|
dict[str, ndarray] | None
|
Expert usage ratio per expert as numpy array, or None if no expert usage data |
Note: this function supports multiple expert usage keys in metadata, but in practice we always only have one.
Source code in src/versatil/metrics/accumulators.py
compute_latent_visualization_data
¶
Compute latent visualization data with aligned categorical labels.
Handles the shape mismatch between: - Latent z: (B, latent_dim) - one per sample - Labels: (B, T) or (B, T, 1) - multiple labels per sample
Reduces sequence labels to one per sample using the mode.
Returns:
| Type | Description |
|---|---|
LatentVisualizationData
|
LatentVisualizationData with posterior/prior arrays and optional labels. |
Source code in src/versatil/metrics/accumulators.py
compute_latent_statistics
¶
Compute scalar statistics from latent distribution metadata.
Computes mean and std of mu, logvar, and z for both posterior and prior distributions when available. Useful for monitoring training stability and distribution behavior.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary with scalar statistics for latent distributions. |
Source code in src/versatil/metrics/accumulators.py
compute_vq_codebook_metrics
¶
Compute VQ posterior code usage metrics from hard assignments.
Aggregates posterior code indices over an epoch. The global metrics are averaged over residual VQ layers; per-layer code frequencies are emitted separately to make single-code collapse visible in W&B scalar plots.
Source code in src/versatil/metrics/accumulators.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
to_dict
¶
Convert to dictionary of averaged metrics.
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary of metric values including optional phase and latent metrics |
Source code in src/versatil/metrics/accumulators.py
reset
¶
Reset accumulator to initial state.
to_scalar
¶
Convert a value to a scalar float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Tensor, numpy array, or scalar value |
required |
Returns:
| Type | Description |
|---|---|
float
|
Scalar float value |