<p>我的问题与您完全相同,但在Tensorflow 2.6.0和Python3.9.1中得到了解决。下面的解决方案同时适用于CPU和GPU</p>
<h3>使用修改后的TF1示例</h3>
<p>以下步骤使TensorFlow Hub示例工作:</p>
<ul>
<li><code>pip install tensorflow-text</code></li>
<li>将<code>import tensorflow_text as text</code>添加到Python导入中</li>
<li>禁用即时执行:<code>tf.disable_eager_execution()</code></li>
<li>使用TF1会话获取结果,包括手动初始化变量和表</李>
</ul>
<p>整个程序如下所示:</p>
<pre class="lang-py prettyprint-override"><code>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)
</code></pre>
<h3>使用KerasLayer(TF2)</h3>
<p>尽管文档没有提到它,但它实际上似乎与用于TF2模型的<code>KerasLayer</code>一起工作。如果您可以接受在控制台中获得大量的警告,那么这应该可以做到:</p>
<pre class="lang-py prettyprint-override"><code>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)
</code></pre>
<p>注意,文档需要包装在<code>tf.constant</code>中才能在这种情况下工作</p>
<p><strong>注意</strong>:您需要更长的文档文本才能获得有意义的摘要。这两个示例(例如,“这是第一份文件中的文本”)未进行总结</p>