Tensorflow Bert模型转换
Created by: roreagan
下载 https://storage.googleapis.com/albert_zh/albert_xlarge_zh_183k.zip 后在TensorFlow 1.14.0
下使用如下脚本转为pb:
import tensorflow as tf
from tensorflow.python.framework import graph_util
import sys
def freeze_model(sess, output_tensor_names, freeze_model_path):
out_graph = graph_util.convert_variables_to_constants(
sess, sess.graph.as_graph_def(), output_tensor_names)
with tf.gfile.GFile(freeze_model_path, 'wb') as f:
f.write(out_graph.SerializeToString())
print("freeze model saved in {}".format(freeze_model_path))
sess = tf.Session()
saver = tf.train.import_meta_graph('albert_xlarge_zh_183k/albert_model.ckpt.meta')
saver.restore(sess, tf.train.latest_checkpoint('albert_xlarge_zh_183k'))
output_names = ['bert/pooler/dense/Tanh']
freeze_model(sess, output_names, './bert.pb')