Multiple inputs of keras model with tf.data.Dataset.from_generator in Tensorflow 2
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Feeding a multi-input Keras model from tf.data.Dataset.from_generator works well when the dataset structure exactly matches the model input structure. Most failures come from mismatched nesting, shapes, or dtypes rather than from the model itself. The safest workflow is to name the inputs, define an explicit output_signature, and inspect one batch before training.
Build the Model with Named Inputs
Named inputs make the data contract much clearer than relying on positional tuples.
Now the input structure is explicit: the model expects a dictionary with keys tokens and meta.
Make the Generator Yield the Same Structure
The generator must yield one sample at a time in the exact structure the model expects.
If the model expects a dictionary and the generator yields a tuple, or vice versa, training usually fails immediately with a structure mismatch.
Define output_signature Precisely
from_generator needs a clear tensor specification so TensorFlow knows what the generator will produce.
If the signature does not match the real generator output, the error is often reported on first iteration or first fit call.
Inspect One Batch Before Training
This step catches most input-pipeline mistakes early.
If these shapes and dtypes are not exactly what the model expects, fix the pipeline before wasting time inside a training loop.
Train the Model Normally Once the Contract Matches
After the structure is correct, training is ordinary Keras code.
The main lesson is that multi-input training is not special at the fit call. It is special in the data structure contract between the model and the dataset.
Three-Value Generators Also Work for Sample Weights
If you need sample weights, the generator can yield three values: inputs, label, and weight. In that case, the output_signature must include the third tensor as well.
This is a common place where people update the generator but forget to update the signature, then start debugging the wrong part of the stack.
from_generator Is Flexible but Not Always Fastest
from_generator is useful when data must come from Python logic, external iterators, or unusual sampling rules. But it runs Python code, which can become a throughput bottleneck.
If training speed matters, keep the generator light and move expensive transforms into TensorFlow graph operations or precomputed files when possible.
Common Pitfalls
- Yielding a tuple structure when the model expects a dictionary of named inputs.
- Using the wrong dtype, especially
int64where embedding inputs expectint32. - Forgetting to update
output_signatureafter changing the generator output. - Debugging the model architecture when the actual issue is the dataset structure.
- Using heavy Python logic inside the generator and then blaming TensorFlow for slow input throughput.
Summary
- Multi-input Keras models work with
from_generatorwhen the dataset structure matches the model inputs exactly. - Named input dictionaries reduce ambiguity.
- '
output_signaturemust describe the real shapes and dtypes precisely.' - Validate one batch before training.
- Keep generator logic deterministic and lightweight so debugging and performance stay manageable.

