tf.compat.v1.zeros_initializer

Initializer that generates tensors initialized to 0.

Migrate to TF2

tf.compat.v1.zeros_initializer is compatible with eager execution and tf.function.

To migrate to TF2, please use tf.zeros_initializer instead. The dtype argument in 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 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

Used in the notebooks

Used in the guide

Methods

from_config

View source

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

View source

Returns the configuration of the initializer as a JSON-serializable dict.

Returns
A JSON-serializable Python dict.

__call__

View source

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.