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

Network layer for deep learning

Description

Layers that define the architecture of neural networks for deep learning.

Creation

To specify the architecture of a neural network with all layers connected sequentially, create an array of layers directly. To specify the architecture of a network where layers can have multiple inputs or outputs, use a LayerGraph object. Use the following functions to create different layer types.

Layer TypeFunction
Image input layerimageInputLayer
Sequence input layersequenceInputLayer
2-D convolutional layerconvolution2dLayer
2-D transposed convolutional layertransposedConv2dLayer
Fully connected layerfullyConnectedLayer
Long short-term memory (LSTM) layerLSTMLayer
Rectified linear unit (ReLU) layerreluLayer
Leaky rectified linear unit (ReLU) layerleakyReluLayer
Clipped rectified linear unit (ReLU) layerclippedReluLayer
Batch normalization layerbatchNormalizationLayer
Channel-wise local response normalization (LRN) layercrossChannelNormalizationLayer
Dropout layerdropoutLayer
Addition layeradditionLayer
Depth concatenation layerdepthConcatenationLayer
Average pooling layeraveragePooling2dLayer
Max pooling layermaxPooling2dLayer
Max unpooling layermaxUnpooling2dLayer
Softmax layersoftmaxLayer
Classification layerclassificationLayer
Regression layerregressionLayer

For an example showing how to create a layer array, see Construct Network Architecture.

Alternatively, you can import layers from Caffe using importCaffeLayers.

Object Functions

trainNetworkTrain neural network for deep learning

Examples

expand all

Define a convolutional neural network architecture for classification with 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]
layers = 
  6x1 Layer array with layers:

     1   ''   Image Input             28x28x3 images with 'zerocenter' normalization
     2   ''   Convolution             10 5x5 convolutions with stride [1  1] and padding [0  0  0  0]
     3   ''   ReLU                    ReLU
     4   ''   Fully Connected         10 fully connected layer
     5   ''   Softmax                 softmax
     6   ''   Classification Output   crossentropyex

layers is a Layer object.

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

input = imageInputLayer([28 28 3]);
conv = convolution2dLayer([5 5],10);
relu = reluLayer;
fc = fullyConnectedLayer(10);
sm = softmaxLayer;
co = classificationLayer;

layers = [ ...
    input
    conv
    relu
    fc
    sm
    co]
layers = 
  6x1 Layer array with layers:

     1   ''   Image Input             28x28x3 images with 'zerocenter' normalization
     2   ''   Convolution             10 5x5 convolutions with stride [1  1] and padding [0  0  0  0]
     3   ''   ReLU                    ReLU
     4   ''   Fully Connected         10 fully connected layer
     5   ''   Softmax                 softmax
     6   ''   Classification Output   crossentropyex

Define a convolutional neural network architecture for classification with 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 by selecting the first layer.

layers(1)
ans = 
  ImageInputLayer with properties:

                Name: ''
           InputSize: [28 28 3]

   Hyperparameters
    DataAugmentation: 'none'
       Normalization: 'zerocenter'

View the input size of the image input layer.

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. Assume 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

Create the middle layers of the network. First, create a convolutional layer with twelve 4-by-3 filters, a ReLU layer, a local response normalization layer, and a max pooling layer with 2-by-2 nonoverlapping pooling regions.

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

Add a convolutional layer with sixteen 5-by-5 filters, a ReLU layer, a local response normalization layer, and a max pooling with 2-by-2 nonoverlapping pooling regions.

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

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

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

Final Layers

Construct the final layers of the network. Assume 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, 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?