Deep learning accuracy metric
AccuracyMetric object to track the network accuracy when you
train a deep neural network.
To plot the metrics during training, in the training options, specify
"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
You can also access the metrics after training using the
ValidationHistory fields from
the second output of the
metric = accuracyMetric
AccuracyMetric object. You can then specify
Metrics name-value argument in the
function. This metric is valid only for classification tasks.
With no additional options specified, this syntax is equivalent to setting
Metrics="accuracy" in the training options.
Name — Metric name
"Accuracy" (default) | string scalar | character vector
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
NetworkOutput — Name of output layer to apply metric to
 (default) | string scalar | character vector
This property is read-only.
Name of the output layer to apply the metric to, specified as
string scalar, or a character vector. When the value is
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
name-value argument. To apply built-in metrics to multiple outputs, you must create
a metric object for each output.
AverageType — Type of averaging to use
"micro" (default) |
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.
ClassificationMode — Type of classification task
"single-label" (default) |
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
If the final layer has an output size of two, such as with a softmax layer, use
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).
Plot and Record Accuracy During Training
Plot and record the training and validation accuracy 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];
AccuracyMetric object and set
"macro". You can use this object to record and plot the training and validation accuracy.
metric = accuracyMetric(AverageType="macro")
metric = AccuracyMetric with properties: Name: "Accuracy" AverageType: "macro" ClassificationMode: "single-label" NetworkOutput: 
Specify the accuracy metric in the training options. To plot the accuracy during training, set
"training-progress". To output the values during training, set
options = trainingOptions("adam", ... MaxEpochs=5, ... Metrics=metric, ... ValidationData=imdsVal, ... ValidationFrequency=50, ... Plots="training-progress", ... Verbose=true);
Train the network using the
[net,info] = trainnet(imdsTrain,layers,"crossentropy",options);
Iteration Epoch TimeElapsed LearnRate TrainingLoss ValidationLoss TrainingAccuracy ValidationAccuracy _________ _____ ___________ _________ ____________ ______________ ________________ __________________ 0 0 00:00:03 0.001 13.488 9.84 1 1 00:00:03 0.001 13.974 7.2727 50 1 00:00:26 0.001 2.7427 2.7444 67.098 65 100 2 00:00:44 0.001 1.2925 1.2188 73.746 79 150 3 00:01:00 0.001 0.65133 0.79837 86.382 84.08 200 4 00:01:18 0.001 0.19458 0.5328 94.948 88.92 250 5 00:01:38 0.001 0.14988 0.4882 95.06 89.48 290 5 00:01:58 0.001 0.2756 0.40297 94.064 91.04 Training stopped: Max epochs completed
Access the loss and accuracy values for the validation data.
ans=7×3 table Iteration Loss Accuracy _________ _______ ________ 0 13.488 9.84 50 2.7444 65 100 1.2188 79 150 0.79837 84.08 200 0.5328 88.92 250 0.4882 89.48 290 0.40297 91.04
Introduced in R2023b