({{, (RefLink[NetChain, paclet:ref/NetChain])[{(Subscript[layer, 1]),(Subscript[layer, 2]),…}] 
specifies a neural net in which the output of (Subscript[layer, i]) is connected to the input of (Subscript[layer, i+1]).}, {, (RefLink[NetChain, paclet:ref/NetChain])[\[LeftAssociation]"(Subscript[name, 1])"->(Subscript[layer, 1]),"(Subscript[name, 2])"->(Subscript[layer, 2]),…\[RightAssociation]]
specifies a net consisting of a chain of explicitly named layers. }})

OpenersNotesSection(Spacer1)Details

The input of a (RefLink[NetChain, paclet:ref/NetChain]) is taken to be the input of the first layer.

The output of a (RefLink[NetChain, paclet:ref/NetChain]) is taken to be the output of the last layer.

All intermediary layers must have exactly one input and one output port.

(RefLink[NetChain, paclet:ref/NetChain])[…][data] gives the result of applying the net to data.

(RefLink[NetChain, paclet:ref/NetChain])[…][data,(RefLink[NetPortGradient, paclet:ref/NetPortGradient])["Input"]] gives the gradient of the output with respect to the input data.

(RefLink[NetChain, paclet:ref/NetChain]) supports the following special layer specifications:

(RefLink[Normal, paclet:ref/Normal])[NetChain[…]] will return a list or association of the layers used to construct the chain.

The (RefLink[StandardForm, paclet:ref/StandardForm]) of (RefLink[NetChain, paclet:ref/NetChain]) shows a summary of the layers in the chain and the array dimensions of the output of each layer. Clicking a layer in the chain shows more information about that layer.

The overall input and output array shapes for the chain can be specified using "Input"->shape and "Output"->shape options for (RefLink[NetChain, paclet:ref/NetChain]).

Possible forms for shape include:

Any of the lengths (Subscript[n, i]) given as (RefLink[Automatic, paclet:ref/Automatic]) are inferred from the structure of the chain.

(RefLink[NetChain, paclet:ref/NetChain])[…][data,opts] specifies that options should be used in applying the net to data. Possible options include:

Possible settings for (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision]) include:

(RefLink[WorkingPrecision, paclet:ref/WorkingPrecision])->"Mixed" is only supported for (RefLink[TargetDevice, paclet:ref/TargetDevice])->"GPU", where it can result in significant performance increases on certain devices.

When given a (RefLink[NumericArray, paclet:ref/NumericArray]) as input, the output will be a (RefLink[NumericArray, paclet:ref/NumericArray]). In this case, its numeric type is derived from (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision]).

With the setting (RefLink[NetEvaluationMode, paclet:ref/NetEvaluationMode])->"Training", layers such as (RefLink[DropoutLayer, paclet:ref/DropoutLayer]) will behave as they do for training rather than ordinary evaluation.

(RefLink[NetChain, paclet:ref/NetChain])[…][[spec]] extracts the layer specified by spec from the net.

(RefLink[NetTake, paclet:ref/NetTake])[NetChain[…],{start,end}] returns the sequence of layers between start and end as a new (RefLink[NetChain, paclet:ref/NetChain]).

(RefLink[NetDrop, paclet:ref/NetDrop])[NetChain[…],{start,end}] removes a sequence of layers from a (RefLink[NetChain, paclet:ref/NetChain]), returning a new chain.

(RefLink[NetInsert, paclet:ref/NetInsert])[NetChain[…],layer,pos] inserts a new layer into a (RefLink[NetChain, paclet:ref/NetChain]) at a specified position.

(RefLink[NetDelete, paclet:ref/NetDelete])[NetChain[…],layer] deletes one or more layers from a (RefLink[NetChain, paclet:ref/NetChain]), returning a new chain.

(RefLink[NetFlatten, paclet:ref/NetFlatten])[NetChain[…]] will flatten away any nested chains that appear as layers of a (RefLink[NetChain, paclet:ref/NetChain]).

(RefLink[NetReplacePart, paclet:ref/NetReplacePart])[NetChain[…],pos->layer] can be used to replace an existing layer of a (RefLink[NetChain, paclet:ref/NetChain]) with a new layer.

(RefLink[NetReplace, paclet:ref/NetReplace])[NetChain[…],patt->layer] can be used to replace layers matching patt within a (RefLink[NetGraph, paclet:ref/NetGraph]) with a new layer.

(RefLink[NetRename, paclet:ref/NetRename])[NetChain[…],"old"->"new"] can be used to rename layers of a (RefLink[NetChain, paclet:ref/NetChain]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (12)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Construct a chain consisting of one layer:

net=NetChain[{ElementwiseLayer[Ramp]}]

Apply the net to an input:

net[{-1,0,1}]

Construct a chain consisting of two layers:

net=NetChain[{ElementwiseLayer[Ramp],SummationLayer[]}]

Apply the net to an input:

net[{-2,-1,0,1,2}]

OpenersExampleSection1(Spacer1)Scope  (4)

Construct a chain consisting of two layers and specifying that the input is a length-2 vector:

net=NetChain[{LinearLayer[3],LinearLayer[4]},"Input"->2]

Initialize the net with random weights:

net=NetInitialize[net];

Apply the net to an input vector:

net[{1.2,0.23}]

Construct a chain with explicitly named layers:

net=NetChain[<|"linear1"->LinearLayer[3],"linear2"->LinearLayer[4]|>]

Extract the second layer by name:

NetExtract[net,"linear2"]

Use (RefLink[Part, paclet:ref/Part]) syntax to extract the first layer:

net[["linear1"]]

Construct a chain:

net=NetChain[{LinearLayer[1],LinearLayer[2],LinearLayer[3]}]

Construct a new network consisting of the first two layers:

NetTake[net,2]

Construct a new network consisting of the last two layers:

NetTake[net,-2]

Use special syntax for (RefLink[ElementwiseLayer, paclet:ref/ElementwiseLayer]) and (RefLink[LinearLayer, paclet:ref/LinearLayer]):

NetChain[{3,Tanh,4,Ramp,5}]

OpenersExampleSection3(Spacer1)Properties & Relations  (5)

(RefLink[NetChain, paclet:ref/NetChain]) objects can be used as layers in a (RefLink[NetGraph, paclet:ref/NetGraph]):

chain=NetChain[{ElementwiseLayer[Ramp],SummationLayer[]}]

NetGraph[{BatchNormalizationLayer[],chain},{1-> 2}]

(RefLink[NetGraph, paclet:ref/NetGraph]) objects with one output and one input port can be used as layers inside (RefLink[NetChain, paclet:ref/NetChain]) objects:

net=NetGraph[{LinearLayer[3],LinearLayer[3],CatenateLayer[]},{1->2,{1,2}->3}]

NetChain[{ElementwiseLayer[Ramp], net}]

(RefLink[NetChain, paclet:ref/NetChain]) objects can be converted to the equivalent (RefLink[NetGraph, paclet:ref/NetGraph]) objects by writing (RefLink[NetGraph, paclet:ref/NetGraph])[chain]:

net=NetChain[{1,2,3,4}]

NetGraph[net]

The layers used to construct a (RefLink[NetChain, paclet:ref/NetChain]) can be extracted using (RefLink[Normal, paclet:ref/Normal]):

net=NetChain[{1,Ramp,2,Tanh}]

Normal[net]

Use (RefLink[NetInformation, paclet:ref/NetInformation])[chain,"SummaryGraphic"] to get a (RefLink[Graphics, paclet:ref/Graphics])[…] expression that shows the underlying connectivity of a chain:

net=NetChain[{2,Ramp,3,ElementwiseLayer[Log[1-#]&]},"Input"->4]

NetInformation[net,"SummaryGraphic"]

OpenersExampleSection4(Spacer1)Neat Examples  (1)

Construct a base network that takes vector inputs of size 2 and produces vector outputs of size 3:

net=NetChain[{30,Sin,3,Tanh,3,LogisticSigmoid},"Input"->2]

Make a table of 16 randomly initialized copies of the base network:

nets=Table[
NetInitialize[net,Method->{"Random","Weights"->3,"Biases"->2},RandomSeeding->Automatic],16];

Plot them in a gallery by treating them as functions mapping (x,y) positions to (r,g,b) color values:

row=Range[-2,2,0.04];
coords=Tuples[row,2];
plot[net_]:=Image[Partition[net[coords],Length[row]]];
Multicolumn@Table[plot[net],{net,nets}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[NetModel, paclet:ref/NetModel])  ▪  (RefLink[NetGraph, paclet:ref/NetGraph])  ▪  (RefLink[NetInitialize, paclet:ref/NetInitialize])  ▪  (RefLink[NetTrain, paclet:ref/NetTrain])  ▪  (RefLink[NetExtract, paclet:ref/NetExtract])  ▪  (RefLink[LinearLayer, paclet:ref/LinearLayer])  ▪  (RefLink[ElementwiseLayer, paclet:ref/ElementwiseLayer])  ▪  (RefLink[NetEncoder, paclet:ref/NetEncoder])  ▪  (RefLink[NetDecoder, paclet:ref/NetDecoder])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Neural Networks in the Wolfram Language, paclet:tutorial/NeuralNetworksOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Neural Networks, paclet:guide/NeuralNetworks])}, {▪(RefLinkPlain[Machine Learning, paclet:guide/MachineLearning])}, {▪(RefLinkPlain[Image Computation for Microscopy, paclet:guide/ImageComputationForMicroscopy])}, {▪(RefLinkPlain[Audio Processing, paclet:guide/AudioProcessing])}})}})