Keras TimeDistributed with multiple Inputs in different shapes
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
TimeDistributed is useful when you want to apply the same layer to each timestep of a sequence. The pattern becomes more interesting with multiple inputs that have different feature shapes, such as frame tensors plus tabular sensor vectors. The key is aligning sequence length and building separate per-input encoders before temporal fusion.
What TimeDistributed Does
TimeDistributed(layer) wraps a layer so it runs independently on each timestep. If input shape is (batch, time, features), wrapped layers process each time slice while sharing weights.
Common use cases:
- per-frame image feature extraction
- per-step tabular embedding
- per-token dense transformations before recurrent modeling
The wrapper does not fuse modalities by itself. You still design fusion explicitly.
Multi-Input Sequence Setup
Assume two inputs:
- video-like sequence shape
(batch, time, height, width, channels) - sensor sequence shape
(batch, time, sensor_features)
Build one branch per input, then merge.
This architecture keeps branch-specific feature extraction clear.
Different Feature Shapes Are Fine, Time Must Align
Branches can have different feature dimensions, but sequence length must be compatible at fusion time if you concatenate across feature axis.
If one branch has 10 timesteps and another has 12, direct concatenate fails. You need resampling, padding, or model design where fusion happens after independent temporal aggregation.
Handling Variable Length Sequences
Use masking or ragged-compatible flow when sequence lengths vary.
For multi-input models, apply consistent padding strategy so masks represent the same logical timesteps.
Training Input Format
Feed data as list or dictionary keyed by input names.
Name-based feeding helps avoid branch-order mistakes.
Debugging Shape Errors
When shape errors occur, inspect each branch output before fusion.
Useful tactics:
- print
model.summary() - create sub-models for branch outputs
- verify batch and time axes in actual arrays
Most TimeDistributed errors come from swapped axis order or mismatch between expected and actual timestep count.
Performance Considerations
TimeDistributed can increase compute significantly with high-resolution sequences. Consider:
- reducing per-frame resolution
- using lightweight convolutions
- precomputing frame embeddings offline
For long sequences, temporal models may become bottlenecks. Pooling strategies or transformer variants can help depending on workload.
Common Pitfalls
- Assuming
TimeDistributedautomatically aligns branches with different timestep counts. - Mixing axis order and feeding arrays as
(time, batch, features)by mistake. - Concatenating branches before ensuring compatible per-step dimensions.
- Ignoring masking and padding consistency for variable-length sequences.
- Building heavy per-timestep encoders that exceed memory limits.
Summary
- '
TimeDistributedapplies the same layer independently across timesteps.' - Multiple input branches can have different feature shapes but must align for fusion.
- Build branch-specific encoders first, then merge and model temporal context.
- Validate axis ordering and timestep lengths early to avoid shape errors.
- Use masking, padding, and lightweight encoders to keep training stable and efficient.
Related reading
- Keras unable to calculate number of parameters in a Keras Custom Layer
- Keras Use the same layer in different models share weights
- Keras Use the same layer in different models share weights
- Keras uses way too much GPU memory when calling train_on_batch, fit, etc
- Keras Tokenizer num_words doesn't seem to work
- Keras TypeError can't pickle _thread.lock objects with KerasClassifier
- Keras ValueError Input 0 is incompatible with layer conv2d_1 expected ndim4, found ndim5
- Keras ValueError No data provided for input_1. Need data for each key
.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 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.