Skip to main content
App version: 3.4.8

Log metrics

A metric can be accuracy, MSE, or any numerical value. Neptune displays all float series as charts.

To log a numerical series, use the log_metrics() function:

from neptune_scale import Run

run = Run()

for step in epoch:
# your training loop
run.log_metrics(
data={"acc": 0.78},
step=step,
)
info

Neptune supports 64-bit floating-point numbers, which have a precision of 16 digits.

To log multiple metrics in a single call, pass multple key-value pairs to the data dictionary:

run.log_metrics(
data={"loss": 0.13, "acc": 0.79},
step=step,
)

Setting step values

To specify the index of metric values, use the step parameter:

run.log_metrics(
data={"loss": 0.13, "acc": 0.79},
step=2,
)

The step argument can be an integer or floating point value.

Float values can be useful, for example, as substeps:

run.log_metrics(
data={"loss": 0.11, "acc": 0.81},
step=2.1,
)
note

Within a particular metric, the step values must increase:

run.log_metrics(data={"loss": 0.13}, step=2)
run.log_metrics(data={"loss": 0.11}, step=3) # OK
run.log_metrics(data={"loss": 0.12}, step=1) # not OK

If you're logging separate FloatSeries attributes, the step doesn't have to increase across log_metrics() calls within the same run:

run.log_metrics(data={"loss": 0.13}, step=2)
run.log_metrics(data={"acc": 0.68}, step=1) # OK - different attribute

Setting custom timestamp

To provide a custom timestamp, pass a Python datetime value to the timestamp argument:

run.log_metrics(
data=...,
step=...,
timestamp=datetime.utcnow(),
)

If the timestamp argument isn't provided, the current time is used.

note

If timestamp.tzinfo is not set, the time is assumed to be in the local timezone.

Logging metrics from different processes

You can safely log metrics to the same run in separate processes, as long as steps aren't logged out of order within the same series attribute.

For details, see Log from different processes.

Logging metrics to fork runs

When forking a run, metrics are inherited up until and including the step specified as the fork point.

For details, see Fork an experiment.

Logging incomplete metrics

You can log partial series results and mark them as preview values.

For details, see Metric previews.

Error handling

Neptune may raise exceptions if there are malformed or inconsistent values.

Non-increasing steps

If steps logged to the client are not increasing, Neptune raises the NeptuneSeriesStepNonIncreasing exception. This triggers the default on_error_callback, which terminates the training process.

You can override the default behavior by implementing your own callbacks to handle error scenarios. For details, see Neptune API error handling.

Non-finite values

By default, Neptune skips non-finite values such as Inf and NaN when logging a series. To raise an exception when such values are encountered, set the NEPTUNE_SKIP_NON_FINITE_METRICS environment variable to False:

Append a line with the export command to your .profile or other shell initialization file:

export NEPTUNE_SKIP_NON_FINITE_METRICS=False