【问题标题】:Difficulties while using Tensorflow_hub and universal-sentence-encoder使用 Tensorflow_hub 和 Universal-sentence-encoder 时的困难
【发布时间】:2020-05-26 00:54:39
【问题描述】:

运行以下代码时,出现错误。 有谁能帮助我吗?我正在使用 TensorFlow 2.1.0

import pandas as pd
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

print("GPU is", "available" if tf.test.is_gpu_available() else "NOT AVAILABLE")

tweets = pd.read_csv('dataset.csv')
print(tweets.columns)
labels = tweets[['Sentiment']]
tweets = tweets[['SentimentText']]
tweets = tweets['SentimentText'].to_numpy()
embed = hub.KerasLayer("./model",  output_shape=[20],  input_shape=[],dtype=tf.string)
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
with tf.compat.v1.Session() as sess:
     sess.run([tf.compat.v1.global_variables_initializer()])
     sess.run(embed(tweets))

我收到以下错误:

文件“/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py”,第 4727 行,在 _override_gradient_function 断言不是 self._gradient_function_map

断言错误

【问题讨论】:

  • 您使用的是来自 tfhub.dev 的模型吗?如果是这样,请添加您正在使用的显式模型,并添加您从 csv 加载的数据示例。
  • @kempy 感谢您的回复。我的问题解决了。这段代码没有问题。我减少了数据集的长度并且它一直在工作。非常感谢

标签: python sentiment-analysis keras-layer tensorflow-hub tensorflow2.x


【解决方案1】:

为了社区的利益,在回答部分指定解决方案(即使在评论部分提到)。

在这种特定情况下,reducing the length of dataset has resolved the error 但一般来说,提供相应预训练模型的 URL 是一种很好的做法,以便于检查该模型的属性模型包括版本兼容性,这也提高了可读性。

【讨论】:

    【解决方案2】:

    我也遇到了这个问题。用tensorflow==2.0.0解决了(我原来用的是tensorflow==2.1.0)

    【讨论】:

    • 我也降级了我的TF版本。我现在使用的是 TF 2.0.0。
    猜你喜欢
    • 2021-05-07
    • 2020-11-19
    • 2020-01-30
    • 2019-03-28
    • 2022-07-23
    • 2019-08-18
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多