Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

predict

Class: SeriesNetwork

Predict responses using a trained convolutional neural network

You can predict class scores or numeric responses using a trained convolutional neural network (ConvNet, CNN) on either a CPU or GPU. Using a GPU requires the Parallel Computing Toolbox™ and a CUDA®-enabled NVIDIA® GPU with compute capability 3.0 or higher. Specify the hardware requirements using the ExecutionEnvironment name-value pair argument.

Syntax

YPred = predict(net,X)
YPred = predict(net,X,Name,Value)

Description

YPred = predict(net,X) predicts responses for data in X using the trained network net.

YPred = predict(net,X,Name,Value) predicts responses with the additional option specified by the Name,Value pair argument.

Input Arguments

expand all

Trained network, specified as a SeriesNetwork object, returned by the trainNetwork function.

Input data, specified as an array of a single image, a 4-D array of images, images stored as an ImageDatastore, or images or image paths in a table.

  • If X is a single image, then the dimensions correspond to the height, width, and channels of the image.

  • If X is an array of images, then the first three dimensions correspond to height, width, and channels of an image, and the fourth dimension corresponds to the image number.

  • Images that are stored as an ImageDatastore object. For more information about this data type, see ImageDatastore.

  • A table, where the first column contains either image paths or images.

Data Types: single | double | table

Name-Value Pair Arguments

Example: 'MiniBatchSize',256 specifies the mini-batch size as 256.

Specify optional comma-separated pair of Name,Value argument. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' ').

expand all

Size of mini-batches for prediction, specified as an integer number. Larger mini-batch sizes require more memory, but lead to faster predictions.

Example: 'MiniBatchSize',256

Data Types: single | double

Hardware resource for predict to run the network, specified as the comma-separated pair consisting of 'ExecutionEnvironment' and one of the following:

  • 'auto' — Use a GPU if it is available, otherwise use the CPU.

  • 'gpu' — Use the GPU. To use a GPU, you must have Parallel Computing Toolbox and a CUDA-enabled NVIDIA GPU with compute capability 3.0 or higher. If a suitable GPU is not available, predict returns an error message.

  • 'cpu' — Use the CPU.

Example: 'ExecutionEnvironment','cpu'

Data Types: char

Output Arguments

expand all

Predicted scores, returned as one of the following:

  • For a classification problem, Ypred is an n-by-k matrix, where n is the number of observations and k is the number of classes.

  • For a regression problem, the format of Ypred depends on the format of the responses in the training data. Ypred can be an

    • n-by-r numeric matrix, where n is the number of observations and r is the number of responses

    • h-by-w-by-c-by-n numeric array, where n is the number of observations and h-by-w-by-c is the size of a single response

Examples

expand all

Load the sample data.

[XTrain,TTrain] = digitTrain4DArrayData;

digitTrain4DArrayData loads the digit training set as 4-D array data. XTrain is a 28-by-28-by-1-by-4940 array, where 28 is the height and 28 is the width of the images. 1 is the number of channels and 4940 is the number of synthetic images of handwritten digits. TTrain is a categorical vector containing the labels for each observation.

Construct the convolutional neural network architecture.

layers = [imageInputLayer([28 28 1]);
          convolution2dLayer(5,20);
          reluLayer();
          maxPooling2dLayer(2,'Stride',2);
          fullyConnectedLayer(10);
          softmaxLayer();
          classificationLayer()];

Set the options to default settings for the stochastic gradient descent with momentum.

options = trainingOptions('sgdm');

Train the network.

rng(1)
net = trainNetwork(XTrain,TTrain,layers,options);
Training on single CPU.
Initializing image normalization.
|=========================================================================================|
|     Epoch    |   Iteration  | Time Elapsed |  Mini-batch  |  Mini-batch  | Base Learning|
|              |              |  (seconds)   |     Loss     |   Accuracy   |     Rate     |
|=========================================================================================|
|            1 |            1 |         0.56 |       2.3028 |       11.72% |       0.0100 |
|            2 |           50 |        17.07 |       2.2653 |       30.47% |       0.0100 |
|            3 |          100 |        33.83 |       1.5949 |       48.44% |       0.0100 |
|            4 |          150 |        50.75 |       1.2292 |       58.59% |       0.0100 |
|            6 |          200 |        68.65 |       1.0559 |       64.06% |       0.0100 |
|            7 |          250 |        86.97 |       1.0304 |       64.06% |       0.0100 |
|            8 |          300 |       104.85 |       0.7178 |       78.12% |       0.0100 |
|            9 |          350 |       122.68 |       0.6900 |       78.12% |       0.0100 |
|           11 |          400 |       139.96 |       0.5104 |       85.94% |       0.0100 |
|           12 |          450 |       156.90 |       0.4311 |       89.06% |       0.0100 |
|           13 |          500 |       173.84 |       0.2796 |       92.19% |       0.0100 |
|           15 |          550 |       190.99 |       0.2389 |       96.09% |       0.0100 |
|           16 |          600 |       207.15 |       0.2566 |       92.97% |       0.0100 |
|           17 |          650 |       223.88 |       0.1773 |       96.88% |       0.0100 |
|           18 |          700 |       240.29 |       0.1260 |       99.22% |       0.0100 |
|           20 |          750 |       256.88 |       0.1297 |      100.00% |       0.0100 |
|           21 |          800 |       273.22 |       0.1080 |       97.66% |       0.0100 |
|           22 |          850 |       290.02 |       0.1176 |       98.44% |       0.0100 |
|           24 |          900 |       306.83 |       0.0762 |      100.00% |       0.0100 |
|           25 |          950 |       323.40 |       0.0774 |      100.00% |       0.0100 |
|           26 |         1000 |       340.14 |       0.0877 |       99.22% |       0.0100 |
|           27 |         1050 |       357.61 |       0.0645 |       99.22% |       0.0100 |
|           29 |         1100 |       373.88 |       0.0624 |      100.00% |       0.0100 |
|           30 |         1150 |       390.18 |       0.0488 |      100.00% |       0.0100 |
|           30 |         1170 |       396.74 |       0.0816 |       99.22% |       0.0100 |
|=========================================================================================|

Run the trained network on a test set and predict the scores.

[XTest,TTest]= digitTest4DArrayData;
YTestPred = predict(net,XTest);

predict, by default, uses a CUDA-enabled GPU with compute capability 3.0, when available. You can also choose to run predict on a CPU using the 'ExecutionEnvironment','cpu' name-value pair argument.

Display the first 10 images in the test data and compare to the predictions from predict.

TTest(1:10,:)
ans = 

  10×1 categorical array

     0 
     0 
     0 
     0 
     0 
     0 
     0 
     0 
     0 
     0 

YTestPred(1:10,:)
ans =

  10×10 single matrix

  Columns 1 through 7

    0.9993    0.0000    0.0002    0.0003    0.0000    0.0000    0.0001
    0.8579    0.0000    0.0551    0.0003    0.0000    0.0002    0.0139
    0.9999    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
    0.9558    0.0000    0.0000    0.0000    0.0000    0.0000    0.0060
    0.9616    0.0000    0.0041    0.0001    0.0000    0.0000    0.0004
    0.9915    0.0000    0.0005    0.0000    0.0000    0.0000    0.0016
    0.9733    0.0000    0.0003    0.0000    0.0000    0.0000    0.0247
    1.0000    0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
    0.9126    0.0000    0.0016    0.0002    0.0003    0.0007    0.0001
    0.9409    0.0000    0.0102    0.0020    0.0001    0.0001    0.0278

  Columns 8 through 10

    0.0000    0.0000    0.0002
    0.0001    0.0035    0.0690
    0.0000    0.0000    0.0001
    0.0000    0.0010    0.0372
    0.0002    0.0335    0.0002
    0.0000    0.0044    0.0020
    0.0000    0.0016    0.0001
    0.0000    0.0000    0.0000
    0.0000    0.0012    0.0833
    0.0000    0.0143    0.0047

TTest contains the digits corresponding to the images in XTest. The columns of YTestPred contain predict's estimation of a probability that an image contains a particular digit. That is, the first column contains the probability estimate that the given image is digit 0, the second column contains the probability estimate that the image is digit 1, the third column contains the probability estimate that the image is digit 2, and so on. You can see that predict's estimation of probabilities for the correct digits are almost 1 and the probability for any other digit is almost 0. predict correctly estimates the first 10 observations as digit 0.

Algorithms

If the image data contains NaNs, predict propagates them through the network. If the network has ReLU layers, these layers ignore NaNs. However, if the network does not have a ReLU layer, then predict returns NaNs as predictions.

Alternatives

You can compute the predicted scores and the predicted classes from a trained network using the classify method.

You can also compute the activations from a network layer using the activations method.

Introduced in R2016a

Was this topic helpful?