Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.RepeatVector

RepeatVector copies each input vector n times, turning a 2D batch of feature vectors into a 3D sequence tensor. The new middle dimension has length n.

How it works

RepeatVector accepts a batch of feature vectors and inserts a time-like axis containing n copies of each vector. Thus, a 2D input becomes a 3D output whose middle dimension is the configured repetition count.

Syntax

keras.layers.RepeatVector(n, **kwargs)

Parameters

NameTypeDescription
nintegerInteger repetition factor.

Returns

A 3D tensor with shape (batch_size, n, features).

Key points

  • The layer duplicates each input feature vector n times.
  • n is an integer that controls how many copies appear in the output sequence.
  • Inputs must be rank-2 tensors with batch and feature dimensions.
  • The layer adds a middle dimension of length n while retaining batch size and feature count.
  • The Keras constructor accepts the repetition count plus standard layer keyword arguments.
  • With n equal to 3 and 32 input features, the symbolic output has shape (None, 3, 32).

Example: Repeat vectors for a sequence

import keras

inputs = keras.Input(shape=(32,))
outputs = keras.layers.RepeatVector(3)(inputs)
model = keras.Model(inputs, outputs)

x = keras.ops.ones((4, 32))
y = model(x)
print(y.shape)  # (4, 3, 32)

This creates a model that converts each 32-feature input vector into a sequence of three identical 32-feature vectors.

When to use it

Create a repeated sequence

A model has one fixed-size feature vector but a downstream sequence component expects timesteps.

Broadcast a context vector across timesteps

A decoder needs the same context representation at every output timestep.

Add a sequence dimension

A vector must be expanded into a batch-first 3D tensor with a chosen sequence length.

Interview questions

QWhat does RepeatVector do to its input?

RepeatVector repeats every input vector n times. For an input shaped (batch_size, features), the result is shaped (batch_size, n, features).

Follow-up: What input rank does the layer expect? It expects a 2D tensor containing a batch dimension and a features dimension.

QWhat is the purpose of the n argument?

The constructor takes n, an integer repetition factor. Calling RepeatVector(3) on inputs with 32 features produces outputs shaped (batch_size, 3, 32).

Follow-up: What changes if n is 5? The output shape becomes (batch_size, 5, features), while the batch and feature dimensions remain unchanged.

QHow do the input and output shapes differ?

RepeatVector transforms a 2D tensor into a 3D tensor. Its input shape is (batch_size, features), and its output shape is (batch_size, n, features).

Follow-up: Why is this useful before a sequence layer? It supplies an explicit sequence-length axis by replicating a fixed feature representation across timesteps.

  • Reshaping layers
  • keras.ops.repeat

Source: RepeatVector Layer. Checked 2026-10-15.