represents a net in which net is folded over a sequence of inputs, maintaining a recurrent state.


represents a net in which net is folded over its inputs, maintaining a recurrent state by feeding the outi of each step back to the ini of the next step.


treats the inputs consti to net as being constant instead of mapped over.


returns the sequence of values for the specific outputs outi.


  • NetFoldOperator[net] is equivalent to NetFoldOperator[net,{"Output"->"State"}] and specifies that the output called "Output" of the folded net will be fed back to the input called "State".
  • NetFoldOperator[] represents a net that takes one or more sequences and outputs one or more sequences of the same length.
  • NetFoldOperator[net,{"out1""in1",}] accepts the same inputs as net, except for those ini given by one of the feedback connections "outi"->"ini".
  • NetFoldOperator[net,feedback,constants] will map over any inputs that are not specified in feedback or constants. All mapped-over inputs should have the same length.
  • In the typical case in which net has two inputs named "Input" and "State" and one output named "Output", the single argument form NetFoldOperator[net] accepts a single input sequence {x1,x2,,xn} named "Input" and an initial state s0 named "State" and produces a sequence of states {s1,s2,,sn} named "Output", computed by applying the net repeatedly to xi and si-1 to compute si , effectively using net[<|"Input"->xi,"State"->si-1|>].
  • NetFoldOperator[net,{"out1""in1",}] will have state ports named for all the recurrent ports ini whose values are fed back.
  • When given a NumericArray as input, the output will be a NumericArray.
  • Within a NetGraph, a connection of the form src->NetPort[layer,"state"] can be used to provide the initial value of a recurrent state of a NetFoldOperator. Similarly, a connection of the form NetPort[layer,"state"]->dst can be used to obtain the final value of a recurrent state. If left unconnected, these states can be tracked by NetStateObject.
  • NetFoldOperator[net,] can be seen as allowing a form of weight sharing between multiple copies of net, one for each sequence element that is being mapped over.
  • NetFoldOperator[,"Input"->shape] allows the shape of the input to be specified. Possible forms for shape are:
  • NetEncoder[]encoder producing an array
    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×


open all close all

Basic Examples  (1)

Create a "core" net that takes a state and an input and produces an output:

Click for copyable input

Evaluate the core on an input:

Click for copyable input

Create a NetFoldOperator that uses the core to operate on sequences of arrays:

Click for copyable input

Evaluate the net on an input sequence of length-2 vectors:

Click for copyable input

Scope  (5)

Applications  (1)

Introduced in 2017
Updated in 2019