How to create Keras model with optional inputs
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
Keras models do not treat declared Input tensors as truly optional. Once a functional model is built with two inputs, both inputs are part of the signature. The usual workaround is to always provide the auxiliary input with a default value and a mask, or to design separate model paths for the "with extra input" and "without extra input" cases.
Why Functional Inputs Are Not Optional
In the functional API, the input signature is fixed when you build the graph.
If you create a model from these two inputs, Keras expects both every time. You cannot omit aux during fit() or predict() and expect the graph to rewire itself dynamically.
That is why the real problem is usually data representation, not optional function arguments in the Python sense.
Pattern 1: Always Pass the Auxiliary Tensor and a Mask
The most common solution is to always pass an auxiliary tensor. When the extra data is missing, pass zeros and provide a mask that says whether the values are real.
When the auxiliary input is missing:
This avoids signature mismatch and lets the model learn the difference between "missing" and "present."
Pattern 2: Use a Subclassed Model with a Default
If you need Python-level flexibility, a subclassed model can inject a default tensor when an auxiliary input is absent.
This is flexible, but it is more manual than a standard functional graph and can complicate serialization or tooling expectations if overused.
Pattern 3: Separate Models
Sometimes the cleanest design is two models:
- one model that expects only the main input
- one model that expects main plus auxiliary input
This is often easier when the "with aux" and "without aux" cases are semantically different enough that sharing one graph becomes awkward.
The right choice depends on whether the auxiliary input is truly optional metadata or whether it changes the problem definition itself.
Keep the Data Pipeline Consistent
The model architecture is only part of the solution. Your training pipeline also has to represent missing auxiliary data consistently. If some batches contain one structure and others contain another, training code becomes fragile quickly.
In practice, most teams normalize the pipeline so every example has the same fields, even if one field sometimes contains defaults plus a presence mask.
Common Pitfalls
Declaring an auxiliary input in the functional API and then omitting it entirely at call time causes a signature mismatch because Keras still expects it.
Using all-zero auxiliary data without a mask is ambiguous if zero is also a valid real value for that feature.
Building a highly flexible subclassed model when two separate simpler models would be clearer can make training and debugging harder.
Ignoring how the training data represents missing values often creates mismatch between training-time behavior and inference-time behavior.
Thinking of Keras inputs as optional Python parameters rather than fixed graph inputs leads to the wrong mental model.
Summary
- Functional Keras inputs are fixed once the model graph is built.
- The usual workaround is to always provide the input and represent absence with defaults plus a mask.
- A subclassed model can inject defaults dynamically if you need more call-time flexibility.
- Separate models may be cleaner when the optional path really represents a different problem.
- Keep the data pipeline consistent so missing auxiliary data is represented the same way during training and inference.
Related reading
- How to create only one copy of graph in tensorboard events file with custom tf.Estimator?
- How to create own dataset for using Mask-RCNN models from the Tensorflow Object Detection API?
- How to deal with batches with variable-length sequences in TensorFlow?
- How to deal with large2GB embedding lookup table in tensorflow?
- How to deal with array of string features in traditional machine learning?
- How to deal with different state space size in reinforcement learning?
- How to create key or append an element to key?
- How to create module-wide variables in Python?
.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.