# tf_agents.networks.mask_splitter_network.MaskSplitterNetwork

<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/mask_splitter_network.py#L28-L152) |

Separates and passes the observation and mask to the wrapped network.

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

    tf_agents.networks.mask_splitter_network.MaskSplitterNetwork(
        splitter_fn: https://www.tensorflow.org/agents/api_docs/python/tf_agents/typing/types/Splitter,
        wrapped_network: https://www.tensorflow.org/agents/api_docs/python/tf_agents/networks/Network,
        passthrough_mask: bool = False,
        input_tensor_spec: Optional[https://www.tensorflow.org/agents/api_docs/python/tf_agents/typing/types#DistributionSpecV2] = None,
        name: Text = 'MaskSplitterNetwork'
    )

In some environments, different sets of actions are available given different
observations. To represent this, `env.observation` actually contains both the
raw observation, and an action mask for this particular observation. Our
network needs to know how to split `env.observation` into these two parts. The
raw observation will be fed into the wrapped network, and the action mask will
be optionally passed into the wrapped network to ensure that the network only
outputs possible actions.

The network uses the `splitter_fn` to separate the observation from the action
mask (i.e. `observation, mask = splitter_fn(inputs)`). Depending on the value
of `pass_mask_to_wrapped_network` the mask is passed into the wrapped network
or dropped, i.e.

    obs, mask = splitter_fn(inputs)

    wrapped_network(obs, ...)  # If pass_mask_to_wrapped_network is `False`

    wrapped_network(obs, ..., mask=mask)  # Otherwise, i.e. it is `True`.

In each case the observation part is fed into the `wrapped_network`. It is
expected that the input spec of wrapped network is compatible with the
observation part of the input of the `MaskSplitterNetwork`.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `splitter_fn` | A function used to process observations with action constraints (i.e. mask). *Note* : The input spec of the wrapped network must be compatible with the network-specific half of the output of the `splitter_fn` on the input spec. |
| `wrapped_network` | A [`network.Network`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/networks/Network) used to process the network-specific part of the observation, and the mask passed as the `mask` parameter of the method `call` of the wrapped network. |
| `passthrough_mask` | If it is set to `True`, the mask is fed into wrapped network. If it is set to `False`, the mask portion of the input is dropped and *not* fed into the wrapped network. |
| `input_tensor_spec` | A `tensor_spec.TensorSpec` or a tuple of specs representing the input observations including the specs of the action constraints. |
| `name` | A string representing name of the network. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If input_tensor_spec is not an instance of network.InputSpec. |

<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. |
| `output_spec` | <br /> <br /> |
| `state_spec` | <br /> <br /> |

<br />

## Methods

### `copy`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/networks/mask_splitter_network.py#L143-L152)

    copy(
        **kwargs
    )

Create a shallow copy of this network.

> [!NOTE]
> **Note:** Network layer weights are *never* copied. This method recreates the `Network` instance with the same arguments it was initialized with (excepting any new kwargs).

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

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

<br />

### `create_variables`

[View source](https://github.com/tensorflow/agents/blob/v0.19.0/tf_agents/networks/mask_splitter_network.py#L130-L141)

    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 />