Main Content

identityLayer

Identity layer

Since R2024b

    Description

    An identity layer is a layer whose output is identical to its input. You can use an identity layer to create a skip connection, which allows the input to skip one or more layers in the main branch of a neural network. For more information about skip connections, see More About.

    Creation

    Description

    layer = identityLayer creates an identity layer and stores it in an IdentityLayer object.

    example

    layer = identityLayer(Name=name) additionally specifies the layer name.

    example

    Input Arguments

    expand all

    Layer name, specified as a character vector or string scalar. This argument sets the Name property of an IdentityLayer object. For Layer array input, the trainnet and dlnetwork functions automatically assign names to layers with the name "".

    Data Types: char | string

    Properties

    expand all

    Layer name, specified as a character vector or string scalar. For Layer array input, the trainnet and dlnetwork functions automatically assign names to layers with the name "".

    The IdentityLayer object stores this property as a character vector.

    Data Types: char | string

    This property is read-only.

    Number of inputs to the layer, returned as 1. This layer accepts a single input only.

    Data Types: double

    This property is read-only.

    Input names, returned as {'in'}. This layer accepts a single input only.

    Data Types: cell

    This property is read-only.

    Number of outputs from the layer, returned as 1. This layer has a single output only.

    Data Types: double

    This property is read-only.

    Output names, returned as {'out'}. This layer has a single output only.

    Data Types: cell

    Examples

    collapse all

    Include an identity layer in a layer array.

    layers = [ ...
        imageInputLayer([28 28 1])
        convolution2dLayer(5,20)
        identityLayer
        maxPooling2dLayer(2,Stride=2)
        fullyConnectedLayer(10)
        softmaxLayer]
    layers = 
      6x1 Layer array with layers:
    
         1   ''   Image Input       28x28x1 images with 'zerocenter' normalization
         2   ''   2-D Convolution   20 5x5 convolutions with stride [1  1] and padding [0  0  0  0]
         3   ''   Identity          Identity
         4   ''   2-D Max Pooling   2x2 max pooling with stride [2  2] and padding [0  0  0  0]
         5   ''   Fully Connected   10 fully connected layer
         6   ''   Softmax           softmax
    

    The third layer in layers is an IdentityLayer object that contains the properties of the identity layer.

    Display the properties of the identity layer.

    layers(3)
    ans = 
      IdentityLayer with properties:
    
        Name: ''
    
    

    Create a deep learning neural network that includes residual blocks and an identity layer. For more information about residual neural networks, see More About.

    Define Block Architecture

    Write a function that returns a network layer representing a residual block.

    function resblock = residualBlockLayer(name)
    
    reslayers = dlnetwork;
    
    layers = [
        identityLayer(Name="split")
        convolution2dLayer(3,32,Padding="same")
        batchNormalizationLayer
        reluLayer
        convolution2dLayer(3,32,Padding="same")
        batchNormalizationLayer
        additionLayer(2,Name="add")
        reluLayer];
    
    reslayers = addLayers(reslayers,layers);
    reslayers = connectLayers(reslayers,"split","add/in2");
    
    resblock = networkLayer(reslayers,Name=name);
    
    end

    The residualBlockLayer function accepts a layer name and returns a networkLayer object representing a residual block. The function uses identityLayer with the connectLayers function to create a skip connection between the input of the residual block and the second input of its final layer.

    Create and Analyze Residual Network

    Create a residual network using residualBlockLayer to generate the residual blocks.

    layers = [
        imageInputLayer([224 224 3])
        convolution2dLayer(7,32,Stride=2,Padding="same")
        batchNormalizationLayer
        reluLayer
        maxPooling2dLayer(3,Stride=2)
        residualBlockLayer("resBlock1")
        residualBlockLayer("resBlock2")
        globalAveragePooling2dLayer
        fullyConnectedLayer(5)
        softmaxLayer];
    
    resnet = dlnetwork(layers)
    resnet = 
      dlnetwork with properties:
    
             Layers: [10x1 nnet.cnn.layer.Layer]
        Connections: [9x2 table]
         Learnables: [22x3 table]
              State: [10x3 table]
         InputNames: {'imageinput'}
        OutputNames: {'softmax'}
        Initialized: 1
    
      View summary with summary.
    
    

    resnet is a dlnetwork object that represents a residual network. resnet contains two networkLayer objects corresponding to two residual blocks.

    Inspect Network

    Use the expandLayers and analyzeNetwork functions to inspect the residual block layers in the Deep Network Designer app. The leftmost pane in Deep Network Designer shows the network layers and their connections.

    xresnet = expandLayers(resnet);
    analyzeNetwork(xresnet)

    dnd_identitylayer.png

    The names of the layers inside the first and second residual blocks begin with resBlock1: and resBlock2:, respectively. The first layer inside each block is an identity layer that accepts input from outside the residual block. Each identity layer has a skip connection to an addition layer. The names of the identity layers are resBlock1:split and resBlock2:split, and the names of the addition layers are resBlock1:add and resBlock2:add.

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2024b