View source on GitHub |
Initializer that generates tensors initialized to 0.
tf.compat.v1.keras.initializers.Zeros(
dtype=tf.dtypes.float32
)
Migrate to TF2
tf.compat.v1.zeros_initializer
is compatible with eager execution
and tf.function
.
To migrate to TF2, please use tf.zerosinitializer
instead. The dtype
argument in <a href="../../../../../tf/compat/v1/keras/initializers/Zeros#init_">tf.compat.v1.zerosinitializer.init_()
does not exist in
tf.zerosinitializer.init_()
. However, you can specify the dtype
in
__call__()
in both cases.
Structural Mapping to Native TF2
Before:
initializer = tf.compat.v1.zeros_initializer(dtype=tf.float32)
variable = tf.Variable(initializer(shape=[3, 3]))
After:
initializer = tf.zeros_initializer()
variable = tf.Variable(initializer(shape=[3, 3], dtype=tf.float32))
How to Map Arguments
TF1 Arg Name | TF2 Arg Name | Note |
---|---|---|
dtype |
dtype |
In __call__() method |
partition_info |
- | (__call__ arg in TF1) Not supported |
Before & After Usage Example
Before:
initializer = tf.compat.v1.zeros_initializer(dtype=tf.float32)
tf.Variable(initializer(shape=[3])).numpy()
array([0., 0., 0.], dtype=float32)
tf.Variable(initializer(shape=[3, 3])).numpy()
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]], dtype=float32)
initializer = tf.compat.v1.zeros_initializer()
tf.Variable(initializer(shape=[3], dtype=tf.float32)).numpy()
array([0., 0., 0.], dtype=float32)
tf.Variable(initializer(shape=[3, 3], dtype=tf.float32)).numpy()
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]], dtype=float32)
After:
initializer = tf.zeros_initializer()
tf.Variable(initializer(shape=[3], dtype=tf.float32)).numpy()
array([0., 0., 0.], dtype=float32)
tf.Variable(initializer(shape=[3, 3], dtype=tf.float32)).numpy()
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]], dtype=float32)
Description
Methods
from_config
@classmethod
from_config( config )
Instantiates an initializer from a configuration dictionary.
Example:
initializer = RandomUniform(-1, 1)
config = initializer.get_config()
initializer = RandomUniform.from_config(config)
Args | |
---|---|
config
|
A Python dictionary. It will typically be the output of
get_config .
|
Returns | |
---|---|
An Initializer instance. |
get_config
get_config()
Returns the configuration of the initializer as a JSON-serializable dict.
Returns | |
---|---|
A JSON-serializable Python dict. |
__call__
__call__(
shape, dtype=None, partition_info=None
)
Returns a tensor object initialized as specified by the initializer.
Args | |
---|---|
shape
|
Shape of the tensor. |
dtype
|
Optional dtype of the tensor. If not provided use the initializer dtype. |
partition_info
|
Optional information about the possible partitioning of a tensor. |