简体   繁体   English

尝试基本 tensorflow 代码时出现运行时错误

[英]Runtime error while trying basic tensorflow code

Update:更新:

Whether it is a word, sentence or phrase, The Universal Sentence Encoder will always return vector size of 512. I will like to know why 512 and not something else.无论是单词、句子还是短语,Universal Sentence Encoder 总是会返回 512 的向量大小。我想知道为什么是 512而不是别的什么。


The following question was resolved by the answer provided.通过提供的答案解决了以下问题。

I tried the example provided on tensorflow home page:我尝试了 tensorflow 主页上提供的示例:

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

I got runtime error like this:我遇到这样的运行时错误:

RuntimeError: Exporting/importing meta graphs is not supported when eager execution is enabled. RuntimeError:启用急切执行时不支持导出/导入元图。 No graph exists when eager execution is enabled.启用急切执行时不存在图表。

The code that I tried is:我尝试的代码是:

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))

How to run this code correctly?如何正确运行此代码?

It's a matter of the tensorflow version you are using.这是您使用的 tensorflow 版本的问题。

In Tensorflow 2.0 you should use hub.load() or hub.KerasLayer() .在 Tensorflow 2.0 中,您应该使用hub.load()hub.KerasLayer()

Based on a discussion from github: https://github.com/tensorflow/hub/issues/350基于 github 的讨论: https://github.com/tensorflow/hub/issues/350

The below solution worked for me:以下解决方案对我有用:

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

The above code disables the eager execution.上面的代码禁用了急切执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM