TF2 和 python 中的 BERT 预处理器模型存在问题

问题描述 投票:0回答:1

我正在尝试使用 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])

我看过无数的教程,甚至使用了张量流文档上的教程,即使我复制和粘贴,它们仍然不起作用。我尝试过不同版本的 tf、tf-text 和 tf-hub。我在这个项目中使用了tensorflow-gpu-jupyter docker容器。

这是我安装库的方式:

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

版本有: 张量流:2.16.1 张量流文本:2.16.1 张量流中心:0.16.1

我看到的所有其他论坛都说可以解决这个问题

tf.config.run_functions_eagerly(True)
但这不起作用。

任何事情都会有所帮助。如果您知道如何解决请回答。

python machine-learning tensorflow2.0 bert-language-model tensorflow-hub
1个回答
0
投票

您尝试从 Kaggle 加载的模型可能已过时。尝试直接从 TF hub 获取 BERT 模型。请参阅本教程: BERT教程

© www.soinside.com 2019 - 2024. All rights reserved.