TensorFlow
machine learning
data slicing
programming
deep learning

Tensorflow slicing

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TensorFlow slicing is the same core idea as slicing in Python or NumPy: select a subset of a tensor by position. The main difference is that in TensorFlow you often care about shapes much more carefully because the sliced tensor usually feeds another operation in a model or input pipeline.

Python-Style Slicing Works on Tensors

In eager execution, TensorFlow tensors support familiar slice syntax.

python
1import tensorflow as tf
2
3x = tf.constant(
4    [
5        [1, 2, 3, 4],
6        [5, 6, 7, 8],
7        [9, 10, 11, 12],
8    ]
9)
10
11print(x[0])
12print(x[:, 1:3])
13print(x[::2, :])

That gives you:

  • One row with x[0].
  • Columns 1 and 2 from every row with x[:, 1:3].
  • Every second row with x[::2, :].

For many day-to-day tasks, this syntax is all you need.

Use tf.slice When You Want Explicit Start and Size

TensorFlow also provides tf.slice, which is more explicit and sometimes easier to build programmatically.

python
1import tensorflow as tf
2
3x = tf.constant(
4    [
5        [1, 2, 3, 4],
6        [5, 6, 7, 8],
7        [9, 10, 11, 12],
8    ]
9)
10
11result = tf.slice(x, begin=[1, 1], size=[2, 2])
12print(result)

This starts at row 1, column 1, and takes a 2 x 2 block. It is especially useful when the slice bounds are computed at runtime.

Batch and Feature Slicing Is a Common Pattern

In machine learning code, slicing often means splitting batches or selecting feature columns.

python
1import tensorflow as tf
2
3batch = tf.constant(
4    [
5        [0.1, 0.2, 0.3, 1.0],
6        [0.4, 0.5, 0.6, 0.0],
7    ],
8    dtype=tf.float32,
9)
10
11features = batch[:, :3]
12labels = batch[:, 3]
13
14print(features)
15print(labels)

That pattern appears constantly in input pipelines, custom training loops, and model preprocessing.

Rank Changes Matter

Slicing can change the rank of a tensor in ways that surprise people. For example:

python
1row = x[0]
2block = x[0:1]
3
4print(row.shape)
5print(block.shape)

x[0] removes a dimension and returns shape (4,), while x[0:1] keeps the row axis and returns shape (1, 4). This matters when downstream code expects a batch dimension to remain present.

Boolean and Advanced Cases

TensorFlow also supports more specialized selection patterns through related APIs such as tf.boolean_mask and tf.gather. Those are not the same as basic slicing, but they solve nearby problems.

Use ordinary slicing when the selection is positional and contiguous. Use other selection ops when the selection is based on arbitrary indexes or masks.

Slicing Inside tf.data Pipelines

Slicing is also common in dataset preprocessing functions.

python
1import tensorflow as tf
2
3samples = tf.data.Dataset.from_tensor_slices(
4    tf.constant(
5        [
6            [1.0, 2.0, 0.0],
7            [3.0, 4.0, 1.0],
8        ]
9    )
10)
11
12def split_record(record):
13    return record[:2], record[2]
14
15for features, label in samples.map(split_record):
16    print(features, label)

The idea is the same: slice by position, but do it consistently so model inputs stay aligned with labels.

Common Pitfalls

  • Forgetting that x[0] and x[0:1] do not have the same shape.
  • Using tf.slice when ordinary Python-style slicing would have been clearer.
  • Slicing along the wrong axis and silently feeding wrong shapes into the model.
  • Confusing positional slicing with indexed selection, which may require tf.gather instead.
  • Ignoring shape changes until a later layer throws an error.

Summary

  • TensorFlow supports familiar Python-style slicing on tensors.
  • 'tf.slice is useful when you want explicit begin and size control.'
  • Slicing is common for separating batches, features, and labels.
  • Pay close attention to whether a slice removes or preserves dimensions.
  • If the selection is not a simple contiguous slice, use a more appropriate TensorFlow selection op.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.