def test_derivative(): # Test construction: p = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=Graph()) yield eq, str(p.diff(1)), 'GP(d(1) EQ(), d(1) <lambda>)' # Test case: B.backend_to_tf() s = B.Session() model = Graph() x = np.linspace(0, 1, 100)[:, None] y = 2 * x p = GP(EQ(), graph=model) dp = p.diff() # Test conditioning on function. yield le, abs_err(s.run(dp.condition(p(x), y)(x).mean - 2)), 1e-3 # Test conditioning on derivative. post = p.condition((B.cast(0., np.float64), B.cast(0., np.float64)), (dp(x), y)) yield le, abs_err(s.run(post(x).mean - x ** 2)), 1e-3 s.close() B.backend_to_np()
def test_derivative(): # Test construction: p = GP(EQ(), TensorProductMean(lambda x: x ** 2), graph=Graph()) assert str(p.diff(1)) == 'GP(d(1) EQ(), d(1) <lambda>)' # Test case: model = Graph() x = B.linspace(tf.float64, 0, 1, 100)[:, None] y = 2 * x p = GP(EQ(), graph=model) dp = p.diff() # Test conditioning on function. assert abs_err(dp.condition(p(x), y)(x).mean, 2) <= 1e-3 # Test conditioning on derivative. post = p.condition((B.cast(tf.float64, 0), B.cast(tf.float64, 0)), (dp(x), y)) assert abs_err(post(x).mean, x ** 2) <= 1e-3