Keras Multitask learning with two different input sample size
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
Multitask learning in Keras works well when tasks share part of a model, but the data setup matters. If the two tasks have different numbers of samples, you cannot simply pass mismatched arrays into one model.fit call and expect Keras to align them automatically.
Same Examples Versus Different Datasets
There are two very different cases that often get mixed together:
- one dataset where each sample has multiple targets
- two separate datasets with different sample counts
If every sample has both task labels, a normal multi-output model is enough. If task A has one dataset and task B has another dataset with a different number of rows, you need a custom training strategy because there is no one-to-one pairing between samples.
That is the real issue behind the "different sample size" question.
Multi-Output Keras Works Only When Batches Align
A standard multitask model in Keras might look like this:
This design assumes the same input batch produces both outputs. So x_train, y_class, and y_score must all have the same first dimension. If they do not, model.fit will reject the data because it has no rule for matching sample 17 from one task with sample 17 from another task.
Use Separate Task Datasets With a Custom Training Step
When tasks truly have different sample counts, a common solution is to share part of the network and train each task on its own dataset inside a custom train_step.
Now each task can use its own dataset, its own label shape, and even its own batch size.
Feed the Two Datasets Intentionally
With separate datasets, you also need to decide how often each task contributes updates. One simple pattern is to repeat the smaller dataset and zip the two streams:
This keeps both tasks active even when one dataset is much smaller. You can also adjust task weights, batch sizes, or sampling frequency if one task starts dominating the shared representation.
Different Feature Shapes Are Fine
If the two tasks also have different input shapes, give each task its own input branch before the shared or merged layers. Keras handles multiple input tensors well. The hard part is not the feature shape. The hard part is sample alignment when the datasets do not describe the same examples.
So the rule is:
- different feature shapes are fine with multiple
Inputlayers - different sample counts need separate batching or a custom training loop
Common Pitfalls
- Passing arrays with different first dimensions into one
model.fitcall and expecting Keras to align them magically. - Treating two unrelated datasets as if row
iin both datasets represents the same training example. - Forgetting to rebalance the task losses, which can let one head dominate training.
- Repeating the smaller dataset forever without considering whether it leads to overfitting that task.
- Confusing different sample counts with different feature shapes. They are separate design issues.
Summary
- Standard Keras multitask models require aligned samples across all outputs in a batch.
- Different dataset sizes usually mean you need separate task datasets and a custom training loop or
train_step. - Shared layers can still be trained jointly even when the tasks do not share identical examples.
- Task weighting and sampling frequency matter when one dataset is much larger than the other.
- Solve sample-count mismatch and feature-shape mismatch as two separate problems.
Related reading
- Keras neural network outputs same result for every input
- Keras not training on entire dataset
- Keras not training on entire dataset
- Keras not using full CPU cores for training
- Keras not using full CPU cores for training
- Keras occupies an indefinitely increasing amount of memory for each epoch
- Keras or Tensorflow function to draw a 3D diagram of a neural network structure?
- keras predict always output same value in multi-classification
.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.