Keras
deep learning
repeat function
data preprocessing
machine learning

How to use repeat function when building data in Keras?

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

Repeating values is common when building Keras inputs, especially for sequence models, duplicated labels, and shape alignment. The important distinction is whether you are repeating raw data before the model sees it or repeating activations inside the model graph, because those two jobs use different APIs.

Use keras.ops.repeat for Tensor Data

If you are building or reshaping tensors before passing them into a model, use a tensor operation rather than a layer. In current Keras APIs, keras.ops.repeat is the backend-friendly repeat function.

python
1import keras
2from keras import ops
3
4x = ops.array([[1.0, 2.0], [3.0, 4.0]])
5
6repeated_rows = ops.repeat(x, repeats=2, axis=0)
7repeated_cols = ops.repeat(x, repeats=3, axis=1)
8
9print(repeated_rows)
10print(repeated_cols)

Repeating along axis=0 duplicates rows. Repeating along axis=1 duplicates elements within each row. That distinction is easy to miss, so printing shapes during development is a good habit.

Use RepeatVector Inside the Model

RepeatVector is a Keras layer, so it belongs in the network architecture rather than in preprocessing code. It takes a 2D input shaped like batch_size, features and repeats it along a new time axis.

python
1import keras
2from keras import layers
3
4inputs = keras.Input(shape=(16,))
5encoded = layers.Dense(8, activation="relu")(inputs)
6repeated = layers.RepeatVector(5)(encoded)
7outputs = layers.LSTM(4)(repeated)
8
9model = keras.Model(inputs, outputs)
10model.summary()

This is common in encoder-decoder models where a fixed-size latent vector must be copied across several time steps before a recurrent decoder processes it.

Repeating Samples in an Input Pipeline

If your project uses tf.data, repetition may belong in the dataset pipeline instead of in tensor-building code. That keeps the data flow lazy and avoids creating large materialized arrays in memory.

python
1import tensorflow as tf
2
3features = tf.constant([[1.0], [2.0], [3.0]])
4labels = tf.constant([0, 1, 0])
5
6dataset = tf.data.Dataset.from_tensor_slices((features, labels))
7dataset = dataset.flat_map(
8    lambda x, y: tf.data.Dataset.from_tensors((x, y)).repeat(3)
9)
10
11for item in dataset.take(5):
12    print(item)

This repeats whole samples rather than repeating elements inside a single tensor. That is a different operation with a different meaning, even though the word "repeat" appears in both APIs.

Keep Features and Labels Aligned

When you repeat samples, repeat every parallel structure the same way. If features are repeated three times but labels or sample weights are not, the training data becomes corrupted.

python
1import tensorflow as tf
2
3features = tf.constant([[1.0, 2.0], [3.0, 4.0]])
4labels = tf.constant([0, 1])
5
6rep = 3
7features_rep = tf.repeat(features, repeats=rep, axis=0)
8labels_rep = tf.repeat(labels, repeats=rep, axis=0)
9
10print(features_rep.shape)
11print(labels_rep.shape)

After any repetition step, verify both shape and semantic alignment before calling fit.

Watch Memory and Shape Growth

Repeat operations can multiply tensor size very quickly. A tensor with shape 1024 x 512 becomes much larger when repeated across a new axis or copied many times. That affects GPU memory, training speed, and even data-loading latency.

A practical workflow is:

  • print the original shape
  • apply the repeat operation
  • print the new shape
  • estimate whether the new tensor size still fits comfortably in memory

If repetition inflates the dataset too much, move the operation into a lazy pipeline or reconsider whether you really need explicit duplication at all.

Pick the Right API for the Job

A simple decision rule works well:

  • use keras.ops.repeat when you are transforming tensors as data
  • use tf.data.Dataset.repeat or sample-level pipeline logic when you want repeated dataset items
  • use layers.RepeatVector when repetition is part of the model architecture

These tools sound similar, but they solve different problems. Mixing them up is the fastest way to get shapes that technically run but mean the wrong thing.

Common Pitfalls

The most common mistake is using RepeatVector in preprocessing code when the real need is a tensor operation. Another is repeating along the wrong axis and silently changing the meaning of the data. Developers also forget to repeat labels, masks, or sample weights along with the features, which leads to training bugs that are hard to diagnose. Finally, repeated tensors can become much larger than expected, so memory pressure often appears before the shape logic itself looks obviously wrong.

Summary

  • Use keras.ops.repeat for backend-friendly tensor repetition in data building.
  • Use RepeatVector only when repetition belongs inside the model graph.
  • Use dataset-level repetition when you want to duplicate samples lazily.
  • Repeat labels and related metadata consistently with the features.
  • Check shapes after every repeat operation so semantic mistakes are caught early.

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.