Re-initialize variables in Tensorflow

2019-04-07 17:31发布

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!

1条回答
SAY GOODBYE
2楼-- · 2019-04-07 17:51

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
查看更多
登录 后发表回答