调用create_tokenizer_from_hub_module出现'AutoTrackable' object is not callable错误
报错修复方案
核心原因
'AutoTrackable' object is not callable 报错由TensorFlow Hub模块的调用方式和环境版本不匹配导致:你当前使用的是TF1.x时期的BERT模块调用写法,tf_hub.load()在TF2.x环境加载模块后返回的AutoTrackable对象不支持直接传参调用获取签名信息。
修复步骤
- 替换函数内的tokenization_info获取逻辑,适配TF2.x的调用规则,修改后的完整函数如下:
import tensorflow_hub as tf_hub from bert.tokenization import FullTokenizer def create_tokenizer_from_hub_module(bert_path): """Get the vocab file and casing info from the Hub module.""" bert_module = tf_hub.load(bert_path) # 从signatures属性中获取tokenization_info签名并执行 tokenization_info = bert_module.signatures["tokenization_info"]() # 直接用numpy方法取值,无需tf.Session运行 vocab_file = tokenization_info["vocab_file"].numpy().decode("utf-8") do_lower_case = tokenization_info["do_lower_case"].numpy() return FullTokenizer(vocab_file=vocab_file, do_lower_case=do_lower_case)
- 如果你使用的BERT Hub模型没有内置
tokenization_info签名,可直接使用transformers库加载对应版本的分词器,无需从TF Hub模块读取配置,示例代码如下:
from transformers import BertTokenizer # 替换为你使用的BERT模型对应的预训练分词器名 tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
- 如果你仍在使用TF1.x环境,将加载模块的代码替换为
bert_module = tf_hub.Module(bert_path)即可兼容原有调用逻辑。
内容的提问来源于stack exchange,提问作者Agustina Villaggi
相关产品推荐
相关产品推荐

