Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-initialize variables in Tensorflow

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!

like image 480
Wassim Gr Avatar asked Aug 15 '16 00:08

Wassim Gr


1 Answers

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
like image 149
Yaroslav Bulatov Avatar answered Nov 17 '22 18:11

Yaroslav Bulatov