# Metadata: Track and Compare Your Metrics

Print your metrics as JSON and Valohai automatically tracks, visualizes, and lets you compare them across experiments. No special libraries required. Moreover, Valohai doesn't restrict what metrics you can track, let it be `accuracy`, `loss`, `f1_score`, `ROC`, `AUC`, etc. The metrics are not restricted to numeric values.

> 💡 **Already logging metrics?** Just wrap them in JSON format and print. Valohai does the rest.

### How It Works

1. **Print JSON to stdout** — Any valid JSON key value pairs
2. **Valohai captures automatically** — Real-time collection during training
3. **View and compare** — Sort experiments, plot graphs, set alerts

```python
import json

# That's it - just print JSON
print(json.dumps({"accuracy": 0.92, "loss": 0.08}))
```

### Quick Example

#### Training Loop with Metrics

```python
import json

for epoch in range(epochs):
    # Your training code...
    train_loss = train_one_epoch()
    val_acc = validate()

    # Track metrics - just print as JSON
    print(
        json.dumps(
            {
                "epoch": epoch,
                "train_loss": train_loss,
                "val_accuracy": val_acc,
            },
        ),
    )
```

Now you can:

* **Sort experiments** by the metric values
* **Plot timeseries graphs** across multiple runs
* **Set early-stopping rules** based on the metrics
  * E.g. "stop execution when accuracy is over 0.9"

<details>

<summary>Optional: Use the <code>valohai-utils</code> Python helper tool</summary>

The `valohai-utils` helper library offers a simpler syntax:

```python
import valohai

# Log incrementally
for epoch in range(epochs):
    with valohai.logger() as log:
        log.log("accuracy", accuracy)
        log.log("loss", loss)
```

</details>

### Common Patterns

#### Multiple Metrics

```python
import json

# Track everything you care about
metrics = {
    "accuracy": 0.94,
    "precision": 0.92,
    "recall": 0.95,
    "f1_score": 0.93,
    "auc_roc": 0.97,
    "inference_time_ms": 23.5,
}
print(json.dumps(metrics))
```

#### Metrics Over Time

```python
import json

for step in range(training_steps):
    # Training...

    if step % log_interval == 0:
        print(
            json.dumps(
                {
                    "step": step,
                    "loss": current_loss,
                    "learning_rate": scheduler.get_lr()[0],
                    "gradient_norm": grad_norm,
                },
            ),
        )
```

### How to Use the Metrics in Valohai

#### Execution Table

See latest values for each execution:

* Sort by any metric
* Filter by thresholds
* Compare at a glance

#### Automatic Visualizations

* Time series plots per execution
* Compare multiple executions
* Download data as CSV

### Best Practices

#### Log Regularly

```python
# Log every N batches, not just epochs
if batch_idx % 100 == 0:
    print(
        json.dumps(
            {
                "batch": batch_idx,
                "loss": loss.item(),
            },
        ),
    )
```

#### Include Context

```python
# Add helpful context
print(
    json.dumps(
        {
            "epoch": epoch,
            "phase": "validation",  # training/validation/test
            "accuracy": acc,
            "samples_seen": epoch * len(train_loader),
        },
    ),
)
```

### Quick Reference

#### Basic Pattern

```python
import json

print(json.dumps({"metric_name": value}))
```

#### What Works

* Any value
* Multiple metrics in one print

#### What to Track

* Model performance (accuracy, loss, F1)
* Training dynamics (learning rate, gradients)
* Any metadata you want log from executions

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.valohai.com/migration-strategy/migrate-metrics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
