I was looking at how to save/load specific variables in tensorflow.
I can load and save specific variables with no problem, however, I can't figure out how to initialize the remaining unsaved variables without using
sess.run(tf.global_variables_initializer())
then overwriting the saved variable with:
new_saver.restore(sess,'my_test_model2')
This works and initializes the unsaved Variable (w2) and restores the saved variable (w1) but seems very kludgy and unphythonic.
I want to know how to get rid of the
tf.global_variables_initializer()
,at the end where I restore the w1 variable, to something work pythonic.
I tried sess.run(tf.variables_initializer([w2]))
and got input: "^w2/Assign" is not an element of this graph.)
I also tried sess.run(tf.variables_initializer(["w2:0"]))
and got AttributeError: 'str' object has no attribute 'initializer'
import tensorflow as tf
print(tf.__version__)
w1 = tf.Variable(tf.linspace(0.0, 0.5, 6), name="w1")
w2 = tf.Variable(tf.linspace(1.0, 5.0, 6), name="w2")
saver = tf.train.Saver({'w1':w1})
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for v in tf.global_variables():
print (v.name)
print(sess.run(["w1:0"]))
print(sess.run(["w2:0"]))
saver.save(sess, 'my_test_model')
tf.reset_default_graph()
print ('-'*80 )
w1 = tf.Variable(tf.linspace(10.0, 50.0, 6), name="w1")
w2 = tf.Variable(tf.linspace(100.0, 500.0, 6), name="w2")
saver = tf.train.Saver({'w1':w1})
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for v in tf.global_variables():
print (v.name)
print(sess.run(["w1:0"]))
print(sess.run(["w2:0"]))
saver.save(sess, 'my_test_model2')
tf.reset_default_graph()
print ('-'*80 )
print("Let's load w1 \n")
with tf.Session() as sess:
# Loading the model structure from 'my_test_model.meta'
new_saver = tf.train.import_meta_graph('my_test_model.meta')
# I do this to make sure w1:0 and w2:0 are variables
for v in tf.global_variables():
print (v.name)
sess.run(tf.global_variables_initializer()) #<----- line I want to make more pythonic
# sess.run(tf.variables_initializer([w2])) # input: "^w2/Assign" is not an element of this graph.)
# sess.run(tf.variables_initializer(["w2:0"])) #AttributeError: 'str' object has no attribute 'initializer'
# Loading the saved "w1" Variable
new_saver.restore(sess,'my_test_model2')
print(sess.run(["w1:0"]))
print(sess.run(["w2:0"]))