Python TensorFlow:如果变量是';是否重新初始化,如果已初始化,是否重新使用?

Python TensorFlow:如果变量是';是否重新初始化,如果已初始化,是否重新使用?,python,coding-style,tensorflow,Python,Coding Style,Tensorflow,我想写“干净”的代码。我想做的是 if(var exists) var = tf.get_variable(...) else var = init_var 我试着这样做,除了: try: with scope('my scope'): var = tf.get_variable('v', reuse=True) except ValueError: with scope('my scope'): var = tf.get_variabl

我想写“干净”的代码。我想做的是

if(var exists)
    var = tf.get_variable(...)
else
    var = init_var
我试着这样做,除了:

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)
但这似乎不起作用。 我试着在网上查找示例,但没有找到任何与我试图解决的问题类似的例子,即干净地使用大型网络,其中几乎每个变量都在不同的范围内。

tf.get\u variable()没有重用参数

这里已经回答了这个问题:

我不知道TensorFlow部分,但如果要检查变量是否存在,我认为例外情况应该是
NameError
(而不是
ValueError
)。