예제 #1
0
 def test_interpolation_two_intervals(self):
     dtype = tf.float64
     interval_times = tf.constant([0.25, 0.5], dtype=dtype)
     interval_values = tf.constant([0.05, 0.051], dtype=dtype)
     test_times = tf.constant([0.25, 0.5], dtype=dtype)
     actual, integrated_actual = self.evaluate(
         monotone_convex.interpolate(test_times, interval_values,
                                     interval_times))
     expected = [0.0505, 0.05125]
     np.testing.assert_allclose(actual, expected)
     integrated_expected = [0.0, 0.01275]
     np.testing.assert_allclose(integrated_actual, integrated_expected)
 def test_flat_values(self):
   dtype = np.float64
   interval_times = np.array([0.3, 1.0, 1.43, 3.7, 9.2, 12.48], dtype=dtype)
   interval_values = np.array([8.0] * 6, dtype=dtype)
   test_times = tf.constant(
       [0.1, 1.1, 1.22, 0.45, 1.8, 3.8, 7.45, 7.73, 9.6, 11.7, 12.],
       dtype=dtype)
   actual, _ = self.evaluate(
       monotone_convex.interpolate(test_times, interval_values,
                                   interval_times))
   expected = np.zeros([11], dtype=dtype) + 8.
   np.testing.assert_allclose(actual, expected)
예제 #3
0
 def test_interpolation(self):
     dtype = tf.float64
     interval_times = tf.constant([0.25, 0.5, 1.0, 2.0, 3.0], dtype=dtype)
     interval_values = tf.constant([0.05, 0.051, 0.052, 0.053, 0.055],
                                   dtype=dtype)
     test_times = tf.constant([0.25, 0.5, 1.0, 2.0, 3.0, 1.1], dtype=dtype)
     expected = [0.0505, 0.05133333, 0.05233333, 0.054, 0.0555, 0.05241]
     actual, integrated_actual = self.evaluate(
         monotone_convex.interpolate(test_times, interval_values,
                                     interval_times))
     np.testing.assert_allclose(actual, expected)
     integrated_expected = [0, 0, 0, 0, 0.055, 0.005237]
     np.testing.assert_allclose(integrated_actual, integrated_expected)
예제 #4
0
 def test_integrated_value(self):
     dtype = np.float64
     interval_times = tf.constant([0.25, 0.5, 1.0, 2.0, 3.0], dtype=dtype)
     interval_values = tf.constant([0.05, 0.051, 0.052, 0.053, 0.055],
                                   dtype=dtype)
     # Checks that the integral is correct by computing it using a Riemann sum.
     test_times = tf.constant(np.linspace(1.0, 1.1, num=51), dtype=dtype)
     dt = np.array(0.002, dtype=dtype)
     values, integrated = self.evaluate(
         monotone_convex.interpolate(test_times, interval_values,
                                     interval_times))
     actual = integrated[-1]
     expected = np.sum(dt * (values[1:] + values[:-1]) / 2)
     self.assertAlmostEqual(actual, expected)
예제 #5
0
 def test_interpolation_differentiable(self):
     dtype = tf.float64
     interval_times = tf.constant([0.25, 0.5, 1.0, 2.0, 3.0], dtype=dtype)
     knot_1y = tf.constant([0.052], dtype=dtype)
     interval_values = tf.concat([
         tf.constant([0.05, 0.051], dtype=dtype), knot_1y,
         tf.constant([0.053, 0.055], dtype=dtype)
     ],
                                 axis=0)
     test_time = tf.constant([1.1, 2.7], dtype=dtype)
     interpolated, _ = monotone_convex.interpolate(test_time,
                                                   interval_values,
                                                   interval_times)
     gradient_1y = self.evaluate(tf.gradients(interpolated[0], knot_1y)[0])
     gradient_zero = self.evaluate(
         tf.gradients(interpolated[1], knot_1y)[0])
     self.assertAlmostEqual(gradient_1y[0], 0.42)
     self.assertAlmostEqual(gradient_zero[0], 0.0)