This example shows how to use metric functions to evaluate the results of an experiment. By default, when you run a deep learning experiment, Experiment Manager computes the loss, accuracy (for classification experiments), and root mean squared error (for regression experiments) for each trial in your experiment. To compute other measures, create your own metric function. For example, you can define metric functions to:
Test the prediction performance of a trained network.
Evaluate the training progress by computing the slope of the validation loss over the final epoch.
Display the size of the network used in an experiment that uses different network architectures for each trial.
When each trial finishes training, Experiment Manager evaluates the metric functions and displays their values in the results table.
In this example, you train a network to classify images of handwritten digits. Two metric functions determine how well the trained network identifies the images of the numerals one and seven. For more information on using Experiment Manager to train a network for image classification, see Sweep Hyperparameters to Train a Classification Network.
Add a metric function to an experiment.
1. In the Experiment pane, under Metrics, click Add.
2. In the Add metric dialog box, enter a name for the metric function and click OK. If you enter the name of a function that already exists in the project, Experiment Manager adds it to the experiment. Otherwise, Experiment Manager creates a function defined by a default template.
3. Select the name of the metric function and click Edit. The metric function opens in MATLAB Editor.
The input to a metric function is a
struct with three fields:
trainedNetwork is the
SeriesNetwork object or
DAGNetwork object returned by the
trainNetwork function. For more information, see
trainingInfo is a
struct containing the training information returned by the
trainNetwork function. For more information, see
parameters is a
struct with fields from the hyperparameter table.
The output of a custom metric function must be a scalar number, a logical value, or a string.
First, open the example. Experiment Manager loads a project with a preconfigured experiment that you can inspect and run. To open the experiment, in the Experiment Browser, double-click the name of the experiment (
An experiment definition consists of a description, a hyperparameter table, a setup function, and a collection of metric functions to evaluate the results of the experiment. For more information, see Configure Deep Learning Experiment.
The Description box contains a textual description of the experiment. For this example, the description is:
Classification of digits, evaluating results by using metric functions: - OnesAsSevens returns the percentage of 1s misclassified as 7s. - SevensAsOnes returns the percentage of 7s misclassified as 1s.
The Hyperparameter Table contains the names and values of the hyperparameters used in the experiment. When you run the experiment, Experiment Manager sweeps through the hyperparameter values and trains the network multiple times. Each trial uses a different combination of the hyperparameter values specified in the table. This example uses the hyperparameters
The Setup Function configures the training data, network architecture, and training options for the experiment. To inspect the setup function, under Setup Function, click Edit. The setup function opens in MATLAB Editor.
In this example, the setup function has three sections.
Load Image Data defines image datastores containing the training and validation data. This example loads images from the Digits data set. For more information on this data set, see Image Data Sets.
Define Network Architecture defines the architecture for a convolutional neural network for deep learning classification. This example uses the default classification network provided by the setup function template.
Specify Training Options defines a
object for the experiment. The example loads the values for the training options
'Momentum' from the hyperparameter table.
The Metrics section specifies optional functions that evaluate the results of the experiment. Experiment Manager evaluates these functions each time it finishes training the network. To inspect a metric function, select the name of the metric function and click Edit. The metric function opens in MATLAB Editor.
This example includes two metric functions.
OnesAsSevens returns percentage of images of the numeral one that the trained network misclassifies as sevens.
SevensAsOnes returns percentage of images of the numeral seven that the trained network misclassifies as ones.
Each of these functions uses the trained network to classify the entire Digits data set. Then, the functions determine the number of images for which the actual label and the predicted label disagree. For example, the function
OnesAsSevens computes the number of images with an actual label of
'1' and a predicted label of
function metricOutput = SevensAsOnes(trialInfo)
actualValue = '7'; predValue = '1';
net = trialInfo.trainedNetwork;
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet', ... 'nndemos','nndatasets','DigitDataset'); imds = imageDatastore(digitDatasetPath, ... 'IncludeSubfolders',true, ... 'LabelSource','foldernames');
YActual = imds.Labels; YPred = classify(net,imds);
K = sum(YActual == actualValue & YPred == predValue); N = sum(YActual == actualValue);
metricOutput = 100*K/N;
Similarly, the function
SevensAsOnes computes the number of images with an actual label of
'7' and a predicted label of
On the Experiment Manager tab, click Run. Experiment Manager trains the network defined by the setup function six times. Each trial uses a different combination of hyperparameters. A table of results displays the metric function values for each trial.
To find the best result for your experiment, sort the table of results. For example, find the trial with the smallest number of misclassified ones.
Point to the OnesAsSevens column.
Click the triangle icon.
Select Sort in Ascending Order.
Similiarly, find the trial with the smallest number of misclassified sevens by opening the drop-down menu for the SevensAsOnes column and selecting Sort in Ascending Order.
If no single trial minimizes both metric functions simultaneously, consider giving preference to a trial that ranks well for each metric. For instance, in these results, trial 6 has the second smallest value for each metric function.
In the Experiment Browser, right-click the name of the project and select Close Project. Experiment Manager saves your results and closes all of the experiments contained in the project.