def test_versus_numpy_multidim_x(self): with self.test_session() as sess: x = constant_op.constant([[0., -1.], [0.5, -2.]]) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose(np.arcsinh(x_), y_) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_negative_values_64_bit(self): with self.test_session() as sess: x = constant_op.constant(-np.logspace(0, 200, num=1000)) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose(np.arcsinh(x_), y_, rtol=1e-5) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_at_zero(self): # Zero is especially difficult. with self.test_session() as sess: x = constant_op.constant(0.) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose(np.arcsinh(x_), y_) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_negative_values_64_bit(self): with self.test_session() as sess: x = constant_op.constant(-np.logspace(0, 200, num=1000)) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose( np.arcsinh(x_), y_, rtol=1e-5) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_positive_values_32_bit(self): with self.test_session() as sess: # Larger than 38 is Inf in float32. x = constant_op.constant(np.logspace(0, 38, num=2000).astype(np.float32)) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose( np.arcsinh(x_), # numpy does this in 64bit y_, rtol=1e-6) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_moderate_negative_values_32_bit(self): # The moderate negative values were the most difficult to get close to # numpy. with self.test_session() as sess: x = constant_op.constant(-np.logspace(0, 10, num=1000).astype(np.float32)) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose( np.arcsinh(x_), # numpy does this in 64bit y_, rtol=1e-4) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_positive_values_32_bit(self): with self.test_session() as sess: # Larger than 38 is Inf in float32. x = constant_op.constant( np.logspace(0, 38, num=2000).astype(np.float32)) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose( np.arcsinh(x_), # numpy does this in 64bit y_, rtol=1e-6) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_extreme_negative_values_32_bit(self): # For these extreme values arcsinh uses the approximation 1 / (2 * x), and # 1 / 10^38 = 0 in 32bit...so stop at 10^37. with self.test_session() as sess: x = constant_op.constant( -np.logspace(10, 37, num=1000).astype(np.float32)) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose( np.arcsinh(x_), # numpy does this in 64bit y_, rtol=1e-6) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_moderate_negative_values_32_bit(self): # The moderate negative values were the most difficult to get close to # numpy. with self.test_session() as sess: x = constant_op.constant( -np.logspace(0, 10, num=1000).astype(np.float32)) y = trig.arcsinh(x) grad = gradients_impl.gradients(y, x)[0] x_, y_, grad_ = sess.run([x, y, grad]) self.assertAllClose( np.arcsinh(x_), # numpy does this in 64bit y_, rtol=1e-4) self._assert_all_finite(y_) self._assert_all_finite(grad_)
def test_versus_numpy_scalar_positive_x(self): x = 1.23 with self.test_session(): self.assertAllClose(np.arcsinh(x), trig.arcsinh(x).eval())