# tf.contrib.eager.Network

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/network.py#L73-L525) |

Represents the composition of a set of Layers.

Inherits From: [`Layer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/layers/Layer)

    tf.contrib.eager.Network(
        name=None
    )

*Deprecated* . Please inherit from [`tf.keras.Model`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/Model), and see its documentation
for details. [`tf.keras.Model`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/Model) should be a drop-in replacement for
`tfe.Network` in most cases, but note that `track_layer` is no longer
necessary or supported. Instead, `Layer` instances are tracked on attribute
assignment (see the section of [`tf.keras.Model`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/Model)'s documentation on
subclassing). Since the output of `track_layer` is often assigned to an
attribute anyway, most code can be ported by simply removing the `track_layer`
calls.

[`tf.keras.Model`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/Model) works with all TensorFlow `Layer` instances, including those
from [`tf.layers`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/layers), but switching to the [`tf.keras.layers`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/layers) versions along with
the migration to [`tf.keras.Model`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/keras/Model) is recommended, since it will preserve
variable names. Feel free to import it with an alias to avoid excess typing
:).

`Network` implements the `Layer` interface and adds convenience methods for
managing sub-`Layer`s, such as listing variables.

`Layer`s (including other `Network`s) should be added via `track_layer`. They
can then be used when overriding the [`Network.call`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/networks/network/DistributionNetwork#call) method:

    class TwoLayerNetwork(tfe.Network):

      def __init__(self, name):
        super(TwoLayerNetwork, self).__init__(name=name)
        self.layer_one = self.track_layer(tf.compat.v1.layers.Dense(16,
        input_shape=(8,)))
        self.layer_two = self.track_layer(tf.compat.v1.layers.Dense(1,
        input_shape=(16,)))

      def call(self, inputs):
        return self.layer_two(self.layer_one(inputs))

After constructing an object and calling the `Network`, a list of variables
created by tracked `Layer`s is available via [`Network.variables`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/networks/network/Network#variables):

    net = TwoLayerNetwork(name="net")
    output = net(tf.ones([1, 8]))
    print([v.name for v in net.variables])

This example prints variable names, one kernel and one bias per
[`tf.compat.v1.layers.Dense`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/layers/Dense) layer:

    ['net/dense/kernel:0',
     'net/dense/bias:0',
     'net/dense_1/kernel:0',
     'net/dense_1/bias:0']

These variables can be passed to a `Saver` ([`tf.compat.v1.train.Saver`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/train/Saver), or
[`tf.contrib.eager.Saver`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/contrib/eager/Saver) when executing eagerly) to save or restore the
`Network`, typically alongside a global step and
[`tf.compat.v1.train.Optimizer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/train/Optimizer)
variables when checkpointing during training.

Note that the semantics of calling a `Network` with graph execution (i.e. not
executing eagerly) may change slightly in the future. Currently stateful ops
are pruned from the graph unless they or something that depends on them is
executed in a session, but this behavior is not consistent with eager
execution (where stateful ops are executed eagerly). `Layer`s from [`tf.layers`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/layers)
do not depend on this pruning and so will not be affected, but `Network`s
which rely on stateful ops being added to the graph but not executed (e.g. via
custom `Layer`s which manage stateful ops) may break with this change.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `name` | The name to use for this `Network`. If specified, it must be unique in the context where this `Network` is first (1) added to another `Network` (in which case it must not share a name with other `Layers` added to that `Network`), or (2) built/called (in which case no other 'top-level' `Network`s may share this name). If unspecified or None, the `Network` will be named using its class name, with a number appended if necessary for uniqueness (e.g. MyNetwork -\> 'my_network_1'). |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If `name` is not valid. Note that some naming errors will instead be raised when the `Network` is called. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `graph` | DEPRECATED FUNCTION <br /> > [!WARNING] > **Warning:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Stop using this property because tf.layers layers no longer track their graph. |
| `layers` | <br /> |
| `scope_name` | <br /> <br /> |

<br />

## Methods

### `get_layer`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/network.py#L413-L447)

    get_layer(
        name=None, index=None
    )

Get a contained [`tf.compat.v1.layers.Layer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/layers/Layer) either by name or index.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | String matching one of the names of a contained `Layer`. Note that the names of `Layer`s added to `Network`s may not be unique when doing layer sharing (i.e. adding a `Layer` to this `Network` which was already added to another `Network`). The lowest index `Layer` with a matching name will be returned. |
| `index` | Integer in \[0, number of layers). Layers are assigned an index by the order they are added. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A [`tf.compat.v1.layers.Layer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/layers/Layer) object. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If neither or both of 'index' or 'name' is specified, or the lookup failed. |

<br />

### `track_layer`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/eager/python/network.py#L350-L411)

    track_layer(
        layer
    )

Track a Layer in this Network.

`Network` requires that all `Layer`s used in `call()` be tracked so that the
`Network` can export a complete list of variables.

<br />

<br />

<br />

| Args ||
|---|---|
| `layer` | A [`tf.compat.v1.layers.Layer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/layers/Layer) object. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The passed in `layer`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `RuntimeError` | If **init** has not been called. |
| `TypeError` | If `layer` is the wrong type. |
| `ValueError` | If a `Layer` with the same name has already been added. |

<br />