diff options
author | Justine Tunney <jart@google.com> | 2016-12-29 22:46:24 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2016-12-29 23:06:59 -0800 |
commit | e121667dc609de978a223c56ee906368d2c4ceef (patch) | |
tree | 7d4e1f1e1b4fd469487872c0cd34ddace5ac570c /tensorflow/contrib/integrate | |
parent | 7815fcba7767aa1eb3196c5861e174f8b3c43bab (diff) |
Remove so many more hourglass imports
Change: 143230429
Diffstat (limited to 'tensorflow/contrib/integrate')
-rw-r--r-- | tensorflow/contrib/integrate/BUILD | 6 | ||||
-rw-r--r-- | tensorflow/contrib/integrate/python/ops/odes_test.py | 137 |
2 files changed, 81 insertions, 62 deletions
diff --git a/tensorflow/contrib/integrate/BUILD b/tensorflow/contrib/integrate/BUILD index 9afb330920..68bf362244 100644 --- a/tensorflow/contrib/integrate/BUILD +++ b/tensorflow/contrib/integrate/BUILD @@ -30,7 +30,11 @@ py_test( srcs_version = "PY2AND3", deps = [ ":integrate_py", - "//tensorflow:tensorflow_py", + "//tensorflow/python:array_ops", + "//tensorflow/python:client_testlib", + "//tensorflow/python:errors", + "//tensorflow/python:framework_for_generated_wrappers", + "//tensorflow/python:math_ops", "//third_party/py/numpy", ], ) diff --git a/tensorflow/contrib/integrate/python/ops/odes_test.py b/tensorflow/contrib/integrate/python/ops/odes_test.py index 55d92fe9cf..009e1d1f77 100644 --- a/tensorflow/contrib/integrate/python/ops/odes_test.py +++ b/tensorflow/contrib/integrate/python/ops/odes_test.py @@ -12,25 +12,30 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== - """Tests for ODE solvers.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np -import tensorflow as tf from tensorflow.contrib.integrate.python.ops import odes +from tensorflow.python.framework import constant_op +from tensorflow.python.framework import dtypes +from tensorflow.python.framework import errors_impl +from tensorflow.python.framework import tensor_shape +from tensorflow.python.ops import array_ops +from tensorflow.python.ops import math_ops +from tensorflow.python.platform import test -class OdeIntTest(tf.test.TestCase): +class OdeIntTest(test.TestCase): def setUp(self): super(OdeIntTest, self).setUp() # simple defaults (solution is a sin-wave) - matrix = tf.constant([[0, 1], [-1, 0]], dtype=tf.float64) - self.func = lambda y, t: tf.matmul(matrix, y) + matrix = constant_op.constant([[0, 1], [-1, 0]], dtype=dtypes.float64) + self.func = lambda y, t: math_ops.matmul(matrix, y) self.y0 = np.array([[1.0], [0.0]]) def test_odeint_exp(self): @@ -38,11 +43,11 @@ class OdeIntTest(tf.test.TestCase): # dy / dt = y, y(0) = 1.0. # Its analytical solution is y = exp(t). func = lambda y, t: y - y0 = tf.constant(1.0, dtype=tf.float64) + y0 = constant_op.constant(1.0, dtype=dtypes.float64) t = np.linspace(0.0, 1.0, 11) - y_solved = tf.contrib.integrate.odeint(func, y0, t) + y_solved = odes.odeint(func, y0, t) self.assertIn('odeint', y_solved.name) - self.assertEqual(y_solved.get_shape(), tf.TensorShape([11])) + self.assertEqual(y_solved.get_shape(), tensor_shape.TensorShape([11])) with self.test_session() as sess: y_solved = sess.run(y_solved) y_true = np.exp(t) @@ -55,7 +60,7 @@ class OdeIntTest(tf.test.TestCase): k = 1j - 0.1 func = lambda y, t: k * y t = np.linspace(0.0, 1.0, 11) - y_solved = tf.contrib.integrate.odeint(func, 1.0 + 0.0j, t) + y_solved = odes.odeint(func, 1.0 + 0.0j, t) with self.test_session() as sess: y_solved = sess.run(y_solved) y_true = np.exp(k * t) @@ -67,7 +72,7 @@ class OdeIntTest(tf.test.TestCase): # Its analytical solution is y = 1.0 / (2.0 - t) + t. func = lambda t, y: (y - t)**2 + 1.0 t = np.linspace(0.0, 1.0, 11) - y_solved = tf.contrib.integrate.odeint(func, np.float64(0.5), t) + y_solved = odes.odeint(func, np.float64(0.5), t) with self.test_session() as sess: y_solved = sess.run(y_solved) y_true = 1.0 / (2.0 - t) + t @@ -82,13 +87,14 @@ class OdeIntTest(tf.test.TestCase): # Its analytical solution is # y1 = sin(4.0 * t) * exp(3.0 * t), # y2 = cos(4.0 * t) * exp(3.0 * t). - matrix = tf.constant([[3.0, 4.0], [-4.0, 3.0]], dtype=tf.float64) - func = lambda y, t: tf.matmul(matrix, y) + matrix = constant_op.constant( + [[3.0, 4.0], [-4.0, 3.0]], dtype=dtypes.float64) + func = lambda y, t: math_ops.matmul(matrix, y) - y0 = tf.constant([[0.0], [1.0]], dtype=tf.float64) + y0 = constant_op.constant([[0.0], [1.0]], dtype=dtypes.float64) t = np.linspace(0.0, 1.0, 11) - y_solved = tf.contrib.integrate.odeint(func, y0, t) + y_solved = odes.odeint(func, y0, t) with self.test_session() as sess: y_solved = sess.run(y_solved) @@ -99,12 +105,13 @@ class OdeIntTest(tf.test.TestCase): def test_odeint_higher_rank(self): func = lambda y, t: y - y0 = tf.constant(1.0, dtype=tf.float64) + y0 = constant_op.constant(1.0, dtype=dtypes.float64) t = np.linspace(0.0, 1.0, 11) for shape in [(), (1,), (1, 1)]: expected_shape = (len(t),) + shape - y_solved = tf.contrib.integrate.odeint(func, tf.reshape(y0, shape), t) - self.assertEqual(y_solved.get_shape(), tf.TensorShape(expected_shape)) + y_solved = odes.odeint(func, array_ops.reshape(y0, shape), t) + self.assertEqual(y_solved.get_shape(), + tensor_shape.TensorShape(expected_shape)) with self.test_session() as sess: y_solved = sess.run(y_solved) self.assertEquals(y_solved.shape, expected_shape) @@ -112,40 +119,43 @@ class OdeIntTest(tf.test.TestCase): def test_odeint_all_dtypes(self): func = lambda y, t: y t = np.linspace(0.0, 1.0, 11) - for y0_dtype in [tf.float32, tf.float64, tf.complex64, tf.complex128]: - for t_dtype in [tf.float32, tf.float64]: - y0 = tf.cast(1.0, y0_dtype) - y_solved = tf.contrib.integrate.odeint(func, y0, tf.cast(t, t_dtype)) + for y0_dtype in [ + dtypes.float32, dtypes.float64, dtypes.complex64, dtypes.complex128 + ]: + for t_dtype in [dtypes.float32, dtypes.float64]: + y0 = math_ops.cast(1.0, y0_dtype) + y_solved = odes.odeint(func, y0, math_ops.cast(t, t_dtype)) with self.test_session() as sess: y_solved = sess.run(y_solved) expected = np.asarray(np.exp(t)) self.assertAllClose(y_solved, expected, rtol=1e-5) - self.assertEqual(tf.as_dtype(y_solved.dtype), y0_dtype) + self.assertEqual(dtypes.as_dtype(y_solved.dtype), y0_dtype) def test_odeint_required_dtypes(self): with self.assertRaisesRegexp(TypeError, '`y0` must have a floating point'): - tf.contrib.integrate.odeint(self.func, tf.cast(self.y0, tf.int32), [0, 1]) + odes.odeint(self.func, math_ops.cast(self.y0, dtypes.int32), [0, 1]) with self.assertRaisesRegexp(TypeError, '`t` must have a floating point'): - tf.contrib.integrate.odeint(self.func, self.y0, tf.cast([0, 1], tf.int32)) + odes.odeint(self.func, self.y0, math_ops.cast([0, 1], dtypes.int32)) def test_odeint_runtime_errors(self): - with self.assertRaisesRegexp( - ValueError, 'cannot supply `options` without'): - tf.contrib.integrate.odeint(self.func, self.y0, [0, 1], - options={'first_step': 1.0}) - - y = tf.contrib.integrate.odeint(self.func, self.y0, [0, 1], method='dopri5', - options={'max_num_steps': 0}) + with self.assertRaisesRegexp(ValueError, 'cannot supply `options` without'): + odes.odeint(self.func, self.y0, [0, 1], options={'first_step': 1.0}) + + y = odes.odeint( + self.func, + self.y0, [0, 1], + method='dopri5', + options={'max_num_steps': 0}) with self.test_session() as sess: - with self.assertRaisesRegexp( - tf.errors.InvalidArgumentError, 'max_num_steps'): + with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, + 'max_num_steps'): sess.run(y) - y = tf.contrib.integrate.odeint(self.func, self.y0, [1, 0]) + y = odes.odeint(self.func, self.y0, [1, 0]) with self.test_session() as sess: - with self.assertRaisesRegexp( - tf.errors.InvalidArgumentError, 'monotonic increasing'): + with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, + 'monotonic increasing'): sess.run(y) def test_odeint_different_times(self): @@ -155,10 +165,10 @@ class OdeIntTest(tf.test.TestCase): with self.test_session() as sess: y_solved_0, info_0 = sess.run( - tf.contrib.integrate.odeint( + odes.odeint( self.func, self.y0, times0, full_output=True)) y_solved_1, info_1 = sess.run( - tf.contrib.integrate.odeint( + odes.odeint( self.func, self.y0, times1, full_output=True)) self.assertAllClose(y_solved_0, y_solved_1[::10]) @@ -168,53 +178,58 @@ class OdeIntTest(tf.test.TestCase): def test_odeint_5th_order_accuracy(self): t = [0, 20] - kwargs = dict(full_output=True, - method='dopri5', - options=dict(max_num_steps=2000)) + kwargs = dict( + full_output=True, method='dopri5', options=dict(max_num_steps=2000)) with self.test_session() as sess: - _, info_0 = sess.run(tf.contrib.integrate.odeint( - self.func, self.y0, t, rtol=0, atol=1e-6, **kwargs)) - _, info_1 = sess.run(tf.contrib.integrate.odeint( - self.func, self.y0, t, rtol=0, atol=1e-9, **kwargs)) - self.assertAllClose(info_0['integrate_points'].size * 1000 ** 0.2, - float(info_1['integrate_points'].size), - rtol=0.01) + _, info_0 = sess.run( + odes.odeint( + self.func, self.y0, t, rtol=0, atol=1e-6, **kwargs)) + _, info_1 = sess.run( + odes.odeint( + self.func, self.y0, t, rtol=0, atol=1e-9, **kwargs)) + self.assertAllClose( + info_0['integrate_points'].size * 1000**0.2, + float(info_1['integrate_points'].size), + rtol=0.01) -class StepSizeTest(tf.test.TestCase): +class StepSizeTest(test.TestCase): def test_error_ratio_one(self): - new_step = odes._optimal_step_size(last_step=tf.constant(1.0), - error_ratio=tf.constant(1.0)) + new_step = odes._optimal_step_size( + last_step=constant_op.constant(1.0), + error_ratio=constant_op.constant(1.0)) with self.test_session() as sess: new_step = sess.run(new_step) self.assertAllClose(new_step, 0.9) def test_ifactor(self): - new_step = odes._optimal_step_size(last_step=tf.constant(1.0), - error_ratio=tf.constant(0.0)) + new_step = odes._optimal_step_size( + last_step=constant_op.constant(1.0), + error_ratio=constant_op.constant(0.0)) with self.test_session() as sess: new_step = sess.run(new_step) self.assertAllClose(new_step, 10.0) def test_dfactor(self): - new_step = odes._optimal_step_size(last_step=tf.constant(1.0), - error_ratio=tf.constant(1e6)) + new_step = odes._optimal_step_size( + last_step=constant_op.constant(1.0), + error_ratio=constant_op.constant(1e6)) with self.test_session() as sess: new_step = sess.run(new_step) self.assertAllClose(new_step, 0.2) -class InterpolationTest(tf.test.TestCase): +class InterpolationTest(test.TestCase): def test_5th_order_polynomial(self): # this should be an exact fit - f = lambda x: x ** 4 + x ** 3 - 2 * x ** 2 + 4 * x + 5 - f_prime = lambda x: 4 * x ** 3 + 3 * x ** 2 - 4 * x + 4 + f = lambda x: x**4 + x**3 - 2 * x**2 + 4 * x + 5 + f_prime = lambda x: 4 * x**3 + 3 * x**2 - 4 * x + 4 coeffs = odes._interp_fit( f(0.0), f(10.0), f(5.0), f_prime(0.0), f_prime(10.0), 10.0) times = np.linspace(0, 10, dtype=np.float32) - y_fit = tf.stack( + y_fit = array_ops.stack( [odes._interp_evaluate(coeffs, 0.0, 10.0, t) for t in times]) y_expected = f(times) with self.test_session() as sess: @@ -224,9 +239,9 @@ class InterpolationTest(tf.test.TestCase): # attempt interpolation outside bounds y_invalid = odes._interp_evaluate(coeffs, 0.0, 10.0, 100.0) with self.test_session() as sess: - with self.assertRaises(tf.errors.InvalidArgumentError): + with self.assertRaises(errors_impl.InvalidArgumentError): sess.run(y_invalid) if __name__ == '__main__': - tf.test.main() + test.main() |