Skip to content

Instantly share code, notes, and snippets.

@mrry
Created March 29, 2017 19:40
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 mrry/01e2de711e62c84018a7c6102925c5ec to your computer and use it in GitHub Desktop.
Save mrry/01e2de711e62c84018a7c6102925c5ec to your computer and use it in GitHub Desktop.
Saver SO question
import tensorflow as tf
import numpy as np
## save to a file
## need to use the same shape and dtype when restore
W = tf.Variable([[1,2,3],[3,4,5]], dtype=tf.float32, name='W')
b = tf.Variable([[1,2,3]],dtype=tf.float32, name='b')
# initialization
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
saver.save(sess, '/tmp/save.ckpt')
tf.reset_default_graph()
W = tf.Variable(np.arange(6).reshape((2,3)), dtype=tf.float32, name='W')
b = tf.Variable(np.arange(3).reshape((1,3)), dtype=tf.float32, name='b')
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, '/tmp/save.ckpt')
print('weights', sess.run(W))
print('biases', sess.run(b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment