【问题标题】:TensorFlow: Is there a way to initialize variables in variable scope if they're not initialized, and reuse them if they are?TensorFlow:有没有办法在变量范围内初始化变量,如果它们没有被初始化,如果它们被重用它们?
【发布时间】:2016-12-18 20:33:38
【问题描述】:

我正在尝试编写“干净”的代码。我想做的是

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)

但这似乎不起作用。 我尝试在网上寻找示例,但没有找到任何与我试图解决的情况相近的东西,即干净地使用一个大型网络,其中几乎每个变量都在不同的范围内。

【问题讨论】:

  • 我不知道TensorFlow部分,但我认为如果你想检查变量是否存在,应该是NameError(而不是ValueError)。

标签: python coding-style tensorflow


【解决方案1】:

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

这里已经回答了这个问题: Tensorflow variable scope: reuse if variable exists

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2017-08-16
    • 2014-11-17
    • 2015-10-20
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多