Skip to content

Instantly share code, notes, and snippets.

@PatWie
Last active March 19, 2017 06:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatWie/98013e6ad0281f0f07abcdd8d2b1a8b7 to your computer and use it in GitHub Desktop.
Save PatWie/98013e6ad0281f0f07abcdd8d2b1a8b7 to your computer and use it in GitHub Desktop.
create, save, destroy, load
import tensorflow as tf
# save
dummy = tf.Variable(tf.truncated_normal(shape=[10]), name='dummy')
with tf.Session() as sess:
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
expected = sess.run(tf.reduce_sum(dummy))
saver.save(sess, 'model')
# destroy
tf.reset_default_graph()
# read Graph + Checkpoint
with tf.Session() as sess:
loader = tf.train.import_meta_graph('model.meta')
loader.restore(sess, tf.train.latest_checkpoint('.'))
dummy = tf.get_default_graph().get_tensor_by_name("dummy:0")
actual = sess.run(tf.reduce_sum(dummy))
assert expected == actual
# destroy
tf.reset_default_graph()
# read Checkpoint
with tf.Session() as sess:
dummy = tf.Variable(tf.truncated_normal(shape=[10]), name='dummy')
sess.run(tf.global_variables_initializer())
actual = sess.run(tf.reduce_sum(dummy))
assert expected != actual
loader = tf.train.Saver()
loader.restore(sess, tf.train.latest_checkpoint('.'))
actual = sess.run(tf.reduce_sum(dummy))
assert expected == actual
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment