kdict = { 'k1': { 'type': 'linear', 'scaling': 1., }, 'k2': { 'type': 'constant', 'const': 1., }, 'k3': { 'type': 'gaussian', 'width': 1., 'scaling': 1., 'dimension': 'single' }, 'k4': { 'type': 'quadratic', 'slope': 1., 'degree': 1., 'scaling': 1., 'dimension': 'single', }, 'k5': { 'type': 'laplacian', 'width': 1., 'scaling': 1., 'dimension': 'single' }, } st = time.time() print('train model') gp = GaussianProcess( train_fp=train_data, train_target=train_target, kernel_dict=kdict, regularization=1e-1, optimize_hyperparameters=True, scale_data=True ) print('trained {}'.format(time.time() - st)) with open('models/catapp_gp_model.pickle', 'wb') as model: pickle.dump(gp, model, protocol=pickle.HIGHEST_PROTOCOL)
org_gradients = np.asarray(gradients) gradients = org_gradients # Gaussian Process. # Define prediction parameters. sdt1 = 0.01 w1 = 1.0 # Too large widths results in a biased model. scaling = 1.0 kdict = {'k1': {'type': 'gaussian', 'width': w1, 'scaling': scaling}} gp = GaussianProcess(kernel_dict=kdict, regularization=sdt1**2, train_fp=train, train_target=target, optimize_hyperparameters=False, gradients=gradients, scale_optimizer=False, scale_data=True) # Hyperaparam optimization algorithms change from default. gp.optimize_hyperparameters(algomin='TNC', global_opt=False) print('Optimized kernel:', gp.kernel_dict) # Do the optimized predictions. pred = gp.predict(test_fp=test, uncertainty=True) prediction = np.array(pred['prediction'][:, 0]) # Calculate the uncertainty of the predictions. uncertainty = np.array(pred['uncertainty'])
# Plot the prediction. plt3d.plot_surface(test_x1, test_x2, np.array(rr_predictions).reshape(np.shape(test_x1)), alpha=0.3, color='r') # Model example 2 - Gausian linear kernel regression. if True: # Define prediction parameters kdict = {'k1': {'type': 'linear', 'scaling': 0.9}, 'c1': {'type': 'constant', 'const': 0.0}} # Starting guess for the noise parameter sdt1 = noise_magnitude # Set up the gaussian process. gp1 = GaussianProcess(kernel_dict=kdict, regularization=sdt1, train_fp=std['train'], train_target=train_targets['target'], optimize_hyperparameters=True, scale_optimizer=False) # Do predictions. linear = gp1.predict(test_fp=std['test'], get_validation_error=True, test_target=afunc(test)) prediction = np.array(linear['prediction']) * train_targets['std'] + \ train_targets['mean'] error = get_error(prediction, afunc(test)) print('Gaussian linear regression prediction:', error['absolute_average']) # Plot the prediction. plt3d.plot_surface(test_x1, test_x2, prediction.reshape(np.shape(test_x1)), alpha=0.3, color='g') # Model example 3 - Gaussian Process with sqe kernel.
# Get optimized parameters. ga_r = ga.pop[0][1][0] ga_w = ga.pop[0][0] ga_s = 1. # ga.pop[0][2] # Set up the prediction routine. kdict = { 'k1': { 'type': 'gaussian', 'width': w1, } } # 'scaling': 0.9}} gp = GaussianProcess(kernel_dict=kdict, regularization=sdt1, train_fp=std['train'], train_target=target[0], optimize_hyperparameters=True) # Do the optimized predictions. optimized = gp.predict(test_fp=std['test'], test_target=actual[0], uncertainty=True, get_validation_error=True) opt_upper = np.array(optimized['prediction']) + \ (np.array(optimized['uncertainty'])) opt_lower = np.array(optimized['prediction']) - \ (np.array(optimized['uncertainty'])) tgp1 = gp.kernel_dict['k1']['width'][0]
# Call underlying function to produce the gradients of the target values. gradients = [] for i in org_train: gradients.append(afunc(i)[1]) org_gradients = np.asarray(gradients) # Gaussian Process. # Set up the prediction routine and optimize hyperparameters. kdict = {'k1': {'type': 'gaussian', 'width': w1, 'scaling': scaling_exp}} gp = GaussianProcess(kernel_dict=kdict, regularization=reg**2, train_fp=train, train_target=target, optimize_hyperparameters=True, gradients=gradients, scale_data=True) print('Optimized kernel:', gp.kernel_dict) # Do the optimized predictions. pred = gp.predict(test_fp=test, uncertainty=True) prediction = np.array(pred['prediction'][:, 0]) # Calculate the uncertainty of the predictions. uncertainty = np.array(pred['uncertainty']) # Get confidence interval on predictions. upper = prediction + uncertainty lower = prediction - uncertainty
kdict = { 'k1': { 'type': 'linear', 'scaling': 1. }, 'c1': { 'type': 'constant', 'const': 0. } } # Starting guess for the noise parameter sdt1 = noise_magnitude # Set up the gaussian process. gp1 = GaussianProcess(kernel_dict=kdict, regularization=sdt1, train_fp=std['train'], train_target=train_targets['target'], optimize_hyperparameters=True) # Do predictions. linear = gp1.predict(test_fp=std['test'], uncertainty=True) # Put predictions back on real scale. prediction = np.vstack(linear['prediction']) * train_targets['std'] + \ train_targets['mean'] # Put uncertainties back on real scale. uncertainty = np.vstack(linear['uncertainty']) * train_targets['std'] # Get confidence interval on predictions. over_upper = prediction + uncertainty * tstd over_lower = prediction - uncertainty * tstd # Plot the uncertainties upper and lower bounds. plt3d.plot_surface(test_x1, test_x2,
ax224 = fig.add_subplot(224) plt.title('Uncertainty Profiles') plt.xlabel('Descriptor') plt.ylabel('Uncertainty') if True: # Model example 1 - biased model. # Define prediction parameters. sdt1 = 0.001 # Too large width results in a biased model. w1 = 3.0 kdict = {'k1': {'type': 'gaussian', 'width': w1}} # Set up the prediction routine. gp = GaussianProcess(kernel_dict=kdict, regularization=sdt1**2, train_fp=std['train'], train_target=train_targets['target'], optimize_hyperparameters=False) # Do predictions. under_fit = gp.predict(test_fp=std['test'], uncertainty=True) # Scale predictions back to the original scale. under_prediction = np.vstack(under_fit['prediction']) * \ train_targets['std'] + train_targets['mean'] under_uncertainty = np.vstack(under_fit['uncertainty']) * \ train_targets['std'] # Get average errors. error = get_error(under_prediction.reshape(-1), afunc(test).reshape(-1)) print('Gaussian linear regression prediction:', error['absolute_average']) # Get confidence interval on predictions. upper = under_prediction + under_uncertainty * tstd lower = under_prediction - under_uncertainty * tstd