Record metric values for custom training loops
records the specified metric value in the training plot and the
MetricData property of the
records the metric values specified by the structure
Track Progress and Produce Training Plots
TrainingProgressMonitor object to track training progress and produce training plots for custom training loops.
TrainingProgressMonitor object. The monitor automatically tracks the start time and the elapsed time. The timer starts when you create the object.
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, and group the training and validation accuracy in the same plot.
monitor.XLabel = "Iteration"; groupSubPlot(monitor,"Loss",["TrainingLoss","ValidationLoss"]); groupSubPlot(monitor,"Accuracy",["TrainingAccuracy","ValidationAccuracy"]);
Stopproperty at the start of each step in your custom training loop. When you click the Stop button in the Training Progress window, the
Stopproperty changes to
1. Training stops if your training loop exits when the
Update the information values. The updated values appear in the Training Progress window.
Record the metric values. The recorded values appear in the training plot.
Update the training progress percentage based on the fraction of iterations completed.
The following example code is a template. You must edit this training loop to compute your metric and information values. For a complete example that you can run in MATLAB, see Monitor Custom Training Loop Progress 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, and the information values, training progress bar, and elapsed time.
The training plots update each time you call
The values under Information update each time you call
The elapsed time updates each time you call
updateInfo, and when you update the Progress property.
Specify Metric Values by Using Structure
Use a structure to record metric values.
structure.TrainingLoss = trainingLoss; structure.ValidationLoss = validationLoss; recordMetrics(monitor,iteration,structure);
monitor — Training progress monitor
Training progress monitor, specified as a
step — Custom training loop step
numeric scalar |
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.
metricValue — Metric value
numeric scalar |
Metric value, specified as a numeric scalar or
dlarray object. The
software uses this value as the y-coordinate in the training
The information values appear in the Training Progress window and the training plot shows a record of the metric values. Use information values for text and for numerical values that you want to display in the training window but not in the training plot.
groupSubPlotfunction to group multiple metrics in a single training subplot.
Introduced in R2022b