diff options
author | 2018-04-10 15:48:15 -0700 | |
---|---|---|
committer | 2018-04-10 15:50:11 -0700 | |
commit | 21e1bd6fcd671f41858fca47306e07c76ada7e9a (patch) | |
tree | 3793ee010bca1c79dd766dbae44810a9ae8ec401 /tensorflow/python/ops/variable_scope.py | |
parent | 15b104a047c1ec8ec07045047d46a300ebc6b2e3 (diff) |
In `get_variable`, nest the choice to use `ResourceVariable` under an
`init_scope`.
This makes sure that, when executing eagerly, calls to `get_variable` in a
`defun`-compiled function retrieve `ResourceVariable`s instead of `Variables`.
PiperOrigin-RevId: 192360775
Diffstat (limited to 'tensorflow/python/ops/variable_scope.py')
-rw-r--r-- | tensorflow/python/ops/variable_scope.py | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/tensorflow/python/ops/variable_scope.py b/tensorflow/python/ops/variable_scope.py index e33085ba62..ba213ef884 100644 --- a/tensorflow/python/ops/variable_scope.py +++ b/tensorflow/python/ops/variable_scope.py @@ -307,6 +307,17 @@ class _VariableStore(object): raise ValueError( "Passed a custom_getter which is not callable: %s" % custom_getter) + with ops.init_scope(): + if context.executing_eagerly(): + # Variable creation and initialization takes place in `init_scope`s; + # as such, if an `init_scope` lifts us into the eager context, then we + # need to use `ResourceVariable`s. + use_resource = True + + # Note that it's fine to reuse eager variables whose initialization was + # lifted from a function-building graph into the eager context (that's why + # the following clause is not wrapped in an `init_scope`); lifted variables + # are tracked by the graph's `VariableStore`. if context.executing_eagerly(): if not self._store_eager_variables and reuse: raise RuntimeError( @@ -315,7 +326,6 @@ class _VariableStore(object): " EagerVariableStore for example usage.") if self._store_eager_variables: reuse = AUTO_REUSE - use_resource = True # If a *_ref type is passed in an error would be triggered further down the # stack. We prevent this using base_dtype to get a non-ref version of the |