From 93439e8a8ee346aeb97313e01586d855e4745995 Mon Sep 17 00:00:00 2001 From: James Hensman Date: Mon, 20 May 2013 11:03:35 +0100 Subject: [PATCH 1/5] implemented inverse Gamma prior --- GPy/core/model.py | 2 +- GPy/core/priors.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/GPy/core/model.py b/GPy/core/model.py index 94202396..f2b188d9 100644 --- a/GPy/core/model.py +++ b/GPy/core/model.py @@ -66,7 +66,7 @@ class model(parameterised): # check constraints are okay - if isinstance(what, (priors.gamma, priors.log_Gaussian)): + if isinstance(what, (priors.gamma, priors.inverse_gamma, priors.log_Gaussian)): constrained_positive_indices = [i for i, t in zip(self.constrained_indices, self.constraints) if t.domain == 'positive'] if len(constrained_positive_indices): constrained_positive_indices = np.hstack(constrained_positive_indices) diff --git a/GPy/core/priors.py b/GPy/core/priors.py index a5eae5b2..f9307b94 100644 --- a/GPy/core/priors.py +++ b/GPy/core/priors.py @@ -26,7 +26,6 @@ class Gaussian(prior): :param mu: mean :param sigma: standard deviation - .. Note:: Bishop 2006 notation is used throughout the code """ @@ -144,7 +143,6 @@ def gamma_from_EV(E,V): b = E/V return gamma(a,b) - class gamma(prior): """ Implementation of the Gamma probability function, coupled with random variables. @@ -155,7 +153,6 @@ class gamma(prior): .. Note:: Bishop 2006 notation is used throughout the code """ - def __init__(self,a,b): self.a = float(a) self.b = float(b) @@ -183,3 +180,30 @@ class gamma(prior): def rvs(self,n): return np.random.gamma(scale=1./self.b,shape=self.a,size=n) + +class inverse_gamma(prior): + """ + Implementation of the inverse-Gamma probability function, coupled with random variables. + + :param a: shape parameter + :param b: rate parameter (warning: it's the *inverse* of the scale) + + .. Note:: Bishop 2006 notation is used throughout the code + + """ + def __init__(self,a,b): + self.a = float(a) + self.b = float(b) + self.constant = -gammaln(self.a) + a*np.log(b) + + def __str__(self): + return "iGa("+str(np.round(self.a))+', '+str(np.round(self.b))+')' + + def lnpdf(self,x): + return self.constant - (self.a+1)*np.log(x) - self.b/x + + def lnpdf_grad(self,x): + return -(self.a+1.)/x + self.b/x**2 + + def rvs(self,n): + return 1./np.random.gamma(scale=1./self.b,shape=self.a,size=n) From 9041d28ab26313510b9e3a910fc8757be8754070 Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 20 May 2013 11:23:44 +0100 Subject: [PATCH 2/5] Automatically fetch datasets and first init. attempt for mocap --- GPy/util/datasets.py | 14 ++++++++++++++ GPy/util/mocap_fetch.py | 13 +++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 GPy/util/mocap_fetch.py diff --git a/GPy/util/datasets.py b/GPy/util/datasets.py index 6bc83735..b3675a8e 100644 --- a/GPy/util/datasets.py +++ b/GPy/util/datasets.py @@ -5,6 +5,8 @@ import GPy import scipy.sparse import scipy.io import cPickle as pickle +import urllib2 as url + data_path = os.path.join(os.path.dirname(__file__), 'datasets') default_seed = 10000 @@ -15,6 +17,18 @@ def sample_class(f): c = np.where(c, 1, -1) return c +def fetch_dataset(resource, file_name, messages = True): + if messages: + print "Downloading resource: " , resource, " ... " + response = url.urlopen(resource) + # TODO: Some error checking... + html = response.read() + response.close() + with open(file_name, "w") as text_file: + text_file.write("%s"%html) + if messages: + print "Done!" + def della_gatta_TRP63_gene_expression(gene_number=None): mat_data = scipy.io.loadmat(os.path.join(data_path, 'DellaGattadata.mat')) X = np.double(mat_data['timepoints']) diff --git a/GPy/util/mocap_fetch.py b/GPy/util/mocap_fetch.py new file mode 100644 index 00000000..323cc5d8 --- /dev/null +++ b/GPy/util/mocap_fetch.py @@ -0,0 +1,13 @@ +import GPy +import urllib2 + +# TODO... +class mocap_fetch(base_url = 'http://mocap.cs.cmu.edu:8080/subjects/', skel_store_dir = './', motion_store_dir = './'): + def __init__(self): + self.base_url = base_url + self.store_dir = store_dir + self.motion_dict = [] + + def fetch_motions(self, motion_dict = None): + response = urllib2.urlopen(...) + html = response.read() From 5f1eaf1e64d67a5d3d7433770a9bb69ff7197406 Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 20 May 2013 11:51:56 +0100 Subject: [PATCH 3/5] Removed fisrt prints if display is off --- GPy/inference/SCG.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/GPy/inference/SCG.py b/GPy/inference/SCG.py index f190d002..83ea054f 100644 --- a/GPy/inference/SCG.py +++ b/GPy/inference/SCG.py @@ -39,8 +39,10 @@ def SCG(f, gradf, x, optargs=(), maxiters=500, max_f_eval=500, display=True, xto function_eval number of fn evaluations status: string describing convergence status """ - print " SCG" - print ' {0:{mi}s} {1:11s} {2:11s} {3:11s}'.format("I", "F", "Scale", "|g|", mi=len(str(maxiters))) + + if display: + print " SCG" + print ' {0:{mi}s} {1:11s} {2:11s} {3:11s}'.format("I", "F", "Scale", "|g|", mi=len(str(maxiters))) if xtol is None: xtol = 1e-6 From 9c721c2c399e8c7b92872bb1fcc42e934e6073bd Mon Sep 17 00:00:00 2001 From: Teo de Campos Date: Mon, 20 May 2013 11:59:46 +0100 Subject: [PATCH 4/5] Changed optimization constraints in GPy/examples/dimensionality_reduction.py --- GPy/examples/dimensionality_reduction.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/GPy/examples/dimensionality_reduction.py b/GPy/examples/dimensionality_reduction.py index 95aed1ab..ec332dfe 100644 --- a/GPy/examples/dimensionality_reduction.py +++ b/GPy/examples/dimensionality_reduction.py @@ -443,9 +443,7 @@ def brendan_faces(): m = GPy.models.GPLVM(Yn, Q)#, M=Y.shape[0]/4) # optimize - # m.constrain_fixed('white', 1e-2) - # m.constrain_bounded('noise', 1e-6, 10) - m.constrain('rbf', GPy.core.transformations.logexp_clipped()) + m.constrain('rbf|noise|white', GPy.core.transformations.logexp_clipped()) m.ensure_default_constraints() m.optimize('scg', messages=1, max_f_eval=10000) From 6fe7ac2106e1927e57a19078407325d25d109f8d Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 20 May 2013 12:11:21 +0100 Subject: [PATCH 5/5] Update to fetch_dataset --- GPy/util/datasets.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/GPy/util/datasets.py b/GPy/util/datasets.py index b3675a8e..d679c6c3 100644 --- a/GPy/util/datasets.py +++ b/GPy/util/datasets.py @@ -17,17 +17,24 @@ def sample_class(f): c = np.where(c, 1, -1) return c -def fetch_dataset(resource, file_name, messages = True): +def fetch_dataset(resource, save_name = None, save_file = True, messages = True): if messages: print "Downloading resource: " , resource, " ... " response = url.urlopen(resource) # TODO: Some error checking... + # ... html = response.read() response.close() - with open(file_name, "w") as text_file: - text_file.write("%s"%html) - if messages: - print "Done!" + if save_file: + # TODO: Check if already exists... + # ... + with open(save_name, "w") as text_file: + text_file.write("%s"%html) + if messages: + print "Done!" + return html + + def della_gatta_TRP63_gene_expression(gene_number=None): mat_data = scipy.io.loadmat(os.path.join(data_path, 'DellaGattadata.mat'))