TensorFlow:如果它们未初始化,有没有办法在变量范围内初始化变量,如果它们是,则重用它们?

时间:2016-12-18 20:33:39

标签: python coding-style tensorflow

我试着写#34;清洁"码。我想做的是

if(var exists)
    var = tf.get_variable(...)
else
    var = init_var

我尝试使用try-except执行此操作,如下所示:

try:
   with scope('my scope'):
       var = tf.get_variable('v', reuse=True)
except ValueError:
   with scope('my scope'):
       var = tf.get_variable('v', reuse=False)

但这似乎不起作用。 我尝试过在线查找示例,但是我还没有发现任何与我试图解决的案件有关的事情,即干净地使用大型网络,几乎每个变量都在不同的范围内。

1 个答案:

答案 0 :(得分:1)

tf.get_variable()没有重用参数。

这个问题已在这里得到解答: Tensorflow variable scope: reuse if variable exists