addInputLayer - Add input layer to network - MATLAB (original) (raw)

Add input layer to network

Since R2022b

Syntax

Description

[netUpdated](#mw%5Fddc033d1-1712-452e-8ccd-6e40955b5889) = addInputLayer([net](#mw%5F9d0bcfb0-7917-4330-b675-d3703b7da80c%5Fsep%5Fmw%5F3ffb42b3-6af7-4c22-82b3-4fb0f6a399f2),[layer](#mw%5Feb75d70d-ca7c-42ad-9c75-0420c6e0b120)) adds the input layer layer to the network net by connecting the input layer to the first unconnected input in net. If the updated network supports automatic initialization, then the function automatically initializes the learnable parameters of the network.

example

[netUpdated](#mw%5Fddc033d1-1712-452e-8ccd-6e40955b5889) = addInputLayer([net](#mw%5F9d0bcfb0-7917-4330-b675-d3703b7da80c%5Fsep%5Fmw%5F3ffb42b3-6af7-4c22-82b3-4fb0f6a399f2),[layer](#mw%5Feb75d70d-ca7c-42ad-9c75-0420c6e0b120),[inputName](#mw%5F22104687-4dec-48d0-a562-19041188279e)) also specifies which unconnected input the function connects to the input layer.

example

[netUpdated](#mw%5Fddc033d1-1712-452e-8ccd-6e40955b5889) = addInputLayer(___,Initialize=[tf](#mw%5Ff4d015cc-101c-4c07-bc8a-e20b1b85ea1e)) also specifies whether to initialize the output network using any of the previous syntaxes.

Examples

collapse all

Add Input Layer to dlnetwork

Create an uninitialized dlnetwork object that does not have an input layer.

layers = [ convolution2dLayer(5,20) reluLayer maxPooling2dLayer(2,Stride=2) fullyConnectedLayer(10) softmaxLayer];

net = dlnetwork(layers,Initialize=false)

net = dlnetwork with properties:

     Layers: [5x1 nnet.cnn.layer.Layer]
Connections: [4x2 table]
 Learnables: [4x3 table]
      State: [0x3 table]
 InputNames: {'conv'}
OutputNames: {'softmax'}
Initialized: 0

View summary with summary.

Create an image input layer with an input size of [28 28 1].

layer = imageInputLayer([28 28 1],Normalization="none");

Add the input layer to the network.

net = addInputLayer(net,layer)

net = dlnetwork with properties:

     Layers: [6x1 nnet.cnn.layer.Layer]
Connections: [5x2 table]
 Learnables: [4x3 table]
      State: [0x3 table]
 InputNames: {'imageinput'}
OutputNames: {'softmax'}
Initialized: 1

View summary with summary.

View the Initialized property. Because the network contains all the information required for initialization, the output network is initialized.

Connect Input Layers to Specified dlnetwork Inputs

Create an uninitialized dlnetwork object that has two unconnected inputs.

layers = [ convolution2dLayer(5,16,Name="conv") batchNormalizationLayer reluLayer fullyConnectedLayer(50) flattenLayer concatenationLayer(1,2,Name="cat") fullyConnectedLayer(10) softmaxLayer];

net = dlnetwork(layers,Initialize=false);

Create an image input layer to connect to the convolution layer.

layer = imageInputLayer([28 28 1],Normalization="none");

Connect the image input layer to the "conv" layer. Because the network does not contain the information required to initialize the network, the returned network is uninitialized.

net = addInputLayer(net,layer,"conv")

net = dlnetwork with properties:

     Layers: [9x1 nnet.cnn.layer.Layer]
Connections: [8x2 table]
 Learnables: [8x3 table]
      State: [2x3 table]
 InputNames: {'imageinput'  'cat/in2'}
OutputNames: {'softmax'}
Initialized: 0

View summary with summary.

Create a feature input layer to connect to the second input of the concatenation layer.

layer = featureInputLayer(1);

Connect the feature input layer to the "in2" input of the "cat" layer. Because the network now contains the information required to initialize the network, the returned network is initialized.

net = addInputLayer(net,layer,"cat/in2")

net = dlnetwork with properties:

     Layers: [10x1 nnet.cnn.layer.Layer]
Connections: [9x2 table]
 Learnables: [8x3 table]
      State: [2x3 table]
 InputNames: {'imageinput'  'input'}
OutputNames: {'softmax'}
Initialized: 1

View summary with summary.

Visualize the network in a plot.

Figure contains an axes object. The axes object contains an object of type graphplot.

Input Arguments

collapse all

net — Neural network

dlnetwork object

Neural network, specified as a dlnetwork object.

layer — Input layer to add

Layer object

Input layer to add, specified as a Layer object.

inputName — Unconnected network input which the function connects the layer

string scalar | character vector | cell array containing a character vector

Unconnected network input which the function connects the layer, specified as a string scalar, a character vector, or a cell array containing a character vector.

Data Types: char | string | cell

tf — Flag to initialize output network

"auto" (default) | true or 1 | false or 0

Flag to initialize the learnable parameters of the output network, specified as one of these values:

Output Arguments

collapse all

netUpdated — Updated network

dlnetwork object

Updated network, returned as a dlnetwork object.

The function reorders the layers in the Layers property of the network such that the input layer appears immediately before the layer that it is connected to.

Version History

Introduced in R2022b