Skip to content
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

Fix issue of tf.math.real with numeric input #30049

Merged
merged 3 commits into from Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions tensorflow/python/kernel_tests/cwise_ops_test.py
Expand Up @@ -1005,6 +1005,16 @@ def testMake(self):
self._compareMake(real, 12.0, use_gpu)
self._compareMake(23.0, imag, use_gpu)

def testRealImagNumericType(self):
for use_gpu in [True, False]:
for value in [1., 1j, 1.+1j]:
np_real, np_imag = np.real(value), np.imag(value)
with test_util.device(use_gpu=use_gpu):
tf_real = math_ops.real(value)
tf_imag = math_ops.imag(value)
self.assertAllEqual(np_real, self.evaluate(tf_real))
self.assertAllEqual(np_imag, self.evaluate(tf_imag))

def _compareRealImag(self, cplx, use_gpu):
np_real, np_imag = np.real(cplx), np.imag(cplx)
np_zeros = np_real * 0
Expand Down
2 changes: 2 additions & 0 deletions tensorflow/python/ops/math_ops.py
Expand Up @@ -522,6 +522,7 @@ def real(input, name=None):
A `Tensor` of type `float32` or `float64`.
"""
with ops.name_scope(name, "Real", [input]) as name:
input = ops.convert_to_tensor(input, name="input")
if input.dtype.is_complex:
real_dtype = input.dtype.real_dtype
return gen_math_ops.real(input, Tout=real_dtype, name=name)
Expand Down Expand Up @@ -555,6 +556,7 @@ def imag(input, name=None):
A `Tensor` of type `float32` or `float64`.
"""
with ops.name_scope(name, "Imag", [input]) as name:
input = ops.convert_to_tensor(input, name="input")
if input.dtype.is_complex:
return gen_math_ops.imag(input, Tout=input.dtype.real_dtype, name=name)
else:
Expand Down