how does tensorflow indexing work
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
TensorFlow indexing looks familiar if you already know NumPy, but it is not identical. Simple slices work much the same way, while more advanced selection and update patterns use TensorFlow-specific operations such as tf.gather, tf.gather_nd, tf.boolean_mask, and scatter functions.
Basic Slicing Works Like NumPy
For fixed positions and slice ranges, normal bracket syntax is usually enough.
This is the right tool for straightforward row and column selection. The result is always a new tensor because TensorFlow tensors are immutable.
Use tf.gather for Index Lists
When the positions you want are themselves stored in another tensor, tf.gather is usually clearer than trying to force everything into slice syntax.
For matrices and higher-rank tensors, supply axis to say which dimension should be indexed.
This is especially useful when the index values are computed dynamically during model execution.
Use tf.gather_nd for Coordinate Lookups
If you need arbitrary coordinates instead of one-dimensional positions, use tf.gather_nd.
Each row in coords identifies one position in the source tensor. This is the TensorFlow equivalent of fancy coordinate selection.
Boolean Selection Uses tf.boolean_mask
TensorFlow does not support every NumPy advanced-indexing pattern directly with bracket syntax. For mask-based filtering, use tf.boolean_mask.
The same idea works for filtering rows in higher-dimensional tensors.
This is often easier to reason about than translating boolean logic into explicit index arrays.
Updates Require Scatter Operations or Variables
A common source of confusion is trying to assign into a tensor like you would in NumPy. Plain tensors cannot be modified in place, so update-style logic must build a new tensor.
If you are maintaining mutable model state, use a tf.Variable instead of repeatedly pretending an immutable tensor is mutable.
Watch Shapes Closely
Most indexing bugs in TensorFlow are really shape bugs. The code may look correct while one dimension is off by one or while a batch dimension is missing.
It helps to assert shapes near the indexing operation:
These checks fail early and save time when debugging model code or tf.data pipelines.
Common Pitfalls
The first pitfall is assuming NumPy and TensorFlow indexing behave identically in every advanced case. They do not. TensorFlow expects you to use explicit ops for gathers, masks, and scatter-style updates.
Another issue is forgetting that tensors are immutable. If you need stateful updates, use tf.Variable or functions that return a new tensor.
Shape mismatches are also common, especially when batching is involved. A mask or index tensor that is valid for one example may fail once batched input is introduced.
Finally, keep indexing logic covered by tests with realistic tensor shapes. Silent feature-selection bugs can be expensive in training pipelines.
Summary
- Use normal slice syntax for simple fixed-position indexing.
- Use
tf.gatherfor index lists andtf.gather_ndfor coordinate-based selection. - Use
tf.boolean_maskfor boolean filtering. - Use scatter functions or
tf.Variablewhen you need update-like behavior. - Add shape checks around dynamic indexing code to catch mistakes early.

