8000 Get initialized value of variable without resetting · Issue #8172 · tensorflow/tensorflow · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Get initialized value of variable without resetting #8172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danijar opened this issue Mar 7, 2017 · 3 comments
Closed

Get initialized value of variable without resetting #8172

danijar opened this issue Mar 7, 2017 · 3 comments
Labels
stat:awaiting response Status - Awaiting response from author

Comments

@danijar
Copy link
Contributor
danijar commented Mar 7, 2017

As far as I know, there is currently no way to get the current value of a variable while ensuring that it has been initialized once. tf.Variable.initialized_value() has a dependency on the initializer that causes the variable to be reset to its initial value every time it is accessed. Using return tf.cond(tf.is_variable_initialized(variable), lambda: variable.value(), lambda: variable.initialized_value()) does not work since the true-branch of the conditional requires the variable to be initialized, even though the false-branch becomes active.

@yaroslavvb
Copy link
Contributor
yaroslavvb commented Mar 7, 2017

This is one of snags in dependent variable initialization that can't be handled with initialized_value, see #4920 for discussion.

I used smart_initialize that rewrote the graph to automatically initialize the variable on first read, but with VariableV2 change, things changed so that now adding a control dependency in var/read on initializer causes a hang.

I think perhaps the way to fix it is to have add a piece of code that monkey-patch replaces tf.Variable class with a version that returns a Tensor which triggers variable initialization on first read

@prb12
Copy link
Member
prb12 commented Mar 7, 2017

Yes, this is a bit of a mess with dynamic control flow. Here's a workaround due to @vrv. Note the use of var.read_value() rather than var.value()

def initialized_var(t):
  var = tf.Variable(t)
  is_var_init = tf.is_variable_initialized(var)
  return var, tf.cond(is_var_init, lambda: var.read_value(),
                      lambda: var._initializer_op.outputs[0])

Please let me know if this works for you.

@prb12 prb12 added the stat:awaiting response Status - Awaiting response from author label Mar 7, 2017
@asimshankar
Copy link
Contributor

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting response Status - Awaiting response from author
Projects
None yet
Development

No branches or pull requests

4 participants
0