Create a zero-filled buffer
Allocate a known-size tensor for padding, masks, accumulators, or other initial values.
TensorFlow · Function
Creates a tensor with a specified shape whose elements are all zero. The output dtype defaults to float32 and can be changed with dtype.
tf.zeros constructs a Tensor from the requested shape and fills every element with zero. The element type defaults to float32, but dtype can be changed; an optional layout can make the result a DTensor.
tf.zeros(
shape,
dtype=tf.dtypes.float32,
name=None,
layout=None
)| Name | Type | Description |
|---|---|---|
shape | list[int] | tuple[int, ...] | Tensor | A list or tuple of integers, or a one-dimensional int32 Tensor describing the output dimensions. |
dtype | tf.dtypes.DType | The data type of an element in the resulting Tensor; defaults to tf.float32. |
name | str | None | Optional operation name. |
layout | tf.experimental.dtensor.Layout | None | Optional tf.experimental.dtensor.Layout; if supplied, the result is a DTensor with that layout. |
A Tensor with all elements set to zero.
import tensorflow as tf
zeros = tf.zeros([3, 4], dtype=tf.int32)
print(zeros)Creates and prints a 3-by-4 integer tensor whose entries are all zero.
Allocate a known-size tensor for padding, masks, accumulators, or other initial values.
Provide an explicitly shaped constant tensor as an input or intermediate computation.
Construct a zero-valued tensor with a non-default numeric type such as int32.
Create a distributed tensor with an explicitly specified layout.
It creates a Tensor whose shape is shape and whose elements are all zero. Its default element type is float32 unless dtype is specified.
Follow-up: How would you create zeros matching an existing tensor? Use tf.zeros_like, which derives the output shape and type from the input.
shape may be a list or tuple of integers, or a one-dimensional int32 Tensor. dtype controls the type of each element in the resulting Tensor.
Follow-up: Can shape be supplied as a scalar? No; the documented shape forms describe dimensions, so a scalar does not represent a valid shape specification.
name is an optional operation name. layout is optional; when supplied, the result is a DTensor with that layout.
Follow-up: What alternative creates zeros with the same shape as another value? tf.zeros_like accepts a tensor or array-like input and derives its shape.
tf.zeros_liketf.onestf.filltf.zeros_initializer