Install & Compatibility
Where this runs
tested against v0.16.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 107MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 32.8s · import 6.906s · 2150.4MB
1130MB installed
● package 1130MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
tensorflow_hub
✓ import tensorflow_hub as hub
The standard and recommended import alias for brevity and convention.
This quickstart demonstrates how to load a pre-trained text embedding model from TensorFlow Hub using `hub.KerasLayer` and generate embeddings for sample text. It also shows how to integrate this `hub.KerasLayer` into a larger Keras Sequential model for transfer learning tasks like text classification.
import tensorflow as tf
import tensorflow_hub as hub
# Load a pre-trained text embedding model from TensorFlow Hub
# This model generates 128-dimensional embeddings for text inputs.
model_url = "https://tfhub.dev/google/nnlm-en-dim128/2"
embed = hub.KerasLayer(model_url, input_shape=[], dtype=tf.string, trainable=False)
# Example usage: Generate embeddings for a list of sentences
sentences = [
"Hello TensorFlow Hub!",
"This is a test sentence for text embedding.",
"Machine learning is fun."
]
embeddings = embed(tf.constant(sentences))
print(f"Input sentences: {sentences}")
print(f"Generated embeddings shape: {embeddings.shape}")
print(f"First embedding (first 5 values): {embeddings[0, :5].numpy()}")
# You can then use these embeddings as input to another Keras layer
# for tasks like classification.
classifier_model = tf.keras.Sequential([
embed,
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
classifier_model.summary()
Debug
Known issues
breakingVersions 0.16.0 and 0.16.1 enforce the use of Keras 2 by requiring `tf-keras` to be installed. Older versions might have used a bundled Keras or a different Keras installation strategy.fixEnsure `tf-keras>=2.14.1` is installed in your environment: `pip install tf-keras`.
affects: 0.16.0, 0.16.1
breakingDeprecated TensorFlow 1 (TF1) APIs, such as `LatestModuleExporter` and `register_module_for_export`, were removed in v0.16.0. Code relying on these TF1 APIs will break.fixMigrate your code to use TensorFlow 2.x APIs, primarily `hub.KerasLayer` and SavedModels for model export/import.
affects: >=0.16.0
breakingStarting with version 0.15.0, TensorFlow Hub requires Python 3.9 or higher. Previous versions supported older Python versions (e.g., 3.7+ from v0.13.0).fixUpgrade your Python environment to Python 3.9 or a later compatible version.
affects: >=0.15.0
breakingThe `make_image_classifier` and `make_nearest_neighbour_index` tools were removed in version 0.14.0.fixRefactor your code to implement these functionalities using `hub.KerasLayer` and standard TensorFlow/Keras operations, or look for alternative tools/models on tfhub.dev (Kaggle Models).
affects: >=0.14.0
gotchaAs of November 15, 2023, `tfhub.dev` model links redirect to Kaggle Models. Crucially, unmigrated models (a specific list, including some iNaturalist and NVIDIA models) were permanently deleted on March 18, 2024, and are no longer retrievable via their old `tfhub.dev` handles.fixUse Kaggle Models counterparts for `tfhub.dev` links. If you were using one of the unmigrated models, you must find an alternative or retrain your own model. Check official TensorFlow Hub documentation for the list of deleted models and migration FAQs.
affects: All versions (affects model availability)
gotchaThe `hub.Module` class, part of the TensorFlow 1 API, is deprecated and has known compatibility issues with newer TensorFlow 2.x versions, especially concerning `tf.compat.v1.ragged.placeholder()` with TF2.3.fixAlways prefer `hub.KerasLayer` for loading models in TensorFlow 2.x environments. Avoid `hub.Module` for new development and migrate existing code where possible.
affects: All versions when used with TF2.x
gotchaThe TensorFlow Hub API is under active development and is not yet guaranteed to have a stable API or model format, meaning future updates might introduce breaking changes even outside of major version bumps.fixPin `tensorflow-hub` to a specific version in your `requirements.txt` or `pyproject.toml` to ensure consistent behavior across deployments. Regularly check release notes for upcoming changes.
affects: All versions
Upgrade
Version history
0.16.1latest on PyPI · released Jan 30, 2024
Audit
Dependencies
tf-kerasrequiredRequired for Keras 2 integration and functionality introduced in v0.16.0 and v0.16.1.
protobufrequiredRequired for serialization and deserialization of data, specifically version >=3.19.6 since v0.13.0.
numpyrequiredFundamental package for numerical computing with Python, version >=1.12.0.
pythonrequiredMinimum Python version required is 3.9+ since v0.15.0.