RSVP for your your local TensorFlow Everywhere event today!


The Sequential Network represents a sequence of Keras layers.

Inherits From: Network

Used in the notebooks

Used in the tutorials

It is a TF-Agents network that should be used instead of tf.keras.layers.Sequential. In contrast to keras Sequential, this layer can be used as a pure Layer in tf.functions and when exporting SavedModels, without having to pre-declare input and output shapes. In turn, this layer is usable as a preprocessing layer for TF Agents Networks, and can be exported via PolicySaver.

Stateful Keras layers (e.g. LSTMCell, RNN, LSTM, TF-Agents DynamicUnroll) are all supported. The state_spec of Sequential is a tuple whose length matches the number of stateful layers passed. If no stateful layers or networks are passed to Sequential then state_spec == ().


c = Sequential([layer1, layer2, layer3])
output, next_state = c(inputs, state)

layers A list or tuple of layers to compose. Any layers that are subclasses of tf.keras.layers.{RNN,LSTM,GRU,...} are wrapped in tf_agents.keras_layers.RNNWrapper.
input_spec (Optional.) A nest of tf.TypeSpec representing the input observations to the first layer.
name (Optional.) Network name.

ValueError If layers is empty.
ValueError If layers[0] is a generic Keras layer (not a TF-Agents network) and input_spec is None.
TypeError If any of the layers are not instances of keras Layer.
RuntimeError If not tf.executing_eagerly(); as this is required to be able to create deep copies of layers in layers.

input_tensor_spec Returns the spec of the input to the network of type InputSpec.
layers Get the list of all (nested) sub-layers used in this Network.



View source

Make a copy of a Sequential instance.

**kwargs Args to override when recreating this network. Commonly overridden args include 'name'.

A deep copy of this network.


View source

Force creation of the network's variables.

Return output specs.

input_tensor_spec (Optional). Override or provide an input tensor spec when creating variables.
**kwargs Other arguments to, e.g. training=True.

Output specs - a nested spec calculated from the outputs (excluding any batch dimensions). If any of the output elements is a tfp Distribution, the associated spec entry returned is None.

ValueError If no input_tensor_spec is provided, and the network did not provide one during construction.


View source

Returns an initial state usable by the network.

batch_size Tensor or constant: size of the batch dimension. Can be None in which case not dimensions gets added.

A nested object of type self.state_spec containing properly initialized Tensors.


View source

Retrieves a layer based on either its name (unique) or index.

If name and index are both provided, index will take precedence. Indices are based on order of horizontal graph traversal (bottom-up).

name String, name of layer.
index Integer, index of layer.

A layer instance.

ValueError In case of invalid layer name or index.


View source

Prints a string summary of the network.

line_length Total length of printed lines (e.g. set this to adapt the display to different terminal window sizes).
positions Relative or absolute positions of log elements in each line. If not provided, defaults to [.33, .55, .67, 1.].
print_fn Print function to use. Defaults to print. It will be called on each line of the summary. You can set it to a custom function in order to capture the string summary.

ValueError if summary() is called before the model is built.