mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-07-20 16:51:05 +02:00
better f_inv
This commit is contained in:
parent
323545f2d1
commit
b17cc60182
2 changed files with 25 additions and 10 deletions
|
|
@ -23,6 +23,7 @@ class warpedGP(GP):
|
||||||
self.warping_function = TanhWarpingFunction_d(warping_terms)
|
self.warping_function = TanhWarpingFunction_d(warping_terms)
|
||||||
self.warping_params = (np.random.randn(self.warping_function.n_terms*3+1,) * 1)
|
self.warping_params = (np.random.randn(self.warping_function.n_terms*3+1,) * 1)
|
||||||
|
|
||||||
|
Y = self._scale_data(Y)
|
||||||
self.has_uncertain_inputs = False
|
self.has_uncertain_inputs = False
|
||||||
self.Y_untransformed = Y.copy()
|
self.Y_untransformed = Y.copy()
|
||||||
self.predict_in_warped_space = False
|
self.predict_in_warped_space = False
|
||||||
|
|
@ -30,6 +31,14 @@ class warpedGP(GP):
|
||||||
|
|
||||||
GP.__init__(self, X, likelihood, kernel, normalize_X=normalize_X, Xslices=Xslices)
|
GP.__init__(self, X, likelihood, kernel, normalize_X=normalize_X, Xslices=Xslices)
|
||||||
|
|
||||||
|
def _scale_data(self, Y):
|
||||||
|
self._Ymax = Y.max()
|
||||||
|
self._Ymin = Y.min()
|
||||||
|
return (Y-self._Ymin)/(self._Ymax-self._Ymin) - 0.5
|
||||||
|
|
||||||
|
def _unscale_data(self, Y):
|
||||||
|
return (Y + 0.5)*(self._Ymax - self._Ymin) + self._Ymin
|
||||||
|
|
||||||
def _set_params(self, x):
|
def _set_params(self, x):
|
||||||
self.warping_params = x[:self.warping_function.num_parameters]
|
self.warping_params = x[:self.warping_function.num_parameters]
|
||||||
Y = self.transform_data()
|
Y = self.transform_data()
|
||||||
|
|
@ -79,5 +88,5 @@ class warpedGP(GP):
|
||||||
if self.predict_in_warped_space:
|
if self.predict_in_warped_space:
|
||||||
mu = self.warping_function.f_inv(mu, self.warping_params)
|
mu = self.warping_function.f_inv(mu, self.warping_params)
|
||||||
var = self.warping_function.f_inv(var, self.warping_params)
|
var = self.warping_function.f_inv(var, self.warping_params)
|
||||||
|
mu = self._unscale_data(mu)
|
||||||
return mu, var
|
return mu, var
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ class TanhWarpingFunction(WarpingFunction):
|
||||||
iterations: number of N.R. iterations
|
iterations: number of N.R. iterations
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
y = y.copy()
|
y = y.copy()
|
||||||
z = np.ones_like(y)
|
z = np.ones_like(y)
|
||||||
|
|
||||||
|
|
@ -176,7 +176,7 @@ class TanhWarpingFunction_d(WarpingFunction):
|
||||||
mpsi = psi.copy()
|
mpsi = psi.copy()
|
||||||
d = psi[-1]
|
d = psi[-1]
|
||||||
mpsi = mpsi[:self.num_parameters-1].reshape(self.n_terms, 3)
|
mpsi = mpsi[:self.num_parameters-1].reshape(self.n_terms, 3)
|
||||||
|
|
||||||
#3. transform data
|
#3. transform data
|
||||||
z = d*y.copy()
|
z = d*y.copy()
|
||||||
for i in range(len(mpsi)):
|
for i in range(len(mpsi)):
|
||||||
|
|
@ -185,7 +185,7 @@ class TanhWarpingFunction_d(WarpingFunction):
|
||||||
return z
|
return z
|
||||||
|
|
||||||
|
|
||||||
def f_inv(self, y, psi, iterations = 30):
|
def f_inv(self, z, psi, max_iterations = 1000):
|
||||||
"""
|
"""
|
||||||
calculate the numerical inverse of f
|
calculate the numerical inverse of f
|
||||||
|
|
||||||
|
|
@ -194,13 +194,19 @@ class TanhWarpingFunction_d(WarpingFunction):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
y = y.copy()
|
z = z.copy()
|
||||||
z = np.ones_like(y)
|
y = np.ones_like(z)
|
||||||
|
it = 0
|
||||||
|
update = np.inf
|
||||||
|
|
||||||
for i in range(iterations):
|
while it == 0 or (np.abs(update).sum() > 1e-10 and it < max_iterations):
|
||||||
z -= (self.f(z, psi) - y)/self.fgrad_y(z,psi)
|
update = (self.f(y, psi) - z)/self.fgrad_y(y, psi)
|
||||||
|
y -= update
|
||||||
return z
|
it += 1
|
||||||
|
if it == max_iterations:
|
||||||
|
print "WARNING!!! Maximum number of iterations reached in f_inv "
|
||||||
|
|
||||||
|
return y
|
||||||
|
|
||||||
|
|
||||||
def fgrad_y(self, y, psi, return_precalc = False):
|
def fgrad_y(self, y, psi, return_precalc = False):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue