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

TensorFlow · Function

tf.where

Returns nonzero-element coordinates, or selects element values from x and y using condition as a mask. The function supports broadcasting across the condition and both branches.

How it works

tf.where has two forms. With only condition, it returns the row-major coordinates of nonzero elements as an int64 index tensor. With condition, x, and y, it broadcasts their shapes and selects from x where the condition is nonzero, otherwise from y.

Syntax

tf.where(condition, x=None, y=None, name=None)

Parameters

NameTypeDescription
conditionTensorA boolean or numeric tensor used either to locate nonzero entries or to choose between branches.
xTensor, optionalThe values selected where condition is nonzero; must be provided together with y for selection mode.
yTensor, optionalThe values selected where condition is zero; must be provided together with x for selection mode.
namestring, optionalAn optional name for the operation.

Returns

With only condition, a 2-D int64 tensor of nonzero-element coordinates. With x and y, a tensor whose shape is the broadcasted shape of condition, x, and y, containing values selected from the two branches.

Key points

  • The function supports either coordinate discovery or masked selection.
  • Coordinate mode produces int64 indices, with one coordinate per row.
  • The coordinate output has one row per nonzero condition entry and one column per axis.
  • Selection mode uses the broadcasted shape of all three inputs.
  • Each output position chooses the corresponding branch based on the condition value.
  • Omitting both branch arguments activates coordinate mode.

Common mistakes

  • When returning coordinates, the rows follow row-major traversal order.
  • The condition need not be boolean; numeric values are treated according to whether they are nonzero.
  • The three inputs must have shapes that can broadcast together.

Example: Select values and find true coordinates

import tensorflow as tf

condition = tf.constant([[True, False], [False, True]])
values = tf.where(condition, tf.ones_like(condition, dtype=tf.int32), tf.zeros_like(condition, dtype=tf.int32))
print(values.numpy())
print(tf.where(condition).numpy())

Selects one integer tensor element where the mask is true and the other where it is false, then prints the true-element coordinates.

When to use it

Extract nonzero coordinates

You need the locations of entries satisfying a mask in a tensor.

Apply a tensor mask

You need elementwise conditional values without Python branching.

Use broadcasted selection

Your condition and branch tensors have compatible but different shapes.

Interview questions

QWhat does tf.where(condition) return?

With only condition, tf.where returns a two-dimensional int64 tensor containing the coordinates of its nonzero elements. Its shape is [number of nonzero elements, rank of condition], and coordinates are emitted in row-major order.

Follow-up: What happens for a scalar condition? The result still represents coordinates, with the second dimension determined by the scalar rank, so the coordinate tensor has zero-width coordinate rows when the scalar is nonzero.

QHow does tf.where select between two tensors?

When condition, x, and y are all supplied, the condition acts as a mask. Each output element comes from x where the condition is nonzero and from y where it is zero.

Follow-up: How can differently shaped inputs work? Their shapes are broadcast together, so exact shape equality is unnecessary when the three shapes are mutually broadcastable.

QWhat shape rules apply to the three-input form?

The function accepts a boolean or numeric condition. With both branches present, the resulting shape is obtained by broadcasting condition, x, and y together.

Follow-up: What should be checked when a call fails? Verify that the three inputs have compatible broadcast shapes and that both x and y are supplied for selection mode.

  • tf.boolean_mask
  • tf.gather_nd
  • tf.math.count_nonzero
  • tf.ones_like
  • tf.zeros_like

Source: tf.where. Checked 2026-10-15.