How is the input tensor for TensorFlow's tf.nn.dynamic_rnn operator structured?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
tf.nn.dynamic_rnn expects sequence data with an explicit time dimension, a batch dimension, and one or more feature dimensions. The exact order depends on whether you use the default batch-major layout or set time_major=True, and that distinction is the main thing to get right before debugging shapes.
The Default Shape: Batch Major
By default, dynamic_rnn expects input shaped like:
That means:
- '
batch_sizeis how many sequences are processed together' - '
max_timeis the number of time steps in the padded batch' - '
featuresis the feature width at each time step'
Example:
Here, 32 sequences are processed, each sequence has 20 time steps, and each time step has 8 features.
The Alternative Shape: Time Major
If you set time_major=True, the expected layout becomes:
Example:
This layout can reduce some internal transposes in certain workflows, but it is only correct if the input tensor is actually organized that way.
What the Feature Dimension Means
The last dimension represents the input at one time step. For text, it might be an embedding vector. For sensor data, it might be a vector of measurements. For one-hot categorical input, it might be the one-hot width.
So a shape of (32, 20, 300) usually means:
- 32 sequences per batch
- 20 tokens or time steps per sequence
- 300 features per token
That interpretation is more important than memorizing the raw numbers.
Variable-Length Sequences Use Padding Plus sequence_length
A batch often contains sequences of different true lengths. The common pattern is:
- pad shorter sequences to
max_time - pass the real lengths through
sequence_length
That tells TensorFlow which time steps are real and which padded steps should be ignored for state propagation.
Nested Input Structures Are Also Possible
dynamic_rnn can accept more complex input structures where each time step contains multiple tensors. In that case the structure of the input mirrors the structure expected by the RNN cell.
Still, the same principle holds: each component must carry batch and time dimensions in the correct order.
For most practical uses, though, the question is about a single 3D tensor, and the answer is batch-major by default unless you explicitly switch to time-major.
Common Pitfalls
The most common mistake is confusing batch-major and time-major layouts.
Another mistake is forgetting that the input is a sequence of feature vectors, not a flat 2D batch matrix.
A third issue is padding sequences but forgetting to supply sequence_length, which can make the RNN treat padded zeros as real time steps.
Finally, tf.nn.dynamic_rnn belongs to the TensorFlow 1.x style API family, so if you are working in modern tf.keras, use current recurrent layers unless you specifically need this lower-level interface.
Summary
- The default
dynamic_rnninput shape is[batch_size, max_time, features]. - With
time_major=True, the shape becomes[max_time, batch_size, features]. - The last dimension represents the feature vector at each time step.
- Variable-length sequences are usually padded and paired with
sequence_length. - Most confusion comes from mixing up dimension order, not from the RNN cell itself.
- If you are using modern TensorFlow, prefer current Keras RNN layers unless you specifically need
dynamic_rnn.
Related reading
- How is the smooth dice loss differentiable?
- how is total loss calculated over multiple classes in Keras?
- How LSTM deal with variable length sequence
- How should BatchNorm layer be used in caffe?
- How is the Keras Conv1D input specified? I seem to be lacking a dimension
- how is total loss calculated over multiple classes in Keras?
- How should the learning rate change as the batch size change?
- How SLURM and Pytorch handle multi-node multi-gpu training together
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseTrack 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.