예제 #1
0
def test_cov_matern32():
    with pytest.raises(AssertionError) as error:
        covariance.cov_matern32(np.zeros((1, 2)), np.zeros((1, 2)), np.array([1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_matern32(np.zeros((1, 2)), np.zeros((1, 3)), np.array([1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_matern32(np.zeros((1, 3)), np.zeros((1, 2)), np.array([1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_matern32(np.zeros((1, 2)), np.zeros((1, 2)), np.array([1.0, 1.0]), 1)
    assert np.abs(covariance.cov_matern32(np.zeros((1, 2)), np.zeros((1, 2)), 1.0, 0.1)[0] - 0.01) < TEST_EPSILON

    X = np.array([[1.0, 2.0, 0.0]])
    Xs = np.array([[2.0, 1.0, 1.0]])
    cur_hyps = utils_covariance.get_hyps('matern32', 3)
    cov_ = covariance.cov_matern32(X, Xs, cur_hyps['lengthscales'], cur_hyps['signal'])
    print(cov_)
    truth_cov_ = 0.19914827347145583
    assert np.abs(cov_[0] - truth_cov_) < TEST_EPSILON

    X = np.array([[1.0, 2.0, 0.0]])
    Xs = np.array([[2.0, 1.0, 1.0], [0.0, 0.0, 0.0]])
    cur_hyps = utils_covariance.get_hyps('matern32', 3)
    cur_hyps['lengthscales'] = 1.0
    cov_ = covariance.cov_matern32(X, Xs, cur_hyps['lengthscales'], cur_hyps['signal'])
    print(cov_)
    truth_cov_ = np.array([[0.19914827, 0.1013397]])
    assert np.all(np.abs(cov_[0] - truth_cov_) < TEST_EPSILON)
예제 #2
0
def test_cov_matern52():
    with pytest.raises(AssertionError) as error:
        package_target.cov_matern52(np.zeros((1, 2)), np.zeros((1, 2)),
                                    np.array([1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        package_target.cov_matern52(np.zeros((1, 2)), np.zeros((1, 3)),
                                    np.array([1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        package_target.cov_matern52(np.zeros((1, 3)), np.zeros((1, 2)),
                                    np.array([1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        package_target.cov_matern52(np.zeros((1, 2)), np.zeros((1, 2)),
                                    np.array([1.0, 1.0]), 1)
    assert np.abs(
        package_target.cov_matern52(np.zeros((1, 2)), np.zeros(
            (1, 2)), 1.0, 0.1)[0] - 0.01) < TEST_EPSILON

    X = np.array([[1.0, 2.0, 0.0]])
    Xp = np.array([[2.0, 1.0, 1.0]])
    cur_hyps = utils_covariance.get_hyps('matern52', 3)
    cov_ = package_target.cov_matern52(X, Xp, cur_hyps['lengthscales'],
                                       cur_hyps['signal'])
    print(cov_)
    truth_cov_ = 0.20532087608359792
    assert np.abs(cov_[0] - truth_cov_) < TEST_EPSILON

    X = np.array([[1.0, 2.0, 0.0]])
    Xp = np.array([[2.0, 1.0, 1.0], [0.0, 0.0, 0.0]])
    cur_hyps = utils_covariance.get_hyps('matern52', 3)
    cur_hyps['lengthscales'] = 1.0
    cov_ = package_target.cov_matern52(X, Xp, cur_hyps['lengthscales'],
                                       cur_hyps['signal'])
    print(cov_)
    truth_cov_ = np.array([[0.20532088, 0.09657724]])
    assert np.all(np.abs(cov_[0] - truth_cov_) < TEST_EPSILON)
예제 #3
0
def test_cov_se():
    with pytest.raises(AssertionError) as error:
        package_target.cov_se(np.zeros((1, 2)), np.zeros((1, 2)),
                              np.array([1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        package_target.cov_se(np.zeros((1, 2)), np.zeros((1, 3)),
                              np.array([1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        package_target.cov_se(np.zeros((1, 3)), np.zeros((1, 2)),
                              np.array([1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        package_target.cov_se(np.zeros((1, 2)), np.zeros((1, 2)),
                              np.array([1.0, 1.0]), 1)
    assert np.abs(
        package_target.cov_se(np.zeros((1, 2)), np.zeros(
            (1, 2)), 1.0, 0.1)[0] - 0.01) < TEST_EPSILON

    X = np.array([[1.0, 2.0, 0.0]])
    Xp = np.array([[2.0, 1.0, 1.0]])
    cur_hyps = utils_covariance.get_hyps('se', 3)
    cov_ = package_target.cov_se(X, Xp, cur_hyps['lengthscales'],
                                 cur_hyps['signal'])
    print(cov_)
    truth_cov_ = 0.22313016014842987
    assert np.abs(cov_[0] - truth_cov_) < TEST_EPSILON

    X = np.array([[1.0, 2.0, 0.0]])
    Xp = np.array([[2.0, 1.0, 1.0], [0.0, 0.0, 0.0]])
    cur_hyps = utils_covariance.get_hyps('se', 3)
    cur_hyps['lengthscales'] = 1.0
    cov_ = package_target.cov_se(X, Xp, cur_hyps['lengthscales'],
                                 cur_hyps['signal'])
    print(cov_)
    truth_cov_ = np.array([[0.22313016, 0.082085]])
    assert np.all(np.abs(cov_[0] - truth_cov_) < TEST_EPSILON)
예제 #4
0
def test_cov_set():
    num_instances = 5
    num_dim = 3
    str_cov = 'matern52'
    with pytest.raises(AssertionError) as error:
        covariance.cov_set(1, np.zeros((num_instances, num_dim)), np.zeros((num_instances, num_dim)), np.array([1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_set('abc', np.zeros((num_instances, num_dim)), np.zeros((num_instances, num_dim)), np.array([1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_set(str_cov, np.zeros((num_instances, num_dim)), np.zeros((num_instances, num_dim)), np.array([1.0, 1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_set(str_cov, np.zeros((num_instances, num_dim+1)), np.zeros((num_instances, num_dim)), np.array([1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_set(str_cov, np.zeros((num_instances, num_dim)), np.zeros((num_instances, num_dim+1)), np.array([1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_set(str_cov, np.zeros((num_instances, num_dim)), np.zeros((num_instances, num_dim)), np.array([1.0, 1.0, 1.0]), 1)
    assert np.abs(covariance.cov_set(str_cov, np.zeros((num_instances, num_dim)), np.zeros((num_instances, num_dim)), 1.0, 0.1) - 0.01) < TEST_EPSILON

    bx = np.array([[1.0, 2.0, 0.0], [2.0, 1.0, 0.0]])
    bxp = np.array([[2.0, 1.0, 1.0], [2.0, 2.0, 2.0]])
    cur_hyps = utils_covariance.get_hyps('matern52', 3)
    cov_ = covariance.cov_set(str_cov, bx, bxp, cur_hyps['lengthscales'], cur_hyps['signal'])
    print(cov_)
    truth_cov_ = 0.23061736638896702
    assert np.abs(cov_ - truth_cov_) < TEST_EPSILON
예제 #5
0
def test_get_kernel_inverse():
    dim_X = 3
    X = np.reshape(np.arange(0, 9), (3, dim_X))
    hyps = utils_covariance.get_hyps('se', dim_X)

    with pytest.raises(AssertionError) as error:
        gp.get_kernel_inverse(1, hyps, 'se')
    with pytest.raises(AssertionError) as error:
        gp.get_kernel_inverse(np.arange(0, 100), hyps, 'se')
    with pytest.raises(AssertionError) as error:
        gp.get_kernel_inverse(X, 1, 'se')
    with pytest.raises(AssertionError) as error:
        gp.get_kernel_inverse(X, hyps, 1)
    with pytest.raises(ValueError) as error:
        gp.get_kernel_inverse(X, hyps, 'abc')
    with pytest.raises(AssertionError) as error:
        gp.get_kernel_inverse(X, hyps, 'se', debug=1)

    cov_X_X, inv_cov_X_X = gp.get_kernel_inverse(X, hyps, 'se')
    print(cov_X_X)
    print(inv_cov_X_X)
    truth_cov_X_X = [
        [1.00011000e+00, 1.37095909e-06, 3.53262857e-24],
        [1.37095909e-06, 1.00011000e+00, 1.37095909e-06],
        [3.53262857e-24, 1.37095909e-06, 1.00011000e+00]
    ]
    truth_inv_cov_X_X = [
        [9.99890012e-01, -1.37065753e-06, 1.87890871e-12],
        [-1.37065753e-06, 9.99890012e-01, -1.37065753e-06],
        [1.87890871e-12, -1.37065753e-06, 9.99890012e-01]
    ]
    assert (np.abs(cov_X_X - truth_cov_X_X) < TEST_EPSILON).all()
    assert (np.abs(inv_cov_X_X - truth_inv_cov_X_X) < TEST_EPSILON).all()
    assert cov_X_X.shape == inv_cov_X_X.shape
예제 #6
0
def test_get_kernel_cholesky():
    dim_X = 3
    X = np.reshape(np.arange(0, 9), (3, dim_X))
    hyps = utils_covariance.get_hyps('se', dim_X)

    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_cholesky(1, hyps, 'se')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_cholesky(np.arange(0, 10), hyps, 'se')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_cholesky(X, 1, 'se')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_cholesky(X, hyps, 1)
    with pytest.raises(ValueError) as error:
        gp_common.get_kernel_cholesky(X, hyps, 'abc')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_cholesky(X, hyps, 'se', debug=1)

    cov_X_X, lower, _ = gp_common.get_kernel_cholesky(X, hyps, 'se')
    print(cov_X_X)
    print(lower)
    truth_cov_X_X = [
        [1.00011000e+00, 1.37095909e-06, 3.53262857e-24],
        [1.37095909e-06, 1.00011000e+00, 1.37095909e-06],
        [3.53262857e-24, 1.37095909e-06, 1.00011000e+00],
    ]
    truth_lower = [
        [1.00005500e+00, 0.00000000e+00, 0.00000000e+00],
        [1.37088369e-06, 1.00005500e+00, 0.00000000e+00],
        [3.53243429e-24, 1.37088369e-06, 1.00005500e+00],
    ]
    assert (np.abs(cov_X_X - truth_cov_X_X) < TEST_EPSILON).all()
    assert (np.abs(lower - truth_lower) < TEST_EPSILON).all()
    assert cov_X_X.shape == lower.shape
예제 #7
0
def test_cov_matern52():
    with pytest.raises(AssertionError) as error:
        covariance.cov_matern52(np.zeros(2), np.zeros(2),
                                np.array([1.0, 1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_matern52(np.zeros(2), np.zeros(3), np.array([1.0, 1.0]),
                                0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_matern52(np.zeros(3), np.zeros(2), np.array([1.0, 1.0]),
                                0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_matern52(np.zeros(2), np.zeros(2), np.array([1.0, 1.0]),
                                1)
    assert np.abs(
        covariance.cov_matern52(np.zeros(2), np.zeros(2), 1.0, 0.1) -
        0.01) < TEST_EPSILON

    bx = np.array([1.0, 2.0, 0.0])
    bxp = np.array([2.0, 1.0, 1.0])
    cur_hyps = utils_covariance.get_hyps('matern52', 3)
    cov_ = covariance.cov_matern52(bx, bxp, cur_hyps['lengthscales'],
                                   cur_hyps['signal'])
    print(cov_)
    truth_cov_ = 0.20532087608359792
    assert np.abs(cov_ - truth_cov_) < TEST_EPSILON
예제 #8
0
def test_neg_log_pseudo_l_loocv():
    dim_X = 3
    str_cov = 'se'
    X = np.reshape(np.arange(0, 9), (3, dim_X))
    Y = np.expand_dims(np.arange(3, 10, 3), axis=1)
    dict_hyps = utils_covariance.get_hyps(str_cov, dim_X)
    arr_hyps = utils_covariance.convert_hyps(str_cov,
                                             dict_hyps,
                                             fix_noise=constants.FIX_GP_NOISE)
    prior_mu_X = np.zeros((3, 1))

    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(np.arange(0, 3), Y, arr_hyps,
                                              str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(X, np.arange(0, 3), arr_hyps,
                                              str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(X, Y, dict_hyps, str_cov,
                                              prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(X, Y, arr_hyps, 1, prior_mu_X)
    with pytest.raises(ValueError) as error:
        package_target.neg_log_pseudo_l_loocv(X, Y, arr_hyps, 'abc',
                                              prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(X, Y, arr_hyps, str_cov,
                                              np.arange(0, 3))
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(
            np.reshape(np.arange(0, 12), (4, dim_X)), Y, arr_hyps, str_cov,
            prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(
            X, np.expand_dims(np.arange(0, 4), axis=1), arr_hyps, str_cov,
            prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(
            X, Y, arr_hyps, str_cov, np.expand_dims(np.arange(0, 4), axis=1))
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(X,
                                              Y,
                                              arr_hyps,
                                              str_cov,
                                              prior_mu_X,
                                              fix_noise=1)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_pseudo_l_loocv(X,
                                              Y,
                                              arr_hyps,
                                              str_cov,
                                              prior_mu_X,
                                              debug=1)

    neg_log_pseudo_l_ = package_target.neg_log_pseudo_l_loocv(
        X, Y, arr_hyps, str_cov, prior_mu_X)
    print(neg_log_pseudo_l_)
    truth_log_pseudo_l_ = 21.916822991658695
    assert np.abs(neg_log_pseudo_l_ - truth_log_pseudo_l_) < TEST_EPSILON
예제 #9
0
def test_grad_cov_main():
    cur_hyps = utils_covariance.get_hyps('se', 2)

    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main(123, np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main('se', 123, np.zeros((10, 2)), cur_hyps, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main('se', np.zeros((10, 2)), 123, cur_hyps, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), 123, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, 'abc')
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main('abc', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, same_X_Xs='abc')
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, same_X_Xs=False)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, jitter='abc')

    grad_cov_ = covariance.grad_cov_main('se', np.ones((1, 2)), np.ones((1, 2)), cur_hyps, True)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[2.00002, 0.0, 0.0]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    grad_cov_ = covariance.grad_cov_main('se', np.ones((1, 2)), np.ones((1, 2)), cur_hyps, False)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.02, 2.00002, 0.0, 0.0]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    cur_hyps['lengthscales'] = 1.0
    grad_cov_ = covariance.grad_cov_main('se', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.02, 0.73577888, 0.73577888]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    grad_cov_ = covariance.grad_cov_main('matern32', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.02, 0.59566154, 0.51802578]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    grad_cov_ = covariance.grad_cov_main('matern32', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, True)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.59566154, 0.51802578]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    grad_cov_ = covariance.grad_cov_main('matern52', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.02, 0.63458673, 0.8305486]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)
예제 #10
0
def _cov_main_vector(str_cov, num_dim, num_X, num_Xs):
    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cov_ = covariance.cov_main(str_cov,
                               np.zeros((num_X, num_dim)),
                               np.zeros((num_Xs, num_dim)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    return cov_
예제 #11
0
def test_get_kernel_inverse():
    dim_X = 3
    X = np.reshape(np.arange(0, 9), (3, dim_X))
    hyps = utils_covariance.get_hyps('se', dim_X)

    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_inverse(1, hyps, 'se')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_inverse(np.arange(0, 100), hyps, 'se')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_inverse(X, 1, 'se')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_inverse(X, hyps, 1)
    with pytest.raises(ValueError) as error:
        gp_common.get_kernel_inverse(X, hyps, 'abc')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_inverse(X, hyps, 'se', debug=1)
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_inverse(X, hyps, 'se', is_gradient='abc')
    with pytest.raises(AssertionError) as error:
        gp_common.get_kernel_inverse(X, hyps, 'se', is_fixed_noise='abc')

    cov_X_X, inv_cov_X_X, grad_cov_X_X = gp_common.get_kernel_inverse(
        X, hyps, 'se')
    print(cov_X_X)
    print(inv_cov_X_X)
    truth_cov_X_X = np.array([[1.00011000e+00, 1.37095909e-06, 3.53262857e-24],
                              [1.37095909e-06, 1.00011000e+00, 1.37095909e-06],
                              [3.53262857e-24, 1.37095909e-06,
                               1.00011000e+00]])
    truth_inv_cov_X_X = np.array(
        [[9.99890012e-01, -1.37065753e-06, 1.87890871e-12],
         [-1.37065753e-06, 9.99890012e-01, -1.37065753e-06],
         [1.87890871e-12, -1.37065753e-06, 9.99890012e-01]])
    assert (np.abs(cov_X_X - truth_cov_X_X) < TEST_EPSILON).all()
    assert (np.abs(inv_cov_X_X - truth_inv_cov_X_X) < TEST_EPSILON).all()
    assert cov_X_X.shape == inv_cov_X_X.shape

    cov_X_X, inv_cov_X_X, grad_cov_X_X = gp_common.get_kernel_inverse(
        X, hyps, 'se', is_gradient=True, is_fixed_noise=True)
    print(grad_cov_X_X)
    print(grad_cov_X_X.shape)

    truth_grad_cov_X_X = np.array(
        [[[2.00002000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
          [2.74191817e-06, 3.70158953e-05, 3.70158953e-05, 3.70158953e-05],
          [7.06525714e-24, 3.81523886e-22, 3.81523886e-22, 3.81523886e-22]],
         [[2.74191817e-06, 3.70158953e-05, 3.70158953e-05, 3.70158953e-05],
          [2.00002000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
          [2.74191817e-06, 3.70158953e-05, 3.70158953e-05, 3.70158953e-05]],
         [[7.06525714e-24, 3.81523886e-22, 3.81523886e-22, 3.81523886e-22],
          [2.74191817e-06, 3.70158953e-05, 3.70158953e-05, 3.70158953e-05],
          [2.00002000e+00, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]])
    assert (np.abs(cov_X_X - truth_cov_X_X) < TEST_EPSILON).all()
    assert (np.abs(inv_cov_X_X - truth_inv_cov_X_X) < TEST_EPSILON).all()
    assert (np.abs(grad_cov_X_X - truth_grad_cov_X_X) < TEST_EPSILON).all()
    assert cov_X_X.shape == inv_cov_X_X.shape == grad_cov_X_X.shape[:2]
예제 #12
0
def test_grad_cov_matern52():
    str_cov = 'matern52'
    cur_hyps = utils_covariance.get_hyps(str_cov, 2)
    X_train = np.array([
        [2.0, 1.0],
        [1.0, 1.0],
    ])
    num_hyps = X_train.shape[1] + 1
    cov_ = covariance.cov_main(str_cov, X_train, X_train, cur_hyps, True)
    print(cov_)

    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_matern52('abc', X_train, X_train, cur_hyps, num_hyps, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_matern52(cov_, 'abc', X_train, cur_hyps, num_hyps, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_matern52(cov_, X_train, 'abc', cur_hyps, num_hyps, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_matern52(cov_, X_train, X_train, 'abc', num_hyps, True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_matern52(cov_, X_train, X_train, cur_hyps, 'abc', True)
    with pytest.raises(AssertionError) as error:
        covariance.grad_cov_matern52(cov_, X_train, X_train, cur_hyps, num_hyps, 'abc')

    num_hyps = X_train.shape[1] + 2
    grad_cov_ = covariance.grad_cov_matern52(cov_, X_train, X_train, cur_hyps, num_hyps, False)
    print(grad_cov_)

    truth_grad_cov_ = np.array([
        [
            [0.02, 2.00002, 0., 0.],
            [0., 1.04798822, 0.57644039, 0.57644039],
        ], [
            [0., 1.04798822, 0.57644039, 0.57644039],
            [0.02, 2.00002, 0., 0.]
        ]
    ])

    assert np.all(np.abs(truth_grad_cov_ - grad_cov_) < TEST_EPSILON)

    num_hyps = X_train.shape[1] + 1
    grad_cov_ = covariance.grad_cov_matern52(cov_, X_train, X_train, cur_hyps, num_hyps, True)
    print(grad_cov_)

    truth_grad_cov_ = np.array([
        [
            [2.00002, 0., 0.],
            [1.04798822, 0.57644039, 0.57644039],
        ], [
            [1.04798822, 0.57644039, 0.57644039],
            [2.00002, 0., 0.]
        ]
    ])

    assert np.all(np.abs(truth_grad_cov_ - grad_cov_) < TEST_EPSILON)
예제 #13
0
def test_validate_hyps_arr():
    num_dim = 2
    str_cov = 'matern32'

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cur_hyps = utils_covariance.convert_hyps(str_cov,
                                             cur_hyps,
                                             is_fixed_noise=False)
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_arr(123, str_cov, num_dim)
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_arr(
            cur_hyps, 'abc', num_dim)
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_arr(
            cur_hyps, str_cov, 'abc')
예제 #14
0
def test_compute_posteriors():
    np.random.seed(42)
    arr_range_1 = np.array([
        [0.0, 10.0],
        [-2.0, 2.0],
        [-5.0, 5.0],
    ])
    dim_X = arr_range_1.shape[0]
    num_X = 5
    X = np.random.randn(num_X, dim_X)
    Y = np.random.randn(num_X, 1)

    model_bo = BO(arr_range_1, str_acq='ei', str_exp=None)
    hyps = utils_covariance.get_hyps(model_bo.str_cov,
                                     dim=dim_X,
                                     use_ard=model_bo.use_ard,
                                     use_gp=False)

    cov_X_X, inv_cov_X_X, _ = covariance.get_kernel_inverse(
        X, hyps, model_bo.str_cov)

    X_test = model_bo.get_samples('sobol', num_samples=10, seed=111)

    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(1, Y, X_test, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, 1, X_test, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, 1, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, 1, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, 1, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, inv_cov_X_X, 1)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, inv_cov_X_X, 1.0)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, inv_cov_X_X, 'abc')

    pred_mean, pred_std = model_bo.compute_posteriors(X, Y, X_test, cov_X_X,
                                                      inv_cov_X_X, hyps)

    assert len(pred_mean.shape) == 1
    assert len(pred_std.shape) == 1
    assert pred_mean.shape[0] == pred_mean.shape[0] == X_test.shape[0]
예제 #15
0
def test_log_ml():
    dim_X = 3
    str_cov = 'se'
    X = np.reshape(np.arange(0, 9), (3, dim_X))
    Y = np.expand_dims(np.arange(3, 10, 3), axis=1)
    dict_hyps = utils_covariance.get_hyps(str_cov, dim_X)
    arr_hyps = utils_covariance.convert_hyps(str_cov, dict_hyps, is_fixed_noise=constants.IS_FIXED_GP_NOISE)
    prior_mu_X = np.zeros((3, 1))

    with pytest.raises(AssertionError) as error:
        gp.log_ml(np.arange(0, 3), Y, arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, np.arange(0, 3), arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, Y, dict_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, Y, arr_hyps, 1, prior_mu_X)
    with pytest.raises(ValueError) as error:
        gp.log_ml(X, Y, arr_hyps, 'abc', prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, Y, arr_hyps, str_cov, np.arange(0, 3))
    with pytest.raises(AssertionError) as error:
        gp.log_ml(np.reshape(np.arange(0, 12), (4, dim_X)), Y, arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, np.expand_dims(np.arange(0, 4), axis=1), arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, Y, arr_hyps, str_cov, np.expand_dims(np.arange(0, 4), axis=1))
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, is_cholesky=1)
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, is_fixed_noise=1)
    with pytest.raises(AssertionError) as error:
        gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, debug=1)

    log_ml = gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X)
    print(log_ml)
    truth_log_ml = -65.74995266591506
    assert np.abs(log_ml - truth_log_ml) < TEST_EPSILON

    log_ml = gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, is_cholesky=False)
    print(log_ml)
    truth_log_ml = -65.74995266566506
    assert np.abs(log_ml - truth_log_ml) < TEST_EPSILON
예제 #16
0
def test_validate_hyps_arr():
    num_dim = 2
    str_cov = 'matern32'

    cur_hyps = package_target.get_hyps(str_cov, num_dim)
    cur_hyps = package_target.convert_hyps(str_cov, cur_hyps, fix_noise=False)
    with pytest.raises(AssertionError) as error:
        _, is_valid = package_target.validate_hyps_arr(123, str_cov, num_dim)
    with pytest.raises(AssertionError) as error:
        _, is_valid = package_target.validate_hyps_arr(cur_hyps, 'abc',
                                                       num_dim)
    with pytest.raises(AssertionError) as error:
        _, is_valid = package_target.validate_hyps_arr(cur_hyps, str_cov,
                                                       'abc')
    with pytest.raises(AssertionError) as error:
        _, is_valid = package_target.validate_hyps_arr(cur_hyps,
                                                       str_cov,
                                                       num_dim,
                                                       use_gp='abc')
예제 #17
0
def test_grad_cov_se():
    str_cov = 'se'
    cur_hyps = utils_covariance.get_hyps(str_cov, 2)
    X_train = np.array([
        [2.0, 1.0],
        [1.0, 1.0],
    ])
    num_hyps = X_train.shape[1] + 1
    cov_ = package_target.cov_main(str_cov, X_train, X_train, cur_hyps, True)
    print(cov_)

    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_se('abc', X_train, X_train, cur_hyps, num_hyps,
                                   True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_se(cov_, 'abc', X_train, cur_hyps, num_hyps,
                                   True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_se(cov_, X_train, 'abc', cur_hyps, num_hyps,
                                   True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_se(cov_, X_train, X_train, 'abc', num_hyps,
                                   True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_se(cov_, X_train, X_train, cur_hyps, 'abc',
                                   True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_se(cov_, X_train, X_train, cur_hyps, num_hyps,
                                   'abc')

    num_hyps = X_train.shape[1] + 2
    grad_cov_ = package_target.grad_cov_se(cov_, X_train, X_train, cur_hyps,
                                           num_hyps, False)
    print(grad_cov_)

    truth_grad_cov_ = np.array([[
        [0.02, 2.00002, 0.0, 0.0],
        [0., 1.21306132, 0.60653066, 0.0],
    ], [[0., 1.21306132, 0.60653066, 0.0], [0.02, 2.00002, 0.0, 0.0]]])

    assert np.all(np.abs(truth_grad_cov_ - grad_cov_) < TEST_EPSILON)
예제 #18
0
def test_cov_se():
    with pytest.raises(AssertionError) as error:
        covariance.cov_se(np.zeros(2), np.zeros(2), np.array([1.0, 1.0, 1.0]),
                          0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_se(np.zeros(2), np.zeros(3), np.array([1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_se(np.zeros(3), np.zeros(2), np.array([1.0, 1.0]), 0.1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_se(np.zeros(2), np.zeros(2), np.array([1.0, 1.0]), 1)
    assert np.abs(
        covariance.cov_se(np.zeros(2), np.zeros(2), 1.0, 0.1) -
        0.01) < TEST_EPSILON

    bx = np.array([1.0, 2.0, 0.0])
    bxp = np.array([2.0, 1.0, 1.0])
    cur_hyps = utils_covariance.get_hyps('se', 3)
    cov_ = covariance.cov_se(bx, bxp, cur_hyps['lengthscales'],
                             cur_hyps['signal'])
    print(cov_)
    truth_cov_ = 0.22313016014842987
    assert np.abs(cov_ - truth_cov_) < TEST_EPSILON
예제 #19
0
def test_compute_acquisitions_set():
    np.random.seed(42)
    arr_range_1 = np.array([
        [0.0, 10.0],
        [-2.0, 2.0],
        [-5.0, 5.0],
    ])
    dim_X = arr_range_1.shape[0]
    num_X = 5
    num_instances = 4
    X = np.random.randn(num_X, num_instances, dim_X)
    Y = np.random.randn(num_X, 1)

    model_bo = BO(arr_range_1, str_acq='pi', str_cov='set_se', str_exp='test')
    hyps = utils_covariance.get_hyps(model_bo.str_cov,
                                     dim=dim_X,
                                     use_ard=model_bo.use_ard,
                                     use_gp=False)

    cov_X_X, inv_cov_X_X, _ = covariance.get_kernel_inverse(
        X, hyps, model_bo.str_cov)

    X_test = np.array([
        [
            [1.0, 0.0, 0.0, 1.0],
            [2.0, -1.0, 2.0, 1.0],
            [3.0, -2.0, 4.0, 1.0],
        ],
        [
            [4.0, 2.0, -3.0, 1.0],
            [5.0, 0.0, -2.0, 1.0],
            [6.0, -2.0, -1.0, 1.0],
        ],
    ])

    with pytest.raises(AssertionError) as error:
        model_bo.compute_acquisitions(X_test, X, Y, cov_X_X, inv_cov_X_X, hyps)
예제 #20
0
파일: gp.py 프로젝트: MINGUKKANG/bayeso
def get_optimized_kernel(
        X_train,
        Y_train,
        prior_mu,
        str_cov,
        str_optimizer_method=constants.STR_OPTIMIZER_METHOD_GP,
        str_modelselection_method=constants.STR_MODELSELECTION_METHOD,
        is_fixed_noise=constants.IS_FIXED_GP_NOISE,
        debug=False):
    """
    This function computes the kernel matrix optimized by optimization method specified, its inverse matrix, and the optimized hyperparameters.

    :param X_train: inputs. Shape: (n, d) or (n, m, d).
    :type X_train: numpy.ndarray
    :param Y_train: outputs. Shape: (n, 1).
    :type Y_train: numpy.ndarray
    :param prior_mu: prior mean function or None.
    :type prior_mu: function or NoneType
    :param str_cov: the name of covariance function.
    :type str_cov: str.
    :param str_optimizer_method: the name of optimization method.
    :type str_optimizer_method: str., optional
    :param str_modelselection_method: the name of model selection method.
    :type str_modelselection_method: str., optional
    :param is_fixed_noise: flag for fixing a noise.
    :type is_fixed_noise: bool., optional
    :param debug: flag for printing log messages.
    :type debug: bool., optional

    :returns: a tuple of kernel matrix over `X_train`, kernel matrix inverse, and dictionary of hyperparameters.
    :rtype: tuple of (numpy.ndarray, numpy.ndarray, dict.)

    :raises: AssertionError, ValueError

    """

    # TODO: check to input same is_fixed_noise to convert_hyps and restore_hyps
    assert isinstance(X_train, np.ndarray)
    assert isinstance(Y_train, np.ndarray)
    assert callable(prior_mu) or prior_mu is None
    assert isinstance(str_cov, str)
    assert isinstance(str_optimizer_method, str)
    assert isinstance(str_modelselection_method, str)
    assert isinstance(is_fixed_noise, bool)
    assert isinstance(debug, bool)
    assert len(Y_train.shape) == 2
    assert X_train.shape[0] == Y_train.shape[0]
    _check_str_cov('get_optimized_kernel', str_cov, X_train.shape)
    assert str_optimizer_method in constants.ALLOWED_OPTIMIZER_METHOD_GP
    assert str_modelselection_method in constants.ALLOWED_MODELSELECTION_METHOD
    # TODO: fix this.
    is_gradient = True

    time_start = time.time()

    if debug:
        print('[DEBUG] get_optimized_kernel in gp.py: str_optimizer_method {}'.
              format(str_optimizer_method))
        print(
            '[DEBUG] get_optimized_kernel in gp.py: str_modelselection_method {}'
            .format(str_modelselection_method))

    prior_mu_train = get_prior_mu(prior_mu, X_train)
    if str_cov in constants.ALLOWED_GP_COV_BASE:
        num_dim = X_train.shape[1]
    elif str_cov in constants.ALLOWED_GP_COV_SET:
        num_dim = X_train.shape[2]
        is_gradient = False

    if str_modelselection_method == 'ml':
        neg_log_ml_ = lambda hyps: neg_log_ml(X_train,
                                              Y_train,
                                              hyps,
                                              str_cov,
                                              prior_mu_train,
                                              is_fixed_noise=is_fixed_noise,
                                              is_gradient=is_gradient,
                                              debug=debug)
    elif str_modelselection_method == 'loocv':
        neg_log_ml_ = lambda hyps: neg_log_pseudo_l_loocv(X_train,
                                                          Y_train,
                                                          hyps,
                                                          str_cov,
                                                          prior_mu_train,
                                                          is_fixed_noise=
                                                          is_fixed_noise,
                                                          debug=debug)
        is_gradient = False
    else:  # pragma: no cover
        raise ValueError(
            'get_optimized_kernel: missing conditions for str_modelselection_method.'
        )

    hyps_converted = utils_covariance.convert_hyps(
        str_cov,
        utils_covariance.get_hyps(str_cov, num_dim),
        is_fixed_noise=is_fixed_noise,
    )

    if str_optimizer_method == 'BFGS':
        result_optimized = scipy.optimize.minimize(neg_log_ml_,
                                                   hyps_converted,
                                                   method=str_optimizer_method,
                                                   jac=is_gradient,
                                                   options={'disp': False})
        result_optimized = result_optimized.x
    elif str_optimizer_method == 'L-BFGS-B':
        bounds = utils_covariance.get_range_hyps(str_cov,
                                                 num_dim,
                                                 is_fixed_noise=is_fixed_noise)
        result_optimized = scipy.optimize.minimize(neg_log_ml_,
                                                   hyps_converted,
                                                   method=str_optimizer_method,
                                                   bounds=bounds,
                                                   jac=is_gradient,
                                                   options={'disp': False})
        result_optimized = result_optimized.x
    # TODO: Fill this conditions
    elif str_optimizer_method == 'DIRECT':  # pragma: no cover
        raise NotImplementedError(
            'get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.'
        )
    elif str_optimizer_method == 'CMA-ES':  # pragma: no cover
        raise NotImplementedError(
            'get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.'
        )
    else:  # pragma: no cover
        raise ValueError(
            'get_optimized_kernel: missing conditions for str_optimizer_method'
        )

    hyps = utils_covariance.restore_hyps(str_cov,
                                         result_optimized,
                                         is_fixed_noise=is_fixed_noise)

    hyps, _ = utils_covariance.validate_hyps_dict(hyps, str_cov, num_dim)
    cov_X_X, inv_cov_X_X, grad_cov_X_X = get_kernel_inverse(
        X_train, hyps, str_cov, is_fixed_noise=is_fixed_noise, debug=debug)

    time_end = time.time()

    if debug:
        print('[DEBUG] get_optimized_kernel in gp.py: optimized hyps for gpr',
              hyps)
        print('[DEBUG] get_optimized_kernel in gp.py: time consumed',
              time_end - time_start, 'sec.')
    return cov_X_X, inv_cov_X_X, hyps
예제 #21
0
def test_cov_main():
    cur_hyps = utils_covariance.get_hyps('se', 3)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 2)),
                            np.zeros((20, 3)),
                            cur_hyps,
                            False,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 3)),
                            np.zeros((20, 2)),
                            cur_hyps,
                            False,
                            jitter=0.001)
    with pytest.raises(ValueError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 2)),
                            np.zeros((20, 2)),
                            cur_hyps,
                            False,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            1.0,
                            np.zeros((20, 3)),
                            cur_hyps,
                            False,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 2)),
                            1.0,
                            cur_hyps,
                            False,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main(1.0,
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            cur_hyps,
                            False,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            2.1,
                            False,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            cur_hyps,
                            'abc',
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 3)),
                            np.zeros((12, 3)),
                            cur_hyps,
                            True,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            cur_hyps,
                            False,
                            jitter=1)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('abc',
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            cur_hyps,
                            False,
                            jitter=0.001)

    cur_hyps.pop('signal', None)
    with pytest.raises(ValueError) as error:
        covariance.cov_main('se', np.zeros((10, 3)), np.zeros((20, 3)),
                            cur_hyps, False)
    with pytest.raises(ValueError) as error:
        covariance.cov_main('set_se', np.zeros((10, 5, 3)), np.zeros(
            (20, 5, 3)), cur_hyps, False)

    cur_hyps = utils_covariance.get_hyps('se', 3)
    cov_ = covariance.cov_main('se',
                               np.zeros((10, 3)),
                               np.zeros((20, 3)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 20)))

    cov_ = covariance.cov_main('set_se',
                               np.zeros((10, 5, 3)),
                               np.zeros((20, 5, 3)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 20)))

    cur_hyps = utils_covariance.get_hyps('matern32', 3)
    cov_ = covariance.cov_main('matern32',
                               np.zeros((10, 3)),
                               np.zeros((20, 3)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 20)))

    cov_ = covariance.cov_main('set_matern32',
                               np.zeros((10, 5, 3)),
                               np.zeros((20, 5, 3)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 20)))

    cov_ = covariance.cov_main('set_matern32',
                               np.zeros((10, 5, 3)),
                               np.zeros((10, 5, 3)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 10)))

    cov_ = covariance.cov_main('set_matern32',
                               np.zeros((10, 5, 3)),
                               np.zeros((10, 5, 3)),
                               cur_hyps,
                               True,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 10)) + np.eye(10) * 1e-3)

    cur_hyps = utils_covariance.get_hyps('matern52', 3)
    cov_ = covariance.cov_main('matern52',
                               np.zeros((10, 3)),
                               np.zeros((20, 3)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 20)))

    cov_ = covariance.cov_main('set_matern52',
                               np.zeros((10, 5, 3)),
                               np.zeros((20, 5, 3)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 20)))

    cov_ = covariance.cov_main('set_matern52',
                               np.zeros((10, 5, 3)),
                               np.zeros((10, 5, 3)),
                               cur_hyps,
                               False,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 10)))

    cov_ = covariance.cov_main('set_matern52',
                               np.zeros((10, 5, 3)),
                               np.zeros((10, 5, 3)),
                               cur_hyps,
                               True,
                               jitter=0.001)
    assert np.all(cov_ == np.ones((10, 10)) + np.eye(10) * 1e-3)
예제 #22
0
def test_validate_hyps_dict():
    num_dim = 2
    str_cov = 'matern32'
    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cur_hyps.pop('noise')
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_dict(
            cur_hyps, str_cov, num_dim)
        assert is_valid == True

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_dict(
            cur_hyps, 'abc', num_dim)
        assert is_valid == True

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cur_hyps.pop('lengthscales')
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_dict(
            cur_hyps, str_cov, num_dim)
        assert is_valid == True

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cur_hyps.pop('signal')
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_dict(
            cur_hyps, str_cov, num_dim)
        assert is_valid == True

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cur_hyps['noise'] = 'abc'
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_dict(
            cur_hyps, str_cov, num_dim)
        assert is_valid == True

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cur_hyps['noise'] = np.inf
    cur_hyps, is_valid = utils_covariance.validate_hyps_dict(
        cur_hyps, str_cov, num_dim)
    assert cur_hyps['noise'] == constants.BOUND_UPPER_GP_NOISE

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_dict(
            cur_hyps, str_cov, 123)
        assert is_valid == True

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cur_hyps['lengthscales'] = 'abc'
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_dict(
            cur_hyps, str_cov, num_dim)
        assert is_valid == True

    cur_hyps = utils_covariance.get_hyps(str_cov, num_dim)
    cur_hyps['signal'] = 'abc'
    with pytest.raises(AssertionError) as error:
        _, is_valid = utils_covariance.validate_hyps_dict(
            cur_hyps, str_cov, num_dim)
        assert is_valid == True
예제 #23
0
def test_get_hyps():
    with pytest.raises(AssertionError) as error:
        utils_covariance.get_hyps(1.2, 2.1)
    with pytest.raises(AssertionError) as error:
        utils_covariance.get_hyps(1.2, 2)
    with pytest.raises(AssertionError) as error:
        utils_covariance.get_hyps('se', 2.1)
    with pytest.raises(AssertionError) as error:
        utils_covariance.get_hyps('abc', 2)
    with pytest.raises(AssertionError) as error:
        utils_covariance.get_hyps('se', 2, is_ard='abc')

    cur_hyps = utils_covariance.get_hyps('se', 2)
    assert cur_hyps['noise'] == constants.GP_NOISE
    assert cur_hyps['signal'] == 1.0
    assert len(cur_hyps['lengthscales'].shape) == 1
    assert (cur_hyps['lengthscales'] == np.array([1.0, 1.0])).all()

    cur_hyps = utils_covariance.get_hyps('se', 2, is_ard=False)
    assert cur_hyps['noise'] == constants.GP_NOISE
    assert cur_hyps['signal'] == 1.0
    assert cur_hyps['lengthscales'] == 1.0

    cur_hyps = utils_covariance.get_hyps('matern32', 2, is_ard=False)
    assert cur_hyps['noise'] == constants.GP_NOISE
    assert cur_hyps['signal'] == 1.0
    assert cur_hyps['lengthscales'] == 1.0

    cur_hyps = utils_covariance.get_hyps('matern52', 2, is_ard=False)
    assert cur_hyps['noise'] == constants.GP_NOISE
    assert cur_hyps['signal'] == 1.0
    assert cur_hyps['lengthscales'] == 1.0
예제 #24
0
def get_optimized_kernel(
        X_train: np.ndarray,
        Y_train: np.ndarray,
        prior_mu: constants.TYPING_UNION_CALLABLE_NONE,
        str_cov: str,
        str_optimizer_method: str = constants.STR_OPTIMIZER_METHOD_GP,
        str_modelselection_method: str = constants.STR_MODELSELECTION_METHOD,
        use_ard: bool = constants.USE_ARD,
        fix_noise: bool = constants.FIX_GP_NOISE,
        debug: bool = False) -> constants.TYPING_TUPLE_TWO_ARRAYS_DICT:
    """
    This function computes the kernel matrix optimized by optimization
    method specified, its inverse matrix, and the optimized hyperparameters.

    :param X_train: inputs. Shape: (n, d) or (n, m, d).
    :type X_train: numpy.ndarray
    :param Y_train: outputs. Shape: (n, 1).
    :type Y_train: numpy.ndarray
    :param prior_mu: prior mean function or None.
    :type prior_mu: callable or NoneType
    :param str_cov: the name of covariance function.
    :type str_cov: str.
    :param str_optimizer_method: the name of optimization method.
    :type str_optimizer_method: str., optional
    :param str_modelselection_method: the name of model selection method.
    :type str_modelselection_method: str., optional
    :param use_ard: flag for using automatic relevance determination.
    :type use_ard: bool., optional
    :param fix_noise: flag for fixing a noise.
    :type fix_noise: bool., optional
    :param debug: flag for printing log messages.
    :type debug: bool., optional

    :returns: a tuple of kernel matrix over `X_train`, kernel matrix
        inverse, and dictionary of hyperparameters.
    :rtype: tuple of (numpy.ndarray, numpy.ndarray, dict.)

    :raises: AssertionError, ValueError

    """

    # TODO: check to input same fix_noise to convert_hyps and restore_hyps
    utils_gp.validate_common_args(X_train, Y_train, str_cov, prior_mu, debug)
    assert isinstance(str_optimizer_method, str)
    assert isinstance(str_modelselection_method, str)
    assert isinstance(use_ard, bool)
    assert isinstance(fix_noise, bool)
    utils_covariance.check_str_cov('get_optimized_kernel', str_cov,
                                   X_train.shape)
    assert str_optimizer_method in constants.ALLOWED_OPTIMIZER_METHOD_GP
    assert str_modelselection_method in constants.ALLOWED_MODELSELECTION_METHOD
    use_gradient = bool(str_optimizer_method != 'Nelder-Mead')
    # TODO: Now, use_gradient is fixed as False.
    #    use_gradient = False

    time_start = time.time()

    if debug:
        logger.debug('str_optimizer_method: %s', str_optimizer_method)
        logger.debug('str_modelselection_method: %s',
                     str_modelselection_method)
        logger.debug('use_gradient: %s', use_gradient)

    prior_mu_train = utils_gp.get_prior_mu(prior_mu, X_train)
    if str_cov in constants.ALLOWED_COV_BASE:
        num_dim = X_train.shape[1]
    elif str_cov in constants.ALLOWED_COV_SET:
        num_dim = X_train.shape[2]
        use_gradient = False

    if str_modelselection_method == 'ml':
        neg_log_ml_ = lambda hyps: gp_likelihood.neg_log_ml(
            X_train,
            Y_train,
            hyps,
            str_cov,
            prior_mu_train,
            use_ard=use_ard,
            fix_noise=fix_noise,
            use_gradient=use_gradient,
            debug=debug)
    elif str_modelselection_method == 'loocv':
        # TODO: add use_ard.
        neg_log_ml_ = lambda hyps: gp_likelihood.neg_log_pseudo_l_loocv(
            X_train,
            Y_train,
            hyps,
            str_cov,
            prior_mu_train,
            fix_noise=fix_noise,
            debug=debug)
        use_gradient = False
    else:  # pragma: no cover
        raise ValueError(
            'get_optimized_kernel: missing conditions for str_modelselection_method.'
        )

    hyps_converted = utils_covariance.convert_hyps(str_cov,
                                                   utils_covariance.get_hyps(
                                                       str_cov,
                                                       num_dim,
                                                       use_ard=use_ard),
                                                   fix_noise=fix_noise)

    if str_optimizer_method in ['BFGS', 'SLSQP']:
        result_optimized = scipy.optimize.minimize(neg_log_ml_,
                                                   hyps_converted,
                                                   method=str_optimizer_method,
                                                   jac=use_gradient,
                                                   options={'disp': False})

        if debug:
            logger.debug('negative log marginal likelihood: %.6f',
                         result_optimized.fun)
            logger.debug('scipy message: %s', result_optimized.message)

        result_optimized = result_optimized.x
    elif str_optimizer_method in ['L-BFGS-B', 'SLSQP-Bounded']:
        if str_optimizer_method == 'SLSQP-Bounded':
            str_optimizer_method = 'SLSQP'

        bounds = utils_covariance.get_range_hyps(str_cov,
                                                 num_dim,
                                                 use_ard=use_ard,
                                                 fix_noise=fix_noise)
        result_optimized = scipy.optimize.minimize(neg_log_ml_,
                                                   hyps_converted,
                                                   method=str_optimizer_method,
                                                   bounds=bounds,
                                                   jac=use_gradient,
                                                   options={'disp': False})

        if debug:
            logger.debug('negative log marginal likelihood: %.6f',
                         result_optimized.fun)
            logger.debug('scipy message: %s', result_optimized.message)
        result_optimized = result_optimized.x
    elif str_optimizer_method in ['Nelder-Mead']:
        result_optimized = scipy.optimize.minimize(neg_log_ml_,
                                                   hyps_converted,
                                                   method=str_optimizer_method,
                                                   options={'disp': False})

        if debug:
            logger.debug('negative log marginal likelihood: %.6f',
                         result_optimized.fun)
            logger.debug('scipy message: %s', result_optimized.message)
        result_optimized = result_optimized.x
    else:  # pragma: no cover
        raise ValueError(
            'get_optimized_kernel: missing conditions for str_optimizer_method'
        )

    hyps = utils_covariance.restore_hyps(str_cov,
                                         result_optimized,
                                         use_ard=use_ard,
                                         fix_noise=fix_noise)

    hyps = utils_covariance.validate_hyps_dict(hyps, str_cov, num_dim)
    cov_X_X, inv_cov_X_X, _ = covariance.get_kernel_inverse(
        X_train, hyps, str_cov, fix_noise=fix_noise, debug=debug)
    time_end = time.time()

    if debug:
        logger.debug('hyps optimized: %s', utils_logger.get_str_hyps(hyps))
        logger.debug('time consumed to construct gpr: %.4f sec.',
                     time_end - time_start)
    return cov_X_X, inv_cov_X_X, hyps
예제 #25
0
def test_neg_log_ml():
    dim_X = 3
    str_cov = 'se'
    X = np.reshape(np.arange(0, 9), (3, dim_X))
    Y = np.expand_dims(np.arange(3, 10, 3), axis=1)
    is_fixed_noise = False

    dict_hyps = utils_covariance.get_hyps(str_cov, dim_X)
    arr_hyps = utils_covariance.convert_hyps(str_cov,
                                             dict_hyps,
                                             is_fixed_noise=is_fixed_noise)
    prior_mu_X = np.zeros((3, 1))

    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(np.arange(0, 3), Y, arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X, np.arange(0, 3), arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X, Y, dict_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X, Y, arr_hyps, 1, prior_mu_X)
    with pytest.raises(ValueError) as error:
        gp_scipy.neg_log_ml(X, Y, arr_hyps, 'abc', prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X, Y, arr_hyps, str_cov, np.arange(0, 3))
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(np.reshape(np.arange(0, 12), (4, dim_X)), Y,
                            arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X, np.expand_dims(np.arange(0, 4), axis=1),
                            arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X, Y, arr_hyps, str_cov,
                            np.expand_dims(np.arange(0, 4), axis=1))
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, is_cholesky=1)
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X,
                            Y,
                            arr_hyps,
                            str_cov,
                            prior_mu_X,
                            is_fixed_noise=1)
    with pytest.raises(AssertionError) as error:
        gp_scipy.neg_log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, debug=1)

    neg_log_ml_ = gp_scipy.neg_log_ml(X,
                                      Y,
                                      arr_hyps,
                                      str_cov,
                                      prior_mu_X,
                                      is_fixed_noise=is_fixed_noise,
                                      is_gradient=False,
                                      is_cholesky=True)
    print(neg_log_ml_)
    truth_log_ml_ = 21.916650988532854
    assert np.abs(neg_log_ml_ - truth_log_ml_) < TEST_EPSILON

    neg_log_ml_ = gp_scipy.neg_log_ml(X,
                                      Y,
                                      arr_hyps,
                                      str_cov,
                                      prior_mu_X,
                                      is_fixed_noise=is_fixed_noise,
                                      is_gradient=False,
                                      is_cholesky=False)
    print(neg_log_ml_)
    truth_log_ml_ = 21.91665090519953
    assert np.abs(neg_log_ml_ - truth_log_ml_) < TEST_EPSILON

    neg_log_ml_ = gp_scipy.neg_log_ml(X,
                                      Y,
                                      arr_hyps,
                                      str_cov,
                                      prior_mu_X,
                                      is_fixed_noise=is_fixed_noise,
                                      is_gradient=True,
                                      is_cholesky=False)
    print(neg_log_ml_)
    truth_log_ml_ = 21.91665090519953
    assert np.abs(neg_log_ml_ - truth_log_ml_) < TEST_EPSILON

    neg_log_ml_, neg_grad_log_ml_ = gp_scipy.neg_log_ml(
        X,
        Y,
        arr_hyps,
        str_cov,
        prior_mu_X,
        is_fixed_noise=is_fixed_noise,
        is_gradient=True,
        is_cholesky=True)
    print(neg_log_ml_)
    print(neg_grad_log_ml_)

    truth_log_ml_ = 21.916650988532854
    truth_grad_log_ml_ = np.array([
        -4.09907399e-01,
        -4.09912156e+01,
        -8.88182458e-04,
        -8.88182458e-04,
        -8.88182458e-04,
    ])
    assert np.abs(neg_log_ml_ - truth_log_ml_) < TEST_EPSILON
    assert np.all(np.abs(neg_grad_log_ml_ - truth_grad_log_ml_) < TEST_EPSILON)
예제 #26
0
파일: gp.py 프로젝트: jtkim-lab/bayeso-1
def get_optimized_kernel(
        X_train,
        Y_train,
        prior_mu,
        str_cov,
        str_optimizer_method=constants.STR_OPTIMIZER_METHOD_GP,
        str_modelselection_method=constants.STR_MODELSELECTION_METHOD,
        is_fixed_noise=constants.IS_FIXED_GP_NOISE,
        debug=False):
    # TODO: check to input same is_fixed_noise to convert_hyps and restore_hyps
    assert isinstance(X_train, np.ndarray)
    assert isinstance(Y_train, np.ndarray)
    assert callable(prior_mu) or prior_mu is None
    assert isinstance(str_cov, str)
    assert isinstance(str_optimizer_method, str)
    assert isinstance(str_modelselection_method, str)
    assert isinstance(is_fixed_noise, bool)
    assert isinstance(debug, bool)
    assert len(Y_train.shape) == 2
    assert X_train.shape[0] == Y_train.shape[0]
    _check_str_cov('get_optimized_kernel', str_cov, X_train.shape)
    assert str_optimizer_method in constants.ALLOWED_OPTIMIZER_METHOD_GP
    assert str_modelselection_method in constants.ALLOWED_MODELSELECTION_METHOD

    time_start = time.time()

    if debug:
        print('[DEBUG] get_optimized_kernel in gp.py: str_optimizer_method {}'.
              format(str_optimizer_method))
        print(
            '[DEBUG] get_optimized_kernel in gp.py: str_modelselection_method {}'
            .format(str_modelselection_method))

    prior_mu_train = get_prior_mu(prior_mu, X_train)
    if str_cov in constants.ALLOWED_GP_COV_BASE:
        num_dim = X_train.shape[1]
    elif str_cov in constants.ALLOWED_GP_COV_SET:
        num_dim = X_train.shape[2]

    if str_modelselection_method == 'ml':
        neg_log_ml = lambda hyps: -1.0 * log_ml(X_train,
                                                Y_train,
                                                hyps,
                                                str_cov,
                                                prior_mu_train,
                                                is_fixed_noise=is_fixed_noise,
                                                debug=debug)
    elif str_modelselection_method == 'loocv':
        neg_log_ml = lambda hyps: -1.0 * log_pseudo_l_loocv(X_train,
                                                            Y_train,
                                                            hyps,
                                                            str_cov,
                                                            prior_mu_train,
                                                            is_fixed_noise=
                                                            is_fixed_noise,
                                                            debug=debug)
    else:  # pragma: no cover
        raise ValueError(
            'get_optimized_kernel: missing conditions for str_modelselection_method.'
        )

    hyps_converted = utils_covariance.convert_hyps(
        str_cov,
        utils_covariance.get_hyps(str_cov, num_dim),
        is_fixed_noise=is_fixed_noise,
    )

    if str_optimizer_method == 'BFGS':
        result_optimized = scipy.optimize.minimize(neg_log_ml,
                                                   hyps_converted,
                                                   method=str_optimizer_method)
        result_optimized = result_optimized.x
    elif str_optimizer_method == 'L-BFGS-B':
        bounds = utils_covariance.get_range_hyps(str_cov,
                                                 num_dim,
                                                 is_fixed_noise=is_fixed_noise)
        result_optimized = scipy.optimize.minimize(neg_log_ml,
                                                   hyps_converted,
                                                   method=str_optimizer_method,
                                                   bounds=bounds)
        result_optimized = result_optimized.x
    # TODO: Fill this conditions
    elif str_optimizer_method == 'DIRECT':  # pragma: no cover
        raise NotImplementedError(
            'get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.'
        )
    elif str_optimizer_method == 'CMA-ES':  # pragma: no cover
        raise NotImplementedError(
            'get_optimized_kernel: allowed str_optimizer_method, but it is not implemented.'
        )
    # INFO: It is allowed, but a condition is missed.
    else:  # pragma: no cover
        raise ValueError(
            'get_optimized_kernel: missing conditions for str_optimizer_method'
        )

    hyps = utils_covariance.restore_hyps(str_cov,
                                         result_optimized,
                                         is_fixed_noise=is_fixed_noise)

    hyps, _ = utils_covariance.validate_hyps_dict(hyps, str_cov, num_dim)
    cov_X_X, inv_cov_X_X = get_kernel_inverse(X_train,
                                              hyps,
                                              str_cov,
                                              debug=debug)

    time_end = time.time()

    if debug:
        print('[DEBUG] get_optimized_kernel in gp.py: optimized hyps for gpr',
              hyps)
        print('[DEBUG] get_optimized_kernel in gp.py: time consumed',
              time_end - time_start, 'sec.')
    return cov_X_X, inv_cov_X_X, hyps
예제 #27
0
def test_grad_cov_main():
    cur_hyps = utils_covariance.get_hyps('se', 2)

    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main(123, np.zeros((10, 2)), np.zeros((10, 2)),
                                     cur_hyps, True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main('se', 123, np.zeros((10, 2)), cur_hyps,
                                     True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main('se', np.zeros((10, 2)), 123, cur_hyps,
                                     True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main('se', np.zeros((10, 2)), np.zeros(
            (10, 2)), 123, True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main('se', np.zeros((10, 2)), np.zeros(
            (10, 2)), cur_hyps, 'abc')
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main('abc', np.zeros((10, 2)), np.zeros(
            (10, 2)), cur_hyps, True)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main('se',
                                     np.zeros((10, 2)),
                                     np.zeros((10, 2)),
                                     cur_hyps,
                                     True,
                                     same_X_Xp='abc')
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main('se',
                                     np.zeros((10, 2)),
                                     np.zeros((10, 2)),
                                     cur_hyps,
                                     True,
                                     same_X_Xp=False)
    with pytest.raises(AssertionError) as error:
        package_target.grad_cov_main('se',
                                     np.zeros((10, 2)),
                                     np.zeros((10, 2)),
                                     cur_hyps,
                                     True,
                                     jitter='abc')

    grad_cov_ = package_target.grad_cov_main('se', np.ones((1, 2)),
                                             np.ones((1, 2)), cur_hyps, True)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[2.00002, 0.0, 0.0]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    grad_cov_ = package_target.grad_cov_main('se', np.ones((1, 2)),
                                             np.ones((1, 2)), cur_hyps, False)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.02, 2.00002, 0.0, 0.0]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    cur_hyps['lengthscales'] = 1.0
    grad_cov_ = package_target.grad_cov_main('se', np.ones((1, 2)),
                                             np.zeros((1, 2)), cur_hyps, False)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.02, 0.73577888, 0.73577888]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    grad_cov_ = package_target.grad_cov_main('matern32', np.ones((1, 2)),
                                             np.zeros((1, 2)), cur_hyps, False)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.02, 0.59566154, 0.51802578]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    grad_cov_ = package_target.grad_cov_main('matern32', np.ones((1, 2)),
                                             np.zeros((1, 2)), cur_hyps, True)

    print(grad_cov_)
    truth_grad_cov_ = np.array([[[0.59566154, 0.51802578]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)

    grad_cov_ = package_target.grad_cov_main('matern52', np.ones((1, 2)),
                                             np.zeros((1, 2)), cur_hyps, False)

    print(grad_cov_)
    print(np.squeeze(grad_cov_)[0])
    print(np.squeeze(grad_cov_)[1])
    print(np.squeeze(grad_cov_)[2])
    truth_grad_cov_ = np.array(
        [[[0.02, 0.6345867279080876, 0.5872865507000906]]])
    assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)
예제 #28
0
def test_compute_posteriors_set():
    np.random.seed(42)
    arr_range_1 = np.array([
        [0.0, 10.0],
        [-2.0, 2.0],
        [-5.0, 5.0],
    ])
    dim_X = arr_range_1.shape[0]
    num_X = 5
    num_instances = 4
    X = np.random.randn(num_X, num_instances, dim_X)
    Y = np.random.randn(num_X, 1)

    model_bo = BO(arr_range_1, str_acq='pi', str_cov='set_se', str_exp=None)
    hyps = utils_covariance.get_hyps(model_bo.str_cov,
                                     dim=dim_X,
                                     use_ard=model_bo.use_ard)

    cov_X_X, inv_cov_X_X, _ = covariance.get_kernel_inverse(
        X, hyps, model_bo.str_cov)

    X_test = np.array([
        [
            [1.0, 0.0, 0.0, 1.0],
            [2.0, -1.0, 2.0, 1.0],
            [3.0, -2.0, 4.0, 1.0],
        ],
        [
            [4.0, 2.0, -3.0, 1.0],
            [5.0, 0.0, -2.0, 1.0],
            [6.0, -2.0, -1.0, 1.0],
        ],
    ])

    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(1, Y, X_test, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, 1, X_test, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, 1, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, 1, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, 1, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, inv_cov_X_X, 1)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, inv_cov_X_X, 1.0)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, inv_cov_X_X, 'abc')

    with pytest.raises(AssertionError) as error:
        model_bo.compute_posteriors(X, Y, X_test, cov_X_X, inv_cov_X_X, hyps)

    pred_mean, pred_std = model_bo.compute_posteriors(X, Y,
                                                      X_test[:, :, :dim_X],
                                                      cov_X_X, inv_cov_X_X,
                                                      hyps)

    assert len(pred_mean.shape) == 1
    assert len(pred_std.shape) == 1
    assert pred_mean.shape[0] == pred_mean.shape[0] == X_test.shape[0]
예제 #29
0
def test_compute_acquisitions():
    np.random.seed(42)
    arr_range_1 = np.array([
        [0.0, 10.0],
        [-2.0, 2.0],
        [-5.0, 5.0],
    ])
    dim_X = arr_range_1.shape[0]
    num_X = 5
    X = np.random.randn(num_X, dim_X)
    Y = np.random.randn(num_X, 1)

    model_bo = BO(arr_range_1, str_acq='pi', str_exp='test')
    hyps = utils_covariance.get_hyps(model_bo.str_cov,
                                     dim=dim_X,
                                     use_ard=model_bo.use_ard)

    cov_X_X, inv_cov_X_X, _ = covariance.get_kernel_inverse(
        X, hyps, model_bo.str_cov)

    X_test = model_bo.get_samples('sobol', num_samples=10, seed=111)

    truth_X_test = np.array([
        [
            3.328958908095956,
            -1.8729291455820203,
            0.2839687094092369,
        ],
        [
            8.11741182114929,
            0.3799784183502197,
            -0.05574141861870885,
        ],
        [
            6.735238193068653,
            -0.9264274807646871,
            3.631770429201424,
        ],
        [
            2.13300823001191,
            1.3245289996266365,
            -3.547573888208717,
        ],
        [
            0.6936023756861687,
            -0.018464308232069016,
            -2.1043178741820157,
        ],
        [
            5.438151848502457,
            1.7285785367712379,
            2.0298107899725437,
        ],
        [
            9.085266247857362,
            -1.2144776917994022,
            -4.31197423255071,
        ],
        [
            4.468362366314977,
            0.5345162367448211,
            4.0739051485434175,
        ],
        [
            3.9395463559776545,
            -0.5726078534498811,
            -4.846788686700165,
        ],
        [
            9.92871844675392,
            1.1744442842900753,
            4.774723623413593,
        ],
    ])

    for elem_1 in X_test:
        for elem_2 in elem_1:
            print(elem_2)

    assert np.all(np.abs(X_test - truth_X_test) < TEST_EPSILON)

    with pytest.raises(AssertionError) as error:
        model_bo.compute_acquisitions(1, X, Y, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_acquisitions(X_test, 1, Y, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_acquisitions(X_test, X, 1, cov_X_X, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_acquisitions(X_test, X, Y, 1, inv_cov_X_X, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_acquisitions(X_test, X, Y, cov_X_X, 1, hyps)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_acquisitions(X_test, X, Y, cov_X_X, inv_cov_X_X, 1)
    with pytest.raises(AssertionError) as error:
        model_bo.compute_acquisitions(X_test, X, Y, cov_X_X, inv_cov_X_X,
                                      'abc')

    acqs = model_bo.compute_acquisitions(X_test, X, Y, cov_X_X, inv_cov_X_X,
                                         hyps)

    print('acqs')
    for elem_1 in acqs:
        print(elem_1)

    truth_acqs = np.array([
        0.9140836833364618,
        0.7893422923284443,
        0.7893819649518585,
        0.780516205172671,
        1.170379060386938,
        0.7889956503605072,
        0.7893345684226016,
        0.789773864915061,
        0.7908883762985802,
        0.7893339801719917,
    ])

    assert isinstance(acqs, np.ndarray)
    assert len(acqs.shape) == 1
    assert X_test.shape[0] == acqs.shape[0]
    assert np.all(np.abs(acqs - truth_acqs) < TEST_EPSILON)
예제 #30
0
def test_neg_log_ml():
    dim_X = 3
    str_cov = 'se'
    X = np.reshape(np.arange(0, 9), (3, dim_X))
    Y = np.expand_dims(np.arange(3, 10, 3), axis=1)
    fix_noise = False
    use_gp = False

    dict_hyps = utils_covariance.get_hyps(str_cov, dim_X, use_gp=use_gp)
    arr_hyps = utils_covariance.convert_hyps(str_cov,
                                             dict_hyps,
                                             fix_noise=fix_noise,
                                             use_gp=use_gp)
    prior_mu_X = np.zeros((3, 1))

    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(np.arange(0, 3), Y, arr_hyps, str_cov,
                                  prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(X, np.arange(0, 3), arr_hyps, str_cov,
                                  prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(X, Y, dict_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(X, Y, arr_hyps, 1, prior_mu_X)
    with pytest.raises(ValueError) as error:
        package_target.neg_log_ml(X, Y, arr_hyps, 'abc', prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(X, Y, arr_hyps, str_cov, np.arange(0, 3))
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(np.reshape(np.arange(0, 12), (4, dim_X)), Y,
                                  arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(X, np.expand_dims(np.arange(0, 4), axis=1),
                                  arr_hyps, str_cov, prior_mu_X)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(X, Y, arr_hyps, str_cov,
                                  np.expand_dims(np.arange(0, 4), axis=1))
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(X,
                                  Y,
                                  arr_hyps,
                                  str_cov,
                                  prior_mu_X,
                                  fix_noise=1)
    with pytest.raises(AssertionError) as error:
        package_target.neg_log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, debug=1)

    neg_log_ml_ = package_target.neg_log_ml(X,
                                            Y,
                                            arr_hyps,
                                            str_cov,
                                            prior_mu_X,
                                            fix_noise=fix_noise,
                                            use_gradient=False)
    print(neg_log_ml_)
    truth_log_ml_ = 5.634155417555853
    assert np.abs(neg_log_ml_ - truth_log_ml_) < TEST_EPSILON

    neg_log_ml_, neg_grad_log_ml_ = package_target.neg_log_ml(
        X,
        Y,
        arr_hyps,
        str_cov,
        prior_mu_X,
        fix_noise=fix_noise,
        use_gradient=True)
    print(neg_log_ml_)
    print(neg_grad_log_ml_)
    print(neg_grad_log_ml_[2])
    print(neg_grad_log_ml_[3])
    print(neg_grad_log_ml_[4])

    truth_log_ml_ = 5.634155417555853
    truth_grad_log_ml_ = np.array([
        -1.60446383e-02,
        1.75087448e-01,
        -1.60448396e+00,
        -1.836237221888097e-05,
        -1.836237221888097e-05,
        -1.836237221888097e-05,
    ])
    assert np.abs(neg_log_ml_ - truth_log_ml_) < TEST_EPSILON
    assert np.all(np.abs(neg_grad_log_ml_ - truth_grad_log_ml_) < TEST_EPSILON)

    dict_hyps = utils_covariance.get_hyps(str_cov, dim_X, use_gp=use_gp)
    arr_hyps = utils_covariance.convert_hyps(str_cov,
                                             dict_hyps,
                                             fix_noise=True,
                                             use_gp=use_gp)

    neg_log_ml_, neg_grad_log_ml_ = package_target.neg_log_ml(
        X, Y, arr_hyps, str_cov, prior_mu_X, fix_noise=True, use_gradient=True)
    print(neg_log_ml_)
    print(neg_grad_log_ml_)