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

TensorFlow · Function

tf.ones

Creates a tensor of a specified shape whose elements are all one. The element type defaults to tf.float32 and can be selected with dtype.

How it works

tf.ones constructs a tensor from the requested shape, fills every element with one, and uses the requested element type. If no type is supplied, TensorFlow creates a float32 tensor; an optional name labels the operation, while layout can produce a DTensor with a specified layout.

Syntax

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

Parameters

NameTypeDescription
shapelist, tuple, or 1-D int32 TensorA list or tuple of integers, or a one-dimensional int32 Tensor describing the output dimensions.
dtypetf.DTypeOptional element type for the output; defaults to tf.float32.
namestringOptional operation name.
layouttf.experimental.dtensor.LayoutOptional DTensor layout; when supplied, the result is a DTensor with that layout.

Returns

A Tensor with all elements set to one (1).

Key points

  • Use tf.ones to construct a tensor uniformly filled with the scalar value one.
  • The output's element type is controlled by dtype, while its dimensions come from shape.
  • Every position in the returned tensor contains one rather than an uninitialized value.
  • name optionally assigns an operation name, which can help identify it in TensorFlow graphs.
  • The function returns a Tensor, not a Python list or scalar.

Common mistakes

  • shape must be a list or tuple of integers, or a one-dimensional int32 Tensor.
  • If dtype is omitted, the resulting tensor is float32.
  • Supplying layout changes the result to a DTensor using that layout.

Example: Create an integer tensor of ones

import tensorflow as tf

x = tf.ones([2, 3], dtype=tf.int32)
print(x)

Creates and prints a 2-by-3 integer tensor whose six elements are all one.

When to use it

Create a uniform tensor

Initialize a constant-valued input or buffer with known dimensions.

Choose an explicit dtype

Control numeric behavior for integer or floating-point computation.

Request a DTensor layout

Create distributed tensor data with an explicitly requested arrangement.

Interview questions

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

tf.ones creates a tensor with the supplied shape and sets every element to one. Its dtype defaults to tf.float32, but callers can request another dtype such as tf.int32.

Follow-up: How would you create ones matching an existing tensor? Use tf.ones_like, which derives the output shape from the input and can optionally use a new dtype.

QWhich forms can the shape argument take?

shape can be a list of integers, a tuple of integers, or a one-dimensional int32 Tensor. The returned Tensor has that shape and contains only ones.

Follow-up: What if the desired values are not all one? Use tf.fill with dimensions and a value, or tf.zeros when the value should be zero.

QWhat is the purpose of layout?

The layout argument is optional and accepts tf.experimental.dtensor.Layout. When provided, the result is a DTensor with that layout.

Follow-up: How can a layout be requested through DTensor APIs? DTensor documentation shows wrapping tf.ones with dtensor.call_with_layout and passing a layout and shape.

  • tf.ones_like
  • tf.zeros
  • tf.fill
  • tf.eye

Source: Ones. Checked 2026-10-15.