From 73a122362f422f468c996efa550c309947a1a8ca Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 5 Jun 2013 17:29:46 +0100 Subject: [PATCH 1/2] bugs fixed in tutorial's tests --- GPy/examples/tutorials.py | 112 ++++++++++--------------------------- doc/tuto_GP_regression.rst | 4 +- 2 files changed, 30 insertions(+), 86 deletions(-) diff --git a/GPy/examples/tutorials.py b/GPy/examples/tutorials.py index 5d2dd41c..bb5192d8 100644 --- a/GPy/examples/tutorials.py +++ b/GPy/examples/tutorials.py @@ -17,28 +17,27 @@ def tuto_GP_regression(): X = np.random.uniform(-3.,3.,(20,1)) Y = np.sin(X) + np.random.randn(20,1)*0.05 - kernel = GPy.kern.rbf(D=1, variance=1., lengthscale=1.) + kernel = GPy.kern.rbf(input_dim=1, variance=1., lengthscale=1.) m = GPy.models.GP_regression(X,Y,kernel) print m m.plot() + m.ensure_default_constraints() m.constrain_positive('') - m.unconstrain('') # Required to remove the previous constrains - m.constrain_positive('rbf_variance') - m.constrain_bounded('lengthscale',1.,10. ) - m.constrain_fixed('noise',0.0025) + m.unconstrain('') # may be used to remove the previous constrains + m.constrain_positive('.*rbf_variance') + m.constrain_bounded('.*lengthscale',1.,10. ) + m.constrain_fixed('.*noise',0.0025) m.optimize() - m.optimize_restarts(Nrestarts = 10) - - ########################### - # 2-dimensional example # - ########################### + m.optimize_restarts(num_restarts = 10) + ####################################################### + ####################################################### # sample inputs and outputs X = np.random.uniform(-3.,3.,(50,2)) Y = np.sin(X[:,0:1]) * np.sin(X[:,1:2])+np.random.randn(50,1)*0.05 @@ -53,22 +52,19 @@ def tuto_GP_regression(): m.constrain_positive('') # optimize and plot - pb.figure() m.optimize('tnc', max_f_eval = 1000) - m.plot() print(m) - + return(m) def tuto_kernel_overview(): """The detailed explanations of the commands used in this file can be found in the tutorial section""" - pb.ion() - - ker1 = GPy.kern.rbf(1) # Equivalent to ker1 = GPy.kern.rbf(D=1, variance=1., lengthscale=1.) - ker2 = GPy.kern.rbf(D=1, variance = .75, lengthscale=2.) + ker1 = GPy.kern.rbf(1) # Equivalent to ker1 = GPy.kern.rbf(input_dim=1, variance=1., lengthscale=1.) + ker2 = GPy.kern.rbf(input_dim=1, variance = .75, lengthscale=2.) ker3 = GPy.kern.rbf(1, .5, .5) - + print ker2 + ker1.plot() ker2.plot() ker3.plot() @@ -77,28 +73,13 @@ def tuto_kernel_overview(): k2 = GPy.kern.Matern32(1, 0.5, 0.2) # Product of kernels - k_prod = k1.prod(k2) - k_prodorth = k1.prod_orthogonal(k2) + k_prod = k1.prod(k2) # By default, tensor=False + k_prodtens = k1.prod(k2,tensor=True) # Sum of kernels - k_add = k1.add(k2) - k_addorth = k1.add_orthogonal(k2) - - pb.figure(figsize=(8,8)) - pb.subplot(2,2,1) - k_prod.plot() - pb.title('prod') - pb.subplot(2,2,2) - k_prodorth.plot() - pb.title('prod_orthogonal') - pb.subplot(2,2,3) - k_add.plot() - pb.title('add') - pb.subplot(2,2,4) - k_addorth.plot() - pb.title('add_orthogonal') - pb.subplots_adjust(wspace=0.3, hspace=0.3) - + k_add = k1.add(k2) # By default, tensor=False + k_addtens = k1.add(k2,tensor=True) + k1 = GPy.kern.rbf(1,1.,2) k2 = GPy.kern.periodic_Matern52(1,variance=1e3, lengthscale=1, period = 1.5, lower=-5., upper = 5) @@ -109,18 +90,6 @@ def tuto_kernel_overview(): X = np.linspace(-5,5,501)[:,None] Y = np.random.multivariate_normal(np.zeros(501),k.K(X),1) - # plot - pb.figure(figsize=(10,4)) - pb.subplot(1,2,1) - k.plot() - pb.subplot(1,2,2) - pb.plot(X,Y.T) - pb.ylabel("Sample path") - pb.subplots_adjust(wspace=0.3) - - k = (k1+k2)*(k1+k2) - print k.parts[0].name, '\n', k.parts[1].name, '\n', k.parts[2].name, '\n', k.parts[3].name - k1 = GPy.kern.rbf(1) k2 = GPy.kern.Matern32(1) k3 = GPy.kern.white(1) @@ -128,16 +97,16 @@ def tuto_kernel_overview(): k = k1 + k2 + k3 print k - k.constrain_positive('var') + k.constrain_positive('.*var') k.constrain_fixed(np.array([1]),1.75) - k.tie_params('len') + k.tie_params('.*len') k.unconstrain('white') k.constrain_bounded('white',lower=1e-5,upper=.5) print k - + k_cst = GPy.kern.bias(1,variance=1.) k_mat = GPy.kern.Matern52(1,variance=1., lengthscale=3) - Kanova = (k_cst + k_mat).prod_orthogonal(k_cst + k_mat) + Kanova = (k_cst + k_mat).prod(k_cst + k_mat,tensor=True) print Kanova # sample inputs and outputs @@ -146,9 +115,8 @@ def tuto_kernel_overview(): # Create GP regression model m = GPy.models.GP_regression(X,Y,Kanova) - pb.figure(figsize=(5,5)) m.plot() - + pb.figure(figsize=(20,3)) pb.subplots_adjust(wspace=0.5) pb.subplot(1,5,1) @@ -156,41 +124,17 @@ def tuto_kernel_overview(): pb.subplot(1,5,2) pb.ylabel("= ",rotation='horizontal',fontsize='30') pb.subplot(1,5,3) - m.plot(which_functions=[False,True,False,False]) + m.plot(which_parts=[False,True,False,False]) pb.ylabel("cst +",rotation='horizontal',fontsize='30') pb.subplot(1,5,4) - m.plot(which_functions=[False,False,True,False]) + m.plot(which_parts=[False,False,True,False]) pb.ylabel("+ ",rotation='horizontal',fontsize='30') pb.subplot(1,5,5) pb.ylabel("+ ",rotation='horizontal',fontsize='30') - m.plot(which_functions=[False,False,False,True]) + m.plot(which_parts=[False,False,False,True]) - ker1 = GPy.kern.rbf(D=1) # Equivalent to ker1 = GPy.kern.rbf(D=1, variance=1., lengthscale=1.) - ker2 = GPy.kern.rbf(D=1, variance = .75, lengthscale=3.) - ker3 = GPy.kern.rbf(1, .5, .25) + return(m) - ker1.plot() - ker2.plot() - ker3.plot() - #pb.savefig("Figures/tuto_kern_overview_basicdef.png") - - kernels = [GPy.kern.rbf(1), GPy.kern.exponential(1), GPy.kern.Matern32(1), GPy.kern.Matern52(1), GPy.kern.Brownian(1), GPy.kern.bias(1), GPy.kern.linear(1), GPy.kern.spline(1), GPy.kern.periodic_exponential(1), GPy.kern.periodic_Matern32(1), GPy.kern.periodic_Matern52(1), GPy.kern.white(1)] - kernel_names = ["GPy.kern.rbf", "GPy.kern.exponential", "GPy.kern.Matern32", "GPy.kern.Matern52", "GPy.kern.Brownian", "GPy.kern.bias", "GPy.kern.linear", "GPy.kern.spline", "GPy.kern.periodic_exponential", "GPy.kern.periodic_Matern32", "GPy.kern.periodic_Matern52", "GPy.kern.white"] - - pb.figure(figsize=(16,12)) - pb.subplots_adjust(wspace=.5, hspace=.5) - for i, kern in enumerate(kernels): - pb.subplot(3,4,i+1) - kern.plot(x=7.5,plot_limits=[0.00001,15.]) - pb.title(kernel_names[i]+ '\n') - - # actual plot for the noise - i = 11 - X = np.linspace(0.,15.,201) - WN = 0*X - WN[100] = 1. - pb.subplot(3,4,i+1) - pb.plot(X,WN,'b') def model_interaction(): X = np.random.randn(20,1) diff --git a/doc/tuto_GP_regression.rst b/doc/tuto_GP_regression.rst index 87744c85..9f01de93 100644 --- a/doc/tuto_GP_regression.rst +++ b/doc/tuto_GP_regression.rst @@ -25,7 +25,7 @@ The first step is to define the covariance kernel we want to use for the model. kernel = GPy.kern.rbf(input_dim=1, variance=1., lengthscale=1.) -The parameter ``D`` stands for the dimension of the input space. The parameters ``variance`` and ``lengthscale`` are optional. Many other kernels are implemented such as: +The parameter ``input_dim`` stands for the dimension of the input space. The parameters ``variance`` and ``lengthscale`` are optional. Many other kernels are implemented such as: * linear (``GPy.kern.linear``) * exponential kernel (``GPy.kern.exponential``) @@ -69,7 +69,7 @@ There are various ways to constrain the parameters of the kernel. The most basic but it is also possible to set a range on to constrain one parameter to be fixed. The parameter of ``m.constrain_positive`` is a regular expression that matches the name of the parameters to be constrained (as seen in ``print m``). For example, if we want the variance to be positive, the lengthscale to be in [1,10] and the noise variance to be fixed we can write:: - m.unconstrain('') # Required to remove the previous constrains + m.unconstrain('') # may be used to remove the previous constrains m.constrain_positive('.*rbf_variance') m.constrain_bounded('.*lengthscale',1.,10. ) m.constrain_fixed('.*noise',0.0025) From e511a7ba03de1ac14c1b6fc4df08e901f489496e Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 5 Jun 2013 17:32:26 +0100 Subject: [PATCH 2/2] merged conflict in tutorial's tests (again) --- GPy/examples/tutorials.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/GPy/examples/tutorials.py b/GPy/examples/tutorials.py index fc33d2bc..4371d7a8 100644 --- a/GPy/examples/tutorials.py +++ b/GPy/examples/tutorials.py @@ -114,12 +114,8 @@ def tuto_kernel_overview(): Y = 0.5*X[:,:1] + 0.5*X[:,1:] + 2*np.sin(X[:,:1]) * np.sin(X[:,1:]) # Create GP regression model -<<<<<<< HEAD - m = GPy.models.GP_regression(X,Y,Kanova) -======= m = GPy.models.GPRegression(X, Y, Kanova) pb.figure(figsize=(5,5)) ->>>>>>> efbf169a6a17d824234d538553ffcbe0c4bddc40 m.plot() pb.figure(figsize=(20,3))