Collect Metrics

Print JSON from your code and Valohai automatically captures it as metrics. No logging libraries to configure, no tracking servers to set up—just print structured data and get instant tracking.


The Basics

Any JSON printed to stdout becomes a metadata entry. Valohai parses it automatically and makes it available for visualization and comparison.

💡Tip: The examples here contain mainly numeric metrics but you can also record strings if needed.

Python

import json

print(
    json.dumps(
        {
            "epoch": 1,
            "loss": 0.5,
            "accuracy": 0.82,
        },
    ),
)

Python with valohai-utils helper tool

R


When to Log Metrics

During Training (Progressive Logging)

Log metrics after each epoch or batch to watch training progress in real-time:

Benefits:

  • Monitor convergence in real-time

  • Detect training issues early

  • Stop runs that aren't improving


After Training (Final Results)

Log summary metrics when training completes:

Benefits:

  • Compare final performance across experiments

  • Sort executions by best result

  • Track high-level experiment outcomes


What to Log

Training Metrics

Track how your model learns:


Performance Metrics

Track multiple evaluation metrics:


Data Statistics

Track dataset characteristics:


Using valohai-utils

The valohai-utils library provides a clean interface for logging metrics.

Install

Basic Usage

In Training Loop


Quick Framework Examples

PyTorch


Scikit-learn


XGBoost


Framework-Specific Guides

For detailed integration examples with popular frameworks:

PyTorch Lightning → Use lifecycle hooks like on_train_epoch_end for automatic logging

TensorFlow/Keras → Create custom callbacks that log metrics after each epoch

YOLOv5 & Output File Watchers → Watch output files and stream them to Valohai metadata

Other frameworks? The core pattern works everywhere: print JSON from your code, and Valohai captures it. Apply the same callback/hook approach shown above.


Metadata Format

Each metadata entry is captured as a JSON object with an automatic timestamp.

Example Entry

The _time field is added automatically in UTC format.


Supported Data Types

Scalars (Most Common)

Lists

Note that you can record lists of metadata but for graph visualizations in the Valohai UI, you will need to print out the values sepaeately.


Best Practices

Use Consistent Naming

Keep metric names identical across experiments for easy comparison:


Log Progressively

Print metrics throughout training, not just at the end:


Include Step/Epoch Counter

Always include a step or epoch counter for time-series visualization:


Common Issues

Metrics Not Appearing

Symptom: JSON printed but no metrics in UI

Causes & Fixes:

  • Invalid JSON format → Validate JSON syntax

  • Missing newline after JSON → Ensure print() adds newline

  • Buffered stdout → Flush output: print(..., flush=True)

Test your JSON:


Metrics Mixed with Logs

Symptom: Hard to distinguish metrics from debug logs

Solution: Print only JSON for metrics, use stderr for debug logs:


Duplicate Keys

Symptom: Metrics overwriting each other

Solution: Use unique keys or include step counter:


Where Metrics Appear

Once logged, metrics are available in:

Execution Metadata Tab

View all metrics for a single execution with interactive graphs.

Executions Table

See the latest value of each metric in the table. Sort by any column to find top performers.

Comparison View

Select multiple executions and compare their metrics side-by-side.

Export from the UI

Download metrics as CSV or JSON for custom analysis.

Export with API

You can use the /api/v0/executions/{exec-id}/metadata/ API endpoint to get the execution metadata.


Next Steps

Last updated

Was this helpful?