https://github.com/famousdraw

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

大功告成。

 

附: 部分代码

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 ,速度会快些。
)

  

#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())

  

posted on 2020-05-31 21:16  红色MINI  阅读(3508)  评论(0编辑  收藏  举报

导航