python注册表中缺少图形操作

2024-03-29 05:37:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从bertseq2seq/roberta24_bbc运行示例代码:

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

text_generator = hub.Module(
    'https://tfhub.dev/google/bertseq2seq/roberta24_bbc/1')
input_documents = ['This is text from the first document.',
                   'This is text from the second document.']
output_summaries = text_generator(input_documents)
print(output_summaries)

我使用Python v3.7.4创建了一个虚拟环境,并按照网站上的说明安装了TFv1:

pip install "tensorflow>=1.15,<2.0"
pip install --upgrade tensorflow-hub 

但是当我执行时,我得到以下错误:

Exception has occurred: NotFoundError
Graph ops missing from the python registry ({'SentencepieceOp', 'SentencepieceDetokenizeOp', 'SentencepieceTokenizeOp'}) 
are also absent from the c++ registry.

有没有办法解决这个问题

更新:myrequirements.txt

absl-py==0.10.0
astor==0.8.1
astunparse==1.6.3
cachetools==4.1.1
certifi==2020.6.20
chardet==3.0.4
gast==0.2.2
google-auth==1.21.2
google-auth-oauthlib==0.4.1
google-pasta==0.2.0
grpcio==1.32.0
h5py==2.10.0
idna==2.10
importlib-metadata==1.7.0
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.2
Markdown==3.2.2
mock==4.0.2
numpy==1.18.5
oauthlib==3.1.0
opt-einsum==3.3.0
protobuf==3.13.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
requests==2.24.0
requests-oauthlib==1.3.0
rsa==4.6
scipy==1.4.1
sentencepiece==0.1.92
six==1.15.0
tensorboard==1.15.0
tensorboard-plugin-wit==1.7.0
tensorflow==1.15.3
tensorflow-estimator==1.15.1
tensorflow-hub==0.9.0
tensorflow-text==1.15.1
termcolor==1.1.0
tf-sentencepiece==0.1.92
urllib3==1.25.10
Werkzeug==1.0.1
wrapt==1.12.1
zipp==3.1.0

Tags: thetextfromimporttftensorflowasgoogle
2条回答

Sentencepiece操作已移动到单独的包^{}

pip install tf_sentencepiece

如果脚本无法完全工作,您可能还必须在脚本开始时手动导入它

我的问题与您完全相同,但在Tensorflow 2.6.0和Python3.9.1中得到了解决。下面的解决方案同时适用于CPU和GPU

使用修改后的TF1示例

以下步骤使TensorFlow Hub示例工作:

  • pip install tensorflow-text
  • import tensorflow_text as text添加到Python导入中
  • 禁用即时执行:tf.disable_eager_execution()
  • 使用TF1会话获取结果,包括手动初始化变量和表

整个程序如下所示:

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

tf.disable_eager_execution()

text_generator = hub.Module('https://tfhub.dev/google/bertseq2seq/roberta24_bbc/1')
input_documents = ['This is text from the first document.',
                   'This is text from the second document.']
output_documents = text_generator(input_documents)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.tables_initializer())
    final_output = sess.run(output_documents)

print(final_output)

使用KerasLayer(TF2)

尽管文档没有提到它,但它实际上似乎与用于TF2模型的KerasLayer一起工作。如果您可以接受在控制台中获得大量的警告,那么这应该可以做到:

import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text

text_generator = hub.KerasLayer('https://tfhub.dev/google/bertseq2seq/roberta24_bbc/1')
input_documents = tf.constant(['This is text from the first document.',
                               'This is text from the second document.'])
output_documents = text_generator(input_documents)

print(output_documents)

注意,文档需要包装在tf.constant中才能在这种情况下工作

注意:您需要更长的文档文本才能获得有意义的摘要。这两个示例(例如,“这是第一份文件中的文本”)未进行总结

相关问题 更多 >