初始化给定名称范围内的所有变量

时间:2019-07-12 15:09:27

标签: python tensorflow

如何初始化名称范围内的所有变量,例如:

with graph.name_scope('name9'):
  with graph.as_default():
    sign_in = tf.placeholder(...

    conv = tf.keras.layers.Conv2D( ..., kernel_initializer=tf.initializers.lecun_normal(seed=137), bias_initializer=tf.initializers.lecun_normal(seed=137) )(sign_in)

但是当我使用sess.run()时,即使使用关键字arg,我仍然无法获得初始化:

FailedPreconditionError: Error while reading resource variable gcnn2d_d1/conv_linear/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/gcnn2d_d1/conv_linear/bias)

我想初始化与该范围有关的Conv2Dweightsbiases变量,如果我调用tf.global_variables_initializer afaik,它将为所有变量添加初始化操作在集合中,在Op节点之间创建依赖项箭头。

更新:

已解决:

with graph.as_default():
  with tf.variable_scope('name9'):

并在运行前执行tf.global_variables_initializer()。

但是要运行很多次,同一模型带有检查点,依此类推,所以我不得不做一堆难看的ifs来验证是否有初始化操作。

0 个答案:

没有答案