尝试基本的Tensorflow代码时发生运行时错误

时间:2019-10-01 06:22:29

标签: tensorflow

更新:

无论是单词,句子还是短语,Universal Sentence编码器始终会返回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))

如何正确运行此代码?

2 个答案:

答案 0 :(得分:3)

这与您使用的张量流版本有关。

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

答案 1 :(得分:0)

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

以下解决方案对我有用:

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

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