简体   繁体   English

在Tensorflow中重新初始化变量

[英]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. 我正在使用Tensorflow tf.Saver加载预先训练的模型,我想通过擦除(重新初始化为随机)其适当的权重和偏差来重新训练其几个层,然后训练这些层并保存训练的模型。 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). 我尝试了tf.initialize_variables(fine_tune_vars)但它没有用(我假设因为变量已经初始化了),我也看到你可以将变量传递给tf.Saver以便你部分加载模型,但是是我想要实现的目标的一半(因为当我保存训练的模型时,我希望它保存所有变量,而不仅仅是我加载的变量)。

Thank you in advance! 先感谢您!

initialize_all_variables should work to re-initialize previously initialized var. initialize_all_variables应该可以重新初始化以前初始化的var。

Just did this sanity check in 0.10 刚刚在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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