bert分类模型使用tfserving部署。
bert模型服务化现在已经有对应开源库部署。
例如:1.https://github.com/macanv/BERT-BiLSTM-CRF-NER 该项目支持三种不同的任务
2.使用已有的包
pip install bert-serving-server
# 服务端
pip install bert-serving-client
# 客户端,与服务端互相独立
本文主要记录通过bert预训练的模型如何使用tfserving进行加载,这样在已有的tfserving model_config服务下增加一个config就可以了。
1.bert 分类模型见这篇文章。
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