NetFoldOperator
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.
Details and Options


- 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 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 >]. The initial state s0 is an array of zeros.
- 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,{"out1""in1",…}] will have state ports named for all the recurrent ports ini whose values are fed back.
- 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 n a 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×… - The following training parameter can be included:
-
LearningRateMultipliers Automatic learning rate multipliers for trainable arrays in the net - Options[NetFoldOperator] gives the list of default options to construct the operator. Options[NetFoldOperator[…]] gives the list of default options to evaluate the operator on some data.
- Information[NetFoldOperator[…]] gives a report about the operator.
- Information[NetFoldOperator[…],prop] gives the value of the property prop of NetFoldOperator[…]. Possible properties are the same as for NetGraph.
Examples
open allclose allBasic Examples (1)Summary of the most common use cases
Create a "core" net that takes a state and an input and produces an output:

https://wolfram.com/xid/0ywk5twv00ke-jih2ti

Evaluate the core on an input:

https://wolfram.com/xid/0ywk5twv00ke-xp1bkf

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

https://wolfram.com/xid/0ywk5twv00ke-55hcye

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

https://wolfram.com/xid/0ywk5twv00ke-3ke204

Scope (5)Survey of the scope of standard use cases
Construct a net that takes a weighted average of the state and input:

https://wolfram.com/xid/0ywk5twv00ke-k28iv4

Apply the net to an input and state:

https://wolfram.com/xid/0ywk5twv00ke-qq7ccq

Use NetFoldOperator to create a higher-order net that folds the average over an input sequence:

https://wolfram.com/xid/0ywk5twv00ke-7jlbbr

Fold the accumulator over a sequence of real numbers:

https://wolfram.com/xid/0ywk5twv00ke-zle13v

Use a NetMapOperator to map the folder over the color channels of an image, resulting in the rows of the image being accumulated:

https://wolfram.com/xid/0ywk5twv00ke-jhxo8n

Apply the operator to an image:

https://wolfram.com/xid/0ywk5twv00ke-u0y3c6

Create a net that can take a constant input that is used during the folding operation:

https://wolfram.com/xid/0ywk5twv00ke-el4kre


https://wolfram.com/xid/0ywk5twv00ke-r1q3ta

Apply the net to an input sequence, providing a value for the constant:

https://wolfram.com/xid/0ywk5twv00ke-s189q3

Create a net that has two independent states that are fed back. These states will additionally be used to provide the final output sequences:

https://wolfram.com/xid/0ywk5twv00ke-s4r0xz


https://wolfram.com/xid/0ywk5twv00ke-bocma1

Obtain the output sequences when given the input sequences:

https://wolfram.com/xid/0ywk5twv00ke-8de57c

Construct a net that combines elements from two sequences with a recurrent state using a simple nonlinear function:

https://wolfram.com/xid/0ywk5twv00ke-wq1aun


https://wolfram.com/xid/0ywk5twv00ke-d8tap4

Obtain the output sequence when given the input sequences:

https://wolfram.com/xid/0ywk5twv00ke-mf8np1


https://wolfram.com/xid/0ywk5twv00ke-gubvqw

Create a NetGraph that allows the initial and final states of a NetFoldOperator to be set and obtained:

https://wolfram.com/xid/0ywk5twv00ke-50odgk


https://wolfram.com/xid/0ywk5twv00ke-kvurdg

Apply the net to an input sequence, providing the initial state:

https://wolfram.com/xid/0ywk5twv00ke-ukypzy

The final state is the last element of the output sequence:

https://wolfram.com/xid/0ywk5twv00ke-v1bxir

If the initial and final states do not come from a larger network, it is also possible to use NetPort[All,"States"] to control these without using a NetGraph:

https://wolfram.com/xid/0ywk5twv00ke-w5mvpt

Applications (1)Sample problems that can be solved with this function
Create training data based on strings containing x's and y's and either Less, Greater or Equal by comparing the number of x's and y's. The training data consists of all possible sentences up to length 8:

https://wolfram.com/xid/0ywk5twv00ke-ulr9zg

Define a NetGraph representing the computation performed in a single time step by a basic recurrent layer with a state size of 5 and using a Ramp activation:

https://wolfram.com/xid/0ywk5twv00ke-v786id

Allow the previous NetGraph to be applied to a sequence by embedding it in a NetFoldOperator:

https://wolfram.com/xid/0ywk5twv00ke-fg1vit

Create a net with the NetFoldOperator defined previously to read an input string and predict one of Less, Greater or Equal:

https://wolfram.com/xid/0ywk5twv00ke-hdsfny

https://wolfram.com/xid/0ywk5twv00ke-snjzcc


https://wolfram.com/xid/0ywk5twv00ke-gshd7r

Apply the trained network to a list of inputs:

https://wolfram.com/xid/0ywk5twv00ke-8lyyd4

Measure the accuracy on the entire training set:

https://wolfram.com/xid/0ywk5twv00ke-2176cv

Wolfram Research (2017), NetFoldOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetFoldOperator.html (updated 2020).
Text
Wolfram Research (2017), NetFoldOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetFoldOperator.html (updated 2020).
Wolfram Research (2017), NetFoldOperator, Wolfram Language function, https://reference.wolfram.com/language/ref/NetFoldOperator.html (updated 2020).
CMS
Wolfram Language. 2017. "NetFoldOperator." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/NetFoldOperator.html.
Wolfram Language. 2017. "NetFoldOperator." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/NetFoldOperator.html.
APA
Wolfram Language. (2017). NetFoldOperator. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetFoldOperator.html
Wolfram Language. (2017). NetFoldOperator. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetFoldOperator.html
BibTeX
@misc{reference.wolfram_2025_netfoldoperator, author="Wolfram Research", title="{NetFoldOperator}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/NetFoldOperator.html}", note=[Accessed: 19-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_netfoldoperator, organization={Wolfram Research}, title={NetFoldOperator}, year={2020}, url={https://reference.wolfram.com/language/ref/NetFoldOperator.html}, note=[Accessed: 19-June-2025
]}