def try_bec(): # get train and test data in_provider = InputProvider() harmonic_sims, tr, te, va = in_provider.get_bec_data() data = bec.get_within_range(tr, g_low=30, g_high=50, n=100) # data_test = te.sample(100) # get gp model and fit gp = GP() gp.fit(torch.FloatTensor(data[['g', 'x']].to_numpy()), torch.FloatTensor(data.psi.to_numpy()), True) # predict around one some fixed Dim df = bec.get_closest_sim(harmonic_sims, g=30.) test_gx = np.stack([30. * np.ones(df.x.shape[0]), df.x]).transpose() y_pred, sigma = gp.predict(torch.FloatTensor(test_gx)) print(y_pred, sigma) # plot subplots with multiple fixed dimensions # specify input dimensions, # the first entry is fixed for each iteration # the second entry is plotted against the fixed dimension input_dimensions = ['g', 'x'] out_dimensions = 'psi' sub_plot_multiple_gp(gp, harmonic_sims, [5, 30, 60, 90], input_dimensions, out_dimensions)
y = np.cos(x) + 0.5 * x + np.random.normal(loc=0.0, scale=0.2, size=(50, 1)) #test data X points x_test = np.linspace(-5, 22, 100) x_test = np.reshape(x_test, (100, 1)) a_gp = GP(x, y, "matern", params) b = GP(x, y, "rbf", params) ''' product_of_experts = distributedGP(x,y,8) general_poe = distributedGP(x,y,8, method='gpoe') bcm = distributedGP(x,y,8, method='bcm') rbcm = distributedGP(x,y,8,method='rbcm') ''' mean, cov = a_gp.predict(x_test) m2, c2 = b.predict(x_test) ''' mean2, cov2 = product_of_experts.predict(x_test) mean3, cov3 = general_poe.predict(x_test) mean4, cov4 = bcm.predict(x_test) mean5, cov5 = rbcm.predict(x_test) ''' plt.scatter(x, y) plt.plot(x_test, mean, c='g', label='matern mean') plt.plot(x_test, mean + np.sqrt(cov) * 2, linestyle='--', color='g', label='matern var')
import matplotlib.pyplot as plt from torch.optim import Adam import torch import numpy as np from gp import GP if __name__ == '__main__': gp = GP() f = lambda x: ( torch.cos(2 * x[:, 0]) + torch.sin(x[:, 1]) ).view(-1) n1, n2, ny = 40, 100, 5 domain = (-5, 5) X_data = torch.distributions.Uniform( domain[0] + 2, domain[1] - 2).sample((n1, 2)) y_data = f(X_data) X_test = torch.linspace(domain[0], domain[1], n2) X_test = torch.stack([X_test, X_test]).view(-1, 2) y_test = f(X_test) gp.fit(X_data, y_data, True, epochs=10000) y_pred, _ = gp.predict(X_test) print('MSE : ', ((y_pred - y_test)**2).mean().item() )