NetStateObject
✖
NetStateObject
creates an object derived from net that represents a neural net with additional stored state information that is updated when the net is applied to data.
creates an object in which additional stored state information is initialized using seed.
Details

- NetStateObject[…][data] updates stored state information in the NetStateObject.
- State information is associated with the state ports of recurrent net layers such as LongShortTermMemoryLayer.
- NetStateObject will not store the state of layers whose state ports are initialized from other layers in a NetGraph.
- When a seed is not provided, initial values for recurrent states will consist of arrays of zeros.
- The current value of the stored states is given by NetExtract[NetStateObject[…],"States"].
Examples
open allclose allBasic Examples (3)Summary of the most common use cases

https://wolfram.com/xid/0btogq8kt8a6-lenhyi

Create a state object from the recurrent net:

https://wolfram.com/xid/0btogq8kt8a6-jo9f6l

Evaluate the state object on some data:

https://wolfram.com/xid/0btogq8kt8a6-q3dvup

Due to the presence of the stored states, the behavior of the state object can change between evaluations, even on the same input:

https://wolfram.com/xid/0btogq8kt8a6-6tvvja


https://wolfram.com/xid/0btogq8kt8a6-w5m1cq

Create a state object with a specified initial state:

https://wolfram.com/xid/0btogq8kt8a6-6r4d84

Create a classifier that predicts the next element of a sequence:

https://wolfram.com/xid/0btogq8kt8a6-50km1e

Train the classifier on a set of input sequences:

https://wolfram.com/xid/0btogq8kt8a6-11hss3

Create a state object and use it to efficiently generate a maximum-likelihood sequence, starting from a single 1:

https://wolfram.com/xid/0btogq8kt8a6-frwekw


https://wolfram.com/xid/0btogq8kt8a6-1vpktq

Applications (1)Sample problems that can be solved with this function
Training an English character-level language model. First, create 300,000 training examples of 25 characters each from two novels:

https://wolfram.com/xid/0btogq8kt8a6-mvbupv

https://wolfram.com/xid/0btogq8kt8a6-1kl129
The data is of the form of a classification problem: given a sequence of characters, predict the next one. A sample of the data:

https://wolfram.com/xid/0btogq8kt8a6-8jfew7

Obtain the list of all characters in the text:

https://wolfram.com/xid/0btogq8kt8a6-7280lz

Define a net that takes in a string of characters and returns a prediction for the next character:

https://wolfram.com/xid/0btogq8kt8a6-xklmy7
Train the net. This can take up to an hour on a CPU; it is recommended set option TargetDevice to "GPU" if a supported GPU is available:

https://wolfram.com/xid/0btogq8kt8a6-h5r4f5

Predict the next character, given a sequence of characters:

https://wolfram.com/xid/0btogq8kt8a6-qpr8fs

Generate 100 characters of text, given starting text:

https://wolfram.com/xid/0btogq8kt8a6-0u6153

https://wolfram.com/xid/0btogq8kt8a6-8365bs

One can get more interesting text by sampling from the probability distribution of predictions:

https://wolfram.com/xid/0btogq8kt8a6-4ln4uh

https://wolfram.com/xid/0btogq8kt8a6-1xrxrq

Properties & Relations (3)Properties of the function, and connections to other functions
If the initial value of a recurrent layer's state is provided by a connection in a NetGraph, that state will not be stored by NetStateObject.
Create a graph that uses a connection to provide the initial value of the state of a BasicRecurrentLayer:

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

This graph cannot be used inside a NetStateObject, as there are no states left to store:

https://wolfram.com/xid/0btogq8kt8a6-5ajeac


The current value of the stored states can be obtained using NetExtract.
First create a NetStateObject:

https://wolfram.com/xid/0btogq8kt8a6-71as95

Apply the object to some data:

https://wolfram.com/xid/0btogq8kt8a6-7bp3fz

Extract the current value of the states:

https://wolfram.com/xid/0btogq8kt8a6-scfztm

For recurrent nets, using a NetStateObject is equivalent to manually keeping track of the recurrent states via NetPort[All,"States"].
To see this, create a classifier that predicts the next element of a sequence:

https://wolfram.com/xid/0btogq8kt8a6-nn7f9i

Train the classifier on a set of input sequences:

https://wolfram.com/xid/0btogq8kt8a6-ddl6e0

Create a state object and use it to efficiently generate a maximum-likelihood sequence, starting from a single 1:

https://wolfram.com/xid/0btogq8kt8a6-h2y8gj


https://wolfram.com/xid/0btogq8kt8a6-65uvwt

Generate from the trained net using NetPort[All,"States"] to set and get the recurrent states, which yields the same result:

https://wolfram.com/xid/0btogq8kt8a6-itejp8

Wolfram Research (2018), NetStateObject, Wolfram Language function, https://reference.wolfram.com/language/ref/NetStateObject.html.
Text
Wolfram Research (2018), NetStateObject, Wolfram Language function, https://reference.wolfram.com/language/ref/NetStateObject.html.
Wolfram Research (2018), NetStateObject, Wolfram Language function, https://reference.wolfram.com/language/ref/NetStateObject.html.
CMS
Wolfram Language. 2018. "NetStateObject." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NetStateObject.html.
Wolfram Language. 2018. "NetStateObject." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NetStateObject.html.
APA
Wolfram Language. (2018). NetStateObject. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetStateObject.html
Wolfram Language. (2018). NetStateObject. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NetStateObject.html
BibTeX
@misc{reference.wolfram_2025_netstateobject, author="Wolfram Research", title="{NetStateObject}", year="2018", howpublished="\url{https://reference.wolfram.com/language/ref/NetStateObject.html}", note=[Accessed: 26-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_netstateobject, organization={Wolfram Research}, title={NetStateObject}, year={2018}, url={https://reference.wolfram.com/language/ref/NetStateObject.html}, note=[Accessed: 26-March-2025
]}