TensorFlow
tf.slice
data manipulation
machine learning
Python programming

Tensorflow Using tf.slice to split the input

Master System Design with Codemia

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

Introduction

tf.slice is a core TensorFlow operation for extracting sub-tensors from a larger tensor. It is often used in feature engineering pipelines, sequence windows, and custom model input partitioning. Using it correctly requires careful control of begin indices and slice sizes.

Understand tf.slice Signature

tf.slice(input, begin, size) takes:

  • begin: start indices for each dimension.
  • size: number of elements for each dimension.

A value of -1 in size means take all remaining elements on that axis.

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

This returns a two-by-two block from the center area.

Split Features by Column Ranges

A common use is splitting tabular tensors into feature groups.

python
1features = tf.constant([
2    [0.1, 1.0, 10.0, 100.0],
3    [0.2, 2.0, 20.0, 200.0],
4])
5
6left = tf.slice(features, begin=[0, 0], size=[-1, 2])
7right = tf.slice(features, begin=[0, 2], size=[-1, 2])
8
9print(left.numpy())
10print(right.numpy())

This is useful when model branches consume different feature subsets.

Sequence Windowing with tf.slice

For sequence models, tf.slice can extract context windows.

python
1seq = tf.reshape(tf.range(20), (4, 5))
2# rows are samples, columns are timesteps
3window = tf.slice(seq, begin=[0, 1], size=[-1, 3])
4print(window.numpy())

Window extraction is deterministic and graph-friendly.

Compare with Tensor Indexing

Tensor indexing syntax can be easier to read for simple cases.

python
same_window = seq[:, 1:4]
print(tf.reduce_all(tf.equal(window, same_window)).numpy())

Use tf.slice when you need dynamic begin and size tensors in graph mode. Use slicing syntax for static readability where possible.

Dynamic Splitting in Functions

tf.slice works well in tf.function with runtime-computed bounds.

python
1@tf.function
2def dynamic_slice(x, start_col, width):
3    return tf.slice(x, begin=[0, start_col], size=[-1, width])
4
5print(dynamic_slice(features, tf.constant(1), tf.constant(2)))

This is useful for parameterized pipelines and custom layers.

Split Input for Multi-Branch Models

A common model pattern sends different feature ranges to separate network branches. tf.slice can create those branch inputs efficiently.

python
1import tensorflow as tf
2
3batch = tf.random.normal((8, 12))
4branch_a = tf.slice(batch, begin=[0, 0], size=[-1, 4])
5branch_b = tf.slice(batch, begin=[0, 4], size=[-1, 8])
6
7print(branch_a.shape)
8print(branch_b.shape)

This keeps feature partitioning explicit and reproducible.

Use with tf.data Pipelines

Slicing can happen directly in dataset maps.

python
1features = tf.random.normal((100, 12))
2labels = tf.random.uniform((100,), maxval=2, dtype=tf.int32)
3
4ds = tf.data.Dataset.from_tensor_slices((features, labels))
5
6def split_map(x, y):
7    return {
8        'small_block': tf.slice(x, [0], [4]),
9        'large_block': tf.slice(x, [4], [8]),
10    }, y
11
12mapped = ds.map(split_map).batch(16)
13for batch_x, batch_y in mapped.take(1):
14    print(batch_x['small_block'].shape, batch_x['large_block'].shape, batch_y.shape)

This is useful for models with named multi-input signatures.

Edge Case Handling

When slice bounds depend on runtime values, validate range before slicing to avoid graph errors.

python
1def safe_slice(x, start, width):
2    total = tf.shape(x)[1]
3    tf.debugging.assert_less_equal(start + width, total)
4    return tf.slice(x, begin=[0, start], size=[-1, width])

Range validation improves reliability in dynamic input pipelines.

Migration Notes

If static slicing is simple, prefer regular tensor indexing for readability. Reserve tf.slice for dynamic graph scenarios, exported functions, and reusable utility layers where begin and size are parameters.

A clear style rule keeps tensor manipulation code easier to review.

Common Pitfalls

  • Mismatching rank between begin and input tensor dimensions.
  • Using out-of-range start indices and getting runtime errors.
  • Confusing inclusive and exclusive semantics when converting from Python slices.
  • Overusing tf.slice where clearer static indexing would suffice.

Summary

  • tf.slice extracts sub-tensors using begin and size vectors.
  • Use -1 size to consume remaining elements on an axis.
  • It is ideal for dynamic graph-compatible slicing logic.
  • Validate dimensions and bounds to avoid runtime slice errors.

Course illustration
Course illustration

All Rights Reserved.