bert分类模型使用tfserving部署。
bert模型服务化现在已经有对应开源库部署。
例如:1.https://github.com/macanv/BERT-BiLSTM-CRF-NER 该项目支持三种不同的任务
2.使用已有的包 pip install bert-serving-server
# 服务端 pip install bert-serving-client
# 客户端,与服务端互相独立
3.本文主要记录通过bert预训练的模型如何使用tfserving进行加载,这样在已有的tfserving model_config服务下增加一个config就可以了。
1.bert 分类模型见这篇文章。
[模型实践(二)bert] https://zhuanlan.zhihu.com/p/89804880
2.模型输出文件
训练过程种需要注意在该文件run_classifier.py
额外需要添加
def convert_single_example(ex_index, example, label_list, max_seq_length,
tokenizer):
"""Converts a single `InputExample` into a single `InputFeatures`."""
if isinstance(example, PaddingInputExample):
return InputFeatures(
input_ids=[0] * max_seq_length,
input_mask=[0] * max_seq_length,
segment_ids=[0] * max_seq_length,
label_id=0,
is_real_example=False)
label_map = {}
for (i, label) in enumerate(label_list):
label_map[label] = i
#!!!!额外添加 保存标签信息和枚举的映射关系 并写入label2id.pkl 该文件会一起输出到output文件夹下
output_label2id_file = os.path.join(FLAGS.output_dir, "label2id.pkl")
if not os.path.exists(output_label2id_file):
with open(output_label2id_file,'wb') as w:
pickle.dump(label_map,w)
label2id.pkl其实就是一个映射关系(自定义label和模型使用label),也可以自己序列化写入:
# pickle.load(open("label2id.pkl", 'rb'))
{'1': 0, '2': 1, '3': 2}
实际训练是使用这样的命令:
export DATA_DIR=/data/bert/data
export BERT_BASE_DIR=/data/bert/model/chinese_L-12_H-768_A-12
expo