Example #1
0
 def _optimize_objective(self, fun_acquisition, X_train, Y_train, X_test,
                         cov_X_X, inv_cov_X_X, hyps):
     X_test = np.atleast_2d(X_test)
     pred_mean, pred_std = gp.predict_test_(X_train,
                                            Y_train,
                                            X_test,
                                            cov_X_X,
                                            inv_cov_X_X,
                                            hyps,
                                            str_cov=self.str_cov,
                                            prior_mu=self.prior_mu,
                                            debug=self.debug)
     # no matter which acquisition functions are given, we input pred_mean, pred_std, and Y_train.
     acquisitions = fun_acquisition(pred_mean=pred_mean.flatten(),
                                    pred_std=pred_std.flatten(),
                                    Y_train=Y_train)
     return acquisitions
Example #2
0
def main():
    str_acq = 'aei'
    num_iter = 10
    X_train = np.array([
        [-5],
        [-1],
        [1],
        [2],
    ])
    num_init = X_train.shape[0]
    model_bo = bo.BO(np.array([[-6., 6.]]), str_acq=str_acq)
    X_test = np.linspace(-6, 6, 400)
    X_test = np.reshape(X_test, (400, 1))
    for ind_ in range(1, num_iter + 1):
        Y_train = fun_target(X_train)
        next_x, _, _, cov_X_X, inv_cov_X_X, hyps = model_bo.optimize(
            X_train, fun_target(X_train), str_initial_method='uniform')
        mu_test, sigma_test = gp.predict_test_(X_train, Y_train, X_test,
                                               cov_X_X, inv_cov_X_X, hyps)
        acq_test = acquisition.aei(mu_test.flatten(), sigma_test.flatten(),
                                   Y_train, hyps['noise'])
        acq_test = np.expand_dims(acq_test, axis=1)
        X_train = np.vstack((X_train, next_x))
        Y_train = fun_target(X_train)
        utils_plotting.plot_bo_step(X_train,
                                    Y_train,
                                    X_test,
                                    fun_target(X_test),
                                    mu_test,
                                    sigma_test,
                                    path_save=PATH_SAVE,
                                    str_postfix='bo_{}_'.format(str_acq) +
                                    str(ind_),
                                    int_init=num_init)
        utils_plotting.plot_bo_step_acq(X_train,
                                        Y_train,
                                        X_test,
                                        fun_target(X_test),
                                        mu_test,
                                        sigma_test,
                                        acq_test,
                                        path_save=PATH_SAVE,
                                        str_postfix='bo_{}_'.format(str_acq) +
                                        str(ind_),
                                        int_init=num_init,
                                        is_acq_axis_small=True)
Example #3
0
    def _optimize_objective(self, fun_acquisition, X_train, Y_train, X_test,
                            cov_X_X, inv_cov_X_X, hyps):
        """
        It returns acquisition function values over `X_test`.

        :param fun_acquisition: acquisition function.
        :type fun_acquisition: function
        :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.

        :returns: acquisition function values over `X_test`. Shape: (l, ).
        :rtype: numpy.ndarray

        """

        X_test = np.atleast_2d(X_test)
        pred_mean, pred_std, _ = gp.predict_test_(X_train,
                                                  Y_train,
                                                  X_test,
                                                  cov_X_X,
                                                  inv_cov_X_X,
                                                  hyps,
                                                  str_cov=self.str_cov,
                                                  prior_mu=self.prior_mu,
                                                  debug=self.debug)
        # no matter which acquisition functions are given, we input pred_mean, pred_std, and Y_train.
        acquisitions = fun_acquisition(pred_mean=pred_mean.flatten(),
                                       pred_std=pred_std.flatten(),
                                       Y_train=Y_train)
        return acquisitions
Example #4
0
def test_predict_test_():
    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
    cov_X_X, inv_cov_X_X, hyps = gp.get_optimized_kernel(X, Y, prior_mu, 'se')
    
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, cov_X_X, inv_cov_X_X, hyps, str_cov='se', prior_mu='abc')
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, cov_X_X, inv_cov_X_X, hyps, str_cov=1, prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, cov_X_X, inv_cov_X_X, 1, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, cov_X_X, 1, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, 1, inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, 1, cov_X_X, inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, 1, X_test, cov_X_X, inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(1, Y, X_test, cov_X_X, inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(np.random.randn(num_X, 1), Y, X_test, cov_X_X, inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(np.random.randn(10, dim_X), Y, X_test, cov_X_X, inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, np.random.randn(10, 1), X_test, cov_X_X, inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, np.random.randn(3, 3), inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, np.random.randn(10), inv_cov_X_X, hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, cov_X_X, np.random.randn(10), hyps, str_cov='se', prior_mu=prior_mu)
    with pytest.raises(AssertionError) as error:
        gp.predict_test_(X, Y, X_test, np.random.randn(10), np.random.randn(10), hyps, str_cov='se', prior_mu=prior_mu)