TrainingProgressMonitor - Monitor and plot training progress for deep learning custom training
loops - MATLAB ([original](https://in.mathworks.com/help/deeplearning/ref/deep.trainingprogressmonitor.html)) ([raw](?raw))
Monitor and plot training progress for deep learning custom training loops
Since R2022b
Description
Use a TrainingProgressMonitor
object to track training progress when using a custom training loop.
You can use a TrainingProgressMonitor
object to:
- Create animated custom metric plots and record custom metrics during training.
- Display and record training information during training.
- Stop training early.
- Track training progress with a progress bar.
- Track elapsed time.
This image shows an example of the Training Progress window during training. For more information about configuring the Training Progress window and an example showing how to generate this figure, see Monitor Custom Training Loop Progress.
Creation
Syntax
Description
`monitor` = trainingProgressMonitor
creates aTrainingProgressMonitor
object that you can use to track the training progress and create training plots.
Properties
Metric names, specified as a string scalar, character vector, string array, or cell array of character vectors. Valid names begin with a letter, and contain letters, digits, and underscores. Each metric appears in its own training subplot. To plot more than one metric in a single subplot, use the groupSubPlot function.
Example: ["TrainingLoss","ValidationLoss"];
Data Types: char
| string
| cell
Information names, specified as a string scalar, character vector, string array, or cell array of character vectors. Valid names begin with a letter, and contain letters, digits, and underscores. These names appear in the Training Progress window but do not appear as training plots.
Example: ["GradientDecayFactor","SquaredGradientDecayFactor"];
Data Types: char
| string
| cell
This property is read-only.
Request to stop training, specified as a numeric or logical 0
(false
) or 1
(true
). The value of this property changes to 1
when you click theStop button in the Training Progress window. TheStop button only appears if you set theVisible
property to 'on'
or1
(true
).
Data Types: logical
State of visibility, specified as 'on'
or'off'
, or as numeric or logical 1
(true
) or 0
(false
). A value of 'on'
is equivalent to true
, and'off'
is equivalent to false
. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.
'on'
— Display the Training Progress window.'off'
— Hide the Training Progress window without deleting it. You still can access the properties of an invisible object.
Example: 'off'
Training progress percentage, specified as a scalar or dlarray
object in the range [0, 100]
.
Example: 17;
Horizontal axis label in the training plot, specified as a string scalar or character vector.
Example: "Iteration";
Data Types: char
| string
| cell
Training status, specified as a string scalar or character vector.
Example: "Running";
Data Types: char
| string
| cell
This property is read-only.
Metric values, specified as a structure. Use the Metrics property to specify the field names for the structure. Each field contains a matrix with two columns. The first column contains the custom training loop step values and the second column contains the metric values recorded by the recordMetrics function.
Data Types: struct
This property is read-only.
Information values, specified as a structure. Use the Info property to specify the field names for the structure. Each field is a column vector that contains the values updated by the updateInfo function.
Data Types: struct
Object Functions
groupSubPlot | Group metrics in training plot |
---|---|
recordMetrics | Record metric values for custom training loops |
updateInfo | Update information values for custom training loops |
yscale | Set training plot _y_-axis scale (linear or logarithmic) |
Examples
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:
- Evaluate the
Stop
property at the start of each step in your custom training loop. When you click the Stop button in the Training Progress window, theStop
property changes to1
. Training stops if your training loop exits when theStop
property is1
. - 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.
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.
- The training plots update each time you call recordMetrics.
- The values under Information update each time you call updateInfo.
- The elapsed time updates each time you call recordMetrics orupdateInfo and when you update the Progress property.
A TrainingProgressMonitor
object has the same properties and object functions as an experiments.Monitor object. Therefore, you can easily adapt your plotting code for use in an Experiment Manager setup script.
How you monitor training depends on where you are training.
- If you are using a custom training loop script, you must create and manage a
TrainingProgressMonitor
object yourself. - If you are using a custom training experiment, Experiment Manager creates an
experiments.Monitor
object for each trial of your experiment. By default, Experiment Manager saves theexperiments.Monitor
object as the variablemonitor
.
In Experiment Manager, you can use theexperiments.Monitor
object in place of theTrainingProgressMonitor
object in your custom training loop code.
For example, suppose your training script creates aTrainingProgressMonitor
object to track and plot training and validation loss.
monitor = trainingProgressMonitor( ... Metrics=["TrainingLoss","ValidationLoss"], ... XLabel="Iteration");
groupSubPlot(monitor,"Loss",["TrainingLoss","ValidationLoss"]); yscale(monitor,"Loss","log")
iteration = 1; recordMetrics(monitor,iteration,TrainingLoss=loss,ValidationLoss=lossVal);
To adapt this code for use in Experiment Manager with anexperiments.Monitor
object:
- Convert any code that sets properties using
Name=Value
syntax to use dot notation. - Delete the call to
trainingProgressMonitor
. This is because Experiment Manager creates a monitor for you.
Use the adapted code inside your Experiment Manager setup function.
% Inside custom training experiment setup function
monitor.Metrics=["TrainingLoss","ValidationLoss"]; monitor.XLabel = "Iteration";
groupSubPlot(monitor,"Loss",["TrainingLoss","ValidationLoss"]); yscale(monitor,"Loss","log")
iteration = 1; recordMetrics(monitor,iteration,TrainingLoss=loss,ValidationLoss=lossVal);
Tips
- 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.
- When you click the Stop button in the Training Progress window, the
Stop
property is set to1
(true
). This stops training if your training loop exits when theStop
property is1
. For example, to enable early stopping, include the following code in your custom training loop.
while numEpochs < maxEpochs && ~monitor.Stop
% Custom training loop code.
end - The elapsed time updates each time you call
recordMetrics
orupdateInfo
, and when you update theProgress
property.
Version History
Introduced in R2022b