Saving meta data/information in Keras model
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 can be saved with model.save(), which stores the architecture, weights, and optimizer state. However, custom metadata like class labels, preprocessing parameters, training configuration, or versioning information is not saved automatically. To persist metadata alongside a Keras model, you can use HDF5 file attributes, save a separate JSON sidecar file, use model.save() with the .keras format and custom objects, or store metadata in the model's config via get_config().
Saving Metadata in HDF5 Attributes
The HDF5 format (.h5) supports arbitrary key-value attributes on groups and datasets:
Saving Metadata as a Sidecar JSON File
The simplest and most portable approach — save a JSON file alongside the model:
Saving Training History
Custom Model with Built-in Metadata
Subclass keras.Model and override get_config():
Saving Metadata with SavedModel Format
Using MLflow or Weights & Biases for Metadata
For production workflows, use experiment tracking tools:
Common Pitfalls
- Metadata lost when converting formats: Saving as
.h5with attributes, then converting to SavedModel or.kerasformat drops the HDF5 attributes. Use a sidecar JSON file that stays format-independent, or re-attach metadata after conversion. - HDF5 attributes not supporting complex types:
h5pyattributes only store scalars, strings, and NumPy arrays. Lists and dicts must be serialized withjson.dumps()before storing and deserialized withjson.loads()when reading. - Forgetting custom_objects when loading: If your model uses a custom
get_config(), loading withkeras.models.load_model()fails unless you passcustom_objects={"ClassName": ClassName}. Without it, Keras cannot reconstruct the model. - Not versioning metadata with the model: Metadata and model files can get out of sync if saved separately. Include a version field in both files and validate at load time that they match. Better yet, package both in a single archive (e.g., a zip file or MLflow artifact).
- Saving preprocessing parameters separately from the model: If normalization means/stds are in a JSON file but the model expects normalized input, they can drift. Include preprocessing as Keras layers (
Normalization,Rescaling) inside the model so they are saved together.
Summary
- Keras
model.save()stores architecture, weights, and optimizer — not custom metadata - Use HDF5 attributes (
h5py) to attach key-value metadata to.h5model files - Save a sidecar JSON file alongside the model for portable, format-independent metadata
- Override
get_config()in customkeras.Modelsubclasses to embed metadata in the model config - Save training history as JSON for later analysis and visualization
- Use MLflow or Weights & Biases for production-grade experiment tracking with metadata
Related reading
- Saving Model Checkpoint vs Saving Entire model in Keras
- Saving model on Tensorflow 2.7.0 with data augmentation layer
- Saving TF model trained with keras and then evaluated in Go
- Saving tf.trainable_variables using convert_variables_to_constants
- Saving the objects detected in a dataframe tensorflow object_detection
- Saving weights to memory in tensorflow
- Saving UTF-8 texts with json.dumps as UTF-8, not as a \u escape sequence
- Saving UTF-8 texts with json.dumps as UTF-8, not as a u escape sequence
.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.