首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RuntimeError: variable_scope模块1/未使用,但相应的name_scope已被接受。我怎样才能修好它?

RuntimeError: variable_scope模块1/未使用,但相应的name_scope已被接受。我怎样才能修好它?
EN

Stack Overflow用户
提问于 2022-03-01 16:39:49
回答 1查看 385关注 0票数 0

我和elmo和tensorflow遇到了这个问题,我想在不降级的情况下解决这个问题。我该怎么办呢

代码语言:javascript
复制
`**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) 
`
EN

回答 1

Stack Overflow用户

发布于 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中)中执行以下更改来运行代码,而不会出错。

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
(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),它将成功执行:

代码语言:javascript
复制
%tensorflow_version 1.x
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71311818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档