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

使用TFHub的BERT预处理时出现NotFoundError问题求助

问题描述

在2021款MacBook Pro(Apple Silicon)设备上,使用Python 3.9.13和TensorFlow v2.9.2调用TensorFlow Hub的预训练BERT预处理模型时,执行文本预处理操作返回NotFoundError,无法解决。预处理模型地址为:https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3

代码

import tensorflow_hub as hub

bert_preprocess = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3")
bert_encoder = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/4")
print(bert_preprocess(["test"]))

报错信息

Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
Cell In [42], line 3
      1 bert_preprocess = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3")
      2 bert_encoder = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/4")
----> 3 print(bert_preprocess(["test"]))

File ~/miniforge3/envs/tfenv/lib/python3.9/site-packages/keras/utils/traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb

File ~/miniforge3/envs/tfenv/lib/python3.9/site-packages/tensorflow_hub/keras_layer.py:237, in KerasLayer.call(self, inputs, training)
    234   else:
    235     # Behave like BatchNormalization. (Dropout is different, b/181839368.)
    236     training = False
---> 237   result = smart_cond.smart_cond(training,
    238                                  lambda: f(training=True),
    239                                  lambda: f(training=False))
    241 # Unwrap dicts returned by signatures.
    242 if self._output_key:

File ~/miniforge3/envs/tfenv/lib/python3.9/site-packages/tensorflow_hub/keras_layer.py:239, in KerasLayer.call.<locals>.<lambda>()
...
     [[StatefulPartitionedCall/StatefulPartitionedCall/bert_pack_inputs/PartitionedCall/RaggedConcat/ArithmeticOptimizer/AddOpsRewrite_Leaf_0_add_2]] [Op:__inference_restored_function_body_209194]

Call arguments received by layer "keras_layer_6" (type KerasLayer):
  • inputs=["'test'"]
  • training=None
原因及解决方法

核心原因

Apple Silicon(M系列)芯片的TensorFlow环境中,旧版本TensorFlow(如v2.9.2)与部分TensorFlow Hub预训练模型存在兼容性问题,导致模型执行时出现资源定位失败的NotFoundError。

解决步骤

  • 升级TensorFlow版本:将TensorFlow升级到v2.10及以上版本,新版本对M系列芯片的支持更完善,修复了大量兼容性bug。执行命令:
    pip install --upgrade tensorflow
    
  • 确认环境架构:确保当前Python环境是通过Miniforge/Conda-forge创建的Apple Silicon专属环境,避免使用Rosetta模拟的x86环境,这类环境容易引发适配问题。
  • 本地加载模型:如果升级后仍有问题,手动下载模型到本地目录,再通过本地路径加载,避免网络加载时的潜在问题:
    1. 下载预处理模型和编码器模型到本地文件夹(如./bert_preprocess和./bert_encoder)
    2. 修改代码加载本地模型:
      bert_preprocess = hub.KerasLayer("./bert_preprocess")
      bert_encoder = hub.KerasLayer("./bert_encoder")
      
  • 检查输入格式:报错显示输入为["'test'"],确认代码中输入是纯字符串列表["test"],避免多余引号导致的格式异常。

内容的提问来源于stack exchange,提问作者Joseph Yu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 16:45:31