selection='random'), 'par': PAR(C=1.0, fit_intercept=False, tol=None, shuffle=True, verbose=1, loss='epsilon_insensitive', epsilon=0.01, random_state=rng), 'svr_rbf': SVR(kernel='rbf', C=1e3, shrinking=True, verbose=True), 'svr_ply': SVR(kernel='poly', C=1e3, degree=3, shrinking=True, verbose=True), 'gpr': GPR(kernel=None, alpha=1e-10, optimizer='fmin_l_bfgs_b', random_state=rng), 'dtr': DTR(max_depth=10), 'kr_rbf': KernelRidge(kernel='rbf', gamma=0.1, alpha=1e-2), 'kr_ply': KernelRidge(kernel='poly', gamma=10.1, alpha=1e-2, degree=3), 'mlp_r': MLPRegressor( hidden_layer_sizes=( 10, 8, 5, ), activation='tanh', solver='adam', #'lbfgs', #
def calculate_GPR(self, X_train, y_train): med_w = np.median(pdist(X_train, 'euclidean')) gpr = GPR().fit(X_train / med_w, y_train) return y_train.reshape(-1, 1) - gpr.predict(X_train / med_w).reshape( -1, 1)
import numpy as np import sklearn import imp import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression as LR from sklearn.neural_network import MLPRegressor as NNR from sklearn.tree import DecisionTreeRegressor as DTR from sklearn.gaussian_process import GaussianProcessRegressor as GPR Regression_Method_Evaluator([2,3],[5],GPR(normalize_y=True), plot=True) ##################################################################################################### ############################## Function Definition Part #############################################
train_labels = feature_matrices['train_labels'] train_values = feature_matrices['train_values'] train_targets = feature_matrices['train_targets'] test_labels = feature_matrices['test_labels'] test_values = feature_matrices['test_values'] test_targets = feature_matrices['test_targets'] n_features = train_values.shape[1] # Model Specific Information # ----------------------- lbound = 1e-2 rbound = 1e1 n_restarts = 25 kernel = C(1.0, (lbound, rbound)) * Matern(n_features * [1], (lbound, rbound), nu=0.5) gp = GPR(kernel=kernel, n_restarts_optimizer=n_restarts) gp.fit(train_values, train_targets) test_model, sigma2_pred_test = gp.predict(test_values, return_std=True) train_model, sigma2_pred_train = gp.predict(train_values, return_std=True) # ----------------------- # Undo normalization in FP generation test_model = np.multiply(test_model, std_target) + mean_target sigma2_pred_test = np.multiply(sigma2_pred_test, std_target) # GP Specific test_targets = np.multiply(test_targets, std_target) + mean_target train_model = np.multiply(train_model, std_target) + mean_target sigma2_pred_train = np.multiply(sigma2_pred_train, std_target) # GP Specific train_targets = np.multiply(train_targets, std_target) + mean_target
def __init__(self, X_train, Y_train, X_train_min=None, X_train_max=None, npc=10, nrestarts=0): """ Initialize the emulator with X_train, Y_train, X_train_range, npc, nrestarts NOTE: if X_train_min == None, X_min=0.9*(X_min) if X_train_max == None, X_max=1.1*(X_max) """ logging.info('training emulator for system with (%d PC, %d restarts)', npc, nrestarts) self.npc = npc (_, self.ndim) = X_train.shape (self.nsamples, self.nobs) = Y_train.shape ### standardize X_train if X_train_min is None: X_train_min = 0.9 * np.min(X_train, axis=0) if X_train_max is None: X_train_max = 1.1 * np.max(X_train, axis=0) self.X_min = np.array(X_train_min) self.X_max = np.array(X_train_max) self.Y_train = np.copy(Y_train) self.X_train = (X_train - X_train_min) / (X_train_max - X_train_min) ### standardScaler transformation ### Args: Y; returns: (Y - Y.mean)/Y.std self.scaler = StandardScaler(copy=False) ### principal components analysis ### Args: Y; Y -- (svd) --> Y = U.S.Vt, ### Returns: if Whiten, Z = Y.V/S * np.sqrt(nsamples -1) = U * np.sqrt(nsamples -1) ### else, Z = Y.V = U.S self.pca = PCA(copy=False, whiten=True, svd_solver='full') Z = self.pca.fit_transform(self.scaler.fit_transform( self.Y_train))[:, :npc] ### Kernel and Gaussian Process Emulators k0 = 1. * kernels.RBF(length_scale=np.ones(self.ndim), length_scale_bounds=np.outer( np.ones(self.ndim), (.1, 10))) k1 = kernels.ConstantKernel() # ConstantKernel doesn't help k2 = kernels.WhiteKernel(noise_level=0.01, noise_level_bounds=(1e-8, 10.)) kernel = (k0 + k2) self.GPs = [ GPR(kernel=kernel, alpha=0, n_restarts_optimizer=nrestarts, copy_X_train=False).fit(self.X_train, z) for z in Z.T ] ## construct the full linear transform matrix self._trans_matrix = self.pca.components_ * np.sqrt( self.pca.explained_variance_[:, np.newaxis]) * self.scaler.scale_ ## in-order to propagate the predictive variance: https://en.wikipedia.org/wiki/Propagation_of_uncertainty A = self._trans_matrix[:npc] self._var_trans = np.einsum('ki,kj->kij', A, A, optimize=False).reshape(npc, self.nobs**2) B = self._trans_matrix[npc:] ## covariance matrix for the remaining neglected PCs self._cov_trunc = np.dot(B.T, B) self._cov_trunc.flat[::self.nobs + 1] += 1e-4 * self.scaler.var_
def plot_contours(x, response, case_b=True, ax=None): # Initialize GP model gp = GPR(kernel=1e-7 * RationalQuadratic(), n_restarts_optimizer=10, alpha=5e-5) # Determine extends of Gamma plane in the X direction x_max = total_extents x_min = np.min(x[:, 0]) # Fit gaussian model based on observation points gp.fit(x, response) # Create 100 x 100 grid index = 0 delta = .001 x_1 = np.arange(0, 1, delta) y_1 = np.arange(0, 1, delta) X, Y = np.meshgrid(x_1, y_1) # transform square sample space into the sample space for a gamma plane (triangle) X = X * (x_max - x_min) + x_min if case_b: Y = (2 * Y - 1) * X / 3 else: Y = Y * X / 3 X_1 = np.reshape(X, (X.size, 1)) Y_1 = np.reshape(Y, (X.size, 1)) blarg = np.concatenate((X_1, Y_1), axis=1) Z_1, std = gp.predict(blarg, return_std=True) Z = np.reshape(Z_1, X.shape) std = np.reshape(std, X.shape) if ax is None: fig = plt.figure(facecolor="white", figsize=(22, 15), dpi=1200) # Specify values to plot for the ISO-FIP contours levels = np.asarray([0.025, 0.05, 0.075, 0.10, 0.125, 0.15, 0.175, 0.20]) CS = plt.contour(X, Y, Z, levels, colors=colors, linewidths=4, dpi=1200) fmt = {} for l in CS.levels: # fmt[l] = str(((int(np.exp(l))-1)/10+1)*10) fmt[l] = str(l) # Plot squares and circles corresponding to loading points used. if (case_b): plt.clabel(CS, CS.levels, inline=1, fmt=fmt, fontsize=20) plt.scatter(x[:, 0], x[:, 1], s=10, c='k', marker="o") else: plt.clabel(CS, [], inline=1, fmt=fmt, fontsize=20) valid_as = x[:, 1] > 0 plt.scatter(x[valid_as, 0], x[valid_as, 1], s=20, c='k', marker="s") # Plot bounds of Gamma plane plt.plot([0, x_max], [0, x_max / 3], 'k-') plt.plot([0, x_max], [0, -x_max / 3], 'k-') plt.plot([0, x_max], [0, 0], 'k-') plt.ylim([-x_max / 3, x_max / 3]) plt.xlim([0, x_max]) plt.xlabel(r"$\frac{\epsilon^p_1-\epsilon^p_3}{2}$") plt.ylabel(r"$\frac{\epsilon^p_1+\epsilon^p_3}{2}$") ax = plt.subplot(111) for item in ([ax.xaxis.label, ax.yaxis.label]): item.set_fontsize(40) for item in (ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(30) return ax
def Find_Max_Q(Next_State,Model): Max_Q=-1000 Action=copy.deepcopy(Next_State) for i in range(0,430,10): Action[-1]=i State_Action_Pair=np.concatenate((Next_State,Action)).reshape(1,-1) # print(State_Action_Pair) if Model.predict(State_Action_Pair)>Max_Q: Max_Q=np.max([Max_Q,Model.predict(State_Action_Pair)]) Best_Duration=i return Max_Q,Best_Duration #################################################################################################################################################################### list1=np.random.choice(1008,size=50,replace=False) Model=Q_Function(list1,GPR(normalize_y=True),Exp_File_Name='Exp_x20_z10_ComfortOnly.npy', plot=False,Return_Model=True) rewards=copy.deepcopy(Training_Labels) Updated_Training_Labels=np.zeros(np.shape(rewards)) a=0 for k in range(1000): count=0 for i in list1: Next_State=Training_Data[count,0:4] Updated_Training_Labels[count]=rewards[count]+.95*Find_Max_Q(Next_State,Model)[0] count+=1 Model=Q_Function(list1,GPR(normalize_y=True),Updated_Training_Labels=Updated_Training_Labels,Exp_File_Name='Exp_x20_z10_ComfortOnly.npy', plot=False,Return_Model=True) print(np.sum(Updated_Training_Labels)-a,k) a=np.sum(Updated_Training_Labels)
def do_something(bb): # Storage: data file name, Total amount of Design Points, [parameter names], [parameter min values], # [parameter max values], [parameter truths], [observable names], [observable truths], # [experimental relative uncertainty], [theoretical relative uncertainty], # number of trento runs per design point # savedValues = np.load("listedVerySmall.npy", allow_pickle=True) savedValues = np.load("./2to16/" + str(pairList[bb][0]) + "dp" + str(pairList[bb][1]) + "tr.npy", allow_pickle=True) totDesPoints = savedValues[1] paramNames = savedValues[2] paramMins = savedValues[3] paramMaxs = savedValues[4] paramTruths = savedValues[5] obsNames = savedValues[6] obsTruths = savedValues[7][0] truthUncert = savedValues[7][1] expRelUncert = savedValues[8] theoRelUncert = savedValues[9] nTrento = savedValues[10] # datum: np.array([[design_points], [observables]]) datum = np.load(str(savedValues[0]) + ".npy", allow_pickle=True) desPts = datum[0] observables = datum[1] # desPts = np.load(str(savedValues[0][0]) + ".npy", allow_pickle=True) # observables = np.load(str(savedValues[0][1]) + ".npy", allow_pickle=True) ### Add uncertainty to the observables ### calcUncertList = np.multiply(observables, theoRelUncert) noise = np.zeros(np.shape(calcUncertList)) for ii in range(len(calcUncertList)): for jj in range(len(obsTruths)): noise[ii][jj] = np.random.normal(0, calcUncertList[ii][jj]) calcMeanPlusNoise = np.add(observables, noise) ### Make emulator for each observable ### emul_d = {} for nn in range(len(obsTruths)): # Label for the observable obs_label = obsNames[nn] # Function that returns the value of an observable (just to get the truth) # Kernels k0 = 1. * kernels.RBF( # length_scale=(param1_paramspace_length / 2., param2_paramspace_length / 2.) # length_scale_bounds=( # (param1_paramspace_length / param1_nb_design_pts, 3. * param1_paramspace_length), # (param2_paramspace_length / param2_nb_design_pts, 3. * param2_paramspace_length) # ) ) k2 = 1. * kernels.WhiteKernel( noise_level=truthUncert[nn], # noise_level_bounds='fixed' noise_level_bounds=(truthUncert[nn] / 4., 4 * truthUncert[nn])) kernel = (k0 + k2) nrestarts = 10 emulator_design_pts_value = np.array(desPts) # .tolist() emulator_obs_mean_value = np.array(observables[:, nn]) # .tolist() # Fit a GP (optimize the kernel hyperparameters) to each PC. gaussian_process = GPR(kernel=kernel, alpha=0.0001, n_restarts_optimizer=nrestarts, copy_X_train=True).fit( emulator_design_pts_value, emulator_obs_mean_value) """ # https://github.com/keweiyao/JETSCAPE2020-TRENTO-BAYES/blob/master/trento-bayes.ipynb print('Information on emulator for observable ' + obs_label) print('RBF: ', gaussian_process.kernel_.get_params()['k1']) print('White: ', gaussian_process.kernel_.get_params()['k2']) """ emul_d[obsNames[nn]] = { 'gpr': gaussian_process # 'mean':scipy.interpolate.interp2d(calc_d[obs_name]['x_list'], calc_d[obs_name]['y_list'], np.transpose( # calc_d[obs_name]['mean']), kind='linear', copy=True, bounds_error=False, fill_value=None), # 'uncert':scipy.interpolate.interp2d(calc_d[obs_name]['x_list'], calc_d[obs_name]['y_list'], np.transpose( # calc_d[obs_name]['uncert']), kind='linear', copy=True, bounds_error=False, fill_value=None) } ##################### # Plot the emulator # ##################### # observable vs value of one parameter (with the other parameter fixed) for pl in range(len(paramTruths)): plt.figure(1) plt.xscale('linear') plt.yscale('linear') plt.xlabel(paramNames[pl]) plt.ylabel(obs_label) # Compute the posterior for a range of values of the parameter "x" ranges = np.zeros(50).reshape((1, 50)) for rr in range(0, len(paramMins)): if rr != pl: val = (paramMins[rr] + paramMaxs[rr]) / 2 ranges = np.append(ranges, np.linspace(val, val, 50).reshape( (1, 50)), axis=0) else: ranges = np.append(ranges, np.linspace(paramMins[rr], paramMaxs[rr], 50).reshape( (1, 50)), axis=0) param_value_array = np.transpose(ranges[1:, :]) z_list, z_list_uncert = gaussian_process.predict(param_value_array, return_std=True) # Plot design points plt.errorbar(desPts[:, pl], np.array(observables[:, nn]), yerr=np.array(calcUncertList)[:, nn], fmt='D', color='orange', capsize=4) # Plot interpolator plt.plot(ranges[pl + 1], z_list, color='blue') plt.fill_between(ranges[pl + 1], z_list - z_list_uncert, z_list + z_list_uncert, color='blue', alpha=.4) # Plot the truth plt.plot(paramTruths[pl], obsTruths[nn], "D", color='black') plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0)) plt.tight_layout() plt.close(1) ### Compute the Posterior ### # We assume uniform priors for this example # Here 'x' is the only model parameter def prior(): return 1 # Under the approximations that we're using, the posterior is # exp(-1/2*\sum_{observables, pT} # (model(observable,pT)-data(observable,pT))^2/(model_err(observable,pT)^2+exp_err(observable,pT)^2) # Here 'x' is the only model parameter def likelihood(params): res = 0.0 norm = 1. # Sum over observables for xx in range(len(obsTruths)): # Function that returns the value of an observable data_mean2 = obsTruths[xx] data_uncert2 = truthUncert[xx] tmp_data_mean, tmp_data_uncert = data_mean2, data_uncert2 emulator = emul_d[obsNames[xx]]['gpr'] tmp_model_mean, tmp_model_uncert = emulator.predict( np.atleast_2d(np.transpose(params)), return_std=True) cov = (tmp_model_uncert * tmp_model_uncert + tmp_data_uncert * tmp_data_uncert) res += np.power(tmp_model_mean - tmp_data_mean, 2) / cov norm *= 1 / np.sqrt(cov.astype('float')) res *= -0.5 e = 2.71828182845904523536 return norm * e**res def posterior(params): return prior() * likelihood(params) ### Plot the Posterior ### # Info about parameters param1_label = paramNames[0] param1_truth = paramTruths[0] param2_label = paramNames[1] param2_truth = paramTruths[1] plt.figure() plt.xscale('linear') plt.yscale('linear') plt.xlabel(param1_label) plt.ylabel(param2_label) plt.title("Number of design points: " + str(totDesPoints) + ", Number of trento runs: " + str(nTrento)) # Compute the posterior for a range of values of the parameter "x" div = totDesPoints if totDesPoints < 50: div = 50 param1_range = np.arange(paramMins[0], paramMaxs[0], (paramMaxs[0] - paramMins[0]) / div) param2_range = np.arange(paramMins[1], paramMaxs[1], (paramMaxs[1] - paramMins[1]) / div) param1_mesh, param2_mesh = np.meshgrid(param1_range, param2_range, sparse=False, indexing='ij') posterior_array = np.array([ posterior((param1_val, param2_val)) for (param1_val, param2_val) in zip(param1_mesh, param2_mesh) ]) paramTruthPost = float(posterior(paramTruths)) maxPost = np.amax(posterior_array) print( str(pairList[bb]) + "Posterior at parameter truth: " + str(paramTruthPost)) print(str(pairList[bb]) + "Max Posterior: " + str(maxPost)) ratio = paramTruthPost / maxPost print(str(pairList[bb]) + "Ratio: " + str(ratio)) def trapezoid(myArr, dx): add = np.sum(myArr) - 0.5 * myArr[0] - 0.5 * myArr[-1] return add * dx def doubleTrap(myArr, dx, dy): temp = np.zeros((len(myArr))) for row in range(len(myArr)): temp[row] = trapezoid(myArr[row], dy) return trapezoid(temp, dx) ddxx = param1_mesh[1][0] - param1_mesh[0][0] ddyy = param2_mesh[0][1] - param2_mesh[0][0] vol1 = doubleTrap(posterior_array, ddxx, ddyy) hellDistance = np.sqrt(1 - np.sqrt(paramTruthPost / np.sum(posterior_array))) print(str(pairList[bb]) + "Hellinger Distance: " + str(hellDistance)) """ def postFunc(y, x): return float(posterior((x, y))) vol2 = scipy.integrate.dblquad(postFunc, paramMins[0], paramMaxs[0], paramMins[1], paramMaxs[1])[0] def muTop1(y, x): return x * float(posterior((x, y))) def muTop2(y, x): return y * float(posterior((x, y))) def mu(num): top = 0 if num == 1: top = scipy.integrate.dblquad(muTop1, paramMins[0], paramMaxs[0], paramMins[1], paramMaxs[1])[0] elif num == 2: top = scipy.integrate.dblquad(muTop2, paramMins[0], paramMaxs[0], paramMins[1], paramMaxs[1])[0] bottom = vol2 return top/bottom mu1 = mu(1) mu2 = mu(2) norm = paramTruthPost/vol2 print(str(pairList[bb]) + "normP(truth): " + str(norm)) def closureTop(y, x): return ((x - paramTruths[0])**2) * ((y - paramTruths[1])**2) * float(posterior((x, y))) def closureBottom(y, x): return ((x - mu1)**2) * ((y - mu2)**2) * float(posterior((x, y))) def closure(): top = scipy.integrate.dblquad(closureTop, paramMins[0], paramMaxs[0], paramMins[1], paramMaxs[1])[0] bottom = scipy.integrate.dblquad(closureBottom, paramMins[0], paramMaxs[0], paramMins[1], paramMaxs[1])[0] return top/bottom close = closure() print(str(pairList[bb]) + "JF Closure: " + str(close)) """ normish = paramTruthPost / vol1 print(str(pairList[bb]) + "normP(truth): " + str(normish)) x1Post = np.multiply(param1_mesh, posterior_array) x2Post = np.multiply(param2_mesh, posterior_array) mu1 = doubleTrap(x1Post, ddxx, ddyy) / vol1 mu2 = doubleTrap(x2Post, ddxx, ddyy) / vol1 topIn1 = np.subtract(param1_mesh, paramTruths[0]) topIn2 = np.subtract(param2_mesh, paramTruths[1]) topInside1 = np.multiply(topIn1, topIn1) topInside2 = np.multiply(topIn2, topIn2) topInWhole = np.multiply(np.multiply(topInside1, topInside2), posterior_array) top = doubleTrap(topInWhole, ddxx, ddyy) bottomIn1 = np.subtract(param1_mesh, mu1) bottomIn2 = np.subtract(param2_mesh, mu2) bottomInside1 = np.multiply(bottomIn1, bottomIn1) bottomInside2 = np.multiply(bottomIn2, bottomIn2) bottomInWhole = np.multiply(np.multiply(bottomInside1, bottomInside2), posterior_array) bottom = doubleTrap(bottomInWhole, ddxx, ddyy) pacquet = top / bottom print(str(pairList[bb]) + "JF Closure: " + str(pacquet)) # Plot the posterior cs = plt.contourf(param1_mesh, param2_mesh, posterior_array, levels=20) cbar = plt.colorbar(cs, label="Posterior") plt.plot([param1_truth], [param2_truth], "D", color='red', ms=10) # plt.figtext(.5, 0.01, subtitle, ha='center') plt.tight_layout() plt.show() """
import numpy as np import pandas as pd from sklearn.gaussian_process import GaussianProcessRegressor as GPR from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C from sklearn.cluster import KMeans from fitting import FittingModel from loader import Loader from submit import SubmitFit, SubmitMolpro from time import time from calc_en import Energy settings = Loader() kernel = C(1.0, (1e-5, 1e5)) * RBF(15, (1e-5, 1e5)) gp = GPR(kernel=kernel, n_restarts_optimizer=9, alpha=1e-6) model = FittingModel() coords, _ = utils.read_data(settings.train_set) with open(settings.desc_file, 'rb') as pickled: X_train = pickle.load(pickled) Y_train = np.zeros(X_train.shape[0]) idx_all = np.arange(X_train.shape[0]) idx_left = copy.deepcopy(idx_all) idx_now = idx_all[~np.in1d(idx_all, idx_left)] # idx_failed = np.ones(idx_all.shape[0], dtype=bool) err_train = np.zeros_like(idx_all, dtype=float) os.makedirs(settings.output, exist_ok=True) os.makedirs(settings.calculations, exist_ok=True)
import numpy as np from matplotlib import pyplot as plt from sklearn.gaussian_process import GaussianProcessRegressor as GPR from sklearn.gaussian_process.kernels import RBF, WhiteKernel as WK,\ ExpSineSquared as ESS, RationalQuadratic as RQ, Matern as M # Specify a GP prior kernel = 1 * RBF(length_scale=1) gp = GPR(kernel=kernel, optimizer=None) print("Initial Kernel\n%s" % kernel) X_test = np.array(np.linspace(-9, 5, 1000), ndmin=2).T f_mean, f_var = gp.predict(X_test, return_std=True) # Create a figure fig_prior = plt.figure(figsize=(20, 12)) plt.rcParams.update({'font.size': 20}) # Draw a mean function and 95% confidence interval plt.plot(X_test, f_mean, 'b-', label='mean function') upper_bound = f_mean + 1.96 * f_var lower_bound = f_mean - 1.96 * f_var plt.fill_between(X_test.ravel(), lower_bound, upper_bound, color='b', alpha=0.1, label='95% confidence interval') # Draw samples from the posterior and plot X_samples = np.array(np.linspace(-9, 5, 30), ndmin=2).T seed = np.random.randint(10) # random seed
def load_data(self, train, test, input_vars=None, interpolation=0, plot=False, plot_dir=None): # train and test of form train = [[2013,5,6,7],[2014,5,6,7]] train_dates = list_dates(train) test_dates = list_dates(test) dates = train_dates + test_dates if input_vars is None: input_vars = self.ground_vars + self.height_grad_vars + \ self.height_level_vars else: input_vars = input_vars if interpolation == 0: pass else: if plot: if plot_dir: save_dir = plot_dir else: plot_dir = os.path.join(os.getcwd(), 'plots', 'interpolation') if not os.path.exists(plot_dir): os.mkdir(plot_dir) folder = 'interpolation_' + str(interpolation) + \ 'train_dates_' + to_string(train) + \ 'test_dates_' + to_string(test) save_dir = os.path.join(plot_dir, folder) if not os.path.exists(save_dir): os.mkdir(save_dir) for date, var in product(dates, input_vars + ['PBLH']): print('fitting interpolation for ' + date + var) X = np.arange(self.data[date + var].shape[0])[:, None] y = self.data[date + var] gp = GPR(kernel=C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2)), n_restarts_optimizer=9, random_state=1337) gp.fit(X, y) x = np.linspace(0, X.shape[0], X.shape[0] * interpolation)[:, None] y_pred, sigma = gp.predict(x, return_std=True) self.data[date + var] = y_pred if plot: plt.clf() plt.plot(X, y, 'r-', lw=2, label=u'real ' + var) plt.plot(x, y_pred, 'b-', lw=1, label=u'interpreted ' + var) plt.plot(X, y, 'ro', label=u'real ' + var) plt.plot(x, y_pred, 'bx', label=u'interpreted ' + var) plt.fill(np.concatenate([x, x[::-1]]), np.concatenate([ y_pred - 1.9600 * sigma, (y_pred + 1.9600 * sigma)[::-1] ]), alpha=.5, fc='b', ec='None', label='95% CI') plt.xlabel(date) plt.ylabel(var) plt.legend(loc='best') plt.savefig(os.path.join(save_dir, date + var + '.jpg')) print('plotted : ' + date + var) for var in input_vars: train_list = [self.data[date + var] for date in train_dates] test_list = [self.data[date + var] for date in test_dates] self.data['train' + var] = np.concatenate(train_list) self.data['test' + var] = np.concatenate(test_list) train_inputs_list = [ self.data['train' + var][:, None] for var in input_vars ] test_inputs_list = [ self.data['test' + var][:, None] for var in input_vars ] y_train_list = [ self.data[date + 'PBLH'][:, None] for date in train_dates ] y_test_list = [ self.data[date + 'PBLH'][:, None] for date in test_dates ] x_train = np.concatenate(train_inputs_list, axis=1) y_train = np.concatenate(y_train_list) x_test = np.concatenate(test_inputs_list, axis=1) y_test = np.concatenate(y_test_list) return x_train, y_train, x_test, y_test
N_F = 3 # num of features # Document features Phi = np.random.randn(N_D, N_F) # Dirichlet priors alpha = 1 beta = 1 # k independent GP priors ls = 1 # length-scale for RBF kernel tau = 1 # Observation noise variance kernel = RBF([ls]*N_F) GPRs = [] for k in range(N_K): GPR_k = GPR(kernel=kernel, alpha=tau) GPR_k = GPR_k.fit(Phi, np.zeros(N_D)) GPRs.append(GPR_k) # ------------------------------------------------------------------------------------- # Laplace bridge # ------------------------------------------------------------------------------------- def gauss2dir(mu, Sigma): K = len(mu) Sigma_diag = np.diag(Sigma) alpha = 1/Sigma_diag * (1 - 2/K + np.exp(mu)/K**2 * np.sum(np.exp(-mu))) return alpha
def visualize_es(initial_design, init=None): """ Visualize one-step of ES. :param initial_design: Method for initializing the GP, choice between 'uniform', 'random', and 'presentation' :param init: Number of datapoints to initialize GP with. :return: None """ # 1. Show GP fit on initial dataset, 0 samples, histogram # 2. Show GP fit on initial dataset, 1 sample, histogram # 3. Show GP fit on initial dataset, 3 samples, histogram # 4. Show GP fit on initial dataset, 50 samples, histogram # 5. Show PDF derived from the histogram at 50 samples # 6. Mark maximum of the PDF as next configuration to be evaluated # a. Plot GP # b. Sample GP, mark minima, update histogram of lambda* # c. Repeat 2 for each sample. # d. Show results after multiple iterations boplot.set_rcparams(**{'figure.figsize': (22, 11)}) # Initial setup # ------------------------------------------- logging.debug("Visualizing ES with initial design {} and init {}".format( initial_design, init)) # Initialize dummy dataset x, y = initialize_dataset(initial_design=initial_design, init=init) logging.debug( "Initialized dataset with:\nsamples {0}\nObservations {1}".format( x, y)) # Fit GP to the currently available dataset gp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(x, y)) gp.fit(x, y) # fit the model histogram_precision = 20 X_ = boplot.get_plot_domain(precision=histogram_precision) nbins = X_.shape[0] logging.info("Creating histograms with {} bins".format(nbins)) bin_range = (bounds['x'][0], bounds['x'][1] + 1 / histogram_precision) # ------------------------------------------- def draw_samples(nsamples, ax1, ax2, show_min=False, return_pdf=False): if not nsamples: return seed2 = 1256 seed3 = 65 mu = gp.sample_y(X=X_, n_samples=nsamples, random_state=seed3) boplot.plot_gp_samples(mu=mu, nsamples=nsamples, precision=histogram_precision, custom_x=X_, show_min=show_min, ax=ax1, seed=seed2) data_h = X_[np.argmin(mu, axis=0), 0] logging.info("Shape of data_h is {}".format(data_h.shape)) logging.debug("data_h is: {}".format(data_h)) bins = ax2.hist(data_h, bins=nbins, range=bin_range, density=return_pdf, color='lightgreen', edgecolor='black', alpha=0.0 if return_pdf else 1.0) return bins # 1. Show GP fit on initial dataset, 0 samples, histogram # ------------------------------------------- ax2_title = r'$p_{min}=P(\lambda=\lambda^*)$' bounds['acq_y'] = (0.0, 1.0) fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True) ax1.set_xlim(bounds['x']) ax1.set_ylim(bounds['gp_y']) ax2.set_xlim(bounds['x']) ax2.set_ylim(bounds['acq_y']) ax1.grid() ax2.grid() boplot.plot_objective_function(ax=ax1) boplot.plot_gp(model=gp, confidence_intervals=[3.0], ax=ax1, custom_x=x) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, ax=ax1) nsamples = 0 draw_samples(nsamples=nsamples, ax1=ax1, ax2=ax2, show_min=True) # Plot uniform prior for p_min xplot = boplot.get_plot_domain() ylims = ax2.get_ylim() xlims = ax2.get_xlim() yupper = [(ylims[1] - ylims[0]) / (xlims[1] - xlims[0])] * xplot.shape[0] ax2.plot(xplot[:, 0], yupper, color='green', linewidth=2.0) ax2.fill_between(xplot[:, 0], ylims[0], yupper, color='lightgreen') ax1.legend().set_zorder(20) ax1.set_xlabel(labels['xlabel']) ax1.set_ylabel(labels['gp_ylabel']) ax1.set_title(r"Visualization of $\mathcal{G}^t$", loc='left') ax2.set_xlabel(labels['xlabel']) ax2.set_ylabel(r'$p_{min}$') ax2.set_title(ax2_title, loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig('es_1') else: plt.show() # ------------------------------------------- # 2. Show GP fit on initial dataset, 1 sample, histogram # ------------------------------------------- bounds['acq_y'] = (0.0, 5.0) ax2_title = r'Frequency of $\lambda=\hat{\lambda}^*$' fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True) ax1.set_xlim(bounds['x']) ax1.set_ylim(bounds['gp_y']) ax2.set_xlim(bounds['x']) ax2.set_ylim(bounds['acq_y']) ax1.grid() ax2.grid() boplot.plot_objective_function(ax=ax1) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, ax=ax1) nsamples = 1 draw_samples(nsamples=nsamples, ax1=ax1, ax2=ax2, show_min=True) ax1.legend().set_zorder(20) ax1.set_xlabel(labels['xlabel']) ax1.set_ylabel(labels['gp_ylabel']) ax1.set_title(r"One sample from $\mathcal{G}^t$", loc='left') ax2.set_xlabel(labels['xlabel']) # ax2.set_ylabel(r'$p_{min}$') ax2.set_ylabel(r'Frequency') ax2.set_title(ax2_title, loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig('es_2') else: plt.show() # 3. Show GP fit on initial dataset, 10 samples, histogram # ------------------------------------------- bounds['acq_y'] = (0.0, 10.0) ax2_title = r'Frequency of $\lambda=\hat{\lambda}^*$' fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True) ax1.set_xlim(bounds['x']) ax1.set_ylim(bounds['gp_y']) ax2.set_xlim(bounds['x']) ax2.set_ylim(bounds['acq_y']) ax1.grid() ax2.grid() boplot.plot_objective_function(ax=ax1) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, ax=ax1) nsamples = 10 draw_samples(nsamples=nsamples, ax1=ax1, ax2=ax2) ax1.set_xlabel(labels['xlabel']) ax1.set_ylabel(labels['gp_ylabel']) ax1.set_title(r"Ten samples from $\mathcal{G}^t$", loc='left') ax2.set_xlabel(labels['xlabel']) # ax2.set_ylabel(r'$p_{min}$') ax2.set_ylabel(r'Frequency') ax2.set_title(ax2_title, loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig('es_3') else: plt.show() # ------------------------------------------- # 4. Show GP fit on initial dataset, 200 samples, histogram # ------------------------------------------- bounds["acq_y"] = (0.0, 20.0) ax2_title = r'Frequency of $\lambda=\hat{\lambda}^*$' fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True) ax1.set_xlim(bounds['x']) ax1.set_ylim(bounds['gp_y']) ax2.set_xlim(bounds['x']) ax2.set_ylim(bounds['acq_y']) ax1.grid() ax2.grid() boplot.plot_objective_function(ax=ax1) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, ax=ax1) nsamples = 200 draw_samples(nsamples=nsamples, ax1=ax1, ax2=ax2) ax1.set_xlabel(labels['xlabel']) ax1.set_ylabel(labels['gp_ylabel']) ax1.set_title(r"200 samples from $\mathcal{G}^t$", loc='left') ax2.set_xlabel(labels['xlabel']) # ax2.set_ylabel(r'$p_{min}$') ax2.set_ylabel(r'Frequency') ax2.set_title(ax2_title, loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig('es_4') else: plt.show() # ------------------------------------------- # 5. Show PDF derived from the histogram at 200 samples # ------------------------------------------- ax2_title = "$\hat{P}(\lambda=\lambda^*)$" bounds["acq_y"] = (0.0, 1.0) fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True) ax1.set_xlim(bounds['x']) ax1.set_ylim(bounds['gp_y']) ax2.set_xlim(bounds['x']) ax2.set_ylim(bounds["acq_y"]) ax1.grid() ax2.grid() boplot.plot_objective_function(ax=ax1) boplot.plot_gp(model=gp, confidence_intervals=[3.0], ax=ax1, custom_x=x) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, ax=ax1) nsamples = 200 seed3 = 65 mu = gp.sample_y(X=X_, n_samples=nsamples, random_state=seed3) data_h = X_[np.argmin(mu, axis=0), 0] kde = kd(kernel='gaussian', bandwidth=0.75).fit(data_h.reshape(-1, 1)) xplot = boplot.get_plot_domain() ys = np.exp(kde.score_samples(xplot)) ax2.plot(xplot, ys, color='green', lw=2.) ax2.fill_between(xplot[:, 0], ax2.get_ylim()[0], ys, color='lightgreen') ax1.set_xlabel(labels['xlabel']) ax1.set_ylabel(labels['gp_ylabel']) ax1.set_title(r"Visualization of $\mathcal{G}^t$", loc='left') ax2.set_xlabel(labels['xlabel']) ax2.set_ylabel(r'$p_{min}$') ax2.set_title(ax2_title, loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig('es_5') else: plt.show() # ------------------------------------------- # 6. Mark maximum of the PDF as next configuration to be evaluated # ------------------------------------------- ax2_title = "$\hat{P}(\lambda=\lambda^*)$" bounds["acq_y"] = (0.0, 1.0) fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True) ax1.set_xlim(bounds['x']) ax1.set_ylim(bounds['gp_y']) ax2.set_xlim(bounds['x']) ax2.set_ylim(bounds["acq_y"]) ax1.grid() ax2.grid() boplot.plot_objective_function(ax=ax1) boplot.plot_gp(model=gp, confidence_intervals=[3.0], ax=ax1, custom_x=x) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, ax=ax1) nsamples = 200 seed3 = 65 mu = gp.sample_y(X=X_, n_samples=nsamples, random_state=seed3) data_h = X_[np.argmin(mu, axis=0), 0] kde = kd(kernel='gaussian', bandwidth=0.75).fit(data_h.reshape(-1, 1)) xplot = boplot.get_plot_domain() ys = np.exp(kde.score_samples(xplot)) idx_umax = np.argmax(ys) boplot.highlight_configuration(x=xplot[idx_umax], label='', ax=ax1, disable_ticks=True) boplot.annotate_x_edge(label=r'$\lambda^{(t)}$', xy=(xplot[idx_umax], ax1.get_ylim()[0]), ax=ax1, align='top', offset_param=1.5) boplot.highlight_configuration(x=xplot[idx_umax], label='', ax=ax2, disable_ticks=True) boplot.annotate_x_edge(label=r'$\lambda^{(t)}$', xy=(xplot[idx_umax], ys[idx_umax]), ax=ax2, align='top', offset_param=1.0) ax2.plot(xplot, ys, color='green', lw=2.) ax2.fill_between(xplot[:, 0], ax2.get_ylim()[0], ys, color='lightgreen') ax1.set_xlabel(labels['xlabel']) ax1.set_ylabel(labels['gp_ylabel']) ax1.set_title(r"Visualization of $\mathcal{G}^t$", loc='left') ax2.set_xlabel(labels['xlabel']) ax2.set_ylabel(r'$p_{min}$') ax2.set_title(ax2_title, loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig('es_6') else: plt.show()
def fit(self, X, y): model = GPR(**self.gpr_init_kwargs) scaler_x = StandardScaler().fit(X) scaler_y = StandardScaler().fit(y.reshape(-1, 1)) model.fit(scaler_x.transform(X), scaler_y.transform(y.reshape(-1, 1))) return TrainedModel(scaler_x, scaler_y, model)
emulator_design_pts_value = np.transpose([ np.ravel(calc_d[obs_name]['param1_mesh']), np.ravel(calc_d[obs_name]['param2_mesh']) ]) # emulator_obs_mean_value=np.ravel(calc_d[obs_name]['mean']) emulator_obs_mean_value = np.ravel(calc_d[obs_name]['mean_plus_noise']) # emulator_y_input_transform=emulator_y_input # print(emulator_design_pts_value) # print(emulator_obs_mean_value) # Fit a GP (optimize the kernel hyperparameters) to each PC. gaussian_process = GPR(kernel=kernel, alpha=0.0001, n_restarts_optimizer=nrestarts, copy_X_train=True).fit(emulator_design_pts_value, emulator_obs_mean_value) # https://github.com/keweiyao/JETSCAPE2020-TRENTO-BAYES/blob/master/trento-bayes.ipynb print('Information on emulator for observable ' + obs_label) print('RBF: ', gaussian_process.kernel.get_params()['k1']) print('White: ', gaussian_process.kernel.get_params()['k2']) print(calc_d[obs_name]['param1_list']) emul_d[obs_name] = { 'gpr': gaussian_process # 'mean':scipy.interpolate.interp2d(calc_d[obs_name]['x_list'], calc_d[obs_name]['y_list'], # np.transpose(calc_d[obs_name]['mean']), kind='linear', copy=True, bounds_error=False, fill_value=None), # 'uncert':scipy.interpolate.interp2d(calc_d[obs_name]['x_list'], calc_d[obs_name]['y_list'], # np.transpose(calc_d[obs_name]['uncert']), kind='linear', copy=True, bounds_error=False, fill_value=None)
def housefeat_extractor(label_img, houseLab = 3, global_feat=False, x=0, y=0, l=0, nSamples=1000, radius=1.0): ''' Extract feature of one label image / generate :param label_img: :param houseLab: :return: ''' # some parameters depth = 4 selem = np.ones([2, 2]) sigma = 0.25 mpp_23 = 0.0187 pos_thr = 0.2 try: H, W = label_img.shape except: H, W, _ = label_img.shape if global_feat and l == 0: raise ValueError('Trying to use global feature, but no level specified') # houseGT = np.zeros([H, W]) houseFeat = np.zeros([H, W, depth]) X, Y = np.meshgrid(np.arange(0, W), np.arange(0, H)) houseRaw = (label_img == houseLab) houseRaw = GF(BNE(houseRaw, selem=selem), sigma=sigma) nohouseRaw = np.bitwise_and(label_img > 0, label_img != houseLab) houseRaw[nohouseRaw] = 0 nohouseRaw = BNE(nohouseRaw, selem=selem) if global_feat: cent = np.r_[X.reshape(-1, 1), Y.reshape(-1, 1)] gps = utils.cent2gps(cent, x, y, l) X = gps[:, 0].reshape(H, W) Y = gps[:, 1].reshape(H, W) houseFeat[..., 0] = X houseFeat[..., 1] = Y j = 2 label_img_new = np.zeros_like(label_img) cnt = 1 for i in range(1, np.max(label_img)+1): raw = (label_img != i) if i != houseLab: label_img_new[label_img == i] = cnt cnt += 1 ed = EDT(raw) if global_feat: ed = ed * mpp_23 * 2 ** (23 - l) houseFeat[..., j] = ed j += 1 xh, yh = np.where(houseRaw > pos_thr) xyh = np.c_[xh, yh, houseRaw[xh, yh]] if xyh.shape[0] == 0: return np.zeros([H, W]), label_img_new.astype(np.uint8) elif xyh.shape[0] < 200: nrepeats = int(200 / xyh.shape[0] + 1) xyh = np.tile(xyh, (nrepeats, 1)) np.random.shuffle(xyh) xnh, ynh = np.where(nohouseRaw == 1) xynh = np.c_[xnh, ynh] if xynh.shape[0] < 2000: return houseRaw, label_img_new.astype(np.uint8) # nrepeats = int(200 / xynh.shape[0] + 1) # xynh = np.tile(xynh, (nrepeats, 1)) np.random.shuffle(xynh) X_sample = np.zeros([2 * nSamples, depth]) y_sample = np.r_[xyh[:nSamples, -1], np.zeros([nSamples,])] print(xyh.shape, xynh.shape) for i in range(depth): xyh_i = (xyh[:nSamples, 0].astype('int'), xyh[:nSamples, 1].astype('int'), i * np.ones([nSamples], dtype=np.int32)) xynh_i = (xynh[:nSamples, 0].astype('int'), xynh[:nSamples, 1].astype('int'), i * np.ones([nSamples], dtype=np.int32)) X_sample[:, i] = np.r_[houseFeat[xyh_i].reshape(-1, 1), houseFeat[xynh_i].reshape(-1, 1)].reshape(-1) gpr = GPR(kernel=RBF(radius, length_scale_bounds="fixed")*C(10.0, constant_value_bounds="fixed"), alpha=1e-5, normalize_y=False) gpr.fit(X_sample/256, y_sample) X_pred = np.zeros([H*W, depth]) y_pred = np.zeros([H, W]) for i in range(depth): X_pred[:, i] = houseFeat[..., i].reshape(-1) for i in range(256): start = int(i * W * (H / 256)) end = int(start + W * (H / 256)) y_pred_tmp = gpr.predict(X_pred[start:end, :]/256) y_pred[int(i*(H/256)):int((i+1)*(H/256))] = y_pred_tmp.reshape(int(H/256), int(W)) y_pred = np.clip(np.tanh(y_pred), 0, 1) return y_pred, label_img_new.astype(np.uint8)
def buildSVR2(test_cog_p, test_inh_p, test_output, test_input): #, cus_alpha): # print ("GPR建模方法,alpha:%f"%(cus_alpha)) print('认知不确定参数矩阵:') print(test_cog_p) print('固有不确定参数') print(test_inh_p) print('参考输入:') print(test_input) print('参考输出矩阵:') print(test_output) print('对比输入:') print(test_cmp_input) print('对比输出矩阵') print(test_cmp_output) y_v = double_loop.outer_level_loop( test_cog_p, test_inh_p, test_output, test_input) # 运行仿真系统获得输出向量,即马氏距离的向量 该输出和认知不确定参数Es_p共同构成训练数据集 y_va = numpy.array(y_v) print('训练输出:') print(y_va) print('最佳参数取值对应输出:') best_p = numpy.mat([[4, 1, 8]]) y_vaa = double_loop.outer_level_loop(best_p, test_inh_p, test_output, test_input) print(y_vaa) test_cog_pa = numpy.array(test_cog_p) # X_train, X_test, y_train, y_test = train_test_split(test_cog_pa, y_va, test_size=.4, random_state=0) # gpr = GaussianProcessRegressor(alpha=cus_alpha).fit(X_train, y_train) # # print 'score:' # gpr.score(X_test, y_test) tuned_parameters = [{ 'kernel': [ 1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0)), 1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1), # 1.0 * ExpSineSquared(length_scale=1.0, periodicity=3.0, # length_scale_bounds=(0.1, 10.0), # periodicity_bounds=(1.0, 10.0)), ConstantKernel(0.1, (0.01, 10.0)) * (DotProduct(sigma_0=1.0, sigma_0_bounds=(0.0, 10.0))**2), 1.0 * Matern(length_scale=1.0, length_scale_bounds=(1e-1, 10.0), nu=1.5) ], 'alpha': [1E-10, 0.1, 1] }] X_train, X_test, y_train, y_test = train_test_split(test_cog_pa, y_va, test_size=0.5, random_state=0) print "建立超参数搜索模型" clf = GridSearchCV(GPR(), tuned_parameters) print '开始搜索' clf.fit(X_train, y_train) print '搜索结束' print "在参数集上搜索得到的最佳参数组合为:" print clf.best_params_ print "在参数集上每个参数组合得得分为:" means = clf.cv_results_['mean_test_score'] stds = clf.cv_results_['std_test_score'] for mean, std, params in zip(means, stds, clf.cv_results_['params']): print "%0.3f (+/-%0.03f) for %r" % (mean, std * 2, params) # print '最优值对应得预测输出:' # print clf.predict(best_p) best_pred = clf.predict(best_p) y_pred = clf.predict(X_test) plt.plot(y_pred, 'r') plt.plot(y_test, 'g') plt.plot(best_pred, 'b.') plt.show() # X_train, X_test, y_train, y_test = train_test_split(test_cog_pa, y_va, test_size=.4, random_state=0) # # gpr = GaussianProcessRegressor(alpha=cus_alpha).fit(X_train, y_train) # # y_predict = gpr.predict(X_test) # # plt.plot(y_train, 'r') # plt.plot(y_predict, 'b') # plt.plot(y_test, 'g') # plt.show() return clf
def calculate_GPR_learnable(self, X_train, y_train): gpr = GPR(kernel=self.kernel_learnable, alpha=0.0).fit(X_train, y_train) return y_train.reshape(-1, 1) - gpr.predict(X_train).reshape(-1, 1)
def train_classifier(self, plot_classification=True): #load data, train classifier. Try KLR operator_name = "PushInDir" fit_gp = False fit_rfr = True fn = self.agent_cfg["data_path"] + operator_name + ".npy" data = np.load(fn, allow_pickle=True).all() actual_states = data["actual_next_states"] expected_states = data["expected_next_states"] state = data["states"] actions = data["actions"] input_vec = np.hstack([state, actions]) std_vec = np.std(input_vec, axis=0) std_thresh = 0.01 varying_input_vec = input_vec[:, std_vec > std_thresh] only_color = False varying_input_vec = varying_input_vec[:, [ 1, 0, 2 ]] #for plotting, will give worse results 2,3,4 X = varying_input_vec[:] if only_color: varying_input_vec = varying_input_vec[:, [2]] scaler = preprocessing.StandardScaler().fit(varying_input_vec) input_vec_scaled = scaler.transform(varying_input_vec) deviations = np.linalg.norm(actual_states[:, :3] - expected_states[:, :3], axis=1) #kernel = C(1.0, (1e-3, 1e3)) * Matern(5,(1, 5), nu=2.5) kernel = C(1.0, (1e-3, 1e3)) * Matern(0.5, (0.5, 5), nu=2.5) #kernel = C(1.0, (1e-3, 1e3)) * RBF(1, (1e-3, 1e2)) #[0 and 1 ] of the varying version will be the interesting part gp = GPR(kernel=kernel, n_restarts_optimizer=20) rfr = self.setup_random_forest() if fit_rfr: rfr.fit(input_vec_scaled, deviations) deviations_pred = rfr.predict(input_vec_scaled) if fit_gp: gp.fit(input_vec_scaled, deviations) deviations_pred, sigma = gp.predict(input_vec_scaled, return_std=True) #plot using 2D print("Error", np.linalg.norm(deviations_pred - deviations)) # Input space if plot_classification: from autolab_core import YamlConfig import matplotlib.patches cfg = YamlConfig("cfg/franka_block_push_two_d.yaml") self._board_names = [ name for name in cfg.keys() if "boardpiece" in name ] fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) for _board_name in self._board_names: color = cfg[_board_name]['rb_props']["color"] pose = [ cfg[_board_name]["pose"]["x"], cfg[_board_name]["pose"]["y"] ] dims = [ cfg[_board_name]["dims"]["depth"], cfg[_board_name]["dims"]["width"] ] #draw them. pose[0] -= (dims[0] / 2.) pose[1] -= (dims[1] / 2.) patch = matplotlib.patches.Rectangle(pose, dims[0], dims[1], fill=False, edgecolor=color + [ 0.8, ], linewidth=20) ax.add_patch(patch) y = deviations num_pts = 50 x1 = np.linspace(X[:, 0].min() - 0.05, X[:, 0].max() + 0.05, num_pts) # p x2 = np.linspace(X[:, 1].min() - 0.05, X[:, 1].max() + 0.05, num_pts) # q x = (np.array([x1, x2])).T x1x2 = np.array(list(product(x1, x2))) x1x2_incl_color = np.zeros((x1x2.shape[0], X.shape[1])) x1x2_incl_color[:, :2] = x1x2 for i in range(x1x2.shape[0]): x1x2_incl_color[i, 2:] = color_block_is_on( cfg, np.array([x1x2[i, 1], 0, x1x2[i, 0]]))[0] #match the expected pose layout #get corresponding colors here if only_color: if fit_gp: y_pred, MSE = gp.predict(scaler.transform( x1x2_incl_color[:, 1:2]).reshape(-1, 1), return_std=True) else: if fit_gp: y_pred, MSE = gp.predict(scaler.transform(x1x2_incl_color), return_std=True) if fit_rfr: y_pred = rfr.predict(scaler.transform(x1x2_incl_color)) X0p, X1p = x1x2[:, 0].reshape(num_pts, num_pts), x1x2[:, 1].reshape( num_pts, num_pts) Zp = np.reshape(y_pred, (num_pts, num_pts)) # alternative way to generate equivalent X0p, X1p, Zp # X0p, X1p = np.meshgrid(x1, x2) # Zp = [gp.predict([(X0p[i, j], X1p[i, j]) for i in range(X0p.shape[0])]) for j in range(X0p.shape[1])] # Zp = np.array(Zp).T block_shift = 0 #0.07/2 ax.pcolormesh(X0p - block_shift, X1p - block_shift, Zp, cmap="Greens", vmin=-0.01) #ax.scatter(X0p, X1p, c=Zp, cmap="Greens", vmin=-0.1) plot = ax.scatter(X[:, 0] - block_shift, X[:, 1] - block_shift, c=deviations, cmap="Greens", vmin=-0.01, edgecolor="k") #ax.scatter(X[:,0], X[:,1], c=deviations_pred, cmap="Greens", vmin=-0.05) x_max = 0.3 x_min = -0.3 z_max = 0.6 z_min = 0.1 plt.xlim([x_max, x_min]) plt.ylim([z_max, z_min]) plt.colorbar(plot) #fig.colorbar(ax) plt.show()
def __init__(self, target, NObj, pbounds, constraints=[], verbose=False, Picture=False, TPF=None, n_restarts_optimizer=10, Filename=None, MetricsPS=True, max_or_min='max', RandomSeed=None): """Bayesian optimization object Keyword Arguments: target -- functions to be optimized def target(x): x is a np.array return [f_1, f_2, ..., f_NObj] NObj -- int, Number of objective functions pbounds -- numpy array with bounds for each parameter pbounds.shape == (NParam,2) constraints -- list of dictionary with constraints [{'type': 'ineq', 'fun': constr_fun}, ...] def constr_fun(x): return g(x) # >= 0 verbose -- Whether or not to print progress (default False) Picture -- bool (default True) whether or not to plot PF convergence, for NObj = 2 only TPF -- np.ndarray (default None) Array with the True Pareto Front for calculation of convergence metrics n_restarts_optimizer -- int (default 100) GP parameter, the number of restarts of the optimizer for finding the kernel’s parameters which maximize the log-marginal likelihood. Filename -- string (default None) Partial metrics will be saved at filename, if None nothing is saved MetricsPS -- bool (default True) whether os not to calculate metrics with the Pareto Set points max_or_min -- str (default 'max') whether the optimization problem is a maximization problem ('max'), or a minimization one ('min') RandomSeed -- {None, int, array_like}, optional Random seed used to initialize the pseudo-random number generator. Can be any integer between 0 and 2**32 - 1 inclusive, an array (or other sequence) of such integers, or None (the default). If seed is None, then RandomState will try to read data from /dev/urandom (or the Windows analogue) if available or seed from the clock otherwise. Based heavily on github.com/fmfn/BayesianOptimization """ super(MOBayesianOpt, self).__init__() self.verbose = verbose self.vprint = print if verbose else lambda *a, **k: None self.counter = 0 self.constraints = constraints self.n_rest_opt = n_restarts_optimizer self.Filename = Filename self.MetricsPS = MetricsPS # reset calling variables self.__reset__() # number of objective functions if isinstance(NObj, int): self.NObj = NObj else: raise TypeError("NObj should be int") if Picture and self.NObj == 2: self.Picture = Picture else: if Picture: warn("NObj must be 2 to plot PF convergence") self.Picture = False # objective function returns lists w/ the multiple target functions if callable(target): self.target = max_or_min_wrapper(target, max_or_min) else: raise TypeError("target should be callable") self.pbounds = pbounds # pbounds must hold the bounds for each parameter try: self.NParam = len(pbounds) except TypeError: raise TypeError("pbounds is neither a np.array nor a list") if self.pbounds.shape != (self.NParam, 2): raise IndexError("pbounds must have 2nd dimension equal to 2") self.vprint(f"Dim. of Search Space = {self.NParam}") if TPF is None: self.vprint("no metrics are going to be saved") self.Metrics = False else: self.vprint("metrics are going to be saved") self.Metrics = True self.TPF = TPF if self.Filename is not None: self.__save_partial = True self.vprint("Filename = " + self.Filename) self.FF = open(Filename, "a", 1) self.vprint("Saving:") self.vprint("NParam, iter, N init, NFront," "GenDist, SS, HV, HausDist, Cover, GDPS, SSPS," "HDPS, NewProb, q, FrontFilename") else: self.__save_partial = False self.GP = [None] * self.NObj for i in range(self.NObj): self.GP[i] = GPR(kernel=Matern(nu=1.5), n_restarts_optimizer=self.n_rest_opt) # store starting points self.init_points = [] # test for constraint types for cc in self.constraints: if cc['type'] == 'eq': raise ConstraintError( "Equality constraints are not implemented") self.space = TargetSpace(self.target, self.NObj, self.pbounds, self.constraints, RandomSeed=RandomSeed, verbose=self.verbose) if self.Picture and self.NObj == 2: self.fig, self.ax = pl.subplots(1, 1, figsize=(5, 4)) self.fig.show() return
def __init__(self, D=1, C_count=2, random_state=0, **kwargs): self.GP = GPR(**kwargs) self.D = D self.C_count = C_count self.random_state = random_state
# For simplicity, we sample the emulator uniformly x_design = np.linspace(xmin, xmax - step, num=number_design_emulator) y_design = [observable_1(x) for x in x_design] # plot the design points, along with the full function fig, ax = plt.subplots(1, 1, figsize=(4, 3)) ax.plot(x_design, y_design, 'ro', label='Design') x = np.linspace(xmin, xmax, 2001) kernel = ( 1. * kernels.RBF(length_scale=.2, length_scale_bounds=(.05, .5)) # + kernels.ConstantKernel() + kernels.WhiteKernel(noise_level=1., noise_level_bounds=(1e-5, 1e5))) gp = GPR(kernel=kernel, n_restarts_optimizer=5, copy_X_train=False) gp.fit(np.atleast_2d(x_design).T, y_design) print("C^2 = ", gp.kernel_.get_params()['k1']) print(gp.kernel_.get_params()['k2']) def predict(x, gpx): mean, cov = gpx.predict(return_cov=True, X=np.atleast_2d(x).T) return mean, np.sqrt(np.diag(cov)) xArange = np.linspace(xmin, xmax, 201) y, ystd = predict(xArange, gp) figNew, ax = plt.subplots(1, 1, figsize=(5, 4)) ax.plot(x_design, y_design, 'ro', label='Design') # ax.plot(x, [observable_1(d) for d in x],'k-', label=r'$F(x)$')
##################################################################################################### #Model=Regression_Method_Evaluator([0,1,2,3,4,5],[1],GPR(normalize_y=True),Exp_File_Name='Exp_x25_z30_ComfortOnly.npy', plot=False,Return_Model=True) #Regression_Method_Evaluator([2],[5],LR(), plot=True,Duration_Limit=20) #Plot_Direction_Reward(Model,2.5,3) #optimize.brute(Target_Function,(slice(0,2*np.pi,2*np.pi/360),slice(0,420,10))) x = [5, 10, 15, 20, 25, 30, 35] z = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55] z = [5] for j in z: for i in x: filename = 'Exp_x' + str(i) + '_z' + str(j) + '_ComfortOnly.npy' Model = Regression_Method_Evaluator([0, 1, 2, 3], [4], GPR(normalize_y=True), Exp_File_Name=filename, plot=False, Return_Model=True) print('x=', i / 10, 'z=', j / 10) a = RelativeAngle_DurationRequired(i / 10, j / 10, Model, Min_Comfort_Level=0.95, Plot=True) # print(j) # plt.show() # plt.plot(a,Prediction_Test) # plt.plot(a,Testing_Labels) # plt.show()
def visualize_lcb(initial_design, init=None): """ Visualize one-step of LCB. :param initial_design: Method for initializing the GP, choice between 'uniform', 'random', and 'presentation' :param init: Number of datapoints to initialize GP with. :return: None """ # 1. Plot 3 different confidence envelopes # 2. Plot only LCB for one confidence envelope # 3. Mark next sample boplot.set_rcparams(**{'legend.loc': 'lower right'}) logging.debug("Visualizing PI with initial design {} and init {}".format( initial_design, init)) # Initialize dummy dataset x, y = initialize_dataset(initial_design=initial_design, init=init) logging.debug( "Initialized dataset with:\nsamples {0}\nObservations {1}".format( x, y)) # Fit GP to the currently available dataset gp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(x, y)) gp.fit(x, y) # fit the model # noinspection PyStringFormat logging.debug( "Model fit to dataset.\nOriginal Inputs: {0}\nOriginal Observations: {1}\n" "Predicted Means: {2}\nPredicted STDs: {3}".format( x, y, *(gp.predict(x, return_std=True)))) # 1. Plot 3 different confidence envelopes # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[1.0, 2.0, 3.0], type='both', custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=False, highlight_datapoint=None, highlight_label=None, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig("lcb_1.pdf") else: plt.show() # ------------------------------------------- # 2. Plot only LCB for one confidence envelope # -------------Plotting code ----------------- boplot.set_rcparams(**{'legend.loc': 'upper left'}) kappa = 3.0 fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[kappa], type='lower', custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') plt.tight_layout() if TOGGLE_PRINT: plt.savefig("lcb_2.pdf") else: plt.show() # ------------------------------------------- # 3. Show LCB in parallel # -------------Plotting code ----------------- if TOGGLE_PRINT: fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True, figsize=(18, 9)) else: fig, (ax1, ax2) = plt.subplots(2, 1, squeeze=True) ax1.set_xlim(bounds["x"]) ax1.set_ylim(bounds["gp_y"]) ax1.grid() boplot.plot_gp(model=gp, confidence_intervals=[kappa], type='lower', custom_x=x, ax=ax1) boplot.plot_objective_function(ax=ax1) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax1) lcb_max = get_lcb_maximum(gp, kappa) logging.info("LCB Maximum at:{}".format(lcb_max)) boplot.highlight_configuration(x=lcb_max[0], label=None, lloc='bottom', ax=ax1) ax1.set_xlabel(labels['xlabel']) ax1.set_ylabel(labels['gp_ylabel']) ax1.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') ax2.set_xlim(bounds["x"]) ax2.set_ylim(bounds["acq_y"]) ax2.grid() ax2.set_xlabel(labels['xlabel']) ax2.set_ylabel(labels['acq_ylabel']) ax2.set_title(r"Visualization of $LCB$", loc='left') boplot.highlight_configuration(x=lcb_max[0], label=None, lloc='bottom', ax=ax2) boplot.plot_acquisition_function(acquisition_functions['LCB'], 0.0, gp, kappa, ax=ax2) plt.tight_layout() if TOGGLE_PRINT: plt.savefig("lcb_3.pdf") else: plt.show() # ------------------------------------------- # 4. Mark next sample # -------------Plotting code ----------------- fig, ax = plt.subplots(1, 1, squeeze=True) ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) ax.grid() boplot.plot_gp(model=gp, confidence_intervals=[3.0], type='lower', custom_x=x, ax=ax) boplot.plot_objective_function(ax=ax) boplot.mark_observations(X_=x, Y_=y, mark_incumbent=True, highlight_datapoint=None, highlight_label=None, ax=ax) lcb_max = get_lcb_maximum(gp, 3.0) logging.info("LCB Maximum at:{}".format(lcb_max)) boplot.highlight_configuration(x=lcb_max[0], label=None, lloc='bottom', ax=ax) boplot.highlight_output(y=lcb_max[1], label='', lloc='left', ax=ax) boplot.annotate_y_edge(label=r'${\hat{c}}^{(t)}(%.2f)$' % lcb_max[0], xy=lcb_max, align='left', ax=ax) ax.legend().set_zorder(20) ax.set_xlabel(labels['xlabel']) ax.set_ylabel(labels['gp_ylabel']) ax.set_title(r"Visualization of $\mathcal{G}^{(t)}$", loc='left') ax.legend().remove() plt.tight_layout() if TOGGLE_PRINT: plt.savefig("lcb_4.pdf") else: plt.show()
cube[:, nPF:] = np.sort(cube[:, nPF:]) pca = PCA(n_components=inv.nL) pca.fit(cube[np.argsort(rms)[:200]]) Leig = pca.components_ index = np.argsort(rms) rms = rms[index] cube = cube[index, :] rms = rms[::100] cube = cube[::100, :] # Instanciate a Gaussian Process model #gp = GaussianProcess(corr='cubic', theta0=1e-2,thetaL=1e-4,thetaU=1e-1) gp = GPR() #corr='squared_exponential',theta0=1e-2,thetaL=1e-4,thetaU=1e-1 gp.fit(cube, rms) npoint = 2e3 lo = np.linspace(0, 1, int(np.sqrt(npoint))) l1 = np.linspace(0, 1, int(np.sqrt(npoint))) Lo, L1 = np.meshgrid(lo, l1, indexing='ij') x = np.array([Lo.reshape(-1), L1.reshape(-1)]) y = gp.predict(x.T) pl.pcolor(lo, l1, y.reshape(len(lo), len(l1)).T, cmap=cm.Spectral) CB = pl.colorbar() CS = pl.contour(lo, l1, y.reshape(len(lo), len(l1)).T,
def buildGPR(snb, cog_p, inh_p, output1, input_v1, n_id): y_v = DoubleLoop.outer_level_loop(cog_p, inh_p, output1, input_v1) y_va = numpy.array(y_v) cog_pa = numpy.array(cog_p) tuned_parameters = [{ 'kernel': [ 1.0 * RBF(length_scale=1.0, length_scale_bounds=(1e-1, 10.0)), 1.0 * RationalQuadratic(length_scale=1.0, alpha=0.1), ConstantKernel(0.1, (0.01, 10.0)) * (DotProduct(sigma_0=1.0, sigma_0_bounds=(0.0, 10.0))**2), 1.0 * Matern(length_scale=1.0, length_scale_bounds=(1e-1, 10.0), nu=1.5) ], 'alpha': [1E-10, 0.1, 1] }] X_train, X_test, y_train, y_test = train_test_split(cog_pa, y_va, test_size=0.5, random_state=0) showlog = '' showlog = showlog + '建立超参数搜索模型' + '\n' clf = GridSearchCV(GPR(), tuned_parameters) showlog = showlog + '开始搜索' + '\n' clf.fit(X_train, y_train) showlog = showlog + '搜索结束' + '\n' showlog = showlog + '在参数集上搜索得到的最佳参数组合为' + '\n' #print '在参数集上搜索得到的最佳参数组合为' showlog = showlog + '%r' % (clf.best_params_) + '\n' #print clf.best_params_ showlog = showlog + '在参数集上每个参数组合得得分为' + '\n' means = clf.cv_results_['mean_test_score'] stds = clf.cv_results_['std_test_score'] for mean, std, params in zip(means, stds, clf.cv_results_['params']): showlog = showlog + "%0.3f (+/-%0.03f) for %r" % (mean, std * 2, params) + '\n' show_panel = snb.scrolledWindow grid = snb.grid_out grid.CreateGrid(1, len(y_v)) grid.SetRowLabelValue(0, '一致性') grid.SetRowLabelAlignment(horiz=wx.ALIGN_TOP, vert=wx.ALIGN_TOP) for i in range(len(y_v)): grid.SetColLabelValue(i, '%d' % (i + 1)) grid.SetCellValue(0, i, str(round(y_v[i], 3))) # csw = snb.sw # csw.text_ctrl.SetValue(showlog) best_p = rm_new.cog_p_r best_pred = clf.predict(best_p) y_pred = clf.predict(X_test) #保存到数据库 Sql.insert_metamodel(n_id, "gpr", clf) axes = snb.axes canvas = snb.canvas lpred, = axes.plot(y_pred, 'r', label='predict value') ltest, = axes.plot(y_test, 'g', label='real value') axes.plot(best_pred, 'b.') axes.legend(handles=[lpred, ltest], labels=['predict value', 'real value']) canvas.draw() # show_panel.SetupScrolling() show_panel.Layout() cp.sym2 = 1 dlg = wx.MessageDialog(None, message='元模型建模已经完成') dlg.ShowModal() # plt.plot(y_pred, 'r') # plt.plot(y_test, 'g') # plt.plot(best_pred, 'b.') # plt.title('Prediction accuracy diagram') # plt.show() return clf
def __init__(self, system_str, npc, nrestarts=2): print("Emulators for system " + system_str) print("with viscous correction type {:d}".format(idf)) print("NPC : " + str(npc)) print("Nrestart : " + str(nrestarts)) #list of observables is defined in calculations_file_format_event_average #here we get their names and sum all the centrality bins to find the total number of observables nobs self.nobs = 0 self.observables = [] self._slices = {} for obs, cent_list in obs_cent_list[system_str].items(): #for obs, cent_list in calibration_obs_cent_list[system_str].items(): self.observables.append(obs) n = np.array(cent_list).shape[0] self._slices[obs] = slice(self.nobs, self.nobs + n) self.nobs += n print("self.nobs = " + str(self.nobs)) #read in the model data from file print("Loading model calculations from " \ + SystemsInfo[system_str]['main_obs_file']) # things to drop delete = [] # build a matrix of dimension (num design pts) x (number of observables) Y = [] for ipt, data in enumerate(trimmed_model_data[system_str]): row = np.array([]) for obs in self.observables: #n_bins_bayes = len(calibration_obs_cent_list[system_str][obs]) # only using these bins for calibration #values = np.array(trimmed_model_data[system_str][pt, idf][obs]['mean'][:n_bins_bayes] ) values = np.array(data[idf][obs]['mean']) if np.isnan(values).sum() > 0: print( "WARNING! FOUND NAN IN MODEL DATA WHILE BUILDING EMULATOR!" ) print("Design pt = " + str(pt) + "; Obs = " + obs) row = np.append(row, values) Y.append(row) Y = np.array(Y) print("Y_Obs shape[Ndesign, Nobs] = " + str(Y.shape)) #Principal Components self.npc = npc self.scaler = StandardScaler(copy=False) #whiten to ensure uncorrelated outputs with unit variances self.pca = PCA(copy=False, whiten=True, svd_solver='full') # Standardize observables and transform through PCA. Use the first # `npc` components but save the full PC transformation for later. Z = self.pca.fit_transform( self.scaler.fit_transform(Y) )[:, : npc] # save all the rows (design points), but keep first npc columns design, design_max, design_min, labels = prepare_emu_design(system_str) #delete undesirable data if len(delete_design_pts_set) > 0: print("Warning! Deleting " + str(len(delete_design_pts_set)) + " points from data") design = np.delete(design, list(delete_design_pts_set), 0) ptp = design_max - design_min print("Design shape[Ndesign, Nparams] = " + str(design.shape)) # Define kernel (covariance function): # Gaussian correlation (RBF) plus a noise term. # noise term is necessary since model calculations contain statistical noise k0 = 1. * kernels.RBF( length_scale=ptp, length_scale_bounds=np.outer(ptp, (4e-1, 1e2)), #nu = 3.5 ) k1 = kernels.ConstantKernel() k2 = kernels.WhiteKernel(noise_level=.1, noise_level_bounds=(1e-2, 1e2)) #kernel = (k0 + k1 + k2) #this includes a consant kernel kernel = (k0 + k2) # this does not # Fit a GP (optimize the kernel hyperparameters) to each PC. self.gps = [] for i, z in enumerate(Z.T): print("Fitting PC #", i) self.gps.append( GPR(kernel=kernel, alpha=0.1, n_restarts_optimizer=nrestarts, copy_X_train=False).fit(design, z)) for n, (z, gp) in enumerate(zip(Z.T, self.gps)): print("GP " + str(n) + " score : " + str(gp.score(design, z))) print("Constructing full linear transformation matrix") # Construct the full linear transformation matrix, which is just the PC # matrix with the first axis multiplied by the explained standard # deviation of each PC and the second axis multiplied by the # standardization scale factor of each observable. self._trans_matrix = (self.pca.components_ * np.sqrt( self.pca.explained_variance_[:, np.newaxis]) * self.scaler.scale_) # Pre-calculate some arrays for inverse transforming the predictive # variance (from PC space to physical space). # Assuming the PCs are uncorrelated, the transformation is # # cov_ij = sum_k A_ki var_k A_kj # # where A is the trans matrix and var_k is the variance of the kth PC. # https://en.wikipedia.org/wiki/Propagation_of_uncertainty print("Computing partial transformation for first npc components") # Compute the partial transformation for the first `npc` components # that are actually emulated. A = self._trans_matrix[:npc] self._var_trans = np.einsum('ki,kj->kij', A, A, optimize=False).reshape(npc, self.nobs**2) # Compute the covariance matrix for the remaining neglected PCs # (truncation error). These components always have variance == 1. B = self._trans_matrix[npc:] self._cov_trunc = np.dot(B.T, B) # Add small term to diagonal for numerical stability. self._cov_trunc.flat[::self.nobs + 1] += 1e-4 * self.scaler.var_
from sklearn.metrics import mean_squared_error as mse from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler data_train = np.loadtxt('data_train.csv', delimiter=",", dtype="float") x = data_train[..., 0:20] ss = MinMaxScaler() x = ss.fit_transform(x) y = data_train[..., 20] kernel = ConstantKernel(0.1, (0.001, 0.1)) * RBF(0.5, (1e-4, 10)) x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=16) model = GPR(kernel=kernel) model.fit(x_train, y_train) rmse = np.sqrt(mse(y_train, model.predict(x_train))) r2 = r2_score(y_train, model.predict(x_train)) rmset = np.sqrt(mse(y_test, model.predict(x_test))) r2t = r2_score(y_test, model.predict(x_test)) print(model.predict(x_test)) print(y_test) print(rmse) print(r2) print(rmset) print(r2t)
def __init__(self, system, npc=10, nrestarts=0): logging.info('training emulator for system %s (%d PC, %d restarts)', system, npc, nrestarts) Y = [] self._slices = {} # Build an array of all observables to emulate. nobs = 0 for obs, subobslist in self.observables: self._slices[obs] = {} for subobs in subobslist: Y.append(model.data[system][obs][subobs]['Y']) n = Y[-1].shape[1] self._slices[obs][subobs] = slice(nobs, nobs + n) nobs += n Y = np.concatenate(Y, axis=1) self.npc = npc self.nobs = nobs self.scaler = StandardScaler(copy=False) self.pca = PCA(copy=False, whiten=True, svd_solver='full') # Standardize observables and transform through PCA. Use the first # `npc` components but save the full PC transformation for later. Z = self.pca.fit_transform(self.scaler.fit_transform(Y))[:, :npc] # Define kernel (covariance function): # Gaussian correlation (RBF) plus a noise term. design = Design(system) ptp = design.max - design.min kernel = (1. * kernels.RBF(length_scale=ptp, length_scale_bounds=np.outer(ptp, (.1, 10))) + kernels.WhiteKernel(noise_level=.1**2, noise_level_bounds=(.01**2, 1))) # Fit a GP (optimize the kernel hyperparameters) to each PC. self.gps = [ GPR(kernel=kernel, alpha=0, n_restarts_optimizer=nrestarts, copy_X_train=False).fit(design, z) for z in Z.T ] # Construct the full linear transformation matrix, which is just the PC # matrix with the first axis multiplied by the explained standard # deviation of each PC and the second axis multiplied by the # standardization scale factor of each observable. self._trans_matrix = (self.pca.components_ * np.sqrt( self.pca.explained_variance_[:, np.newaxis]) * self.scaler.scale_) # Pre-calculate some arrays for inverse transforming the predictive # variance (from PC space to physical space). # Assuming the PCs are uncorrelated, the transformation is # # cov_ij = sum_k A_ki var_k A_kj # # where A is the trans matrix and var_k is the variance of the kth PC. # https://en.wikipedia.org/wiki/Propagation_of_uncertainty # Compute the partial transformation for the first `npc` components # that are actually emulated. A = self._trans_matrix[:npc] self._var_trans = np.einsum('ki,kj->kij', A, A, optimize=False).reshape(npc, nobs**2) # Compute the covariance matrix for the remaining neglected PCs # (truncation error). These components always have variance == 1. B = self._trans_matrix[npc:] self._cov_trunc = np.dot(B.T, B) # Add small term to diagonal for numerical stability. self._cov_trunc.flat[::nobs + 1] += 1e-4 * self.scaler.var_
def visualize_look_ahead(initial_design, init=None): """ Visualize one-step of look-ahead. :param initial_design: Method for initializing the GP, choice between 'uniform', 'random', and 'presentation' :param init: Number of datapoints to initialize GP with. :return: None """ # boplot.set_rcparams(**{'legend.loc': 'lower left'}) logging.debug( "Visualizing Look-Ahead with initial design {} and init {}".format( initial_design, init)) # Initialize dummy dataset x, y = initialize_dataset(initial_design=initial_design, init=init) logging.debug( "Initialized dataset with:\nsamples {0}\nObservations {1}".format( x, y)) # Fit GP to the currently available dataset gp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(x, y)) gp.fit(x, y) # fit the model mu_star_t_xy = get_mu_star(gp) logging.info("Mu-star at time t: {}".format(mu_star_t_xy)) # noinspection PyStringFormat logging.debug( "Model fit to dataset.\nOriginal Inputs: {0}\nOriginal Observations: {1}\n" "Predicted Means: {2}\nPredicted STDs: {3}".format( x, y, *(gp.predict(x, return_std=True)))) # Assume next evaluation location x_ = np.array([[5.0]]) print(x_) y_ = f(x_[0]) # Update dataset with new observation X2_ = np.append(x, x_, axis=0) Y2_ = y + [y_] logging.info("x: {}, y: {}".format(x_, y_)) # Fit GP to the updated dataset gp2 = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(X2_, Y2_)) gp2.fit(X2_, Y2_) # fit the model mu_star_t1_xy = get_mu_star(gp2) logging.info("Mu-star at time t+1: {}".format(mu_star_t1_xy)) # -------------------------Plotting madness begins--------------------------- def draw_basic_figure(tgp=gp, tx=x, tX_=x, tY_=y, title='', highlight_datapoint=None, highlight_label="", ax=None): if ax is None: fig, ax = plt.subplots(1, 1, squeeze=True) plt.subplots_adjust(0.05, 0.15, 0.95, 0.85) figflag = True else: figflag = False ax.set_xlim(bounds["x"]) ax.set_ylim(bounds["gp_y"]) if title: ax.set_title(title, loc='left') ax.grid() boplot.plot_objective_function(ax=ax) boplot.plot_gp(model=tgp, confidence_intervals=[1.0, 2.0, 3.0], ax=ax, custom_x=tx) if highlight_datapoint: boplot.mark_observations(X_=tX_, Y_=tY_, mark_incumbent=False, ax=ax, highlight_datapoint=highlight_datapoint, highlight_label=highlight_label) else: boplot.mark_observations(X_=tX_, Y_=tY_, mark_incumbent=False, ax=ax) if figflag: return fig, ax else: return ax def perform_finishing_tasks(ax, filename="", remove_legend=True): ax.legend().set_zorder(zorders['legend']) ax.set_xlabel(labels['xlabel']) if remove_legend: ax.legend().remove() # plt.tight_layout() if TOGGLE_PRINT: # plt.savefig(f"{OUTPUT_DIR}/{filename}", bbox_inches='tight') plt.savefig(f"{OUTPUT_DIR}/{filename}") else: plt.show() # --------------------------------------- # Draw look ahead 1. labels['gp_mean'] = r'Mean: $\mu^{(t)}(\cdot)$' fig, ax = draw_basic_figure(title="") perform_finishing_tasks(ax=ax, filename="look_ahead_1.pdf", remove_legend=False) # --------------------------------------- # Draw look ahead 2 fig, ax = draw_basic_figure(title="") logging.debug("Placing vertical on configuration: {}".format(x_)) # boplot.highlight_configuration(x=x_, label='', lloc='bottom', ax=ax, ha='center') boplot.highlight_configuration(x=x_, label='', lloc='bottom', ax=ax, disable_ticks=True) boplot.annotate_x_edge(label=r'$\lambda$', xy=(x_, y_), align='bottom', ax=ax) perform_finishing_tasks(ax=ax, filename="look_ahead_2.pdf", remove_legend=True) # --------------------------------------- # Draw look ahead 3 fig, ax = draw_basic_figure(title="") boplot.highlight_configuration(x=x_, label='', lloc='bottom', ax=ax, ha='right') boplot.annotate_x_edge(label=r'$\lambda$', xy=(x_, y_), align='bottom', ax=ax) boplot.highlight_output(y_, label='', lloc='right', ax=ax, fontsize=28) boplot.annotate_y_edge(label=r'$c(\lambda)$', xy=(x_, y_), align='right', ax=ax) ax.scatter(x_, y_, color=colors['highlighted_observations'], marker='X', label=r"Hypothetical Observation $<\lambda, c(\lambda)>$", zorder=zorders['annotations_normal']) perform_finishing_tasks(ax=ax, filename="look_ahead_3.pdf", remove_legend=True) # --------------------------------------- # Draw look ahead 4. labels['gp_mean'] = r'Mean: $\mu^{(t+1)}(\cdot)|_\lambda$' fig, ax = draw_basic_figure( tgp=gp2, tx=x, tX_=X2_, tY_=Y2_, title='', highlight_datapoint=np.where(np.isclose(X2_, x_))[0], highlight_label=r"Hypothetical Observation $<\lambda, c(\lambda)>$") perform_finishing_tasks(ax=ax, filename="look_ahead_4.pdf", remove_legend=False) # --------------------------------------- # Vertical comparison of look-ahead at any given x def draw_vertical_comparison(imaginary_lambda, ax1, ax2): tx_ = np.array([[imaginary_lambda]]) ty_ = f(tx_[0]) # Update dataset with new observation tX_ = np.append(x, tx_, axis=0) tY_ = y + [ty_] logging.info("x: {}, y: {}".format(tx_, ty_)) # Fit GP to the updated dataset tgp = GPR(kernel=Matern()) logging.debug("Fitting GP to\nx: {}\ny:{}".format(tX_, tY_)) tgp.fit(tX_, tY_) # fit the model tmu_star_t1_xy = get_mu_star(tgp) # Draw the left hand figure using the old gp on ax1 draw_basic_figure(tgp=gp, title=r"$\hat{c}^{(t)}$", ax=ax1) logging.debug("Placing vertical on configuration: {}".format(tx_)) ax1.scatter(tx_, ty_, color=colors['highlighted_observations'], marker='X', label=r"Hypothetical Observation $<\lambda, c(\lambda)>$", zorder=zorders["annotations_normal"]) ax1.legend().remove() # Draw the right hand figure using the hypothetical gp tgp on ax2 draw_basic_figure( tgp=tgp, tx=tX_, tX_=tX_, tY_=tY_, title=r"$\hat{c}^{(t+1)}|_\lambda$", highlight_datapoint=np.where(np.isclose(tX_, tx_))[0], highlight_label=r"Hypothetical Observation $<\lambda, c(\lambda)>$", ax=ax2) def finishing_touches_parallel(ax1, ax2, filename=""): ax1.set_xlabel(labels['xlabel']) ax2.set_xlabel(labels['xlabel']) plt.tight_layout() if TOGGLE_PRINT: plt.savefig(f"{OUTPUT_DIR}/{filename}") else: plt.show() # --------------------------------------- # Draw look ahead 5 fig, (ax1, ax2) = plt.subplots(1, 2, squeeze=True, figsize=(22, 9)) draw_vertical_comparison(imaginary_lambda=5.0, ax1=ax1, ax2=ax2) finishing_touches_parallel(ax1=ax1, ax2=ax2, filename="look_ahead_5.pdf") # --------------------------------------- # Draw look ahead 6 fig, (ax1, ax2) = plt.subplots(1, 2, squeeze=True, figsize=(22, 9)) draw_vertical_comparison(imaginary_lambda=5.5, ax1=ax1, ax2=ax2) finishing_touches_parallel(ax1=ax1, ax2=ax2, filename="look_ahead_6.pdf") # --------------------------------------- # Draw look ahead 5 fig, (ax1, ax2) = plt.subplots(1, 2, squeeze=True, figsize=(22, 9)) draw_vertical_comparison(imaginary_lambda=3.5, ax1=ax1, ax2=ax2) finishing_touches_parallel(ax1=ax1, ax2=ax2, filename="look_ahead_7.pdf") # --------------------------------------- # Draw KG 1 labels['gp_mean'] = r'Mean: $\mu^{(t)}(\cdot)$' fig, ax = draw_basic_figure(title="") perform_finishing_tasks(ax=ax, filename="kg_1.pdf", remove_legend=False) # --------------------------------------- # Draw kg 2 fig, ax = draw_basic_figure(title="") boplot.highlight_configuration(mu_star_t_xy[0], lloc='bottom', ax=ax, disable_ticks=True) boplot.annotate_x_edge(label="%.2f" % mu_star_t_xy[0], xy=mu_star_t_xy, ax=ax, align='bottom', offset_param=1.5) boplot.highlight_output(mu_star_t_xy[1], label='', lloc='right', ax=ax, fontsize=30, disable_ticks=True) boplot.annotate_y_edge(label=r'${(\mu^*)}^{(t)}$', xy=mu_star_t_xy, align='right', ax=ax, yoffset=1.5) perform_finishing_tasks(ax=ax, filename="kg_2.pdf", remove_legend=True) # --------------------------------------- # Draw kg 3 fig, ax = draw_basic_figure( tgp=gp2, tx=x, tX_=X2_, tY_=Y2_, title='', highlight_datapoint=np.where(np.isclose(X2_, x_))[0], highlight_label=r"Hypothetical Observation $<\lambda, c(\lambda)>$") perform_finishing_tasks(ax=ax, filename="kg_3.pdf", remove_legend=True) # --------------------------------------- # Draw kg 4 fig, ax = draw_basic_figure( tgp=gp2, tx=x, tX_=X2_, tY_=Y2_, title='', highlight_datapoint=np.where(np.isclose(X2_, x_))[0], highlight_label=r"Hypothetical Observation $<\lambda, c(\lambda)>$") boplot.highlight_configuration(mu_star_t1_xy[0], lloc='bottom', ax=ax, disable_ticks=True) boplot.annotate_x_edge(label="%.2f" % mu_star_t1_xy[0], xy=mu_star_t1_xy, ax=ax, align='bottom', offset_param=1.5) boplot.highlight_output(mu_star_t1_xy[1], label='', lloc='right', ax=ax, fontsize=28) boplot.annotate_y_edge(label=r'${(\mu^*)}^{(t+1)}|_\lambda$', xy=mu_star_t1_xy, align='right', ax=ax, yoffset=1.5) perform_finishing_tasks(ax=ax, filename="kg_4.pdf", remove_legend=True) # --------------------------------------- # Draw kg 5 fig, (ax1, ax2) = plt.subplots(1, 2, squeeze=True, figsize=(22, 9)) draw_vertical_comparison(imaginary_lambda=x_.squeeze(), ax1=ax1, ax2=ax2) boplot.highlight_output(mu_star_t_xy[1], label='', lloc='right', ax=ax1, fontsize=30) boplot.annotate_y_edge(label='${(\mu^*)}^{(t)}$', xy=mu_star_t_xy, align='right', ax=ax1, yoffset=1.5) boplot.highlight_output(mu_star_t1_xy[1], label='', lloc='right', ax=ax2, fontsize=28) boplot.annotate_y_edge(label='${(\mu^*)}^{(t+1)}|_\lambda$', xy=mu_star_t1_xy, align='left', ax=ax2, yoffset=1.5) finishing_touches_parallel(ax1=ax1, ax2=ax2, filename="kg_5.pdf") return