Bert issue: cannot import name 'modeling' from 'bert'
测试Bert代码复现
from bert import modeling
No module named 'bert_serving' 解决方法
pip install bert-serving-server --user
pip install bert-serving-client --user
问题依旧
pip install bert
ImportError: cannot import name 'modeling' from 'bert' (C:\ProgramData\Anaconda3\lib\site-packages\bert\__init__.py)
PyTorch版本的谷歌AI BERT模型,带有加载谷歌预训练模型的脚本
https://www.ctolib.com/huggingface-pytorch-pretrained-BERT.html
pip install bert-tensorflow
出现新的问题
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-0ee46f3b6da2> in <module>
6
7 # 这里是下载下来的bert配置文件
----> 8 bert_config = modeling.BertConfig.from_json_file("chinese_L-12_H-768_A-12/bert_config.json")
9 # 创建bert的输入
10 input_ids=tf.placeholder (shape=[64,128],dtype=tf.int32,name="input_ids")
C:\ProgramData\Anaconda3\lib\site-packages\bert\modeling.py in from_json_file(cls, json_file)
91 def from_json_file(cls, json_file):
92 """Constructs a `BertConfig` from a json file of parameters."""
---> 93 with tf.gfile.GFile(json_file, "r") as reader:
94 text = reader.read()
95 return cls.from_dict(json.loads(text))
AttributeError: module 'tensorflow' has no attribute 'gfile'
解决方法:
将tf.gfile 换成tf.io.gfile.
继续找方法
降低tensorflow的版本,将tensorflow2.X的降低为tensorflow1.X
#卸载tensorflow
pip uninstall tensorflow
conda uninstall tensorflow #这一步非常漫长,大概持续了3个小时
#安装制定版本的tensorflow
pip install tensorflow==1.8.0
2020.5.31
CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghu
a.edu.cn/anaconda/pkgs/main/win-64/qt-5.9.7-vc14h73c81de_0.conda>
Elapsed: -
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
(base) C:\Users\Administrator>
conda uninstall tensorflow
#第二天重新尝试,失败告终
干脆跳过这一步算了。
pip install tensorflow==1.8.0
(base) C:\Users\Administrator>pip install tensorflow==1.8.0
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
ERROR: Could not find a version that satisfies the requirement tensorflow==1.8.0
(from versions: 1.13.0rc1, 1.13.0rc2, 1.13.1, 1.13.2, 1.14.0rc0, 1.14.0rc1, 1.1
4.0, 1.15.0rc0, 1.15.0rc1, 1.15.0rc2, 1.15.0rc3, 1.15.0, 1.15.2, 1.15.3, 2.0.0a0
, 2.0.0b0, 2.0.0b1, 2.0.0rc0, 2.0.0rc1, 2.0.0rc2, 2.0.0, 2.0.1, 2.0.2, 2.1.0rc0,
2.1.0rc1, 2.1.0rc2, 2.1.0, 2.1.1, 2.2.0rc0, 2.2.0rc1, 2.2.0rc2, 2.2.0rc3, 2.2.0
rc4, 2.2.0)
ERROR: No matching distribution found for tensorflow==1.8.0
WARNING: You are using pip version 20.1; however, version 20.1.1 is available.
You should consider upgrading via the 'c:\programdata\anaconda3\python.exe -m pi
p install --upgrade pip' command.
pip install tensorflow==1.15.0
大功告成。
附: 部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import tensorflow as tf #import bert from bert import modeling import os # 这里是下载下来的bert配置文件 bert_config = modeling.BertConfig.from_json_file( "chinese_L-12_H-768_A-12/bert_config.json" ) # 创建bert的输入 input_ids = tf.placeholder (shape = [ 64 , 128 ],dtype = tf.int32,name = "input_ids" ) input_mask = tf.placeholder (shape = [ 64 , 128 ],dtype = tf.int32,name = "input_mask" ) segment_ids = tf.placeholder (shape = [ 64 , 128 ],dtype = tf.int32,name = "segment_ids" ) # 创建bert模型 model = modeling.BertModel( config = bert_config, is_training = True , input_ids = input_ids, input_mask = input_mask, token_type_ids = segment_ids, use_one_hot_embeddings = False # 这里如果使用TPU 设置为True,速度会快些。使用CPU 或GPU 设置为False ,速度会快些。 ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #bert模型参数初始化的地方 init_checkpoint = "chinese_L-12_H-768_A-12/bert_model.ckpt" use_tpu = False # 获取模型中所有的训练参数。 tvars = tf.trainable_variables() # 加载BERT模型 (assignment_map, initialized_variable_names) = modeling.get_assignment_map_from_checkpoint(tvars, init_checkpoint) tf.train.init_from_checkpoint(init_checkpoint, assignment_map) tf.logging.info( "**** Trainable Variables ****" ) # 打印加载模型的参数 for var in tvars: init_string = "" if var.name in initialized_variable_names: init_string = ", *INIT_FROM_CKPT*" tf.logging.info( " name = %s, shape = %s%s" , var.name, var.shape, init_string) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!