Add a hopefully more helpful error msg when some boolean mirrored variable is used in TF 1.x control flows.

PiperOrigin-RevId: 302961869
Change-Id: I3c14ba215c0d564954129878ee40b3065f002d49
This commit is contained in:
Chenkai Kuang 2020-03-25 13:51:44 -07:00 committed by TensorFlower Gardener
parent 1fe1c9a9af
commit 5943793102

View File

@ -893,7 +893,13 @@ class MirroredVariable(DistributedVariable, Mirrored):
"""Converts a variable to a tensor."""
# Try to avoid assignments to and other mutations of MirroredVariable
# state except through a DistributionStrategy.extended.update() call.
assert not as_ref
if as_ref:
# A TF 1.x case where the variable is a boolean variable and used like:
# tf.cond(v, true_fn, false_fn).
raise ValueError(
"You may be using variable created under distribute strategy in TF "
"1.x control flows. Try explicitly converting the variable to Tensor "
"using variable.read_value(), or switch to TF 2.x.")
return ops.convert_to_tensor(
self._get(), dtype=dtype, name=name, as_ref=as_ref)