You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

复现TensorFlow BERT文本分类教程时遇KerasTensor符号张量错误及依赖冲突问题求助

复现TensorFlow BERT文本分类教程时遇KerasTensor符号张量错误及依赖冲突问题求助

我在复现TensorFlow官方文档里的BERT文本分类教程时碰到了两个十分棘手的问题,折腾了好几天都没找到解决办法,特意来求助大家!

环境与操作背景

我分别在Google Colab和Kaggle环境中都做了尝试,原本想按照教程推荐的相近版本,使用TensorFlow 2.14.*版本,先执行了以下依赖安装命令:

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

安装过程中系统提示存在依赖冲突,还要求重启运行时,我按要求重启后问题依然存在;后来我也试过教程里明确提到的2.13.*版本依赖组合,还是没能解决问题。

我的代码实现

import tensorflow as tf
from tensorflow.keras.datasets import imdb
import tensorflow_hub as hub
import tensorflow_text as text
from tensorflow.keras.callbacks import EarlyStopping

tfhub_handle_encoder = "https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-128_A-2/1"
tfhub_handle_preprocess = "https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3"

def build_classifier_model():
    text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
    preprocessing_layer = hub.KerasLayer(
        tfhub_handle_preprocess, name='preprocessing', trainable=False
    )
    encoder_layer = hub.KerasLayer(
        tfhub_handle_encoder, name='BERT_encoder', trainable=True
    )
    
    # 直接将符号输入传递给预处理层(按教程说法现在是允许这么做的)
    processed_inputs = preprocessing_layer(text_input)
    encoder_outputs = encoder_layer(processed_inputs)
    
    # 使用BERT编码器输出的pooled_output
    pooled_output = encoder_outputs['pooled_output']
    net = tf.keras.layers.Dropout(0.1)(pooled_output)
    net = tf.keras.layers.Dense(1, activation=None, name='classifier')(net)
    return tf.keras.Model(text_input, net)

classifier_model = build_classifier_model()

执行时遇到的错误

当代码运行到classifier_model = build_classifier_model()这一行时,直接抛出了ValueError错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-f031ba765f1f> in <cell line: 0>()
----> 1 classifier_model = build_classifier_model()

7 frames
/usr/local/lib/python3.11/dist-packages/keras/src/backend/common/keras_tensor.py in __array__(self)
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=False, name=text>
 • training=None

依赖冲突详情

安装依赖时终端输出的冲突提示如下:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
dopamine-rl 4.1.2 requires tf-keras>=2.18.0, but you have tf-keras 2.15.1 which is incompatible.
thinc 8.3.6 requires numpy<3.0.0,>=2.0.0, but you have numpy 1.26.4 which is incompatible.
grpcio-status 1.71.0 requires protobuf<6.0dev,>=5.26.1, but you have protobuf 4.25.7 which is incompatible.
jax 0.5.2 requires ml_dtypes>=0.4.0, but you have ml-dtypes 0.3.2 which is incompatible.
ydf 0.11.0 requires protobuf<6.0.0,>=5.29.1, but you have protobuf 4.25.7 which is incompatible.
tensorflow-decision-forests 1.11.0 requires tensorflow==2.18.0, but you have tensorflow 2.15.1 which is incompatible.
tensorflow-decision-forests 1.11.0 requires tf-keras~=2.17, but you have tf-keras 2.15.1 which is incompatible.
Successfully installed keras-2.15.0 ml-dtypes-0.3.2 numpy-1.26.4 protobuf-4.25.7 tensorboard-2.15.2 tensorflow-2.15.1 tensorflow-estimator-2.15.0 tensorflow-text-2.15.0 tf-keras-2.15.1 wrapt-1.14.1
WARNING: The following packages were previously imported in this runtime: [keras,ml_dtypes,tensorflow,tensorflow_text,tf_keras,wrapt]
You must restart the runtime in order to use newly installed versions.

我已经尝试过的操作

  1. 严格按照提示重启了运行时,但错误依旧存在
  2. 替换为教程里明确推荐的tensorflow-text==2.13.*和tf-models-official==2.13.*版本,还是出现类似的KerasTensor错误和依赖冲突

有没有大佬能帮我梳理下这两个问题的解决思路?尤其是那个KerasTensor的错误,我的代码和教程里的几乎完全一致,为什么会弹出“不能将符号张量转换为NumPy数组”的提示呢?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 11:19:35