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,}]
  • Normal[NetChain[]] will return a list or association of the layers used to construct the chain.
  • The StandardForm of 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 NetChain.
  • Possible forms for shape include:
  • "Real"a single real number
    "Integer"a single integer
    na vector of length n
    {n1,n2,}an array of dimensions n1×n2×
    "Varying"a vector whose length is variable
    {"Varying",n2,n3,}an array whose first dimension is variable and remaining dimensions are n2×n3×
    NetEncoder[]an encoder (for input ports)
    NetEncoder[{,"Dimensions"{n1,}}]an encoder mapped over an array of dimensions n1×
    NetDecoder[]a decoder (for output ports)
    NetDecoder[{,"InputDepth"n}}]a decoder applied to an array of rank 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:
  • BatchSizeAutomaticfor lists of inputs, the number of inputs to evaluate at once
    NetEvaluationMode"Test"what mode to use in performing evaluation
    TargetDevice"CPU"the target device on which to perform evaluation
    WorkingPrecision"Real32"the numerical precision used for evaluating the net
  • Possible settings for WorkingPrecision include:
  • "Real32"use single-precision real (32-bit)
    "Real64"use double-precision real (64-bit)
    "Mixed"use half-precision real for certain operations
  • WorkingPrecision->"Mixed" is only supported for TargetDevice->"GPU", where it can result in significant performance increases on certain devices.
  • When given a NumericArray as input, the output will be a NumericArray. In this case, its numeric type is derived from WorkingPrecision.
  • 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.
  • NetTake[NetChain[],{start,end}] returns the sequence of layers between start and end as a new NetChain.
  • NetDrop[NetChain[],{start,end}] removes a sequence of layers from a NetChain, returning a new chain.
  • NetInsert[NetChain[],layer,pos] inserts a new layer into a NetChain at a specified position.
  • NetDelete[NetChain[],layer] deletes one or more layers from a NetChain, returning a new chain.
  • NetFlatten[NetChain[]] will flatten away any nested chains that appear as layers of a NetChain.
  • NetReplacePart[NetChain[],pos->layer] can be used to replace an existing layer of a NetChain with a new layer.
  • NetReplace[NetChain[],patt->layer] can be used to replace layers matching patt within a NetGraph with a new layer.
  • NetRename[NetChain[],"old"->"new"] can be used to rename layers of a NetChain.

Examples

open all close all

Basic Examples  (2)

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]=

Scope  (4)

Properties & Relations  (5)

Neat Examples  (1)

Introduced in 2016
(11.0)
|
Updated in 2019
(12.0)