mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-18 13:55:14 +02:00
[mean_func] added parameters in additive mean func and tests for mean functions
This commit is contained in:
parent
02457660a2
commit
e0c70a52a0
3 changed files with 43 additions and 3 deletions
|
|
@ -23,9 +23,10 @@ class Additive(Mapping):
|
||||||
assert(mapping1.input_dim==mapping2.input_dim)
|
assert(mapping1.input_dim==mapping2.input_dim)
|
||||||
assert(mapping1.output_dim==mapping2.output_dim)
|
assert(mapping1.output_dim==mapping2.output_dim)
|
||||||
input_dim, output_dim = mapping1.input_dim, mapping1.output_dim
|
input_dim, output_dim = mapping1.input_dim, mapping1.output_dim
|
||||||
Mapping.__init__(self, input_dim=input_dim, output_dim=output_dim)
|
super(Additive, self).__init__(input_dim=input_dim, output_dim=output_dim)
|
||||||
self.mapping1 = mapping1
|
self.mapping1 = mapping1
|
||||||
self.mapping2 = mapping2
|
self.mapping2 = mapping2
|
||||||
|
self.link_parameters(self.mapping1, self.mapping2)
|
||||||
|
|
||||||
def f(self, X):
|
def f(self, X):
|
||||||
return self.mapping1.f(X) + self.mapping2.f(X)
|
return self.mapping1.f(X) + self.mapping2.f(X)
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ class Linear(Mapping):
|
||||||
return np.dot(X, self.A)
|
return np.dot(X, self.A)
|
||||||
|
|
||||||
def update_gradients(self, dL_dF, X):
|
def update_gradients(self, dL_dF, X):
|
||||||
self.A.gradient = np.dot( X.T, dL_dF)
|
self.A.gradient = np.dot(X.T, dL_dF)
|
||||||
|
|
||||||
def gradients_X(self, dL_dF, X):
|
def gradients_X(self, dL_dF, X):
|
||||||
return np.dot(dL_dF, self.A.T)
|
return np.dot(dL_dF, self.A.T)
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,49 @@ class MFtests(unittest.TestCase):
|
||||||
A linear mean function with parameters that we'll learn alongside the kernel
|
A linear mean function with parameters that we'll learn alongside the kernel
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
X = np.linspace(-1,10,50).reshape(-1,1)
|
||||||
|
|
||||||
|
Y = 3-np.abs((X-6))
|
||||||
|
Y += .5*np.cos(3*X) + 0.3*np.random.randn(*X.shape)
|
||||||
|
|
||||||
|
mf = GPy.mappings.PiecewiseLinear(1, 1, [-1,1], [9,2])
|
||||||
|
|
||||||
|
k =GPy.kern.RBF(1)
|
||||||
|
lik = GPy.likelihoods.Gaussian()
|
||||||
|
m = GPy.core.GP(X, Y, kernel=k, likelihood=lik, mean_function=mf)
|
||||||
|
self.assertTrue(m.checkgrad())
|
||||||
|
|
||||||
|
def test_parametric_mean_function_composition(self):
|
||||||
|
"""
|
||||||
|
A linear mean function with parameters that we'll learn alongside the kernel
|
||||||
|
"""
|
||||||
|
|
||||||
X = np.linspace(0,10,50).reshape(-1,1)
|
X = np.linspace(0,10,50).reshape(-1,1)
|
||||||
Y = np.sin(X) + 0.5*np.cos(3*X) + 0.1*np.random.randn(*X.shape) + 3*X
|
Y = np.sin(X) + 0.5*np.cos(3*X) + 0.1*np.random.randn(*X.shape) + 3*X
|
||||||
|
|
||||||
mf = GPy.mappings.Linear(1,1)
|
mf = GPy.mappings.Compound(GPy.mappings.Linear(1,1),
|
||||||
|
GPy.mappings.Kernel(1, 1, np.random.normal(0,1,(1,1)),
|
||||||
|
GPy.kern.RBF(1))
|
||||||
|
)
|
||||||
|
|
||||||
|
k =GPy.kern.RBF(1)
|
||||||
|
lik = GPy.likelihoods.Gaussian()
|
||||||
|
m = GPy.core.GP(X, Y, kernel=k, likelihood=lik, mean_function=mf)
|
||||||
|
self.assertTrue(m.checkgrad())
|
||||||
|
|
||||||
|
def test_parametric_mean_function_additive(self):
|
||||||
|
"""
|
||||||
|
A linear mean function with parameters that we'll learn alongside the kernel
|
||||||
|
"""
|
||||||
|
|
||||||
|
X = np.linspace(0,10,50).reshape(-1,1)
|
||||||
|
Y = np.sin(X) + 0.5*np.cos(3*X) + 0.1*np.random.randn(*X.shape) + 3*X
|
||||||
|
|
||||||
|
mf = GPy.mappings.Additive(GPy.mappings.Constant(1,1,3),
|
||||||
|
GPy.mappings.Additive(GPy.mappings.MLP(1,1),
|
||||||
|
GPy.mappings.Identity(1,1)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
k =GPy.kern.RBF(1)
|
k =GPy.kern.RBF(1)
|
||||||
lik = GPy.likelihoods.Gaussian()
|
lik = GPy.likelihoods.Gaussian()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue