aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/ops/variables.py
diff options
context:
space:
mode:
authorGravatar Alexandre Passos <apassos@google.com>2018-07-24 09:35:59 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-07-24 09:43:15 -0700
commit00f9eb9c589ad3c5ce5b4dde84763553c56ad0ee (patch)
tree46009790820710b927ebd9d6828263078712a1ab /tensorflow/python/ops/variables.py
parent1b33df1814e35015953c7cba392ba2a7387ce875 (diff)
Improvement resource variable documentation.
Removes stale mentions of how to construct them from docstrings and adds a new section to the programmer's guide explaining resource handles and resource variable semantics. PiperOrigin-RevId: 205842012
Diffstat (limited to 'tensorflow/python/ops/variables.py')
-rw-r--r--tensorflow/python/ops/variables.py41
1 files changed, 16 insertions, 25 deletions
diff --git a/tensorflow/python/ops/variables.py b/tensorflow/python/ops/variables.py
index d03d93beeb..fc00ce68ae 100644
--- a/tensorflow/python/ops/variables.py
+++ b/tensorflow/python/ops/variables.py
@@ -220,27 +220,31 @@ class Variable(six.with_metaclass(VariableMetaclass,
various `Optimizer` classes use this collection as the default list of
variables to optimize.
- WARNING: tf.Variable objects have a non-intuitive memory model. A Variable is
- represented internally as a mutable Tensor which can non-deterministically
- alias other Tensors in a graph. The set of operations which consume a Variable
- and can lead to aliasing is undetermined and can change across TensorFlow
- versions. Avoid writing code which relies on the value of a Variable either
- changing or not changing as other operations happen. For example, using
- Variable objects or simple functions thereof as predicates in a `tf.cond` is
- dangerous and error-prone:
+ WARNING: tf.Variable objects by default have a non-intuitive memory model. A
+ Variable is represented internally as a mutable Tensor which can
+ non-deterministically alias other Tensors in a graph. The set of operations
+ which consume a Variable and can lead to aliasing is undetermined and can
+ change across TensorFlow versions. Avoid writing code which relies on the
+ value of a Variable either changing or not changing as other operations
+ happen. For example, using Variable objects or simple functions thereof as
+ predicates in a `tf.cond` is dangerous and error-prone:
```
v = tf.Variable(True)
tf.cond(v, lambda: v.assign(False), my_false_fn) # Note: this is broken.
```
- Here replacing tf.Variable with tf.contrib.eager.Variable will fix any
- nondeterminism issues.
+ Here replacing adding `use_resource=True` when constructing the variable will
+ fix any nondeterminism issues:
+ ```
+ v = tf.Variable(True, use_resource=True)
+ tf.cond(v, lambda: v.assign(False), my_false_fn)
+ ```
To use the replacement for variables which does
not have these issues:
- * Replace `tf.Variable` with `tf.contrib.eager.Variable`;
+ * Add `use_resource=True` when constructing `tf.Variable`;
* Call `tf.get_variable_scope().set_use_resource(True)` inside a
`tf.variable_scope` before the `tf.get_variable()` call.
"""
@@ -869,19 +873,7 @@ class RefVariable(Variable):
ValueError: If the initial value is not specified, or does not have a
shape and `validate_shape` is `True`.
RuntimeError: If eager execution is enabled.
-
- @compatibility(eager)
- `tf.Variable` is not compatible with eager execution. Use
- `tfe.Variable` instead which is compatible with both eager execution
- and graph construction. See [the TensorFlow Eager Execution
- guide](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/eager/python/g3doc/guide.md#variables-and-optimizers)
- for details on how variables work in eager execution.
- @end_compatibility
"""
- if context.executing_eagerly():
- raise RuntimeError(
- "tf.Variable not supported when eager execution is enabled. "
- "Please use tf.contrib.eager.Variable instead")
self._in_graph_mode = True
if variable_def:
# If variable_def is provided, recreates the variable from its fields.
@@ -992,8 +984,7 @@ class RefVariable(Variable):
# Ensure that we weren't lifted into the eager context.
if context.executing_eagerly():
raise RuntimeError(
- "tf.Variable not supported when eager execution is enabled. "
- "Please use tf.contrib.eager.Variable instead")
+ "RefVariable not supported when eager execution is enabled. ")
with ops.name_scope(name, "Variable", [] if init_from_fn else
[initial_value]) as name: