示例#1
0
def test_derivative_eq():
    # Test derivative of kernel `EQ()`.
    k = EQ()
    x1 = B.randn(tf.float64, 10, 1)
    x2 = B.randn(tf.float64, 5, 1)

    # Test derivative with respect to first input.
    allclose(k.diff(0, None)(x1, x2), -k(x1, x2) * (x1 - B.transpose(x2)))
    allclose(k.diff(0, None)(x1), -k(x1) * (x1 - B.transpose(x1)))

    # Test derivative with respect to second input.
    allclose(k.diff(None, 0)(x1, x2), -k(x1, x2) * (B.transpose(x2) - x1))
    allclose(k.diff(None, 0)(x1), -k(x1) * (B.transpose(x1) - x1))

    # Test derivative with respect to both inputs.
    ref = k(x1, x2) * (1 - (x1 - B.transpose(x2)) ** 2)
    allclose(k.diff(0, 0)(x1, x2), ref)
    allclose(k.diff(0)(x1, x2), ref)
    ref = k(x1) * (1 - (x1 - B.transpose(x1)) ** 2)
    allclose(k.diff(0, 0)(x1), ref)
    allclose(k.diff(0)(x1), ref)
示例#2
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()