bertseq2seq : RuntimeError: variable_scope module_1/ 未使用,但相应的 name_scope 已被占用

问题描述

我在 Colab 上尝试从 TFHub 运行 bertseq2seq 时遇到此错误。请帮助解决错误。 (我正在尝试使用 colab,因为我已经在我的机器中使用了 TF2,并且不想通过降级或并行 TF1 来弄乱它。如果可行,那么我将尝试在服务器中安装 TF1)

%tensorflow_version 1.x

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

#### Sentence Fusion #########
text_generator = hub.Module(
    'https://tfhub.dev/google/bertseq2seq/roberta24_discofuse/1')

enter image description here

解决方法

我无法使用您的代码片段和新的 Colab 笔记本重现该问题,但以下代码在 Colab 中对我有用:

# You can achieve TF1 behavior by still using a TF2 installation using the
# compat API. See the Migration Guide (https://www.tensorflow.org/guide/migrate)
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

!pip install tensorflow_text

import tensorflow_hub as hub
import tensorflow_text

text_generator = hub.Module('https://tfhub.dev/google/bertseq2seq/roberta24_discofuse/1')
input_texts = ['Sentence 1a. Sentence 1b.','Sentence 2a. Sentence 2b. Sentence 2c.']

with tf.Session() as sess:
  sess.run(tf.group(tf.global_variables_initializer(),tf.tables_initializer()))
  print(sess.run(text_generator(input_texts)))
  # [b'Sentence 1a. Sentence 1b' b'Sentence 2a. Sentence 2b. Sentence 2c']

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...