|View source on GitHub|
Represents the composition of a set of Layers.
tf.contrib.eager.Network( name=None )
Deprecated. Please inherit from
tf.keras.Model, and see its documentation
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'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
tf.keras.Model works with all TensorFlow
Layer instances, including those
tf.layers, but switching to the
tf.keras.layers versions along with
the migration to
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
Layers, such as listing variables.
Layers (including other
Networks) should be added via
can then be used when overriding the
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
Layers is available via
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
['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
tf.contrib.eager.Saver when executing eagerly) to save or restore the
Network, typically alongside a global step and
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).
do not depend on this pruning and so will not be affected, but
which rely on stateful ops being added to the graph but not executed (e.g. via
Layers which manage stateful ops) may break with this change.
The name to use for this
get_layer( name=None, index=None )
Get a contained
tf.compat.v1.layers.Layer either by name or index.
String matching one of the names of a contained
||Integer in [0, number of layers). Layers are assigned an index by the order they are added.|
||If neither or both of 'index' or 'name' is specified, or the lookup failed.|
track_layer( layer )
Track a Layer in this Network.
Network requires that all
Layers used in
call() be tracked so that the
Network can export a complete list of variables.
The passed in
||If init has not been called.|