简体   繁体   中英

How to initialize tensorflow variable that wasn't saved other than with tf.global_variables_initializer()

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"]))    

Finally after looking at:

In TensorFlow is there any way to just initialize uninitialised variables?

I liked https://stackoverflow.com/users/1090562/salvador-dali answer and modified it to use itertools.compress which is much faster if the variables are more than a handful.

def initialize_uninitialized_vars(sess):
    from itertools import compress
    global_vars = tf.global_variables()
    is_not_initialized = sess.run([~(tf.is_variable_initialized(var)) \
                                   for var in global_vars])
    not_initialized_vars = list(compress(global_vars, is_not_initialized))

    if len(not_initialized_vars):
        sess.run(tf.variables_initializer(not_initialized_vars))

My code then becomes:

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')  

  # Loading the saved "w1" Variable
  new_saver.restore(sess,'my_test_model2')

  # initialize the unitialized variables
  initialize_uninitialized_vars(sess)

  print(sess.run(["w1:0"]))
  print(sess.run(["w2:0"]))  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM