Ir para o conteúdo

Lição 15 · Parameters, Metrics and Plots

DVC Plots: Visualize and Compare Curves

Define DVC plots in dvc.yaml from CSV, JSON or image files, choose templates, render HTML reports with dvc plots show and compare versions with plots diff.

A single number cannot show how training behaved. Did the loss keep falling, or did it flatten early and then rise? DVC plots turn data files into charts and let you overlay several versions to compare them.

What can be plotted

DVC handles two kinds of input:

  • Data series in CSV, TSV, JSON or YAML files. For CSV and TSV, columns become series. For JSON and YAML, use a list of objects with consistent fields. These are rendered with Vega-Lite.
  • Images (JPEG, GIF, PNG, SVG), including folders of images. DVC shows them as they are, which suits charts your own code draws.

A typical training log:

TEXT
epoch,train_loss,val_loss
1,0.92,0.95
2,0.61,0.70
3,0.45,0.62
4,0.33,0.64

Define plots

The simplest definition lists a file. DVC then uses the linear template on the last column.

YAML
plots:
  - reports/history.csv

A richer definition has an ID, columns, labels and possibly several series:

YAML
plots:
  - loss_curves:
      x: epoch
      y:
        reports/history.csv: [train_loss, val_loss]
      x_label: Epoch
      y_label: Loss
      title: Training and validation loss
  - confusion:
      template: confusion
      x: actual
      y:
        reports/predictions.csv: predicted
  - reports/roc_curve.png

If you leave out x, DVC uses an automatic step index. Built-in templates include linear, simple, scatter, smooth, confusion, confusion_normalized, bar_horizontal and bar_horizontal_sorted. Custom templates live in .dvc/plots, and dvc plots templates helps you create them.

Make the data file a stage output too, with cache: false if it is small enough for Git, or cached if it is large.

Render and compare

Terminal
# Read-only: make HTML reports
dvc plots show                       # current workspace
dvc plots diff main HEAD             # compare two revisions
dvc plots show reports/history.csv -x epoch -y val_loss

DVC writes a static HTML report and prints the path to open in a browser. dvc plots diff overlays the chosen versions. The VS Code extension offers a plots view too. Treat generated report folders as outputs and keep them out of Git unless you have a reason to commit them.

Read the curves

Curves help you diagnose training:

Pattern Possible meaning
Training loss falls, validation loss rises Overfitting
Both stay high Underfitting, weak features or too little training
Very jagged curve Learning rate may be too high, or the evaluation set is tiny
Validation clearly better than training Check the split for leakage or an easier validation set

These are clues, not proof. Check the data before drawing conclusions. Data Visualization explains how to design honest charts.

Try the idea

This stand-in reads a log like the CSV above and finds the best epoch. It also draws a tiny text chart. It is not a DVC report.

Python Stand-in: find where validation loss turns up

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.

Write plot data well

  • Save one row per step, with stable column names. Renaming a column breaks comparisons with old versions.
  • Keep files reasonably small. Millions of points make reports slow, so sample long logs.
  • For a confusion matrix, save one row per example with actual and predicted columns.
  • Include units in axis labels.
  • Draw custom charts yourself when needed, save them as image files, then list the image in plots.

If you use DVCLive (covered in the experiments chapter), it creates plot data and plot definitions for you.

Common mistake: comparing curves with different x meanings, such as batches in one run and epochs in another. Keep the same step definition in every version, or the overlay will mislead you.

Sharing results safely

Reports and images may reveal sensitive information, for example sample predictions that include names or private text. Before you attach a report to a ticket or publish it, check what it shows. Aggregate or mask values where needed, and follow your organization's rules about sharing data.

A comparison walkthrough

Suppose you want to know whether a new feature helped training. Train on the main branch and commit the history file or let DVC cache it. Create a branch, add the feature, run dvc repro, and then compare:

Terminal
# Read-only: compare the main branch with your workspace
dvc plots diff main

Look at three things. Did the new validation curve end lower? Did it get there faster? Is the gap between training and validation wider? A lower training loss alone is not evidence that the feature helps. If the two curves cross several times or differ by tiny amounts, run both versions with a few different seeds before deciding.

Choosing a chart type

Pick the template that matches the question. Use linear for values over steps, scatter for relations between two numbers, confusion or confusion_normalized for classification errors, and a horizontal bar chart for comparing categories such as scores by group. The normalized confusion matrix is easier to read when classes have very different sizes, but it hides how many examples each cell holds, so report counts somewhere as well.

Keep a plot honest

A chart can exaggerate small differences. If the y axis starts just below your values, a change of half a percent looks huge. Read the axis labels before you react, and report the numbers next to the picture. When you share a comparison, name the versions being compared and the data they used, so the reader can reproduce it. A plot without that context is a picture, not evidence. Where a difference is small, say so plainly instead of letting the shape of the lines suggest more.

Save the plot definitions in the same commit as the code that writes the data, so each version of the project can draw its own charts.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which inputs can DVC plot as data series?

    These formats are parsed into series. Image files are shown directly.
  2. 2. What does dvc plots diff do?

    It renders the chosen versions together.
  3. 3. Training loss keeps falling while validation loss rises. What is a likely issue?

    The model learns the training data better while generalizing worse.
  4. 4. If x is not set in a plot definition, what does DVC use?

    DVC generates a step index for the x axis.

Summary

  • Plots come from CSV, TSV, JSON or YAML data, or from image files.
  • Define them under plots in dvc.yaml and choose a template.
  • dvc plots show renders HTML; dvc plots diff compares versions.
  • Keep columns and step meanings stable.
  • Check reports for sensitive content before sharing.

Next: Run Experiments with DVC

Seu progresso fica salvo neste navegador. Não precisa de conta.