Can a TensorFlow Hub module be used in TensorFlow 2.0?

January 2021

To use a model from TF Hub, including ELMO e.g., with tensorflow 2.x load and unpack a model locally:

cd ~/tfhub/elmo3
model_link='https://tfhub.dev/google/elmo/3'
model_link=$model_link'?tf-hub-format=compressed'
wget $model_link -O model
tar xvzf model
rm model

Then use hub.load():

import tensorflow as tf
import tensorflow_hub as hub
elmo = hub.load("/home/user/tfhub/elmo3")

embeddings = elmo.signatures["default"](tf.constant([
                "i like green eggs and ham",
                "i like green ham and eggs"
                ])
                )["elmo"]

This function can handle the deprecated TF1 Hub format


this function load will work with tensorflow 2

embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/3")

instead of

embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")

[this is not accepted in tf2] use hub.load()


In Tensorflow 2.0 you should be using hub.load() or hub.KerasLayer().

[April 2019] - For now only Tensorflow 2.0 modules are loadable via them. In the future many of 1.x Hub modules should be loadable as well.

For the 2.x only modules you can see examples in the notebooks created for the modules here