def main():
    num_points = 100
    fix_noise = False
    bounds = obj_fun.get_bounds()

    model_bo = bo.BO(bounds, debug=True)
    X_init = model_bo.get_initials('uniform', num_points)
    X_test = utils_common.get_grids(bounds, 50)
    mu, sigma, Sigma = gp.predict_with_optimized_hyps(
        X_init,
        fun_target(X_init),
        X_test,
        str_optimizer_method='Nelder-Mead',
        fix_noise=fix_noise,
        debug=True)
def main():
    num_train = 200
    num_test = 1000
    X_train = np.random.randn(num_train, 1) * 5.0
    Y_train = np.cos(X_train) + 10.0
    X_test = np.linspace(-10, 10, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test) + 10.0

    mu, sigma, Sigma = gp.predict_with_optimized_hyps(X_train,
                                                      Y_train,
                                                      X_test,
                                                      debug=True)
    utils_plotting.plot_gp_via_distribution(X_train, Y_train, X_test, mu,
                                            sigma, Y_test,
                                            'test_optimized_many_points')
Exemplo n.º 3
0
def main(str_cov):
    np.random.seed(42)
    X_train = np.array([
        [-3.0],
        [-1.0],
        [3.0],
        [1.0],
        [2.0],
    ])
    Y_train = np.cos(X_train) + np.random.randn(X_train.shape[0], 1) * 0.2
    num_test = 200
    X_test = np.linspace(-3, 3, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test)

    mu, sigma, Sigma = gp.predict_with_optimized_hyps(X_train, Y_train, X_test, str_cov=str_cov, fix_noise=False, debug=True)
    utils_plotting.plot_gp_via_distribution(X_train, Y_train, X_test, mu, sigma, Y_test, path_save=PATH_SAVE, str_postfix='cos_' + str_cov)
Exemplo n.º 4
0
def main(fun_prior, str_prior):
    X_train = np.array([
        [-3.0],
        [-2.0],
        [-1.0],
    ])
    Y_train = np.cos(X_train) + 2.0
    num_test = 200
    X_test = np.linspace(-3, 6, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test) + 2.0

    mu, sigma, Sigma = gp.predict_with_optimized_hyps(X_train,
                                                      Y_train,
                                                      X_test,
                                                      prior_mu=fun_prior)
    utils_plotting.plot_gp_via_distribution(
        X_train, Y_train, X_test, mu, sigma, Y_test, PATH_SAVE,
        'optimized_prior_{}'.format(str_prior))
Exemplo n.º 5
0
def main(scale, str_postfix):
    X_train = np.array([
        [-3.0],
        [-2.0],
        [-1.0],
        [2.0],
        [1.2],
        [1.1],
    ])
    Y_train = np.cos(X_train) * scale
    num_test = 200
    X_test = np.linspace(-3, 3, num_test)
    X_test = X_test.reshape((num_test, 1))
    Y_test = np.cos(X_test) * scale

    mu, sigma, Sigma = gp.predict_with_optimized_hyps(X_train,
                                                      Y_train,
                                                      X_test,
                                                      fix_noise=False,
                                                      debug=True)
    utils_plotting.plot_gp_via_distribution(
        X_train, Y_train, X_test, mu, sigma, Y_test, PATH_SAVE,
        'test_optimized_{}_y'.format(str_postfix))
Exemplo n.º 6
0
def thompson_sampling_gp_iteration(range_X: np.ndarray,
    X: np.ndarray, Y: np.ndarray,
    normalize_Y: bool=constants.NORMALIZE_RESPONSE,
    str_sampling_method: str='sobol',
    num_samples: int=200,
    debug: bool=False,
) -> np.ndarray:
    """
    It chooses the next query point via Thompson sampling.

    :param range_X: bounds for a search space. Shape: (d, 2).
    :type range_X: numpy.ndarray
    :param X: inputs. Shape: (n, d).
    :type X: numpy.ndarray
    :param Y: outputs. Shape: (n, 1).
    :type Y: numpy.ndarray
    :param normalize_Y: flag for normalizing responses.
    :type normalize_Y: bool., optional
    :param str_sampling_method: the name of sampling method.
    :type str_sampling_method: str., optional
    :param num_samples: the number of samples.
    :type num_samples: int., optional
    :param debug: flag for a debug option.
    :type debug: bool., optional

    :returns: the next point. Shape: (d, ).
    :rtype: numpy.ndarray

    :raises: AssertionError, ValueError

    """

    assert isinstance(range_X, np.ndarray)
    assert isinstance(X, np.ndarray)
    assert isinstance(Y, np.ndarray)
    assert isinstance(normalize_Y, bool)
    assert isinstance(str_sampling_method, str)
    assert isinstance(num_samples, int)
    assert isinstance(debug, bool)
    assert len(range_X.shape) == 2
    assert len(X.shape) == 2
    assert len(Y.shape) == 2
    assert X.shape[0] == Y.shape[0]
    assert range_X.shape[0] == X.shape[1]
    assert range_X.shape[1] == 2

    str_cov = 'matern52'
    prior_mu = None
    str_optimizer_method_gp = 'BFGS'
#    use_ard = True

    if normalize_Y:
        if debug:
            logger.debug('Responses are normalized.')

        Y = utils_bo.normalize_min_max(Y)

    model_bo = bo.BOwGP(range_X)
    X_test = model_bo.get_samples(str_sampling_method, num_samples=num_samples)

    mu_Xs, _, Sigma_Xs = gp.predict_with_optimized_hyps(X, Y, X_test,
        str_cov=str_cov, str_optimizer_method=str_optimizer_method_gp,
        prior_mu=prior_mu, debug=debug)
    mu_Xs = np.squeeze(mu_Xs, axis=1)

    Y_sampled = None
    list_jitters = [0.0, 1e-4, 1e-2, 1e0, 1e1, 1e2, 1e3, 1e4]

    for jitter_cov in list_jitters:
        try:
            Sigma_Xs_ = Sigma_Xs + jitter_cov * np.eye(Sigma_Xs.shape[0])
            Y_sampled = gp.sample_functions(mu_Xs, Sigma_Xs_, num_samples=1)

            break
        except ValueError: # pragma: no cover
            pass

    if Y_sampled is None: # pragma: no cover
        raise ValueError('jitter_cov is not large enough.')

    ind_min = np.argmin(Y_sampled[:, 0])
    next_point = X_test[ind_min]

    return next_point
Exemplo n.º 7
0
def test_predict_with_optimized_hyps():
    np.random.seed(42)
    dim_X = 2
    num_X = 5
    num_X_test = 20
    X = np.random.randn(num_X, dim_X)
    Y = np.random.randn(num_X, 1)
    X_test = np.random.randn(num_X_test, dim_X)
    prior_mu = None

    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(X,
                                                   Y,
                                                   X_test,
                                                   str_cov='se',
                                                   prior_mu='abc')
    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(X,
                                                   Y,
                                                   X_test,
                                                   str_cov=1,
                                                   prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(X,
                                                   Y,
                                                   1,
                                                   str_cov='se',
                                                   prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(X,
                                                   1,
                                                   X_test,
                                                   str_cov='se',
                                                   prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(1,
                                                   Y,
                                                   X_test,
                                                   str_cov='se',
                                                   prior_mu=prior_mu)

    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(np.random.randn(num_X, 1),
                                                   Y,
                                                   X_test,
                                                   str_cov='se',
                                                   prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(np.random.randn(10, dim_X),
                                                   Y,
                                                   X_test,
                                                   str_cov='se',
                                                   prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(X,
                                                   np.random.randn(10, 1),
                                                   X_test,
                                                   str_cov='se',
                                                   prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(X,
                                                   Y,
                                                   X_test,
                                                   str_optimizer_method=1)
    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(X, Y, X_test, fix_noise=1)

    with pytest.raises(AssertionError) as error:
        package_target.predict_with_optimized_hyps(X, Y, X_test, debug=1)

    mu_test, sigma_test, Sigma_test = package_target.predict_with_optimized_hyps(
        X, Y, X_test, debug=True)
    print(mu_test)
    print(sigma_test)
    print(Sigma_test)