def __init__(self, task='yacht', n_units=50, activation='tanh', seed=0):

        # ------------ saving paths created ------------
        self.saving_model_path = None
        # ------------ generate train and test data ------------
        np.random.seed(seed)
        # ------------ generate data ------------
        if task in ['yacht', 'concrete', 'wine-quality-red', 'bostonHousing']:
            X_train, y_train, X_test, y_test = load_uci_data(
                data_directory=f'exps_tasks/datasets/{task}',
                split_number=seed)
        else:
            maths_f, x_bounds, _, true_fmin = get_function(task)
            n_train = 5000
            n_test = 2000

            X_train, y_train = get_init_data(obj_func=maths_f,
                                             noise_var=1e-6,
                                             n_init=n_train,
                                             bounds=x_bounds)
            X_test, y_test = get_init_data(obj_func=maths_f,
                                           noise_var=1e-6,
                                           n_init=n_test,
                                           bounds=x_bounds)
        # X_train, y_train, X_test, y_test = load_uci_data(data_directory=f'./datasets/{task}', split_number=seed)

        self.X_train = X_train
        self.y_train = y_train.flatten()
        self.X_test = X_test
        self.y_test = y_test.flatten()

        # ------------  bnn hyperparameters ------------
        self.num_samples = 300
        self.keep_every = 3
        self.activation = activation
        self.seed = seed
        # bnds for hyperparameters to be tuned
        self.true_bnds = np.array([
            [2, 50.0],  # num_steps_per_sample
            [100, 5000],  # tau_out
            [1e-3, 1e-1],  # length_scale
            [1e-4, 5e-2]
        ]  # step_size
                                  )
        self.d = len(self.true_bnds)
"""
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
from exps_tasks.math_functions import get_function
from models.gp import GPModel
from old.dropoutnn import DropoutNet
from utilities.utilities import get_init_data
import os
'''
Test
'''
# func_name = 'GM-1d'
func_name = 'camelback-2d'
f, x_bounds, _, true_fmin = get_function(func_name)
d = x_bounds.shape[0]
n_init = 40
var_noise = 1.0e-10

def test_compare_gp_with_dropnet():
    #  Specify the objective function and parameters (noise variance, input dimension, initial observation
    np.random.seed(3)
    x_ob, y_ob = get_init_data(obj_func=f, noise_var=var_noise, n_init =n_init, bounds=x_bounds)


    # ------ Test grid -------- #
    if d == 2:
        x1, x2 = np.mgrid[-1:1:50j, -1:1:50j]
        X = np.vstack((x1.flatten(), x2.flatten())).T
        y = f(X)
示例#3
0
def BNN_BO_Exps(obj_func,
                model_type,
                bo_method,
                batch_option,
                batch_size,
                num_iter=40,
                seed_size=20,
                util_type='se_y',
                activation='tanh'):

    #  Specify the objective function and parameters (noise variance, input dimension, number of initial observation)
    f, x_bounds, _, true_fmin = get_function(obj_func)
    var_noise = 1.0e-10
    d = x_bounds.shape[0]
    n_init = d * 10

    saving_path = 'data/' + obj_func

    if not os.path.exists(saving_path):
        os.makedirs(saving_path)

    if model_type == 'LCBNN' or model_type == 'LCCD':
        results_file_name = saving_path + '/' + model_type + activation + util_type + bo_method + str(
            batch_size)
    else:
        results_file_name = saving_path + '/' + model_type + activation + bo_method + str(
            batch_size)

    if os.path.exists(results_file_name):
        with open(results_file_name, 'rb') as exist_data_filefile2:
            existing_results = pickle.load(exist_data_filefile2)

        X_opt_all_seeds = existing_results['X_opt']
        Y_opt_all_seeds = existing_results['Y_opt']
        X_query_all_seeds = existing_results['X_query']
        Y_query_all_seeds = existing_results['Y_query']
        time_all_seeds = existing_results['runtime']

        if isinstance(X_query_all_seeds, list):
            X_query_all_seeds = X_query_all_seeds
            Y_query_all_seeds = Y_query_all_seeds
            time_all_seeds = time_all_seeds
        else:
            X_query_all_seeds = list(X_query_all_seeds)
            Y_query_all_seeds = list(Y_query_all_seeds)
            time_all_seeds = list(time_all_seeds)

        s_start = len(Y_opt_all_seeds)
        print(f"Using existing data from seed{s_start} onwards")

    else:
        s_start = 0
        X_opt_all_seeds = []
        Y_opt_all_seeds = []
        X_query_all_seeds = []
        Y_query_all_seeds = []
        time_all_seeds = []

    for j in range(s_start, seed_size):
        # specify the random seed and generate observation data
        seed = j
        np.random.seed(seed)
        x_init, y_init = get_init_data(obj_func=f,
                                       noise_var=var_noise,
                                       n_init=n_init,
                                       bounds=x_bounds)

        # run Bayesian optimisation:
        bayes_opt = Bayes_opt(func=f, bounds=x_bounds, noise_var=var_noise)
        # model_type: GP or MCDROP or DNGO or BOHAM
        bayes_opt.initialise(X_init=x_init,
                             Y_init=y_init,
                             model_type=model_type,
                             bo_method=bo_method,
                             batch_option=batch_option,
                             batch_size=batch_size,
                             seed=seed,
                             util_type=util_type,
                             actv_func=activation)

        # output of Bayesian optimisation:
        X_query, Y_query, X_opt, Y_opt, time_record = bayes_opt.iteration_step(
            iterations=num_iter)
        # X_query, Y_query - query points selected by BO;
        # X_opt, Yopt      - guesses of the global optimum/optimiser (= optimum point of GP posterior mean)

        # store data
        X_opt_all_seeds.append(X_opt)
        Y_opt_all_seeds.append(Y_opt)
        X_query_all_seeds.append(X_query)
        Y_query_all_seeds.append(Y_query)
        time_all_seeds.append(time_record)

        results = {
            'X_opt': X_opt_all_seeds,
            'Y_opt': Y_opt_all_seeds,
            'X_query': X_query_all_seeds,
            'Y_query': Y_query_all_seeds,
            'runtime': time_all_seeds
        }

        with open(results_file_name, 'wb') as file:
            pickle.dump(results, file)