Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.zeros

Creates a tensor with a specified shape whose elements are all zero. The output dtype defaults to float32 and can be changed with dtype.

How it works

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.

Syntax

tf.zeros(
    shape,
    dtype=tf.dtypes.float32,
    name=None,
    layout=None
)

Parameters

NameTypeDescription
shapelist[int] | tuple[int, ...] | TensorA list or tuple of integers, or a one-dimensional int32 Tensor describing the output dimensions.
dtypetf.dtypes.DTypeThe data type of an element in the resulting Tensor; defaults to tf.float32.
namestr | NoneOptional operation name.
layouttf.experimental.dtensor.Layout | NoneOptional tf.experimental.dtensor.Layout; if supplied, the result is a DTensor with that layout.

Returns

A Tensor with all elements set to zero.

Key points

  • The function produces a tensor filled entirely with zero values.
  • The returned tensor uses the requested dtype and shape.
  • dtype specifies the type of each element in the output.
  • name can optionally assign a name to the operation.
  • layout accepts an optional DTensor layout.

Common mistakes

  • shape must be a list or tuple of integers, or a one-dimensional int32 Tensor.
  • Providing layout changes the result into a DTensor using that layout.

Example: Create an integer zero tensor

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.

When to use it

Create a zero-filled buffer

Allocate a known-size tensor for padding, masks, accumulators, or other initial values.

Build model inputs or intermediates

Provide an explicitly shaped constant tensor as an input or intermediate computation.

Control output dtype

Construct a zero-valued tensor with a non-default numeric type such as int32.

Use a DTensor layout

Create a distributed tensor with an explicitly specified layout.

Interview questions

QWhat does tf.zeros return, and what is its default dtype?

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.

QWhat forms can the shape argument take?

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.

QWhat are the name and layout arguments used for?

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_like
  • tf.ones
  • tf.fill
  • tf.zeros_initializer

Source: tf.zeros | TensorFlow v2.16.1. Checked 2026-10-15.