Type Module
Namespace tensorflow
Parent AutoTrackable
Interfaces IModule
Base neural network module class. A module is a named container for
tf.Variable
s, other tf.Module
s and
functions which apply to user input. For example a dense layer in a neural
network might be implemented as a tf.Module
:
You can use the Dense layer as you would expect:
By subclassing tf.Module
instead of `object` any tf.Variable
or
tf.Module
instances assigned to object properties can be collected using
the `variables`, `trainable_variables` or `submodules` property:
Subclasses of tf.Module
can also take advantage of the `_flatten` method
which can be used to implement tracking of any other types. All tf.Module
classes have an associated tf.name_scope
which can be used
to group operations in TensorBoard and create hierarchies for variable names
which can help with debugging. We suggest using the name scope when creating
nested submodules/parameters or for forward methods whose graph you might want
to inspect in TensorBoard. You can enter the name scope explicitly using
`with self.name_scope:` or you can annotate methods (apart from `__init__`)
with `@tf.Module.with_name_scope`.
Show Example
class Dense(tf.Module): def __init__(self, in_features, output_features, name=None): super(Dense, self).__init__(name=name) self.w = tf.Variable( tf.random.normal([input_features, output_features]), name='w') self.b = tf.Variable(tf.zeros([output_features]), name='b') def __call__(self, x): y = tf.matmul(x, self.w) + self.b return tf.nn.relu(y)
Methods
Properties
Public static methods
object with_name_scope_dyn<TClass>(object method)
Decorator to automatically enter the module name scope. ```
class MyModule(tf.Module):
@tf.Module.with_name_scope
def __call__(self, x):
if not hasattr(self, 'w'):
self.w = tf.Variable(tf.random.normal([x.shape[1], 64]))
return tf.matmul(x, self.w)
``` Using the above module would produce
mod.w
# ==>
```
tf.Variable
s and tf.Tensor
s whose
names included the module name: ```
mod = MyModule()
mod(tf.ones([8, 32]))
# ==> Parameters
-
object
method - The method to wrap.
Returns
-
object
- The original method wrapped such that it enters the module's name scope.
TClass with_name_scope<TClass>(PythonFunctionContainer method)
Decorator to automatically enter the module name scope. ```
class MyModule(tf.Module):
@tf.Module.with_name_scope
def __call__(self, x):
if not hasattr(self, 'w'):
self.w = tf.Variable(tf.random.normal([x.shape[1], 64]))
return tf.matmul(x, self.w)
``` Using the above module would produce
mod.w
# ==>
```
tf.Variable
s and tf.Tensor
s whose
names included the module name: ```
mod = MyModule()
mod(tf.ones([8, 32]))
# ==> Parameters
-
PythonFunctionContainer
method - The method to wrap.
Returns
-
TClass
- The original method wrapped such that it enters the module's name scope.
Public properties
object name get;
Returns the name of this module as passed or determined in the ctor. NOTE: This is not the same as the `self.name_scope.name` which includes
parent module names.
object name_dyn get;
Returns the name of this module as passed or determined in the ctor. NOTE: This is not the same as the `self.name_scope.name` which includes
parent module names.
object name_scope get;
Returns a
tf.name_scope
instance for this class.
object name_scope_dyn get;
Returns a
tf.name_scope
instance for this class.
object PythonObject get;
ValueTuple<object> submodules get;
Sequence of all sub-modules. Submodules are modules which are properties of this module, or found as
properties of modules which are properties of this module (and so on). ```
a = tf.Module()
b = tf.Module()
c = tf.Module()
a.b = b
b.c = c
assert list(a.submodules) == [b, c]
assert list(b.submodules) == [c]
assert list(c.submodules) == []
```
object submodules_dyn get;
Sequence of all sub-modules. Submodules are modules which are properties of this module, or found as
properties of modules which are properties of this module (and so on). ```
a = tf.Module()
b = tf.Module()
c = tf.Module()
a.b = b
b.c = c
assert list(a.submodules) == [b, c]
assert list(b.submodules) == [c]
assert list(c.submodules) == []
```
object trainable_variables get;
Sequence of variables owned by this module and it's submodules. Note: this method uses reflection to find variables on the current instance
and submodules. For performance reasons you may wish to cache the result
of calling this method if you don't expect the return value to change.
object trainable_variables_dyn get;
Sequence of variables owned by this module and it's submodules. Note: this method uses reflection to find variables on the current instance
and submodules. For performance reasons you may wish to cache the result
of calling this method if you don't expect the return value to change.
object variables get;
Sequence of variables owned by this module and it's submodules. Note: this method uses reflection to find variables on the current instance
and submodules. For performance reasons you may wish to cache the result
of calling this method if you don't expect the return value to change.
object variables_dyn get;
Sequence of variables owned by this module and it's submodules. Note: this method uses reflection to find variables on the current instance
and submodules. For performance reasons you may wish to cache the result
of calling this method if you don't expect the return value to change.