Main Content

aucMetric

Deep learning area under ROC curve (AUC) metric

Since R2023b

    Description

    Use an AUCMetric object to track the area under ROC curve (AUC) value 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.

    Creation

    Description

    example

    metric = aucMetric creates an AUCMetric 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="auc" in the training options.

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

    Properties

    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.

    Note

    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

    Object Functions

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

    Examples

    collapse all

    Plot and record the training and validation area under the ROC curve (AUC) value 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.

    unzip("DigitsData.zip")
    imds = imageDatastore("DigitsData", ...
        IncludeSubfolders=true, ...
        LabelSource="foldernames");

    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])
        convolution2dLayer(5,20)
        reluLayer
        maxPooling2dLayer(2,Stride=2)
        fullyConnectedLayer(10)
        softmaxLayer];

    Create an AUCMetric object and set AverageType to "macro". You can use this object to record and plot the training and validation AUC.

    metric = aucMetric(AverageType="macro")
    metric = 
      AUCMetric with properties:
    
                 Name: "AUC"
          AverageType: "macro"
        NetworkOutput: []
    
    

    Specify the AUC metric in the training options. To plot the AUC 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", ...
        Verbose=true);

    Train the network using the trainnet function.

    [net,info] = trainnet(imdsTrain,layers,"crossentropy",options);
        Iteration    Epoch    TimeElapsed    LearnRate    TrainingLoss    ValidationLoss    TrainingAUC    ValidationAUC
        _________    _____    ___________    _________    ____________    ______________    ___________    _____________
                0        0       00:00:16        0.001                            13.488                         0.50008
                1        1       00:00:17        0.001          13.974                          0.47203                 
               50        1       00:00:59        0.001          2.7427            2.7444        0.88371          0.88024
              100        2       00:01:34        0.001          1.2925            1.2188        0.94273           0.9493
              150        3       00:02:02        0.001         0.65133           0.79837        0.98089          0.96664
              200        4       00:02:33        0.001         0.19458            0.5328        0.99742          0.98253
              250        5       00:03:05        0.001         0.14988            0.4882        0.99634          0.98461
              290        5       00:03:30        0.001          0.2756           0.40297        0.99307          0.98819
    Training stopped: Max epochs completed
    

    Access the loss and AUC values for the validation data.

    info.ValidationHistory
    ans=7×3 table
        Iteration     Loss        AUC  
        _________    _______    _______
    
             0        13.488    0.50008
            50        2.7444    0.88024
           100        1.2188     0.9493
           150       0.79837    0.96664
           200        0.5328    0.98253
           250        0.4882    0.98461
           290       0.40297    0.98819
    
    

    More About

    expand all

    Version History

    Introduced in R2023b