Saltar al contenido

Lección 19 · Experiments

DVCLive: Log Metrics from Training Code

Use DVCLive to log parameters, metrics, plot data and model artifacts from Python training code, manage steps, and connect logged runs to DVC experiments.

So far your scripts wrote metrics files by hand. DVCLive is a companion Python library that does this while training runs. You call a few methods, and it writes metrics, parameters and plot data in a form DVC understands.

The basic pattern

DVCLive is a separate Python package (dvclive); see its install guide. A typical loop looks like this:

Python
# Read-only: real DVCLive code
from dvclive import Live

with Live() as live:
    live.log_param("epochs", 5)
    live.log_params({"model": "logistic", "seed": 42})

    for epoch in range(5):
        train_loss, val_loss = train_one_epoch()   # your function
        live.log_metric("train/loss", train_loss)
        live.log_metric("val/loss", val_loss)
        live.next_step()

    live.log_artifact("model.pkl", type="model", name="review-model")
  • log_param and log_params record settings.
  • log_metric records a value for the current step.
  • next_step closes the step and moves to the next one. According to the documentation, it updates the summary, the DVC metadata and a report.
  • log_artifact records a file such as a trained model with a type and name.
  • Other methods include log_image, log_plot and log_sklearn_plot.

Using with Live() as live: finishes the run cleanly when the block ends. In the library source we checked, Live also has save_dvc_exp=True by default, so finishing a run can save a DVC experiment, even when you start the script yourself. Pass save_dvc_exp=False if you do not want that.

What it writes

By default, DVCLive writes into a dvclive/ folder. You can expect a metrics summary, per-step plot data, recorded parameters, and generated DVC metadata that describes the metrics and plots. Exact file names and options are on the Live reference page. Because the data matches DVC formats, dvc metrics show, dvc plots diff and dvc exp show can work with the results.

With pipelines and experiments

DVCLive can run inside a pipeline stage. List the DVCLive output files in the stage so DVC knows what the stage produces, and decide whether each should be cached or committed with Git. With dvc exp run, each run's logged values can appear in the experiment table.

DVCLive also provides integrations with popular frameworks, including Keras, PyTorch, Lightning and scikit-learn. A framework callback can log metrics automatically, so you write less code. Check each framework's page for exact usage, because framework APIs change.

Optional web tools

DVC Studio is a hosted web app that can display experiments and live updates. DVCLive can send updates there if you configure access. Using it is optional, and sharing metrics with any hosted service means sending data off your machine. Check your organization's rules and the service's current terms before enabling it.

Try the idea

This stand-in implements a miniature logger with similar method names so you can see the flow of steps and files. It is not DVCLive, and its file layout differs.

Python Stand-in: a mini live logger

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the summary keeps only the latest values, while history keeps one row per step. That distinction explains why you can see both a final score and a curve.

When to use it

Use DVCLive when Plain files are enough when
Training has many steps and you want curves You compute one score at the end
A framework callback fits your code You want no extra dependency
You want ready-made plot definitions You already write metrics your own way

Tips

  • Use consistent names such as train/loss and val/loss across runs.
  • Call next_step once per logical step, or steps will not line up between runs.
  • Do not log sensitive values, such as raw user text or secrets, as parameters or images.
  • Pin the DVCLive version with your other libraries.
  • If your team also uses another experiment tracker, choose one source of truth for each metric.

Common mistake: calling log_metric repeatedly in a loop and forgetting next_step. Without step boundaries, a curve cannot be built as you intend.

A small checklist before a long run

Run two or three steps first and open the generated files. Check that names are spelled the way you want, that the step numbers increase, and that no private values were written. It is much cheaper to fix a logging mistake after three steps than after a night of training.

Fit DVCLive into your project

Introduce the library in small steps. First, add one log_metric call for the final score and check that dvc metrics show finds it. Next, log the training loss in the loop with next_step, and look at the curve. Finally, add parameters and the model artifact.

Think about who owns each file. If your pipeline already writes reports/metrics.json, decide whether DVCLive replaces it or whether you keep both. Two files with the same score will eventually disagree and confuse reviewers. Also check what the library writes into DVC metadata files in your repository, and read the diff before committing, so there are no surprises.

If training runs on a remote machine, remember that logged files appear there. Version or copy the results you want to keep before the machine is deleted.

Compare two runs

After two logged runs, compare them with the DVC commands you already know. Look at the final values in the experiment table, then overlay the curves with a plots comparison. Two runs with similar final scores can behave very differently, for example one that is stable and one that jumps around from step to step.

Finally, treat the logging code like the rest of your project. Commit it with the training script, review changes to metric names carefully, and remove calls that nobody reads anymore.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does next_step mark?

    Step boundaries let DVCLive build a history for plots.
  2. 2. Which method records a trained model file as an artifact?

    log_artifact stores information about a file such as a model.
  3. 3. What is the MiniLive class in this lesson?

    It only imitates the flow of the real library.
  4. 4. What should you consider before sending live metrics to a hosted service?

    Hosted services receive your data, so check policy and terms.

Summary

  • DVCLive logs parameters, metrics, plot data and artifacts from Python.
  • next_step separates steps so curves can be drawn.
  • Its output works with DVC metrics, plots and experiments.
  • Framework callbacks reduce boilerplate.
  • Keep names stable and avoid logging sensitive data.

Next: Team Workflow with Git and DVC

Tu progreso se guarda en este navegador. No necesitas una cuenta.