TF2和Python中BERT预处理模型的问题

0 投票
1 回答
79 浏览
提问于 2025-04-13 19:28

我正在尝试使用BERT来做一个文本分类的项目。但是我一直遇到一个错误:

ValueError                                Traceback (most recent call last)
Cell In[37], line 4
      2 text_input = tf.keras.Input(shape=(), dtype=tf.string, name='text')
      3 bert_preprocess = hub.KerasLayer(preprocess_url, name='preprocessing')
----> 4 preprocessed_text = bert_preprocess(text_input)
      5 bert_encoder = hub.KerasLayer(encoder_url, 
      6                               trainable=True, 
      7                               name='BERT_encoder')
      8 outputs = bert_encoder(preprocessed_text)
ValueError: Exception encountered when calling layer 'preprocessing' (type KerasLayer).
A KerasTensor is symbolic: it's a placeholder for a shape an a dtype. It doesn't have any actual numerical value. You cannot convert it to a NumPy array.

Call arguments received by layer 'preprocessing' (type KerasLayer):
  • inputs=<KerasTensor shape=(None,), dtype=string, sparse=None, name=text>
  • training=None

A KerasTensor is symbolic: it's a placeholder for a shape an a dtype. It doesn't have any actual numerical value. You cannot convert it to a NumPy array.


在构建这个模型时:


preprocess_url = 'https://www.kaggle.com/models/tensorflow/bert/frameworks/TensorFlow2/variations/en-uncased-preprocess/versions/3'
encoder_url = 'https://www.kaggle.com/models/tensorflow/bert/frameworks/TensorFlow2/variations/bert-en-uncased-l-12-h-768-a-12/versions/2'

# Bert Layers
text_input = tf.keras.Input(shape=(), dtype=tf.string, name='text')
bert_preprocess = hub.KerasLayer(preprocess_url, name='preprocessing')
preprocessed_text = bert_preprocess(text_input)
bert_encoder = hub.KerasLayer(encoder_url, 
                              trainable=True, 
                              name='BERT_encoder')
outputs = bert_encoder(preprocessed_text)

# Neural network layers
l = tf.keras.layers.Dropout(0.1)(outputs['pooled_output'])
l = tf.keras.layers.Dense(num_classes, activation='softmax', name='output')(l)

# Construct final model
model = tf.keras.Model(inputs=[text_input], outputs=[l])

我看了无数的教程,甚至还用了tensorflow文档上的那些示例,但即使我复制粘贴,它们还是不管用。我尝试了不同版本的tf、tf-text和tf-hub。我在这个项目中使用的是tensorflow-gpu-jupyter的docker容器。

这是我安装库的方式:

!pip install "tensorflow-text"
!pip install "tf-models-official"
!pip install "tensorflow-hub"

版本如下: Tensorflow: 2.16.1 tensorflow-text: 2.16.1 tensorflow-hub: 0.16.1

我在其他论坛看到的关于这个问题的建议是运行 tf.config.run_functions_eagerly(True),但这也没用。

任何帮助都很重要。如果你知道怎么解决,请回复我。

1 个回答

0

我也不是专家,但我遇到过同样的问题。我是通过使用一个稍微旧一点的tf版本来解决这个问题的,具体操作是:

!pip install -U "tensorflow-text==2.15.*"
!pip install -U "tf-models-official==2.15.*"

顺便提一下,我是在Google Colab上运行脚本的(我知道这有时候有它自己的一些特别之处..)。

撰写回答