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 and Options

  • 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.
  • If the first layer has more than one input port, the NetChain will have those same input ports.
  • If the last layer has more than one output port, the NetChain will have those same output ports.
  • NetChain supports the following special layer specifications:
  • Ramp,LogisticSigmoid,ElementwiseLayer[f]
    nLinearLayer[n]
    {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 following optional parameter can be included:
  • LearningRateMultipliersAutomaticlearning rate multipliers for the trainable arrays
  • 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
    Restricted["Integer",n]an integer between 1 and n
    Restricted["Integer",{m,n}]an integer between m and n
    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
    FeatureExtractorFunction[]a feature extractor function
  • 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
    RandomSeedingAutomatichow to seed pseudorandom generators, if any
    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.
  • Normal[NetChain[]] returns a list or association of the layers used to construct the network. EdgeList[NetChain[]] returns the list of connections in the network.
  • NetChain[][[spec]] extracts the layer specified by spec from the net.
  • Transformation on a NetChain[] network can be achieved with NetReplacePart, NetReplace, NetRename, NetFlatten, NetDelete, NetTake, NetDrop, NetInsert, .
  • Options[NetChain] gives the list of default options to construct the network. Options[NetChain[]] gives the list of default options to evaluate the network on some data.
  • Information[NetChain[]] gives a report about the network.
  • Information[NetChain[],prop] gives the value of the property prop of NetChain[]. Possible properties are the same as for NetGraph.

Examples

open allclose all

Basic Examples  (1)

Construct a chain consisting of two layers:

Apply the net to an input:

Scope  (4)

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

Initialize the net with random weights:

Apply the net to an input vector:

Construct a chain with explicitly named layers:

Extract the second layer by name:

Use Part syntax to extract the first layer:

Construct a chain:

Construct a new network consisting of the first two layers:

Construct a new network consisting of the last two layers:

Use special syntax for ElementwiseLayer and LinearLayer:

Properties & Relations  (5)

NetChain objects can be used as layers in a NetGraph:

NetGraph objects with one output and one input port can be used as layers inside NetChain objects:

NetChain objects can be converted to the equivalent NetGraph objects by writing NetGraph[chain]:

The layers used to construct a NetChain can be extracted using Normal:

Use Information[chain,"SummaryGraphic"] to get a Graphics[] expression that shows the underlying connectivity of a chain:

Neat Examples  (1)

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

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

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

Wolfram Research (2016), NetChain, Wolfram Language function, https://reference.wolfram.com/language/ref/NetChain.html (updated 2022).

Text

Wolfram Research (2016), NetChain, Wolfram Language function, https://reference.wolfram.com/language/ref/NetChain.html (updated 2022).

CMS

Wolfram Language. 2016. "NetChain." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/NetChain.html.

APA

Wolfram Language. (2016). NetChain. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetChain.html

BibTeX

@misc{reference.wolfram_2024_netchain, author="Wolfram Research", title="{NetChain}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/NetChain.html}", note=[Accessed: 17-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_netchain, organization={Wolfram Research}, title={NetChain}, year={2022}, url={https://reference.wolfram.com/language/ref/NetChain.html}, note=[Accessed: 17-November-2024 ]}