Question: TensorFlow: Is there a way to initialize variables in variable scope if they're not initialized, and reuse them if they are?

Question

TensorFlow: Is there a way to initialize variables in variable scope if they're not initialized, and reuse them if they are?

Answers 0
Added at 2016-12-18 21:12
Tags
Question

I'm trying to write "clean" code. What I want to do is

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

I tried doing this with a try-except, like so:

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)

But that doesn't seem to work. I've tried looking for examples online, but haven't found anything close to the case I'm trying to solve, which is to cleanly use a large network where almost every variable is under a different scope.

Answers
Source Show
◀ Wstecz