i got this problem with elmo and tensorflow and i wanna fix it without downgrade. what should i do

`**CODE**
import tensorflow_hub as hub
import tensorflow as tf
#Elmo
elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)

# Provide input tensor and create embeddings
input_tensor = ["my birthday is the best day of the year"]
embeddings_tensor = elmo(input_tensor, signature="default", as_dict=True)["elmo"]
* here i got the problem *
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    embeddings = sess.run(embeddings_tensor)
    print(embeddings.shape)
    print(embeddings) 
`
1

There are 1 best solutions below

0
AudioBubble On

hub.module() is only compatible with TF 1.x and deprecated from using in TF 2.x. In place of that we can use hub.load() in TF 2.x code.

You can run your code without error by doing following changes in TF 2.8 (or in TF 2.x).

import tensorflow_hub as hub
import tensorflow as tf
#Elmo
elmo = hub.load("https://tfhub.dev/google/elmo/2").signatures["default"]

# Provide input tensor and create embeddings
input_tensor = ["my birthday is the best day of the year"]
embeddings_tensor = elmo(tf.constant(input_tensor))["elmo"]  #, signature="default", as_dict=True)

#with tf.Session() as sess:
   # sess.run(tf.global_variables_initializer())
    #embeddings = sess.run(embeddings_tensor)
print(embeddings_tensor.shape)
print(embeddings_tensor)

Output:

(1, 9, 1024)
tf.Tensor(
[[[-0.05932237 -0.13262326 -0.12817773 ...  0.05492031  0.55205816
   -0.15840778]
  [ 0.06928141  0.28926378  0.37178952 ... -0.22100006  0.6793189
    1.0836271 ]
  [ 0.02626347  0.16981134 -0.14045191 ... -0.53703904  0.9646159
    0.7538886 ]
  ...
  [-0.09166492  0.19069327 -0.14435166 ... -0.8409722   0.3632321
   -0.41222304]
  [ 0.40042678 -0.02149609  0.38503993 ...  0.49869162  0.07260808
   -0.3582598 ]
  [ 0.10684142  0.23578405 -0.4308424  ...  0.06925966 -0.14734827
    0.13421637]]], shape=(1, 9, 1024), dtype=float32)

OR the same code can be run using below code above your code (by converting it into TF 1.x), it will execute successfully:

%tensorflow_version 1.x