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.

SeriesNetwork class

Series network class

Description

A series network class that contains the layers in the trained network. A series network is a network with layers arranged one after another. There is a single input and a single output.

Construction

trainedNet = trainNetwork(X,Y,layers,options) returns a trained network. trainedNet is a SeriesNetwork object.

For more information on training a convolutional neural network, see trainNetwork.

Input Arguments

expand all

Images, specified as a 4-D numeric array. The array is arranged so that the first three dimensions are the height, width, and channels, and the last dimension indexes the individual images.

Data Types: single | double

Class labels, specified as an array of categorical responses.

Data Types: categorical

An array of network layers, specified as a Layer object.

Training options, specified as an object returned by the trainingOptions function.

For the solver 'sgdm' (stochastic gradient descent with momentum), trainingOptions returns a TrainingOptionsSGDM object.

Methods

activationsCompute convolutional neural network layer activations
classifyClassify data using a trained convolutional neural network
predictPredict responses using a trained convolutional neural network

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects (MATLAB) in the MATLAB® documentation.

Examples

expand all

This example shows how to create a convolutional neural network for classification.

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-5000 array, where 28 is the height and 28 is the width of the images. 1 is the number of channels and 5000 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. Set 'Verbose' to false to suppress detailed output on the training progress.

options = trainingOptions('sgdm','Verbose',false);

Train the network.

net = trainNetwork(XTrain,TTrain,layers,options);

Run the trained network on a test set and predict the image labels (digits).

[XTest,TTest] = digitTest4DArrayData;
YTest = classify(net,XTest);

Calculate the accuracy.

accuracy = sum(YTest==TTest)/numel(TTest)
accuracy =

    0.9770

Load sample data.

[trainImages,~,trainAngles] = digitTrain4DArrayData;

digitTrain4DArrayData loads the digit training set as 4-D array data. XTrain is a 28-by-28-by-1-by-5000 array, where 28 is the height and 28 is the width of the images. 1 is the number of channels and 5000 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(12,25)
    reluLayer
    fullyConnectedLayer(1)
    regressionLayer]
layers = 

  5x1 Layer array with layers:

     1   ''   Image Input         28x28x1 images with 'zerocenter' normalization
     2   ''   Convolution         25 12x12 convolutions with stride [1  1] and padding [0  0]
     3   ''   ReLU                ReLU
     4   ''   Fully Connected     1 fully connected layer
     5   ''   Regression Output   mean-squared-error

Set training options. Specify the solver to "Stochastic gradient descent with momentum", and the initial learn rate to 0.001.

options = trainingOptions('sgdm','InitialLearnRate',.001);

Train the network.

net = trainNetwork(trainImages,trainAngles,layers,options)
Training on single GPU.
Initializing image normalization.
|=========================================================================================|
|     Epoch    |   Iteration  | Time Elapsed |  Mini-batch  |  Mini-batch  | Base Learning|
|              |              |  (seconds)   |     Loss     |     RMSE     |     Rate     |
|=========================================================================================|
|            1 |            1 |         0.08 |     351.3609 |        26.51 |       0.0010 |
|            2 |           50 |         4.18 |     117.3655 |        15.32 |       0.0010 |
|            3 |          100 |         8.24 |      55.0872 |        10.50 |       0.0010 |
|            4 |          150 |        12.25 |      57.7389 |        10.75 |       0.0010 |
|            6 |          200 |        16.34 |      41.2819 |         9.09 |       0.0010 |
|            7 |          250 |        20.40 |      35.3071 |         8.40 |       0.0010 |
|            8 |          300 |        24.44 |      33.7413 |         8.21 |       0.0010 |
|            9 |          350 |        28.48 |      30.2453 |         7.78 |       0.0010 |
|           11 |          400 |        32.54 |      30.9003 |         7.86 |       0.0010 |
|           12 |          450 |        36.56 |      26.3620 |         7.26 |       0.0010 |
|           13 |          500 |        40.60 |      18.0586 |         6.01 |       0.0010 |
|           15 |          550 |        44.61 |      22.5642 |         6.72 |       0.0010 |
|           16 |          600 |        48.67 |      18.3832 |         6.06 |       0.0010 |
|           17 |          650 |        52.72 |      17.1514 |         5.86 |       0.0010 |
|           18 |          700 |        56.80 |      18.7614 |         6.13 |       0.0010 |
|           20 |          750 |        60.90 |      18.8418 |         6.14 |       0.0010 |
|           21 |          800 |        64.95 |      19.0134 |         6.17 |       0.0010 |
|           22 |          850 |        69.00 |      13.2792 |         5.15 |       0.0010 |
|           24 |          900 |        73.08 |      13.5988 |         5.22 |       0.0010 |
|           25 |          950 |        77.10 |      11.4873 |         4.79 |       0.0010 |
|           26 |         1000 |        81.20 |      15.5789 |         5.58 |       0.0010 |
|           27 |         1050 |        85.25 |      11.2764 |         4.75 |       0.0010 |
|           29 |         1100 |        89.25 |      12.1369 |         4.93 |       0.0010 |
|           30 |         1150 |        93.38 |      10.2268 |         4.52 |       0.0010 |
|           30 |         1170 |        95.02 |       9.1796 |         4.28 |       0.0010 |
|=========================================================================================|

net = 

  SeriesNetwork with properties:

    Layers: [5×1 nnet.cnn.layer.Layer]

Run the trained network on a test set and predict the digit angles of rotation.

[testImages,~,testAngles] = digitTest4DArrayData;
predictedTestAngles = predict(net,testImages);

Calculate the root-mean-square error (RMSE).

predictionError = testAngles - predictedTestAngles;
squares = predictionError.^2;
rmse = sqrt(mean(squares))
rmse =

  single

    6.1924

Tips

  • A convolutional neural network model saved with R2016a can only be loaded with a GPU, because in R2016a, the learnable parameters are stored as gpuArrays. Once you load the model, you can resave it in R2016b. This saves the learnable parameters as MATLAB arrays. You can then change the execution environment to CPU while running the network.

Introduced in R2016a

Was this topic helpful?