Explain the reason to call _truediv_python3/_div_python2 explicitly (not through registered '/'
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
diff --git a/tensorflow/python/ops/math_ops.py b/tensorflow/python/ops/math_ops.py
index b981af7..2c14148 100644
--- a/tensorflow/python/ops/math_ops.py
+++ b/tensorflow/python/ops/math_ops.py
@@ -439,13 +439,30 @@
# override names. Use a dummy class to track the runtime division behavior
return DivideDelegateWithName(x, name) / y
else:
- if not (isinstance(x, ops.Tensor) or isinstance(y, ops.Tensor)):
+ # tf.math.divide will compute python style division x / y. As python 2
+ # and python 3 have very much different semantics on `/` (__div__ vs.
+ # __truediv__), it would be natural to just use `x / y` as the operator
+ # '/' has already been registered for tensors, see
+ # _OverrideBinaryOperatorHelper for more details.
+ # However, in case both x and y are not tensors, the registered '/'
+ # _OverrideBinaryOperatorHelper will not take effect. In this case,
+ # python's default '/' operator will take effect which result in the return
+ # value of `tf.math.divide` as a non-Tensor.
+ # For that reason we excplicitly calls _truediv_python3/_div_python2
+ # in case both x and y are not tensors.
+ # Since _truediv_python3/_div_python2 operates on tensors and will convert
+ # to tensor if needed. This avoid the situation of the following if not
+ # explicitly calling _truediv_python3/_div_python2:
+ # >>> tf.divide(5, 2)
+ # 2.5 <= should be <tf.Tensor: shape=(), dtype=float64, numpy=2.5> instead.
+ if not (isinstance(x, ops.Tensor) or isinstance(y, ops.Tensor)):
if sys.version_info.major < 3:
- return _truediv_python2(x, y)
+ return _div_python2(x, y)
else:
return _truediv_python3(x, y)
return x / y
+
@tf_export("math.multiply", "multiply")
@dispatch.add_dispatch_support
def multiply(x, y, name=None):
diff --git a/tensorflow/python/ops/math_ops_test.py b/tensorflow/python/ops/math_ops_test.py
index dab0ea8..1debed5 100644
--- a/tensorflow/python/ops/math_ops_test.py
+++ b/tensorflow/python/ops/math_ops_test.py
@@ -498,10 +498,9 @@
def testWithPythonValue(self):
# Test case for GitHub issue 39475:
# https://github.com/tensorflow/tensorflow/issues/39475
- x = math_ops.divide(5, 2)
+ x = math_ops.divide(5, 2)
self.assertTrue(isinstance(x, ops.Tensor))
-
@test_util.run_all_in_graph_and_eager_modes
class DivNoNanTest(test_util.TensorFlowTestCase):