Main Content


Deep learning recall metric

Since R2023b


    Use a RecallMetric object to track the recall when you train a deep neural network.

    To specify which metrics to use during training, specify the Metrics option of the trainingOptions function. You can use this option only when you train a network using the trainnet function.

    To plot the metrics during training, in the training options, specify Plots as "training-progress". If you specify the ValidationData training option, then the software also plots and records the metric values for the validation data. To output the metric values to the Command Window during training, in the training options, set Verbose to true.

    You can also access the metrics after training using the TrainingHistory and ValidationHistory fields from the second output of the trainnet function.




    metric = recallMetric creates a RecallMetric object. You can then specify metric as the Metrics name-value argument in the trainingOptions function. This metric is valid only for classification tasks.

    With no additional options specified, this syntax is equivalent to setting Metrics="recall" in the training options.

    metric = recallMetric(Name=Value) sets the Name, NetworkOutput, AverageType, and ClassificationMode properties using name-value arguments.


    expand all

    Metric name, specified as a string scalar or character vector. The metric name appears in the training plot, the verbose output, and the training information that you can access as the second output of the trainnet function.

    Data Types: char | string

    This property is read-only.

    Name of the output layer to apply the metric to, specified as [], a string scalar, or a character vector. When the value is [], the software passes all of the network outputs to the metric.


    You can apply the built-in metric to only a single output. If you have a network with multiple outputs, then you must specify the NetworkOutput name-value argument. To apply built-in metrics to multiple outputs, you must create a metric object for each output.

    Data Types: char | string

    This property is read-only.

    Type of averaging to use to compute the metric, specified as one of these values:

    • "micro" — Calculate the metric across all classes.

    • "macro" — Calculate the metric for each class and return the average.

    • "weighted" — Calculate the metric for each class and return the weighted average. The weight for a class is the proportion of observations from that class.

    For more information, see Averaging Type.

    Data Types: char | string

    This property is read-only.

    Type of classification task, specified as one of these values:

    • "single-label" — Each observation is exclusively assigned one class label (single-label classification).

    • "multilabel" — Each observation can be assigned more than one independent class label (multilabel classification). The software uses a softmax threshold of 0.5 to assign class labels.

    To select the classification mode for binary classification, consider the output layer of the network:

    • If the final layer has an output size of one, such as with a sigmoid layer, use "multilabel".

    • If the final layer has an output size of two, such as with a softmax layer, use "single-label".


    This metric is not supported when the ClassificationMode is set to "single-label" and the network output has a channel dimension of size 1. For example, if you have a single class and your output layer is a sigmoidLayer (binary-sigmoid task).

    Data Types: char | string

    Object Functions

    trainingOptionsOptions for training deep learning neural network
    trainnetTrain deep learning neural network


    collapse all

    Plot and record the training and validation recall when you train a deep neural network.

    Unzip the digit sample data and create an image datastore. The imageDatastore function automatically labels the images based on folder names.

    imds = imageDatastore("DigitsData", ...
        IncludeSubfolders=true, ...

    The datastore contains 10,000 synthetic images of digits from 0 to 9. Each image in the data set has a size of 28-by-28-by-1 pixels. You can train a deep learning network to classify the digit in the image.

    Use a subset of the data as the validation set.

    numTrainingFiles = 750;
    [imdsTrain,imdsVal] = splitEachLabel(imds,numTrainingFiles,"randomize");

    Create an image classification network.

    layers = [ ...
        imageInputLayer([28 28 1])

    Create a RecallMetric object and set AverageType to "macro". You can use this object to record and plot the training and validation recall.

    metric = recallMetric(AverageType="macro")
    metric = 
      RecallMetric with properties:
                      Name: "Recall"
               AverageType: "macro"
        ClassificationMode: "single-label"
             NetworkOutput: []

    Specify the recall metric in the training options. To plot the recall during training, set Plots to "training-progress". To output the values during training, set Verbose to true.

    options = trainingOptions("adam", ...
        MaxEpochs=5, ...
        Metrics=metric, ...
        ValidationData=imdsVal, ...
        ValidationFrequency=50, ...
        Plots="training-progress", ...

    Train the network using the trainnet function.

    [net,info] = trainnet(imdsTrain,layers,"crossentropy",options);
        Iteration    Epoch    TimeElapsed    LearnRate    TrainingLoss    ValidationLoss    TrainingRecall    ValidationRecall
        _________    _____    ___________    _________    ____________    ______________    ______________    ________________
                0        0       00:00:02        0.001                            13.488                                0.0984
                1        1       00:00:02        0.001          13.974                            0.072727                    
               50        1       00:00:21        0.001          2.7427            2.7444           0.67098                0.65
              100        2       00:00:37        0.001          1.2925            1.2188           0.73746                0.79
              150        3       00:00:55        0.001         0.65133           0.79837           0.86382              0.8408
              200        4       00:01:11        0.001         0.19458            0.5328           0.94948              0.8892
              250        5       00:01:29        0.001         0.14988            0.4882            0.9506              0.8948
              290        5       00:01:43        0.001          0.2756           0.40297           0.94064              0.9104
    Training stopped: Max epochs completed

    Access the loss and recall values for the validation data.

    ans=7×3 table
        Iteration     Loss      Recall
        _________    _______    ______
             0        13.488    0.0984
            50        2.7444      0.65
           100        1.2188      0.79
           150       0.79837    0.8408
           200        0.5328    0.8892
           250        0.4882    0.8948
           290       0.40297    0.9104

    More About

    expand all

    Version History

    Introduced in R2023b