blei-lab / edward

A probabilistic programming language in TensorFlow. Deep generative models, variational inference.
http://edwardlib.org
Other
4.83k stars 759 forks source link

Resetting graph / scope in a running session #877

Closed sadatnfs closed 6 years ago

sadatnfs commented 6 years ago

I'm trying to run some example code in a Jupyter Notebook, and it seems like whenever I define a variable, I am not able to reuse them, even when I have reuse=True in my scope:

with tf.variable_scope("foo", reuse=True) as scope:    
    qw = Empirical(params=tf.get_variable("qw/params", [T, D]))
    qb = Empirical(params=tf.get_variable("qb/params", [T, 1]))

[stuff]

ValueError: Variable foo/qw/params does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?

I run tf.reset_default_graph() and tf.get_variable_scope().reuse_variables() at the beginning of my notebook, which aren't helping. The only way right now seems to be restarting the kernel every time I want to clean out my workspace and rerun a piece of code.

Is there an Edward method to do this cleanup that I'm not aware of?

mrosenkranz commented 6 years ago

Not sure if this is the Edward method of doing it, but I usually put

tf.reset_default_graph()
sess = tf.InteractiveSession()

at the top of my notebooks for quick iterations.

sadatnfs commented 6 years ago

Ah interesting @mrosenkranz . When you run Edward codes afterwards in your notebook, do you wrap it in a with tf. .. scope? Just checking to see what works for you :)

mrosenkranz commented 6 years ago

Yes, often I wrap stuff in with tf.variable_scope(..., reuse=tf.AUTO_REUSE) blocks.

sadatnfs commented 6 years ago

Perfect, this is working. Thanks @mrosenkranz !!