NetChain

NetChain[{layer1,layer2,}]

specifies a neural net in which the output of layeri is connected to the input of layeri+1.

NetChain["name1"layer1,"name2"layer2,]

specifies a net consisting of a chain of explicitly named layers.

Details

  • The input of a NetChain is taken to be the input of the first layer.
  • The output of a NetChain is taken to be the output of the last layer.
  • All intermediary layers must have exactly one input and one output port.
  • NetChain[][data] gives the result of applying the net to data.
  • NetChain[][data,NetPortGradient["Input"]] gives the gradient of the output with respect to the input data.
  • NetChain supports the following special layer specifications:
  • nLinearLayer[n]
    Ramp,LogisticSigmoid,ElementwiseLayer[f]
    {layer1,layer2,}NetChain[{layer1,layer2,}]
  • Take and Drop work on the sequence of layers in NetChain objects, giving new NetChain objects.
  • Normal[NetChain[]] will return a list or association of the layers used to construct the chain.
  • The StandardForm of NetChain shows the a summary of the layers in the chain and the tensor dimensions of the output of each layer. Clicking a layer in the chain shows more information about that layer.
  • The TraditionalForm of NetChain shows a more publication-appropriate representation of the chain.
  • The overall input and output tensor shapes for the chain can be specified using "Input"->shape and "Output"->shape options for NetChain.
  • Possible forms for shape include:
  • "Real"a single real number
    "Integer"a single integer
    na vector of length n
    {n1,n2,}a tensor of dimensions n1×n2×
    "Varying"a variable-length vector
    {"Varying",n2,n3,}a variable-length sequence of tensors of dimensions n2×n3×
    NetEncoder[]an encoder (for input ports)
    NetDecoder[]a decoder (for output ports)
    "type"NetEncoder["type"] or NetDecoder["type"]
    {n,coder}an encoder or decoder mapped over a sequence of length n
  • Any of the lengths ni given as Automatic are inferred from the structure of the chain.
  • NetChain[][data,opts] specifies that options should be used in applying the net to data. Possible options include:
  • NetEvaluationMode"Test"what mode to use in performing evaluation
    TargetDevice"CPU"the target device on which to perform evaluation
  • With the setting NetEvaluationMode->"Training", layers such as DropoutLayer will behave as they do for training rather than ordinary evaluation.
  • NetChain[][[spec]] extracts the layer specified by spec from the net.

Examples

open allclose all

Basic Examples  (6)

Construct a chain consisting of one layer:

In[1]:=
Click for copyable input
Out[1]=

Apply the net to an input:

In[2]:=
Click for copyable input
Out[2]=

Construct a chain consisting of two layers:

In[1]:=
Click for copyable input
Out[1]=

Apply the net to an input:

In[2]:=
Click for copyable input
Out[2]=

Use special syntax for ElementwiseLayer and LinearLayer:

In[1]:=
Click for copyable input
Out[1]=

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

In[1]:=
Click for copyable input
Out[1]=

Initialize the net with random weights:

In[2]:=
Click for copyable input

Apply the net to an input vector:

In[3]:=
Click for copyable input
Out[3]=

Construct a chain with explicitly named layers:

In[1]:=
Click for copyable input
Out[1]=

Extract the second layer by name:

In[2]:=
Click for copyable input
Out[2]=

Use Part syntax to extract the first layer:

In[3]:=
Click for copyable input
Out[3]=

Construct a chain:

In[1]:=
Click for copyable input
Out[1]=

Construct a new network consisting of the first two layers:

In[2]:=
Click for copyable input
Out[2]=

Construct a new network consisting of the last two layers:

In[3]:=
Click for copyable input
Out[3]=

Properties & Relations  (5)

Neat Examples  (1)

See Also

NetModel  NetGraph  NetInitialize  NetTrain  NetExtract  LinearLayer  ElementwiseLayer  NetEncoder  NetDecoder

Introduced in 2016
(11.0)