Пример #1
0
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()
Пример #2
0
def test_derivative():
    B.backend_to_tf()
    s = B.Session()

    m = TensorProductMean(lambda x: x**2)
    m2 = TensorProductMean(lambda x: x**3)
    x = B.array(np.random.randn(10, 1))

    yield assert_allclose, s.run(m.diff(0)(x)), s.run(2 * x)
    yield assert_allclose, s.run(m2.diff(0)(x)), s.run(3 * x**2)

    s.close()
    B.backend_to_np()
Пример #3
0
def test_derivative():
    # First, check properties.
    k = EQ().diff(0)

    yield eq, k.stationary, False
    yield raises, RuntimeError, lambda: k.length_scale
    yield raises, RuntimeError, lambda: k.var
    yield raises, RuntimeError, lambda: k.period

    # Test equality.
    yield eq, EQ().diff(0), EQ().diff(0)
    yield neq, EQ().diff(0), EQ().diff(1)
    yield neq, Matern12().diff(0), EQ().diff(0)

    yield raises, RuntimeError, lambda: EQ().diff(None, None)(1)

    # Third, check computation.
    B.backend_to_tf()
    s = B.Session()

    # Test derivative of kernel EQ.
    k = EQ()
    x1 = B.array(np.random.randn(10, 1))
    x2 = B.array(np.random.randn(5, 1))

    # Test derivative with respect to first input.
    ref = s.run(-dense(k(x1, x2)) * (x1 - B.transpose(x2)))
    yield assert_allclose, s.run(dense(k.diff(0, None)(x1, x2))), ref
    ref = s.run(-dense(k(x1)) * (x1 - B.transpose(x1)))
    yield assert_allclose, s.run(dense(k.diff(0, None)(x1))), ref

    # Test derivative with respect to second input.
    ref = s.run(-dense(k(x1, x2)) * (B.transpose(x2) - x1))
    yield assert_allclose, s.run(dense(k.diff(None, 0)(x1, x2))), ref
    ref = s.run(-dense(k(x1)) * (B.transpose(x1) - x1))
    yield assert_allclose, s.run(dense(k.diff(None, 0)(x1))), ref

    # Test derivative with respect to both inputs.
    ref = s.run(dense(k(x1, x2)) * (1 - (x1 - B.transpose(x2))**2))
    yield assert_allclose, s.run(dense(k.diff(0, 0)(x1, x2))), ref
    yield assert_allclose, s.run(dense(k.diff(0)(x1, x2))), ref
    ref = s.run(dense(k(x1)) * (1 - (x1 - B.transpose(x1))**2))
    yield assert_allclose, s.run(dense(k.diff(0, 0)(x1))), ref
    yield assert_allclose, s.run(dense(k.diff(0)(x1))), ref

    # Test derivative of kernel Linear.
    k = Linear()
    x1 = B.array(np.random.randn(10, 1))
    x2 = B.array(np.random.randn(5, 1))

    # Test derivative with respect to first input.
    ref = s.run(B.ones((10, 5), dtype=np.float64) * B.transpose(x2))
    yield assert_allclose, s.run(dense(k.diff(0, None)(x1, x2))), ref
    ref = s.run(B.ones((10, 10), dtype=np.float64) * B.transpose(x1))
    yield assert_allclose, s.run(dense(k.diff(0, None)(x1))), ref

    # Test derivative with respect to second input.
    ref = s.run(B.ones((10, 5), dtype=np.float64) * x1)
    yield assert_allclose, s.run(dense(k.diff(None, 0)(x1, x2))), ref
    ref = s.run(B.ones((10, 10), dtype=np.float64) * x1)
    yield assert_allclose, s.run(dense(k.diff(None, 0)(x1))), ref

    # Test derivative with respect to both inputs.
    ref = s.run(B.ones((10, 5), dtype=np.float64))
    yield assert_allclose, s.run(dense(k.diff(0, 0)(x1, x2))), ref
    yield assert_allclose, s.run(dense(k.diff(0)(x1, x2))), ref
    ref = s.run(B.ones((10, 10), dtype=np.float64))
    yield assert_allclose, s.run(dense(k.diff(0, 0)(x1))), ref
    yield assert_allclose, s.run(dense(k.diff(0)(x1))), ref

    s.close()
    B.backend_to_np()