Skip to content

Commit 24b302d

Browse files
Add gradients for TanhGrad and SigmoidGrad.
Change: 132126486
1 parent fadc1f3 commit 24b302d

2 files changed

Lines changed: 42 additions & 18 deletions

File tree

‎tensorflow/python/kernel_tests/cwise_ops_test.py‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -422,27 +422,35 @@ def complex_sign(x):
422422
self._compareCpu(y, complex_sign, tf.sign)
423423
self._compareBothSparse(y, complex_sign, tf.sign)
424424

425-
def testSqrtGradGrad(self):
425+
def testGradGrad(self):
426426
np.random.seed(7)
427427
shape = (5,)
428-
dtype_tols = [(np.float32, 1e-4), (np.float64, 1e-6), (np.complex64, 1e-4),
428+
dtype_tols = [(np.float32, 5e-4), (np.float64, 1e-6), (np.complex64, 1e-3),
429429
(np.complex128, 1e-6)]
430+
op_ranges = [(gen_math_ops._sigmoid_grad, [-1.5, 1.5], [-2, 2]),
431+
(gen_math_ops._sqrt_grad, [1, 3], [-2, -2]),
432+
(gen_math_ops._tanh_grad, [-1.5, 1.5], [-2, 2]),]
430433

431434
def rand(dtype):
432-
x = np.random.uniform(1, 3, size=5).astype(dtype)
435+
x = np.random.uniform(
436+
real_range[0], real_range[1], size=shape[0]).astype(dtype)
433437
if dtype in (np.complex64, np.complex128):
434-
x += 1j * np.random.uniform(-2, 2, size=5).astype(dtype)
438+
x += 1j * np.random.uniform(
439+
imag_range[0], imag_range[1], size=shape[0]).astype(dtype)
435440
return x
436441

437-
with self.test_session():
438-
for dtype, tol in dtype_tols:
439-
x = tf.constant(rand(dtype))
440-
y = tf.constant(rand(dtype))
441-
z = gen_math_ops._sqrt_grad(x, y)
442-
error = tf.test.compute_gradient_error(
443-
[x, y], [shape, shape], z, shape,
444-
x_init_value=[rand(dtype), rand(dtype)])
445-
self.assertLess(error, tol)
442+
for op, real_range, imag_range in op_ranges:
443+
with self.test_session():
444+
for dtype, tol in dtype_tols:
445+
x = tf.constant(rand(dtype))
446+
y = tf.constant(rand(dtype))
447+
z = op(x, y)
448+
error = tf.test.compute_gradient_error(
449+
[x, y], [shape, shape],
450+
z,
451+
shape,
452+
x_init_value=[rand(dtype), rand(dtype)])
453+
self.assertLess(error, tol)
446454

447455

448456
class BinaryOpTest(tf.test.TestCase):

‎tensorflow/python/ops/math_grad.py‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
from tensorflow.python.ops import math_ops
3131

3232

33-
# Gradient ops that do not have gradients themselves.
34-
ops.NoGradient("SigmoidGrad")
35-
ops.NoGradient("TanhGrad")
36-
37-
3833
def _safe_shape_div(x, y):
3934
"""Divides `x / y` assuming `x, y >= 0`, treating `0 / 0 = 0`."""
4035
return x // math_ops.maximum(y, 1)
@@ -308,9 +303,19 @@ def _TanhGrad(op, grad):
308303
y = op.outputs[0] # y = tanh(x)
309304
with ops.control_dependencies([grad.op]):
310305
y = math_ops.conj(y)
306+
# pylint: disable=protected-access
311307
return gen_math_ops._tanh_grad(y, grad)
312308

313309

310+
@ops.RegisterGradient("TanhGrad")
311+
def _TanhGradGrad(op, grad):
312+
with ops.control_dependencies([grad.op]):
313+
a = math_ops.conj(op.inputs[0])
314+
b = math_ops.conj(op.inputs[1])
315+
# pylint: disable=protected-access
316+
return grad * -2 * b * a, gen_math_ops._tanh_grad(a, grad)
317+
318+
314319
@ops.RegisterGradient("Erf")
315320
def _ErfGrad(op, grad):
316321
"""Returns grad * 2/sqrt(pi) * exp(-x**2)."""
@@ -417,9 +422,20 @@ def _SigmoidGrad(op, grad):
417422
y = op.outputs[0] # y = sigmoid(x)
418423
with ops.control_dependencies([grad.op]):
419424
y = math_ops.conj(y)
425+
# pylint: disable=protected-access
420426
return gen_math_ops._sigmoid_grad(y, grad)
421427

422428

429+
@ops.RegisterGradient("SigmoidGrad")
430+
def _SigmoidGradGrad(op, grad):
431+
with ops.control_dependencies([grad.op]):
432+
a = math_ops.conj(op.inputs[0])
433+
b = math_ops.conj(op.inputs[1])
434+
gb = grad * b
435+
# pylint: disable=protected-access
436+
return gb - 2 * gb * a, gen_math_ops._sigmoid_grad(a, grad)
437+
438+
423439
@ops.RegisterGradient("Sign")
424440
def _SignGrad(op, _):
425441
"""Returns 0."""

0 commit comments

Comments
 (0)