Module 'tensorflow_hub' has no attribute 'Module'

540 Views Asked by At

I used Jupyter Notebook to do ELMo to extract features. However, when I am trying to run the code

!pip install tensorflow
!pip install tensorflow-hub
import tensorflow_hub as hub
import tensorflow as tf
elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainig = True)

it gives error

AttributeError: module 'tensorflow_hub' has no attribute 'Module'

My tensorflow and tensorflow_hub version are:

TensorFlow version: 2.15.0
TensorFlow Hub version: 0.16.1

I've tried to search ways online, but I couldn't find similar error. I think it might because of the versions, and I tried to downgrade the tensorflow, but it still not work.

EDIT 1: Now I am running the code below:

import tensorflow as tf
import tensorflow_hub as hub    

elmo = tf.compat.v1.Module("https://tfhub.dev/google/elmo/3", trainable=True)

This is the error I got:

TypeError: Module.__init__() got an unexpected keyword argument 'trainable'

EDIT 2: I am following the example use from elmo model. The code is

elmo = hub.Module("https://www.kaggle.com/models/google/elmo/frameworks/TensorFlow1/variations/elmo/versions/3", trainable=True)

I just replace the hub.Module() using tf.compat.v1.Module, that is why I passing trainable.

However, even I run this code:

elmo = tf.compat.v1.Module("https://tfhub.dev/google/elmo/3")

I got this error:

ValueError: 'https://tfhub.dev/google/elmo/3' is not a valid module name. Module names must be valid Python identifiers (e.g. a valid class name).
2

There are 2 best solutions below

6
Elerium115 On

If you look at the changelog

https://github.com/tensorflow/hub/releases?page=2

Release 0.5.0

Made hub.Module usable within tf.compat.v1.wrap_function.

Hub.Module was for TF1, when using TF2 you need to use the compat attribute.

So use, tf.compat.v1.Module instead

If instead you use TF1 (not recommended since it's very old) you can still use tf.Module as stated in changelog 0.7

Release 0.7.0

For TF1, hub.Module and it's associated APIs remain available.
1
Johnny Tang On

TensorFlow Hub 0.7.0 Use hub.load() and hub.KerasLayer with TF2 (also works in 1.15).

I use hub.load() and it's work.