Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tensorflow/python/kernel_tests/cwise_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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):
Expand All @@ -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)

Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion tensorflow/python/ops/math_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down