我和elmo和tensorflow遇到了这个问题,我想在不降级的情况下解决这个问题。我该怎么办呢
`**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)
`发布于 2022-03-21 17:02:17
hub.module()只兼容TF 1.x和已弃用在TF 2.x中的使用。为此,我们可以在hub.load()代码中使用TF 2.x。
您可以通过在TF 2.8 (或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)输出:
(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)或者使用下面的代码运行相同的代码(通过将其转换为TF 1.x),它将成功执行:
%tensorflow_version 1.xhttps://stackoverflow.com/questions/71311818
复制相似问题