Re-initialize variables in Tensorflow

11,551

initialize_all_variables should work to re-initialize previously initialized var.

Just did this sanity check in 0.10

tf.reset_default_graph()
a = tf.Variable(tf.ones_initializer(()))
init_op = tf.initialize_all_variables()
modify_op = a.assign(5.0)

sess = tf.InteractiveSession()
sess.run(init_op)
print(a.eval())
sess.run(modify_op)
print(a.eval())
sess.run(init_op)
print(a.eval())

Result

1.0
5.0
1.0
Share:
11,551
Wassim Gr
Author by

Wassim Gr

Web Developer and Designer, Computer Vision Undergraduate Researcher. Currently working on the AMP Project at Google

Updated on June 26, 2022

Comments

  • Wassim Gr
    Wassim Gr almost 2 years

    I am using a Tensorflow tf.Saver to load a pre-trained model and I want to re-train a few of its layers by erasing (re-initializing to random) their appropriate weights and biases, then training those layers and saving the trained model. I can not find a method that re-initializes the variables. I tried tf.initialize_variables(fine_tune_vars) but it did not work (I'd assume because the variables are already initialized), I have also seen that you can pass variables to the tf.Saver so that you partially load the model, however that is half of what I want to achieve (because when I save the trained model, I want it to save all variables not only the ones I loaded).

    Thank you in advance!