Can a TensorFlow Hub module be used in TensorFlow 2.0?
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
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()
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/elmo3model_link='https://tfhub.dev/google/elmo/3'model_link=$model_link'?tf-hub-format=compressed'wget $model_link -O modeltar xvzf modelrm model
Then use hub.load()
:
import tensorflow as tfimport tensorflow_hub as hubelmo = 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