Saving a TF2 keras model with custom signature defs
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
In TensorFlow 2, custom SavedModel signatures let you control exactly how a model is called after export. Instead of relying only on Keras defaults, you can define named entry points with explicit input signatures, which is useful for serving, batch inference, or exposing multiple prediction methods from the same model.
What a Signature Really Is
A SavedModel signature is a callable graph with a fixed input contract and named outputs. When a serving system loads the model, it uses those signatures to know what tensors to accept and what tensors to return.
Keras already gives you a default serving path when you call model.save(...), but that default may not be enough if you want:
- custom input names
- multiple entry points
- preprocessing inside the exported call
- different inference methods for different clients
Define a Serving Function With input_signature
The usual pattern is to wrap model inference in a tf.function whose input signature is explicit.
That function becomes a clean serving contract: one input tensor named features, one output named predictions.
Save With Custom Signatures
Once the function is defined, save the model with tf.saved_model.save.
Here the same callable is exported under two signature names. You can also export different callables if the behavior should differ.
Verify the Exported Signatures
After saving, load the model and inspect what signatures exist.
This step matters because signature mistakes are easy to make silently. Verifying the saved artifact is part of the workflow, not an optional extra.
When model.save Is Enough
If you only need the standard Keras serving behavior, model.save(...) is often sufficient. Reach for custom signatures when the exported API should be deliberate rather than implicit.
A good rule is simple:
- use default saving for ordinary training and reload workflows
- use explicit signatures for serving or integration contracts
Custom signatures also make model boundaries clearer for other teams. Instead of telling a serving client to inspect whatever Keras exported by default, you give it a documented entry point with stable tensor names and output keys. That is often the difference between a model artifact that is merely saved and one that is genuinely deployable.
Common Pitfalls
The biggest mistake is forgetting input_signature. Without it, TensorFlow may trace in a way that is less stable or less explicit for serving.
Another mistake is returning raw tensors without naming outputs when downstream systems expect a clear output dictionary.
A third mistake is exporting a signature that uses preprocessing state or variables that are not built yet. Make sure the model has been called at least once or otherwise built before export.
Summary
- Custom SavedModel signatures define explicit exported call contracts.
- Use
@tf.function(input_signature=...)to describe inputs clearly. - Save with
tf.saved_model.save(..., signatures={...})when you need named serving entry points. - Verify the exported signatures after saving instead of assuming they are correct.
- Use custom signatures only when the default Keras export is not expressive enough.
Related reading
- Saving and reading variable size list from TFRecord
- Saving image files in Tensorflow
- Saving Keras models with Custom Layers
- Saving meta data/information in Keras model
- Saving model on Tensorflow 2.7.0 with data augmentation layer
- Saving TF model trained with keras and then evaluated in Go
- Saving Model Checkpoint vs Saving Entire model in Keras
- Saving tf.trainable_variables using convert_variables_to_constants
.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.