Beispiel #1
0
def predict_with_cov(X_train: np.ndarray, Y_train: np.ndarray, X_test: np.ndarray,
    cov_X_X: np.ndarray, inv_cov_X_X: np.ndarray, hyps: dict,
    str_cov: str=constants.STR_COV,
    prior_mu: constants.TYPING_UNION_CALLABLE_NONE=None,
    debug: bool=False
) -> constants.TYPING_TUPLE_THREE_ARRAYS:
    """
    This function returns posterior mean and posterior standard deviation
    functions over `X_test`, computed by Gaussian process regression with
    `X_train`, `Y_train`, `cov_X_X`, `inv_cov_X_X`, and `hyps`.

    :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 X_test: inputs. Shape: (l, d) or (l, m, d).
    :type X_test: numpy.ndarray
    :param cov_X_X: kernel matrix over `X_train`. Shape: (n, n).
    :type cov_X_X: numpy.ndarray
    :param inv_cov_X_X: kernel matrix inverse over `X_train`. Shape: (n, n).
    :type inv_cov_X_X: numpy.ndarray
    :param hyps: dictionary of hyperparameters for Gaussian process.
    :type hyps: dict.
    :param str_cov: the name of covariance function.
    :type str_cov: str., optional
    :param prior_mu: None, or prior mean function.
    :type prior_mu: NoneType, or callable, optional
    :param debug: flag for printing log messages.
    :type debug: bool., optional

    :returns: a tuple of posterior mean function over `X_test`, posterior
        standard deviation function over `X_test`, and posterior covariance
        matrix over `X_test`. Shape: ((l, 1), (l, 1), (l, l)).
    :rtype: tuple of (numpy.ndarray, numpy.ndarray, numpy.ndarray)

    :raises: AssertionError

    """

    utils_gp.validate_common_args(X_train, Y_train, str_cov, prior_mu, debug, X_test)
    assert isinstance(cov_X_X, np.ndarray)
    assert isinstance(inv_cov_X_X, np.ndarray)
    assert isinstance(hyps, dict)
    assert len(cov_X_X.shape) == 2
    assert len(inv_cov_X_X.shape) == 2
    assert (np.array(cov_X_X.shape) == np.array(inv_cov_X_X.shape)).all()
    utils_covariance.check_str_cov('predict_with_cov', str_cov,
        X_train.shape, shape_X2=X_test.shape)

    prior_mu_train = utils_gp.get_prior_mu(prior_mu, X_train)
    prior_mu_test = utils_gp.get_prior_mu(prior_mu, X_test)
    cov_X_Xs = covariance.cov_main(str_cov, X_train, X_test, hyps, False)
    cov_Xs_Xs = covariance.cov_main(str_cov, X_test, X_test, hyps, True)
    cov_Xs_Xs = (cov_Xs_Xs + cov_Xs_Xs.T) / 2.0

    mu_Xs = np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X), Y_train - prior_mu_train) + prior_mu_test
    Sigma_Xs = cov_Xs_Xs - np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X), cov_X_Xs)
    return mu_Xs, np.expand_dims(np.sqrt(np.maximum(np.diag(Sigma_Xs), 0.0)), axis=1), Sigma_Xs
Beispiel #2
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_
Beispiel #3
0
def get_kernel_cholesky(X_train,
                        hyps,
                        str_cov,
                        is_fixed_noise=constants.IS_FIXED_GP_NOISE,
                        is_gradient=False,
                        debug=False):
    """
    This function computes a kernel inverse with Cholesky decomposition.

    :param X_train: inputs. Shape: (n, d) or (n, m, d).
    :type X_train: numpy.ndarray
    :param hyps: dictionary of hyperparameters for Gaussian process.
    :type hyps: dict.
    :param str_cov: the name of covariance function.
    :type str_cov: str.
    :param is_fixed_noise: flag for fixing a noise.
    :type is_fixed_noise: bool., optional
    :param is_gradient: flag for computing and returning gradients of negative log marginal likelihood.
    :type is_gradient: bool., optional
    :param debug: flag for printing log messages.
    :type debug: bool., optional

    :returns: a tuple of kernel matrix over `X_train`, lower matrix computed by Cholesky decomposition, and gradients of kernel matrix. If `is_gradient` is False, gradients of kernel matrix would be None.
    :rtype: tuple of (numpy.ndarray, numpy.ndarray, numpy.ndarray)

    :raises: AssertionError

    """

    assert isinstance(X_train, np.ndarray)
    assert isinstance(hyps, dict)
    assert isinstance(str_cov, str)
    assert isinstance(is_fixed_noise, bool)
    assert isinstance(is_gradient, bool)
    assert isinstance(debug, bool)
    utils_gp.check_str_cov('get_kernel_cholesky', str_cov, X_train.shape)

    cov_X_X = covariance.cov_main(
        str_cov, X_train, X_train, hyps,
        True) + hyps['noise']**2 * np.eye(X_train.shape[0])
    cov_X_X = (cov_X_X + cov_X_X.T) / 2.0
    try:
        lower = scipy.linalg.cholesky(cov_X_X, lower=True)
    except np.linalg.LinAlgError:  # pragma: no cover
        cov_X_X += 1e-2 * np.eye(X_train.shape[0])
        lower = scipy.linalg.cholesky(cov_X_X, lower=True)

    if is_gradient:
        grad_cov_X_X = covariance.grad_cov_main(str_cov,
                                                X_train,
                                                X_train,
                                                hyps,
                                                is_fixed_noise,
                                                same_X_Xs=True)
    else:
        grad_cov_X_X = None
    return cov_X_X, lower, grad_cov_X_X
Beispiel #4
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)
Beispiel #5
0
def predict_test_(X_train,
                  Y_train,
                  X_test,
                  cov_X_X,
                  inv_cov_X_X,
                  hyps,
                  str_cov=constants.STR_GP_COV,
                  prior_mu=None,
                  debug=False):
    assert isinstance(X_train, np.ndarray)
    assert isinstance(Y_train, np.ndarray)
    assert isinstance(X_test, np.ndarray)
    assert isinstance(cov_X_X, np.ndarray)
    assert isinstance(inv_cov_X_X, np.ndarray)
    assert isinstance(hyps, dict)
    assert isinstance(str_cov, str)
    assert isinstance(debug, bool)
    assert callable(prior_mu) or prior_mu is None
    assert len(Y_train.shape) == 2
    assert len(cov_X_X.shape) == 2
    assert len(inv_cov_X_X.shape) == 2
    assert (np.array(cov_X_X.shape) == np.array(inv_cov_X_X.shape)).all()
    _check_str_cov('predict_test_',
                   str_cov,
                   X_train.shape,
                   shape_X2=X_test.shape)
    assert X_train.shape[0] == Y_train.shape[0]
    assert X_train.shape[1] == X_test.shape[1]

    prior_mu_train = get_prior_mu(prior_mu, X_train)
    prior_mu_test = get_prior_mu(prior_mu, X_test)
    cov_X_Xs = covariance.cov_main(str_cov, X_train, X_test, hyps)
    cov_Xs_Xs = covariance.cov_main(str_cov, X_test, X_test, hyps)
    cov_Xs_Xs = (cov_Xs_Xs + cov_Xs_Xs.T) / 2.0

    mu_Xs = np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X),
                   Y_train - prior_mu_train) + prior_mu_test
    Sigma_Xs = cov_Xs_Xs - np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X), cov_X_Xs)
    return mu_Xs, np.expand_dims(np.sqrt(np.maximum(np.diag(Sigma_Xs), 0.0)),
                                 axis=1)
Beispiel #6
0
def get_kernel_cholesky(X_train, hyps, str_cov, debug=False):
    assert isinstance(X_train, np.ndarray)
    assert isinstance(hyps, dict)
    assert isinstance(str_cov, str)
    assert isinstance(debug, bool)
    _check_str_cov('get_kernel_cholesky', str_cov, X_train.shape)

    cov_X_X = covariance.cov_main(
        str_cov, X_train, X_train,
        hyps) + hyps['noise']**2 * np.eye(X_train.shape[0])
    cov_X_X = (cov_X_X + cov_X_X.T) / 2.0
    lower = scipy.linalg.cholesky(cov_X_X, lower=True)
    return cov_X_X, lower
Beispiel #7
0
def get_kernel_inverse(X_train, hyps, str_cov, debug=False):
    assert isinstance(X_train, np.ndarray)
    assert isinstance(hyps, dict)
    assert isinstance(str_cov, str)
    assert isinstance(debug, bool)
    _check_str_cov('get_kernel_inverse', str_cov, X_train.shape)

    cov_X_X = covariance.cov_main(
        str_cov, X_train, X_train,
        hyps) + hyps['noise']**2 * np.eye(X_train.shape[0])
    cov_X_X = (cov_X_X + cov_X_X.T) / 2.0
    inv_cov_X_X = np.linalg.inv(cov_X_X)
    return cov_X_X, inv_cov_X_X
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
0
def oracle(args, model):
    seed = 42
    num_all = 100
    num_iter = 50
    num_init = args.bo_num_init
    str_cov = 'se'

    list_dict = []

    if args.bo_kernel == 'rbf':
        kernel = RBFKernel()
    elif args.bo_kernel == 'matern':
        kernel = Matern52Kernel()
    elif args.bo_kernel == 'periodic':
        kernel = PeriodicKernel()
    else:
        raise ValueError(f'Invalid kernel {args.bo_kernel}')

    for ind_seed in range(1, num_all + 1):
        seed_ = seed * ind_seed

        if seed_ is not None:
            torch.manual_seed(seed_)
            torch.cuda.manual_seed(seed_)

        if os.path.exists(
                get_str_file('./results',
                             args.bo_kernel,
                             'oracle',
                             args.t_noise,
                             seed=ind_seed)):
            dict_exp = np.load(get_str_file('./results',
                                            args.bo_kernel,
                                            'oracle',
                                            args.t_noise,
                                            seed=ind_seed),
                               allow_pickle=True)
            dict_exp = dict_exp[()]
            list_dict.append(dict_exp)

            print(dict_exp)
            print(dict_exp['global'])
            print(np.array2string(dict_exp['minima'], separator=','))
            print(np.array2string(dict_exp['regrets'], separator=','))

            continue

        sampler = GPPriorSampler(kernel, t_noise=args.t_noise)

        xp = torch.linspace(-2, 2, 1000).cuda()
        xp_ = xp.unsqueeze(0).unsqueeze(2)

        yp = sampler.sample(xp_)
        min_yp = yp.min()
        print(min_yp.cpu().numpy())

        model.eval()

        batch = AttrDict()
        indices_permuted = torch.randperm(yp.shape[1])

        batch.x = xp_[:, indices_permuted[:2 * num_init], :]
        batch.y = yp[:, indices_permuted[:2 * num_init], :]

        batch.xc = xp_[:, indices_permuted[:num_init], :]
        batch.yc = yp[:, indices_permuted[:num_init], :]

        batch.xt = xp_[:, indices_permuted[num_init:2 * num_init], :]
        batch.yt = yp[:, indices_permuted[num_init:2 * num_init], :]

        X_train = batch.xc.squeeze(0).cpu().numpy()
        Y_train = batch.yc.squeeze(0).cpu().numpy()
        X_test = xp_.squeeze(0).cpu().numpy()

        list_min = []
        list_min.append(batch.yc.min().cpu().numpy())

        for ind_iter in range(0, num_iter):
            print('ind_seed {} seed {} iter {}'.format(ind_seed, seed_,
                                                       ind_iter + 1))

            cov_X_X, inv_cov_X_X, hyps = bayesogp.get_optimized_kernel(
                X_train,
                Y_train,
                None,
                str_cov,
                is_fixed_noise=False,
                debug=False)

            prior_mu_train = bayesogp.get_prior_mu(None, X_train)
            prior_mu_test = bayesogp.get_prior_mu(None, X_test)
            cov_X_Xs = covariance.cov_main(str_cov, X_train, X_test, hyps,
                                           False)
            cov_Xs_Xs = covariance.cov_main(str_cov, X_test, X_test, hyps,
                                            True)
            cov_Xs_Xs = (cov_Xs_Xs + cov_Xs_Xs.T) / 2.0

            mu_ = np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X),
                         Y_train - prior_mu_train) + prior_mu_test
            Sigma_ = cov_Xs_Xs - np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X),
                                        cov_X_Xs)
            sigma_ = np.expand_dims(np.sqrt(np.maximum(np.diag(Sigma_), 0.0)),
                                    axis=1)

            acq_vals = -1.0 * acquisition.ei(np.ravel(mu_), np.ravel(sigma_),
                                             Y_train)
            ind_ = np.argmin(acq_vals)

            x_new = xp[ind_, None, None, None]
            y_new = yp[:, ind_, None, :]

            batch.x = torch.cat([batch.x, x_new], axis=1)
            batch.y = torch.cat([batch.y, y_new], axis=1)

            batch.xc = torch.cat([batch.xc, x_new], axis=1)
            batch.yc = torch.cat([batch.yc, y_new], axis=1)

            X_train = batch.xc.squeeze(0).cpu().numpy()
            Y_train = batch.yc.squeeze(0).cpu().numpy()

            min_cur = batch.yc.min()
            list_min.append(min_cur.cpu().numpy())

        print(min_yp.cpu().numpy())
        print(np.array2string(np.array(list_min), separator=','))
        print(
            np.array2string(np.array(list_min) - min_yp.cpu().numpy(),
                            separator=','))

        dict_exp = {
            'seed': seed_,
            'str_cov': str_cov,
            'global': min_yp.cpu().numpy(),
            'minima': np.array(list_min),
            'regrets': np.array(list_min) - min_yp.cpu().numpy(),
            'xc': X_train,
            'yc': Y_train,
            'model': 'oracle',
        }

        np.save(
            get_str_file('./results',
                         args.bo_kernel,
                         'oracle',
                         args.t_noise,
                         seed=ind_seed), dict_exp)
        list_dict.append(dict_exp)

    np.save(
        get_str_file('./figures/results', args.bo_kernel, 'oracle',
                     args.t_noise), list_dict)
Beispiel #11
0
def predict_test_(X_train,
                  Y_train,
                  X_test,
                  cov_X_X,
                  inv_cov_X_X,
                  hyps,
                  str_cov=constants.STR_GP_COV,
                  prior_mu=None,
                  debug=False):
    """
    This function returns posterior mean and posterior standard deviation functions over `X_test`, computed by Gaussian process regression with `X_train`, `Y_train`, `cov_X_X`, `inv_cov_X_X`, and `hyps`.

    :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 X_test: inputs. Shape: (l, d) or (l, m, d).
    :type X_test: numpy.ndarray
    :param cov_X_X: kernel matrix over `X_train`. Shape: (n, n).
    :type cov_X_X: numpy.ndarray
    :param inv_cov_X_X: kernel matrix inverse over `X_train`. Shape: (n, n).
    :type inv_cov_X_X: numpy.ndarray
    :param hyps: dictionary of hyperparameters for Gaussian process.
    :type hyps: dict.
    :param str_cov: the name of covariance function.
    :type str_cov: str., optional
    :param prior_mu: None, or prior mean function.
    :type prior_mu: NoneType, or function, optional
    :param debug: flag for printing log messages.
    :type debug: bool., optional

    :returns: a tuple of posterior mean function over `X_test`, posterior standard deviation function over `X_test`, and posterior covariance matrix over `X_test`. Shape: ((l, 1), (l, 1), (l, l)).
    :rtype: tuple of (numpy.ndarray, numpy.ndarray, numpy.ndarray)

    :raises: AssertionError

    """

    assert isinstance(X_train, np.ndarray)
    assert isinstance(Y_train, np.ndarray)
    assert isinstance(X_test, np.ndarray)
    assert isinstance(cov_X_X, np.ndarray)
    assert isinstance(inv_cov_X_X, np.ndarray)
    assert isinstance(hyps, dict)
    assert isinstance(str_cov, str)
    assert isinstance(debug, bool)
    assert callable(prior_mu) or prior_mu is None
    assert len(Y_train.shape) == 2
    assert len(cov_X_X.shape) == 2
    assert len(inv_cov_X_X.shape) == 2
    assert (np.array(cov_X_X.shape) == np.array(inv_cov_X_X.shape)).all()
    _check_str_cov('predict_test_',
                   str_cov,
                   X_train.shape,
                   shape_X2=X_test.shape)
    assert X_train.shape[0] == Y_train.shape[0]
    assert X_train.shape[1] == X_test.shape[1]

    prior_mu_train = get_prior_mu(prior_mu, X_train)
    prior_mu_test = get_prior_mu(prior_mu, X_test)
    cov_X_Xs = covariance.cov_main(str_cov, X_train, X_test, hyps, False)
    cov_Xs_Xs = covariance.cov_main(str_cov, X_test, X_test, hyps, True)
    cov_Xs_Xs = (cov_Xs_Xs + cov_Xs_Xs.T) / 2.0

    mu_Xs = np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X),
                   Y_train - prior_mu_train) + prior_mu_test
    Sigma_Xs = cov_Xs_Xs - np.dot(np.dot(cov_X_Xs.T, inv_cov_X_X), cov_X_Xs)
    return mu_Xs, np.expand_dims(np.sqrt(np.maximum(np.diag(Sigma_Xs), 0.0)),
                                 axis=1), Sigma_Xs
Beispiel #12
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,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 3)),
                            np.zeros((20, 2)),
                            cur_hyps,
                            jitter=0.001)
    with pytest.raises(ValueError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 2)),
                            np.zeros((20, 2)),
                            cur_hyps,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            1.0,
                            np.zeros((20, 3)),
                            cur_hyps,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 2)),
                            1.0,
                            cur_hyps,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main(1.0,
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            cur_hyps,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            2.1,
                            jitter=0.001)
    with pytest.raises(AssertionError) as error:
        covariance.cov_main('se',
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            cur_hyps,
                            jitter=1)

    with pytest.raises(AssertionError) as error:
        covariance.cov_main('abc',
                            np.zeros((10, 3)),
                            np.zeros((20, 3)),
                            cur_hyps,
                            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)
    cur_hyps = utils_covariance.get_hyps('se', 3)
    cov_ = covariance.cov_main('se',
                               np.zeros((10, 3)),
                               np.zeros((20, 3)),
                               cur_hyps,
                               jitter=0.001)

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

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