Machine Learning
Tensor Manipulation
Data Splitting
Training Sets
Test Sets

Split tensor into training and test sets

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

Splitting a tensor into training and test sets is not just slicing at some index. In machine learning, the important part is keeping related tensors aligned, usually shuffling first, and making sure the split is reproducible when experiments need to be compared.

Split by Shuffled Indices

If your features and labels are already in memory as tensors, a reliable pattern is to shuffle indices once and then use tf.gather.

python
1import tensorflow as tf
2
3features = tf.constant([[1.0], [2.0], [3.0], [4.0], [5.0]])
4labels = tf.constant([0, 0, 1, 1, 1])
5
6num_samples = tf.shape(features)[0]
7indices = tf.random.shuffle(tf.range(num_samples))
8
9train_size = tf.cast(tf.cast(num_samples, tf.float32) * 0.8, tf.int32)
10train_idx = indices[:train_size]
11test_idx = indices[train_size:]
12
13x_train = tf.gather(features, train_idx)
14y_train = tf.gather(labels, train_idx)
15x_test = tf.gather(features, test_idx)
16y_test = tf.gather(labels, test_idx)
17
18print(x_train)
19print(x_test)

This is a strong default because the same index split is applied to both tensors, so feature-label alignment is preserved.

Avoid Splitting Features and Labels Separately

A common mistake is to shuffle or split features and labels independently. That destroys the correspondence between each training example and its label.

The correct mental model is that the dataset is split by example index, not by tensor value. Once you have the index sets, gather every related tensor with those same indices.

Make the Split Reproducible

If you need repeatable experiments, use a fixed random seed.

python
1import tensorflow as tf
2
3seed = 123
4indices = tf.random.shuffle(tf.range(tf.shape(features)[0]), seed=seed)

Without a seed, each run may produce a different split. That is often fine in production training pipelines, but it makes debugging and comparison harder.

tf.data.Dataset Can Also Split Cleanly

If you are already using tf.data, you can shuffle once and then use take and skip.

python
1import tensorflow as tf
2
3features = tf.constant([[1.0], [2.0], [3.0], [4.0], [5.0]])
4labels = tf.constant([0, 0, 1, 1, 1])
5
6dataset = tf.data.Dataset.from_tensor_slices((features, labels))
7dataset = dataset.shuffle(buffer_size=5, seed=123, reshuffle_each_iteration=False)
8
9train_size = 4
10train_ds = dataset.take(train_size)
11test_ds = dataset.skip(train_size)
12
13for batch in train_ds:
14    print(batch)

This style is useful when the next steps are batching, mapping, caching, and prefetching anyway.

Stratification Is a Separate Concern

Random splitting is fine for many tasks, but some classification problems need class balance preserved between train and test sets. TensorFlow tensors alone do not automatically give you stratified splitting.

If class balance matters, you either build the stratified index logic yourself or split with a utility designed for that job before converting back to tensors. The important point is that stratification is an extra requirement, not something ordinary random slicing provides.

Watch the Axis You Are Splitting

In most supervised learning datasets, the first axis is the sample axis. That is the axis you split.

If the tensor shape is [batch, height, width, channels], the split is along the batch dimension, not across the image width or height. This sounds obvious, but shape mistakes are common when people move from simple vectors to multidimensional tensors.

Common Pitfalls

  • Splitting features and labels separately and breaking their alignment.
  • Slicing without shuffling when the original tensor order has structure or bias.
  • Forgetting to set a seed when reproducibility matters.
  • Assuming random splitting automatically preserves class balance.
  • Splitting along the wrong axis in multidimensional tensors.

Summary

  • Split datasets by shuffled example indices, not by separate tensor operations on each field.
  • Use tf.gather when working directly with in-memory tensors.
  • Use tf.data.Dataset.shuffle, take, and skip when the pipeline is already dataset-based.
  • Keep reproducibility in mind by setting a seed when needed.
  • Treat stratification as a separate requirement from ordinary train-test splitting.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the 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.