问题描述
TensorFlow 中初始化变量的标准方法是
The standard way of initializing variables in TensorFlow is
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
经过一段时间的学习后,我创建了一组新变量,但是一旦我初始化它们,它就会重置我所有现有的变量.目前我解决这个问题的方法是保存我需要的所有变量,然后在 tf.initalize_all_variables 调用后重新应用它们.这有效,但有点丑陋和笨重.我在文档中找不到这样的东西...
After running some learning for a while I create a new set of variables but once I initialize them it resets all my existing variables. At the moment my way around this is to save all the variable I need and then reapply them after the tf.initalize_all_variables call. This works but is a bit ugly and clunky. I cannot find anything like this in the docs...
有谁知道初始化未初始化变量的任何好方法?
Does anyone know of any good way to just initialize the uninitialized variables?
推荐答案
没有优雅*的方法来枚举图中未初始化的变量.但是,如果您可以访问新的变量对象—让我们称它们为 v_6
、v_7
和 v_8
—您可以有选择地使用tf.initialize_variables()
:
There is no elegant* way to enumerate the uninitialized variables in a graph. However, if you have access to the new variable objects—let's call them v_6
, v_7
, and v_8
—you can selectively initialize them using tf.initialize_variables()
:
init_new_vars_op = tf.initialize_variables([v_6, v_7, v_8])
sess.run(init_new_vars_op)
* 试错过程可用于识别未初始化的变量,如下所示:
* A process of trial and error could be used to identify the uninitialized variables, as follows:
uninitialized_vars = []
for var in tf.all_variables():
try:
sess.run(var)
except tf.errors.FailedPreconditionError:
uninitialized_vars.append(var)
init_new_vars_op = tf.initialize_variables(uninitialized_vars)
# ...
...但是,我不会容忍这种行为:-).
...however, I would not condone such behavior :-).
这篇关于在 TensorFlow 中有什么方法可以只初始化未初始化的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!