本文介绍了在Tensorflow中重新初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tensorflow tf.Saver加载预先训练的模型,我想通过擦除(重新初始化为随机值)适当的权重和偏差来重新训练其某些层,然后训练这些层和保存训练好的模型.我找不到重新初始化变量的方法.我尝试了tf.initialize_variables(fine_tune_vars),但是它没有用(我想是因为变量已经初始化了),我也看到过可以将变量传递给tf.Saver,以便部分加载模型,但这只是模型的一半.我要实现的目标(因为当我保存训练好的模型时,我希望它保存所有变量,而不仅是我加载的变量).

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

提前谢谢!

推荐答案

initialize_all_variables应该可以重新初始化以前初始化的变量.

initialize_all_variables should work to re-initialize previously initialized var.

只需进行0.10的健全性检查

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

结果

1.0
5.0
1.0

这篇关于在Tensorflow中重新初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:58