tensor operations
nonzero indices
numpy guide
data manipulation
machine learning tools

how to produce a tensor containing the indices of nonzero elements?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A "tensor of nonzero indices" means a structure that lists the coordinates of every element whose value is not zero. In TensorFlow, the usual answer is tf.where(...). In NumPy and PyTorch, the equivalent ideas are argwhere and nonzero.

TensorFlow: Use tf.where

If you want the coordinates of nonzero values, use tf.where with a nonzero condition.

python
1import tensorflow as tf
2
3x = tf.constant([
4    [0, 2, 0],
5    [5, 0, 7],
6    [0, 0, 9],
7], dtype=tf.int32)
8
9indices = tf.where(x != 0)
10print(indices.numpy())

Output:

text
1[[0 1]
2 [1 0]
3 [1 2]
4 [2 2]]

Each row is one coordinate. Because x is two-dimensional, each coordinate has two numbers: row and column.

Understanding the Result Shape

The result shape is:

  • number of rows: number of nonzero elements
  • number of columns: number of tensor dimensions

So for a 3D tensor, each row would contain three coordinates. This is why the result is usually a 2D tensor even when the input has more dimensions.

Boolean Conditions Work Too

tf.where is not limited to numeric tensors. It also works naturally with boolean masks.

python
1mask = tf.constant([
2    [False, True, False],
3    [True, False, True],
4])
5
6print(tf.where(mask).numpy())

This is useful in masking pipelines where the condition is already computed separately.

Recover the Values from the Indices

Sometimes you want both the indices and the corresponding nonzero values.

python
1x = tf.constant([
2    [0, 2, 0],
3    [5, 0, 7],
4    [0, 0, 9],
5], dtype=tf.int32)
6
7indices = tf.where(x != 0)
8values = tf.gather_nd(x, indices)
9
10print(indices.numpy())
11print(values.numpy())

That gives you a clean sparse-style representation: coordinates plus values.

NumPy and PyTorch Equivalents

If you work across tensor libraries, the same concept appears with slightly different names.

python
1import numpy as np
2
3x = np.array([
4    [0, 2, 0],
5    [5, 0, 7],
6    [0, 0, 9],
7])
8
9print(np.argwhere(x))
python
1import torch
2
3x = torch.tensor([
4    [0, 2, 0],
5    [5, 0, 7],
6    [0, 0, 9],
7])
8
9print(torch.nonzero(x))

The idea stays the same: return coordinate rows for every nonzero entry.

When This Operation Is Useful

Nonzero indices show up in many common tasks:

  • sparse tensor construction
  • extracting active positions from masks
  • converting dense data to coordinate form
  • filtering or gathering selected values

Once you understand the coordinate-row format, these workflows become much easier to compose.

Building a Sparse Representation

A common next step is turning the coordinates into a sparse tensor representation. In TensorFlow, the nonzero indices can be paired with gathered values and a dense shape to construct tf.sparse.SparseTensor. That is useful when you want to move from dense data into a more memory-efficient sparse pipeline rather than just inspect the coordinates.

python
1indices = tf.where(x != 0)
2values = tf.gather_nd(x, indices)
3sparse = tf.sparse.SparseTensor(
4    indices=indices,
5    values=values,
6    dense_shape=tf.cast(tf.shape(x), tf.int64),
7)
8
9print(sparse)

Common Pitfalls

  • Expecting the result to contain values instead of coordinates is a common misunderstanding.
  • Forgetting that the result is 2D even for higher-rank inputs makes the shape look surprising at first.
  • Using tf.where(x) on a numeric tensor can work, but tf.where(x != 0) is usually clearer to readers.
  • Confusing NumPy’s tuple-style nonzero output with row-wise coordinate output leads to indexing mistakes.
  • Ignoring tensor rank makes it harder to interpret each coordinate correctly.

Summary

  • In TensorFlow, use tf.where(x != 0) to get the indices of nonzero elements.
  • The result is a 2D tensor where each row is one coordinate.
  • Use tf.gather_nd if you also want the corresponding values.
  • NumPy and PyTorch provide the same idea through argwhere and nonzero.
  • Think of the output as coordinates, not as the nonzero values themselves.

Course illustration
Course illustration

All Rights Reserved.