How to save/load a tensorflow hub module to/from a custom path?
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
To save a TensorFlow Hub module to a custom path, load it with hub.load() or hub.KerasLayer(), then use tf.saved_model.save() to export it to your directory. To load from a custom path, pass the local directory path to hub.load() or hub.KerasLayer() instead of a URL. You can also set the TFHUB_CACHE_DIR environment variable to control where TF Hub caches downloaded modules. This is essential for offline deployments, air-gapped environments, and reproducible model serving.
Saving a TF Hub Module to a Custom Path
Method 1: tf.saved_model.save
Method 2: Save as Part of a Keras Model
Loading from a Custom Path
Method 1: hub.load with Local Path
Method 2: hub.KerasLayer with Local Path
Method 3: tf.saved_model.load
Using TFHUB_CACHE_DIR
TF Hub caches downloaded modules. Control the cache location:
Finding the Cache Path
Offline Deployment
For air-gapped or production environments without internet access:
Docker Deployment
Saving with Specific Signatures
Common Pitfalls
- Setting
TFHUB_CACHE_DIRafterimport tensorflow_hub: The cache directory is read when the module is first imported. Set the environment variable before importingtensorflow_hub, or it uses the default location. - Loading a Hub module saved with a different TF version: SavedModels may use ops not available in older TensorFlow versions. Always use the same major TensorFlow version for saving and loading, or use
tf.compat.v1for backward compatibility. - Forgetting
custom_objectswhen loading a Keras model with Hub layers:tf.keras.models.load_modeldoes not know abouthub.KerasLayerby default. Passcustom_objects={'KerasLayer': hub.KerasLayer}to avoidValueError: Unknown layer. - Assuming the cached module path is stable: TF Hub generates hashed directory names in the cache. Do not hardcode cache paths — use
tf.saved_model.save()to export to a known, stable path. - Not checking disk space for large models: Some TF Hub modules (e.g., BERT, T5) are several gigabytes. Ensure the cache directory and export path have sufficient disk space, especially in Docker containers with limited storage.
Summary
- Save with
tf.saved_model.save(hub.load(url), "/path/to/model") - Load with
hub.load("/path/to/model")ortf.saved_model.load("/path/to/model") - Set
TFHUB_CACHE_DIRenvironment variable to control the download cache location - For offline deployments, export the model on a connected machine and copy the saved directory
- Pass
custom_objects={'KerasLayer': hub.KerasLayer}when loading Keras models with Hub layers
Related reading
- How to save/restore large model in tensorflow 2.0 w/ keras?
- How to select specific columns from tensorflow dataset?
- How to sequentially combine 2 tensorflow models?
- How to serve a tensorflow-module, specifically Universal Sentence Encoder?
- How to save/restore a model after training?
- How to save/restore a model after training?
- How to schedule a function to run every hour on Flask?
- How to scp 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.