diff --git a/tensorflow/python/kernel_tests/cwise_ops_test.py b/tensorflow/python/kernel_tests/cwise_ops_test.py index e5406ff87adadd..08054cba418e04 100644 --- a/tensorflow/python/kernel_tests/cwise_ops_test.py +++ b/tensorflow/python/kernel_tests/cwise_ops_test.py @@ -166,6 +166,13 @@ def _rsqrt(self, x): def _sigmoid(self, x): return 1.0 / (1.0 + np.exp(-x)) + def _sigmoid_grad(self , x): + sigm = _self.sigmoid(x) + return sigm * ( 1- sigm) + + def _tanh_grad(self, x): + return (1 - np.square(np.tanh(x))) + def _replace_domain_error_with_inf(self, fn): def func(x): try: @@ -194,7 +201,9 @@ def testFloatBasic(self): self._compareBoth(x, np.exp, tf.exp) self._compareBoth(z, np.log, tf.log) self._compareBoth(x, np.tanh, tf.tanh) + self._compareBoth(x, self._tanh_grad, tf.tanh_grad) self._compareBoth(x, self._sigmoid, tf.sigmoid) + self._compareBoth(x, self._sigmoid_grad, tf.sigmoid_grad) self._compareBoth(y, np.sign, tf.sign) self._compareBoth(x, np.sin, tf.sin) self._compareBoth(x, np.cos, tf.cos) @@ -214,6 +223,7 @@ def testFloatBasic(self): self._compareBothSparse(x, np.square, tf.square) self._compareBothSparse(z, np.sqrt, tf.sqrt, tol=1e-3) self._compareBothSparse(x, np.tanh, tf.tanh) + self._compareBothSparse(x, self._tanh_grad, tf.tanh_grad) self._compareBothSparse(y, np.sign, tf.sign) self._compareBothSparse(x, np.vectorize(math.erf), tf.erf) @@ -236,7 +246,9 @@ def testFloatEmpty(self): self._compareBoth(x, np.exp, tf.exp) self._compareBoth(x, np.log, tf.log) self._compareBoth(x, np.tanh, tf.tanh) + self._compareBoth(x, self._tanh_grad, tf.tanh_grad) self._compareBoth(x, self._sigmoid, tf.sigmoid) + self._compareBoth(x, self._sigmoid_grad, tf.sigmoid_grad) self._compareBoth(x, np.sign, tf.sign) self._compareBoth(x, np.sin, tf.sin) self._compareBoth(x, np.cos, tf.cos) @@ -254,6 +266,7 @@ def testFloatEmpty(self): self._compareBothSparse(x, np.square, tf.square) self._compareBothSparse(x, np.sqrt, tf.sqrt, tol=1e-3) self._compareBothSparse(x, np.tanh, tf.tanh) + self._compareBothSparse(x, self._tanh_grad, tf.tanh_grad) self._compareBothSparse(x, np.sign, tf.sign) self._compareBothSparse(x, np.sign, tf.erf) @@ -273,7 +286,9 @@ def testDoubleBasic(self): self._compareBoth(x, np.exp, tf.exp) self._compareBoth(z, np.log, tf.log) self._compareBoth(x, np.tanh, tf.tanh) + self._compareBoth(x, self._tanh_grad, tf.tanh_grad) self._compareBoth(x, self._sigmoid, tf.sigmoid) + self._compareBoth(x, self._sigmoid_grad, tf.sigmoid_grad) self._compareBoth(y, np.sign, tf.sign) self._compareBoth(x, np.sin, tf.sin) self._compareBoth(x, np.cos, tf.cos) @@ -293,6 +308,7 @@ def testDoubleBasic(self): self._compareBothSparse(x, np.square, tf.square) self._compareBothSparse(z, np.sqrt, tf.sqrt, tol=1e-3) self._compareBothSparse(x, np.tanh, tf.tanh) + self._compareBothSparse(x, self._tanh_grad, tf.tanh_grad) self._compareBothSparse(y, np.sign, tf.sign) self._compareBothSparse(x, np.vectorize(math.erf), tf.erf) @@ -311,7 +327,9 @@ def testHalfBasic(self): self._compareBoth(x, np.exp, tf.exp) self._compareBoth(z, np.log, tf.log) self._compareBoth(x, np.tanh, tf.tanh) + self._compareBoth(x, self._tanh_grad, tf.tanh_grad) self._compareBoth(x, self._sigmoid, tf.sigmoid) + self._compareBoth(x, self._sigmoid_grad, tf.sigmoid_grad) self._compareBoth(y, np.sign, tf.sign) self._compareBoth(x, np.sin, tf.sin) self._compareBoth(x, np.cos, tf.cos) @@ -327,6 +345,7 @@ def testHalfBasic(self): self._compareBothSparse(x, np.square, tf.square) self._compareBothSparse(z, np.sqrt, tf.sqrt, tol=1e-3) self._compareBothSparse(x, np.tanh, tf.tanh) + self._compareBothSparse(x, self._tanh_grad, tf.tanh_grad) self._compareBothSparse(y, np.sign, tf.sign) self._compareBothSparse(x, np.vectorize(math.erf), tf.erf, tol=1e-3) @@ -374,7 +393,9 @@ def testComplex64Basic(self): self._compareCpu(x, np.exp, tf.exp) self._compareCpu(y, np.log, tf.log) self._compareCpu(x, np.tanh, tf.tanh) + self._compareCpu(x, self._tanh_grad, tf.tanh_grad) self._compareCpu(x, self._sigmoid, tf.sigmoid) + self._compareCpu(x, self._sigmoid_grad, tf.sigmoid_grad) self._compareCpu(x, np.sin, tf.sin) self._compareCpu(x, np.cos, tf.cos) @@ -383,6 +404,7 @@ def testComplex64Basic(self): self._compareBothSparse(x, np.square, tf.square) self._compareBothSparse(x, np.sqrt, tf.sqrt, 1e-3) self._compareBothSparse(x, np.tanh, tf.tanh) + self._compareBothSparse(x, self._tanh_grad, tf.tanh_grad) # Numpy uses an incorrect definition of sign; use the right one instead. def complex_sign(x): @@ -405,7 +427,9 @@ def testComplex128Basic(self): self._compareCpu(x, np.exp, tf.exp) self._compareCpu(y, np.log, tf.log) self._compareCpu(x, np.tanh, tf.tanh) + self._compareCpu(x, self._tanh_grad, tf.tanh_grad) self._compareCpu(x, self._sigmoid, tf.sigmoid) + self._compareCpu(x, self._sigmoid_grad, tf.sigmoid_grad) self._compareCpu(x, np.sin, tf.sin) self._compareCpu(x, np.cos, tf.cos) @@ -414,6 +438,7 @@ def testComplex128Basic(self): self._compareBothSparse(x, np.square, tf.square) self._compareBothSparse(x, np.sqrt, tf.sqrt, 1e-3) self._compareBothSparse(x, np.tanh, tf.tanh) + self._compareBothSparse(x, self._tanh_grad, tf.tanh_grad) # Numpy uses an incorrect definition of sign; use the right one instead. def complex_sign(x): diff --git a/tensorflow/python/ops/math_grad.py b/tensorflow/python/ops/math_grad.py index cebcb79a90e69d..30970d3c798446 100644 --- a/tensorflow/python/ops/math_grad.py +++ b/tensorflow/python/ops/math_grad.py @@ -312,7 +312,7 @@ def _TanhGradGrad(op, grad): if y.dtype.is_complex: y = math_ops.conj(y) t = math_ops.tanh(y) - return grad * (-2 * t * (1 - t * t)) + return grad * (-2 * t * (1 - tf.square(t))) @ops.RegisterGradient("Erf") @@ -419,6 +419,7 @@ def _SigmoidGrad(op, grad): @ops.RegisterGradient("SigmoidGrad") def _SigmoidGradGrad(op, grad): + """Returns grad * sigmoid(x) * (1 - sigmoid(x)) * (1 - 2 * sigmoid(x))""" y = op.outputs[0] with ops.control_dependencies([grad.op]): if y.dtype.is_complex: