【问题标题】:Runtime error while trying basic tensorflow code尝试基本 tensorflow 代码时出现运行时错误
【发布时间】:2019-10-01 06:22:29
【问题描述】:

更新:

无论是单词、句子还是短语,Universal Sentence Encoder 将始终返回 512 的向量大小。我想知道为什么是 512,而不是别的。


通过提供的答案解决了以下问题。

我尝试了 tensorflow 主页上提供的示例:

https://tfhub.dev/google/universal-sentence-encoder/2

我遇到这样的运行时错误:

RuntimeError: 不支持导出/导入元图 急切执行已启用。急切执行时不存在图表 已启用。

我试过的代码是:

import tensorflow.compat.v1 as tf
import tensorflow_hub as hub

config = tf.ConfigProto()
session = tf.Session(config=config)

embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder/2")
embeddings = embed(
    [
        "The quick brown fox jumps over the lazy dog.",
        "I am a sentence for which I would like to get its embedding",
    ]
)

print(session.run(embeddings))

如何正确运行这段代码?

【问题讨论】:

  • 这个问题不再活跃了吗?那你为什么不把它删掉。或将更新放在其顶部

标签: python tensorflow


【解决方案1】:

这与您使用的 tensorflow 版本有关。

在 TensorFlow 2.0 中,您应该使用 hub.load()hub.KerasLayer()

【讨论】:

  • ValueError: Non-callable result from hub.load # 你能展示如何创建“会话”对象吗?
  • 感谢您的回答。但我有一个后续问题。请查看更新。
  • @shantanuo 如果您有后续问题,应该将其作为自己的问题提出,您将有更多的运气得到答案。
【解决方案2】:

基于来自 github 的讨论:https://github.com/tensorflow/hub/issues/350

以下解决方案对我有用:

import tensorflow.compat.v1 as tf
tf.disable_eager_execution()

上面的代码禁用了急切执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多