# tf_agents.networks.NestMap

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/networks/nest_map.py#L52-L226) |

The `NestMap` network processes nested inputs via nested layers.

Inherits From: [`Network`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/networks/Network)

#### View aliases


**Main aliases**

[`tf_agents.networks.nest_map.NestMap`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/networks/NestMap)

<br />

    tf_agents.networks.NestMap(
        nested_layers: https://www.tensorflow.org/agents/api_docs/python/tf_agents/typing/types/NestedLayer,
        input_spec: typing.Optional[types.NestedTensorSpec] = None,
        name: typing.Optional[typing.Text] = None
    )

It is a TF-Agents network that can be used to process nested inputs.

Stateful Keras layers (e.g. LSTMCell, RNN, LSTM, TF-Agents DynamicUnroll)
are all supported. The `state_spec` of `NestMap` has a structure matching
that of `nested_layers`.

`NestMap` can be used in conjunction with `NestFlatten` and a combiner
(e.g. [`tf.keras.layers.Add`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Add) or [`tf.keras.layers.Concatenate`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Concatenate)) to process
and aggregate in a preprocessing step.

#### Usage:

    net = NestMap({"inp1": layer1, "inp2": layer2})
    outputs, next_state = net({"inp1": inp1, "inp2": inp2}, state)

<br />

<br />

<br />

| ## Args ||
|---|---|
| `nested_layers` | A nest of layers and/or networks. These will be used to process the inputs (input nest structure will have to match this structure). Any layers that are subclasses of `tf.keras.layers.{RNN,LSTM,GRU,...}` are wrapped in [`tf_agents.keras_layers.RNNWrapper`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/keras_layers/RNNWrapper). |
| `input_spec` | (Optional.) A nest of [`tf.TypeSpec`](https://www.tensorflow.org/api_docs/python/tf/TypeSpec) representing the input observations. The structure of `input_spec` must match that of `nested_layers`. |
| `name` | (Optional.) Network name. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `TypeError` | If any of the layers are not instances of keras `Layer`. |
| `ValueError` | If `input_spec` is provided but its nest structure does not match that of `nested_layers`. |
| `RuntimeError` | If not [`tf.executing_eagerly()`](https://www.tensorflow.org/api_docs/python/tf/executing_eagerly); as this is required to be able to create deep copies of layers in `layers`. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `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. |
| `nested_layers` | <br /> <br /> |
| `state_spec` | <br /> <br /> |

<br />

## Methods

### `copy`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/networks/nest_map.py#L157-L175)

    copy(
        **kwargs
    ) -> 'NestMap'

Make a copy of a `NestMap` instance.

> [!NOTE]
> **Note:** A copy of a `NestMap` instance always performs a deep copy of the underlying layers, so the new instance will not share weights with the original - but it will start with the same weights.

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A deep copy of this network. ||

<br />

### `create_variables`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/networks/network.py#L188-L252)

    create_variables(
        input_tensor_spec=None, **kwargs
    )

Force creation of the network's variables.

Return output specs.

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| 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 a `DistributionSpec`. ||

<br />

<br />

<br />

<br />

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

<br />

### `get_initial_state`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/networks/network.py#L465-L476)

    get_initial_state(
        batch_size=None
    )

Returns an initial state usable by the network.

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A nested object of type `self.state_spec` containing properly initialized Tensors. ||

<br />

### `get_layer`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/networks/network.py#L279-L314)

    get_layer(
        name=None, index=None
    )

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).

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | String, name of layer. |
| `index` | Integer, index of layer. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A layer instance. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | In case of invalid layer name or index. |

<br />

### `summary`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/networks/network.py#L316-L339)

    summary(
        line_length=None, positions=None, print_fn=None
    )

Prints a string summary of the network.

<br />

<br />

<br />

| Args ||
|---|---|
| `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. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | if `summary()` is called before the model is built. |

<br />