recordMetrics - Record metric values for custom training loops - MATLAB (original) (raw)

Record metric values for custom training loops

Since R2022b

Syntax

Description

recordMetrics([monitor](#mw%5F08643335-6e46-46ef-85b2-c3e791942779),[step](#mw%5F5f7dc45a-3382-4ecd-b148-049a6fe4f522),metricName1=metricValue1,...,metricNameN=metricValueN) records multiple metric values.

example

recordMetrics([monitor](#mw%5F08643335-6e46-46ef-85b2-c3e791942779),[step](#mw%5F5f7dc45a-3382-4ecd-b148-049a6fe4f522),[metricStructure](#mw%5F9ab3b380-582e-42d3-a08b-294c784e7ac9)) records the metric values specified by the structuremetricStructure.

example

Examples

collapse all

Use a TrainingProgressMonitor object to track training progress and produce training plots for custom training loops.

Create a TrainingProgressMonitor object. The monitor automatically tracks the start time and the elapsed time. The timer starts when you create the object.

Tip

To ensure that the elapsed time accurately reflects the training time, make sure you create the TrainingProgressMonitor object close to the start of your custom training loop.

monitor = trainingProgressMonitor;

Before you start the training, specify names for the information and metric values.

monitor.Info = ["LearningRate","Epoch","Iteration"]; monitor.Metrics = ["TrainingLoss","ValidationLoss","TrainingAccuracy","ValidationAccuracy"];

Specify the horizontal axis label for the training plot. Group the training and validation loss in the same subplot. Group the training and validation accuracy in the same plot.

monitor.XLabel = "Iteration"; groupSubPlot(monitor,"Loss",["TrainingLoss","ValidationLoss"]); groupSubPlot(monitor,"Accuracy",["TrainingAccuracy","ValidationAccuracy"]);

Specify a logarithmic scale for the loss. You can also switch the y-axis scale by clicking the log scale button in the axes toolbar.

yscale(monitor,"Loss","log")

During training:

epoch = 0; iteration = 0;

monitor.Status = "Running";

while epoch < maxEpochs && ~monitor.Stop epoch = epoch + 1;

while hasData(mbq) && ~monitor.Stop
    iteration = iteration + 1;

    % Add code to calculate metric and information values.
    % lossTrain = ...

   updateInfo(monitor, ...
        LearningRate=learnRate, ...
        Epoch=string(epoch) + " of " + string(maxEpochs), ...
        Iteration=string(iteration) + " of " + string(numIterations));

   recordMetrics(monitor,iteration, ...
        TrainingLoss=lossTrain, ...
        TrainingAccuracy=accuracyTrain, ...
        ValidationLoss=lossValidation, ...
        ValidationAccuracy=accuracyValidation);

    monitor.Progress = 100*iteration/numIterations;
end

end

The Training Progress window shows animated plots of the metrics, as well as the information values, training progress bar, and elapsed time.

Training Progress window. The first plot shows the training and validation loss and the second plot shows the training and validation accuracy.

Use a structure to record metric values.

structure.TrainingLoss = trainingLoss; structure.ValidationLoss = validationLoss; recordMetrics(monitor,iteration,structure);

Input Arguments

collapse all

Custom training loop step, such as the iteration or epoch number, specified as a numeric scalar or dlarray object. The software uses this value as the_x_-coordinate in the training plot.

Metric name, specified as a string scalar or character vector. This input must be an element of the Metrics property of monitor.

Data Types: char | string | cell

Metric value, specified as a numeric scalar or dlarray object. The software uses this value as the _y_-coordinate in the training plot.

Metric names and values, specified as a structure. Names must be elements of theMetrics property of monitor and can appear in any order in the structure.

Example: struct(TrainingLoss=trainingLoss,ValidationLoss=validationLoss)

Data Types: struct

Tips

Version History

Introduced in R2022b