Edit on GitHub

Live.log_metric()

 def log_metric(name: str, val: float, plot: Optional[bool] = True):

Usage

from dvclive import Live

with Live() as live:
    live.log_metric("train/loss", 0.4)
    live.log_metric("val/loss", 0.9)

Description

On each live.log_metric(name, val) call DVCLive will create a metrics history file in {Live.plots_dir}/metrics/{name}.tsv:

dvclive
├── metrics.json
└── plots
    └── metrics
        ├── train
        │   └── loss.tsv
        └── val
            └── loss.tsv
$ cat dvclive/plots/metrics/train/loss.tsv
timestamp      step  loss
1623671484747  0     0.4

The metrics history can be visualized with dvc plots:

dvc plots diff dvclive/plots

Each subsequent call to live.log_metric(name, val) will add a new row to {Live.plots_dir}/metrics/{name}.tsv:

live.next_step()
live.log_metric("train/loss", 0.2)
live.log_metric("val/loss", 0.4)
timestamp      step  loss
1623671484747  0     0.4
1623671484892  1     0.2

In addition, DVCLive will store the latest value logged in Live.summary, so it can be serialized with calls to live.make_summary(), live.next_step() or when exiting the with block:

{
  "step": 1,
  "train": {
    "loss": 0.2
  },
  "val": {
    "loss": 0.4
  }
}

The metrics summary can be visualized with dvc metrics:

dvc metrics diff dvclive/metrics.json

Parameters

  • name - Name of the metric being logged.

  • val - The value to be logged.

  • plot - Whether to add the metric value to the metrics history file for plotting. If false, the metric will only be saved to the metrics summary.

Exceptions

  • dvclive.error.InvalidDataTypeError - thrown if the provided val does not have a supported type.

  • dvclive.error.DataAlreadyLoggedError - thrown if the provided name has already been logged within the same step.

Content

🐛 Found an issue? Let us know! Or fix it:

Edit on GitHub

Have a question? Join our chat, we will help you:

Discord Chat