Skip to content

Instantly share code, notes, and snippets.

@wfng92
Created June 18, 2019 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wfng92/ed5d5b10d06c2349b05cfc975d6efe96 to your computer and use it in GitHub Desktop.
Save wfng92/ed5d5b10d06c2349b05cfc975d6efe96 to your computer and use it in GitHub Desktop.
import tensorflow as tf
import os
#path that contains all 3 ckpt files of your fine-tuned model
path = './bert_output'
#path to output the new optimized model
output_path = os.path.join(path, 'optimized_model')
sess = tf.Session()
imported_meta = tf.train.import_meta_graph(os.path.join(path, 'model.ckpt-236962.meta')) #based on the steps of your fine-tuned model
imported_meta.restore(sess, os.path.join(path, 'model.ckpt-236962')) #based on the steps of your fine-tuned model
my_vars = []
for var in tf.all_variables():
if 'adam_v' not in var.name and 'adam_m' not in var.name:
my_vars.append(var)
saver = tf.train.Saver(my_vars)
saver.save(sess, os.path.join(output_path, 'model.ckpt')) #change model.ckpt to name of your preference
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment