Exemplo n.º 1
0
def main():
    str_acq = 'ei'
    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, dict_info = model_bo.optimize(X_train,
                                              fun_target(X_train),
                                              str_initial_method_ao='uniform')
        cov_X_X = dict_info['cov_X_X']
        inv_cov_X_X = dict_info['inv_cov_X_X']
        hyps = dict_info['hyps']

        mu_test, sigma_test, Sigma_test = gp.predict_test_(
            X_train, Y_train, X_test, cov_X_X, inv_cov_X_X, hyps)
        acq_test = acquisition.ei(mu_test.flatten(), sigma_test.flatten(),
                                  Y_train)
        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)
Exemplo n.º 2
0
def main():
    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.]]))
    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.ei(mu_test.flatten(), sigma_test.flatten(),
                                  Y_train)
        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='basics_bo_' + 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='basics_bo_' + str(ind_),
                                        int_init=num_init,
                                        is_acq_axis_small=True)
Exemplo n.º 3
0
def test_ei():
    with pytest.raises(AssertionError) as error:
        package_target.ei('abc', np.ones(10), np.zeros((5, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.ei(np.ones(10), 'abc', np.zeros((5, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.ei(np.ones(10), np.ones(10), 'abc')
    with pytest.raises(AssertionError) as error:
        package_target.ei(np.ones(10), np.ones(10), np.zeros((5, 1)), 1)
    with pytest.raises(AssertionError) as error:
        package_target.ei(np.ones(5), np.ones(10), np.zeros((5, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.ei(np.ones(10), np.ones(10), np.zeros(5))
    with pytest.raises(AssertionError) as error:
        package_target.ei(np.ones(10), np.ones((10, 1)), np.zeros((5, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.ei(np.ones((10, 1)), np.ones(10), np.zeros((5, 1)))

    val_acq = package_target.ei(np.arange(0, 10), np.ones(10), np.zeros((5, 1)))
    truth_val_acq = np.array([3.98942280e-01, 8.33154706e-02, 8.49070261e-03, 3.82154315e-04, 7.14525833e-06, 5.34616535e-08, 1.56356969e-10, 1.76032579e-13, 7.55026079e-17, 1.22477876e-20])
    assert (np.abs(val_acq - truth_val_acq) < TEST_EPSILON).all()
Exemplo n.º 4
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)
Exemplo n.º 5
0
def bo(args, model):
    if args.mode == 'bo':
        ckpt = torch.load(os.path.join(args.root, 'ckpt.tar'))
        model.load_state_dict(ckpt.model)

    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}')

    seed = 42
    str_cov = 'se'
    num_all = 100
    num_iter = 50
    num_init = args.bo_num_init

    list_dict = []

    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_)

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

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

        yp = obj_prior.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))

            with torch.no_grad():
                outs = model(batch, num_samples=args.bo_num_samples)
                print('ctx_ll {:.4f} tar ll {:.4f}'.format(
                    outs.ctx_ll.item(), outs.tar_ll.item()))
                py = model.predict(batch.xc,
                                   batch.yc,
                                   xp[None, :, None].repeat(1, 1, 1),
                                   num_samples=args.bo_num_samples)
                mu, sigma = py.mean.squeeze(0), py.scale.squeeze(0)

            if mu.dim() == 4:
                print(mu.shape, sigma.shape)
                var = sigma.pow(2).mean(0) + mu.pow(2).mean(0) - mu.mean(
                    0).pow(2)
                sigma = var.sqrt().squeeze(0)
                mu = mu.mean(0).squeeze(0)
                mu_ = mu.cpu().numpy()
                sigma_ = sigma.cpu().numpy()

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

#                acq_vals = []

#                for ind_mu in range(0, mu.shape[0]):
#                    acq_vals_ = -1.0 * acquisition.ei(np.ravel(mu[ind_mu].cpu().numpy()), np.ravel(sigma[ind_mu].cpu().numpy()), Y_train)
#                    acq_vals.append(acq_vals_)

#                acq_vals = np.mean(acq_vals, axis=0)
            else:
                mu_ = mu.cpu().numpy()
                sigma_ = sigma.cpu().numpy()

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


#                var = sigma.pow(2).mean(0) + mu.pow(2).mean(0) - mu.mean(0).pow(2)
#                sigma = var.sqrt().squeeze(0)
#                mu = mu.mean(0).squeeze(0)

            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_,
            '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': args.model,
            'cov': str_cov,
        }

        list_dict.append(dict_exp)

    np.save(
        get_str_file('./figures/results', args.bo_kernel, args.model,
                     args.t_noise), list_dict)