From 976b27b654f991f396d76e88d117da1e1d55bbbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Fusi?= Date: Sun, 3 Feb 2013 15:00:53 +0000 Subject: [PATCH] working on linear kernel --- GPy/examples/BGPLVM_demo.py | 2 +- GPy/kern/kern.py | 38 ++++++++++++++++++------------------- GPy/kern/linear.py | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/GPy/examples/BGPLVM_demo.py b/GPy/examples/BGPLVM_demo.py index a5912462..18a96a47 100644 --- a/GPy/examples/BGPLVM_demo.py +++ b/GPy/examples/BGPLVM_demo.py @@ -17,7 +17,7 @@ K = k.K(X) Y = np.random.multivariate_normal(np.zeros(N),K,D).T # k = GPy.kern.rbf(Q) + GPy.kern.bias(Q) + GPy.kern.white(Q, 0.00001) -k = GPy.kern.linear(Q, ARD = False) + GPy.kern.white(Q, 0.00001) +k = GPy.kern.linear(Q, ARD = True) + GPy.kern.white(Q) m = GPy.models.Bayesian_GPLVM(Y, Q, kernel = k, M=M) m.constrain_positive('(rbf|bias|noise|white|S)') # m.constrain_fixed('S', 1) diff --git a/GPy/kern/kern.py b/GPy/kern/kern.py index 0433d1f4..6bfd224f 100644 --- a/GPy/kern/kern.py +++ b/GPy/kern/kern.py @@ -6,7 +6,7 @@ import numpy as np from ..core.parameterised import parameterised from kernpart import kernpart import itertools -from product_orthogonal import product_orthogonal +from product_orthogonal import product_orthogonal class kern(parameterised): def __init__(self,D,parts=[], input_slices=None): @@ -325,11 +325,11 @@ class kern(parameterised): # MASSIVE TODO: do something smart for white # "crossterms" - psi1_matrices = [np.zeros((mu.shape[0], Z.shape[0])) for p in self.parts] - [p.psi1(Z[s2],mu[s1],S[s1],psi1_target[s1,s2]) for p,s1,s2,psi1_target in zip(self.parts,slices1,slices2, psi1_matrices)] - for a,b in itertools.combinations(psi1_matrices, 2): - tmp = np.multiply(a,b) - target += tmp[:,None,:] + tmp[:, :,None] + # psi1_matrices = [np.zeros((mu.shape[0], Z.shape[0])) for p in self.parts] + # [p.psi1(Z[s2],mu[s1],S[s1],psi1_target[s1,s2]) for p,s1,s2,psi1_target in zip(self.parts,slices1,slices2, psi1_matrices)] + # for a,b in itertools.combinations(psi1_matrices, 2): + # tmp = np.multiply(a,b) + # target += tmp[:,None,:] + tmp[:, :,None] return target @@ -340,21 +340,21 @@ class kern(parameterised): [p.dpsi2_dtheta(partial[s1,s2,s2],Z[s2,i_s],mu[s1,i_s],S[s1,i_s],target[ps]) for p,i_s,s1,s2,ps in zip(self.parts,self.input_slices,slices1,slices2,self.param_slices)] - # "crossterms" - # 1. get all the psi1 statistics - psi1_matrices = [np.zeros((mu.shape[0], Z.shape[0])) for p in self.parts] - [p.psi1(Z[s2],mu[s1],S[s1],psi1_target[s1,s2]) for p,s1,s2,psi1_target in zip(self.parts,slices1,slices2, psi1_matrices)] - partial1 = np.zeros_like(partial1) + # # "crossterms" + # # 1. get all the psi1 statistics + # psi1_matrices = [np.zeros((mu.shape[0], Z.shape[0])) for p in self.parts] + # [p.psi1(Z[s2],mu[s1],S[s1],psi1_target[s1,s2]) for p,s1,s2,psi1_target in zip(self.parts,slices1,slices2, psi1_matrices)] + # partial1 = np.zeros_like(partial1) - # 2. get all the dpsi1/dtheta gradients - psi1_gradients = [np.zeros(self.Nparam) for p in self.parts] - [p.dpsi1_dtheta(partial1[s2,s1],Z[s2,i_s],mu[s1,i_s],S[s1,i_s],psi1g_target[ps]) for p,ps,s1,s2,i_s,psi1g_target in zip(self.parts, self.param_slices,slices1,slices2,self.input_slices,psi1_gradients)] + # # 2. get all the dpsi1/dtheta gradients + # psi1_gradients = [np.zeros(self.Nparam) for p in self.parts] + # [p.dpsi1_dtheta(partial1[s2,s1],Z[s2,i_s],mu[s1,i_s],S[s1,i_s],psi1g_target[ps]) for p,ps,s1,s2,i_s,psi1g_target in zip(self.parts, self.param_slices,slices1,slices2,self.input_slices,psi1_gradients)] - # 3. multiply them somehow - for a,b in itertools.combinations(range(len(psi1_matrices)), 2): - gne = (psi1_gradients[a][None]*psi1_matrices[b].sum(0)[:,None]).sum(0) - - target += (gne[None] + gne[:, None]).sum(0) + # # 3. multiply them somehow + # for a,b in itertools.combinations(range(len(psi1_matrices)), 2): + # gne = (psi1_gradients[a][None]*psi1_matrices[b].sum(0)[:,None]).sum(0) + # target += (gne[None] + gne[:, None]).sum(0) + return target def dpsi2_dZ(self,partial,Z,mu,S,slices1=None,slices2=None): diff --git a/GPy/kern/linear.py b/GPy/kern/linear.py index 52bc1757..7c8ba398 100644 --- a/GPy/kern/linear.py +++ b/GPy/kern/linear.py @@ -30,7 +30,7 @@ class linear(kernpart): if variances is not None: if isinstance(variances, float): variances = np.array([variances]) - + assert variances.shape == (1,) else: variances = np.ones(1)