目前没有整理完善,先留个坑~
Bert模型介绍
BERT的关键技术创新是将Transformers双向训练作为一种流行的注意力模型应用到语言建模中。Masked LM (MLM)在向BERT输入单词序列之前,每个序列中有15%的单词被[MASK]token替换。然后,该模型试图根据序列中其他非MASK词提供的上下文来预测MASK词的原始值。
本文主要记录使用tensorflow serving部署训练好的bert模型,并根据模型获取句子向量表示。
ckpt转saved_model格式
google bert原始预训练模型保存的事ckpt格式,用tfserving部署需要saved_model的pb格式,这里需要一个转化过程。
import json
import os
import tensorflow as tf
import argparse
import modeling
def create_model(bert_config, is_training, input_ids):
# 通过传入的训练数据,进行representation
model = modeling.BertModel(config=bert_config, is_training=is_training, input_ids=input_ids)
output = model.get_pooled_output()
# output = model.get_sequence_output()
return output
def transfer_saved_model(args):
gpu_config = tf.ConfigProto()
gpu_config.gpu_options.allow_growth = True
sess = tf.Session(config=gpu_config)
print("going to restore checkpoint")
bert_config_file = os.path.join(args.model_path, 'bert_config.json')
bert_config = modeling.BertConfig.from_json_file(bert_config_file)
input_ids = tf.placeholder(tf.int32, [None, args.max_seq_len], name="input_ids")
output = create_