TensorFlow Hub
TensorFlow Hub (TF-Hub) is a library designed to promote the publication, discovery, and consumption of reusable parts of machine learning models. It provides a repository of pre-trained model components, facilitating transfer learning and accelerating development in various domains like image classification, text embedding, and object detection. The library is actively maintained, with its latest version being 0.16.1, and typically follows a regular release cadence to align with TensorFlow's ecosystem.
Warnings
- breaking Versions 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.
- breaking Deprecated 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.
- breaking Starting 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).
- breaking The `make_image_classifier` and `make_nearest_neighbour_index` tools were removed in version 0.14.0.
- gotcha As 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.
- gotcha The `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.
- gotcha The 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.
Install
-
pip install tensorflow-hub -
conda install -c conda-forge tensorflow-hub
Imports
- tensorflow_hub
import tensorflow_hub as hub
Quickstart
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()