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.

Layer class

Network layer

Description

Network layer class containing the layer information. Each layer in the architecture of a convolutional neural network is of Layer class.

Construction

To define the architecture of a convolutional neural network, create a vector of layers directly. Alternatively, create the layers individually, and then concatenate them. See Construct Network Architecture.

Copy Semantics

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

Indexing

You can access the properties of a layer in the network architecture by indexing into the vector of layers and using dot notation. For example, an image input layer is the first layer in a convolutional neural network. To access the InputSize property of the image input layer, use layers(1).InputSize. For more examples, see Access Layers and Properties in Layer Array.

Examples

expand all

This example shows how to construct a network architecture.

Define a convolutional neural network architecture for classification, with one convolutional layer, a ReLU layer, and a fully connected layer.

cnnarch = [...
    imageInputLayer([28 28 3])
    convolution2dLayer([5 5],10)
    reluLayer
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer
    ];

Alternatively you can create the layers individually and then concatenate them.

input = imageInputLayer([28 28 3]);
conv = convolution2dLayer([5 5],10);
relu = reluLayer;
fcl = fullyConnectedLayer(10);
sml = softmaxLayer;
col = classificationLayer;

cnnarch = [...
    input
    conv
    relu
    fcl
    sml
    col];

cnnarch is a 6-by-1 array of layers.

Display the class for this array of layers.

class(cnnarch)
ans =

    'nnet.cnn.layer.Layer'

cnnarch is a Layer object.

This example shows how to access layers and properties in a layer array.

Define a convolutional neural network architecture for classification, with only one convolutional layer, a ReLU layer, and a fully connected layer.

layers = [...
    imageInputLayer([28 28 3])
    convolution2dLayer([5 5],10)
    reluLayer
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer];

Display the image input layer.

layers(1)
ans = 

  ImageInputLayer with properties:

                Name: ''
           InputSize: [28 28 3]

   Hyperparameters
    DataAugmentation: 'none'
       Normalization: 'zerocenter'

Extract the input size.

layers(1).InputSize
ans =

    28    28     3

Display the stride for the convolutional layer.

layers(2).Stride
ans =

     1     1

Access the bias learn rate factor for the fully connected layer.

layers(4).BiasLearnRateFactor
ans =

     1

Create typical convolutional neural networks for classification and regression problems.

Create a Convolutional Neural Network for Classification

Input Layer

Create the input layer. Suppose that the input images are grayscale with size 28-by-28. Create an image input layer of size 28-by-28-by-1.

inputLayer = imageInputLayer([28 28 1]);

Middle Layers

Next create the middle layers of the network. First, create a convolutional layer with 12 4-by-3 filters, a ReLU layer, a local response normalization layer, and a max pooling layer with 2-by-2 non-overlapping pooling regions.

middleLayers = [...
    convolution2dLayer([4 3],12)
    reluLayer
    crossChannelNormalizationLayer(4)
    maxPooling2dLayer(2,'Stride',2)];

Next, add a convolutional layer with 16 5-by-5 filters, a ReLU layer, a local response normalization layer, and a max pooling with 2-by-2 non-overlapping pooling regions.

middleLayers = [...
    middleLayers
    convolution2dLayer(5,16)
    reluLayer
    crossChannelNormalizationLayer(4)
    maxPooling2dLayer(2,'Stride',2)];

Next, add a fully connected layer of size 256, and a ReLU layer.

middleLayers = [...
    middleLayers
    fullyConnectedLayer(256)
    reluLayer];

Final Layers

Construct the final layers of the network. Suppose that there are 10 classes. Create a fully connected layer of size 10 followed by a softmax layer and a classification layer.

finalLayers = [...
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer];

Combine the input, middle and final layers to create the network architecture.

layers = [...
    inputLayer
    middleLayers
    finalLayers];

Alternatively, you can create the full network at once.

layers = [...
    imageInputLayer([28 28 1])
    convolution2dLayer([4 3],12)
    reluLayer
    crossChannelNormalizationLayer(4)
    maxPooling2dLayer(2,'Stride',2)
    convolution2dLayer(5,16)
    reluLayer
    crossChannelNormalizationLayer(4)
    maxPooling2dLayer(2,'Stride',2)
    fullyConnectedLayer(256)
    reluLayer
    fullyConnectedLayer(10)
    softmaxLayer
    classificationLayer];

Create a Convolutional Neural Network for Regression

Create a convolutional neural network for regression using a similar architecture as the classification network. Replace the final three layers with a fully connected layer of size 1 and a regression layer.

layers = [...
    imageInputLayer([28 28 1])
    convolution2dLayer([4 3],12)
    reluLayer
    crossChannelNormalizationLayer(4)
    maxPooling2dLayer(2,'Stride',2)
    convolution2dLayer(5,16)
    reluLayer
    crossChannelNormalizationLayer(4)
    maxPooling2dLayer(2,'Stride',2)
    fullyConnectedLayer(256)
    reluLayer
    fullyConnectedLayer(1)
    regressionLayer];

Introduced in R2016a

Was this topic helpful?