identityLayer
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
Input Arguments
name
— Layer name
""
(default) | character vector | string scalar
Properties
Name
— Layer name
""
(default) | character vector | string scalar
NumInputs
— Number of inputs
1
(default)
This property is read-only.
Number of inputs to the layer, returned as 1
. This layer accepts a
single input only.
Data Types: double
InputNames
— Input names
{'in'}
(default)
This property is read-only.
Input names, returned as {'in'}
. This layer accepts a single input
only.
Data Types: cell
NumOutputs
— Number of outputs
1
(default)
This property is read-only.
Number of outputs from the layer, returned as 1
. This layer has a
single output only.
Data Types: double
OutputNames
— Output names
{'out'}
(default)
This property is read-only.
Output names, returned as {'out'}
. This layer has a single output
only.
Data Types: cell
Examples
Create Layer Array with Identity Layer
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 Residual Network That Includes Identity Layer
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)
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)