Main Content

updateInfo

Update information values for custom training loops

Since R2022b

    Description

    updateInfo(monitor,infoName=infoValue) updates the specified information in the Training Progress window and saves the values in the InfoData property of the TrainingProgressMonitor object monitor.

    example

    updateInfo(monitor,infoName1=infoValue1,...,infoNameN=infoValueN) updates the values for multiple information variables.

    example

    updateInfo(monitor,infoStructure) updates the information using the values specified by the structure infoStructure.

    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:

    • 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, the Stop property changes to 1. Training stops if your training loop exits when the Stop property is 1.

    • 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.

    Note

    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, 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 update the information values.

    structure.GradientDecayFactor = gradientDecayFactor;
    structure.SquaredGradientDecayFactor = squaredGradientDecayFactor;
    updateInfo(monitor,structure);

    Input Arguments

    collapse all

    Training progress monitor, specified as a TrainingProgressMonitor object.

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

    Data Types: char | string | cell

    Information value, specified as one of these values:

    • Numeric scalar

    • String scalar

    • Character vector

    • dlarray object scalar

    • Numeric vector (since R2026a)

    • String array (since R2026a)

    • dlarray object vector (since R2026a)

    • Cell array (since R2026a). Each cell must contain one of the supported types listed, and all elements in the cell array must be of the same type.

    To specify multiple values simultaneously, specify infoValue as a vector of values or a cell array (since R2026a). Specifying multiple values simultaneously can help speed up the training process. The monitor displays the value of the last element only. Specify multiple values when you want the monitor to store the specified values for later analysis such as creating plots or calculating statistics after training.

    Before R2026a: The infoValue argument value must be scalar or a character vector.

    The type of data must match the type of data already recorded for the infoName argument.

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

    Example: struct(GradientDecayFactor=gradientDecayFactor,SquaredGradientDecayFactor=squaredGradientDecayFactor)

    Data Types: struct

    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.

    • You can use the information values to perform analysis after training. The monitor stores the information values without step information and only adds information when you call the updateInfo function. If you need the step information to use the values for the analysis, then update the information values at each step.

    Version History

    Introduced in R2022b

    expand all