[warped stuff] plotting and normalizer in warped gps

This commit is contained in:
mzwiessele 2016-08-17 14:51:29 +01:00
parent f50b691ec6
commit d343ec8b41
8 changed files with 112 additions and 78 deletions

View file

@ -6,7 +6,7 @@ Created on Aug 27, 2014
import logging
import numpy as np
class Norm(object):
class _Norm(object):
def __init__(self):
pass
def scale_by(self, Y):
@ -18,7 +18,8 @@ class Norm(object):
"""
Project Y into normalized space
"""
raise NotImplementedError
if not self.scaled():
raise AttributeError("Norm object not initialized yet, try calling scale_by(data) first.")
def inverse_mean(self, X):
"""
Project the normalized object X into space of Y
@ -31,15 +32,46 @@ class Norm(object):
Whether this Norm object has been initialized.
"""
raise NotImplementedError
class MeanNorm(Norm):
class Standardize(_Norm):
def __init__(self):
self.mean = None
def scale_by(self, Y):
Y = np.ma.masked_invalid(Y, copy=False)
self.mean = Y.mean(0).view(np.ndarray)
self.std = Y.std(0).view(np.ndarray)
def normalize(self, Y):
return Y-self.mean
super(Standardize, self).normalize(Y)
return (Y-self.mean)/self.std
def inverse_mean(self, X):
return X+self.mean
return (X*self.std)+self.mean
def inverse_variance(self, var):
return (var*(self.std**2))
def scaled(self):
return self.mean is not None
# Inverse variance to be implemented, disabling for now
# If someone in the future want to implement this,
# we need to implement the inverse variance for
# normalization. This means, we need to know the factor
# for the variance to be multiplied to the variance in
# normalized space. This is easy to compute for standardization
# (see above) but gets tricky here.
# class Normalize(_Norm):
# def __init__(self):
# self.ymin = None
# self.ymax = None
# def scale_by(self, Y):
# Y = np.ma.masked_invalid(Y, copy=False)
# self.ymin = Y.min(0).view(np.ndarray)
# self.ymax = Y.max(0).view(np.ndarray)
# def normalize(self, Y):
# super(Normalize, self).normalize(Y)
# return (Y - self.ymin) / (self.ymax - self.ymin) - .5
# def inverse_mean(self, X):
# return (X + .5) * (self.ymax - self.ymin) + self.ymin
# def inverse_variance(self, var):
#
# return (var*(self.std**2))
# def scaled(self):
# return (self.ymin is not None) and (self.ymax is not None)

View file

@ -31,7 +31,7 @@ class WarpingFunction(Parameterized):
"""gradient of f w.r.t to y"""
raise NotImplementedError
def f_inv(self, z, max_iterations=100, y=None):
def f_inv(self, z, max_iterations=250, y=None):
"""
Calculate the numerical inverse of f. This should be
overwritten for specific warping functions where the
@ -51,14 +51,11 @@ class WarpingFunction(Parameterized):
update = (fy - z) / fgrady
y -= self.rate * update
it += 1
if it == max_iterations:
print("WARNING!!! Maximum number of iterations reached in f_inv ")
print("Sum of roots: %.4f" % np.sum(fy - z))
#if it == max_iterations:
# print("WARNING!!! Maximum number of iterations reached in f_inv ")
# print("Sum of roots: %.4f" % np.sum(fy - z))
return y
def _get_param_names(self):
raise NotImplementedError
def plot(self, xmin, xmax):
y = np.arange(xmin, xmax, 0.01)
f_y = self.f(y)
@ -159,13 +156,6 @@ class TanhFunction(WarpingFunction):
return gradients
def _get_param_names(self):
variables = ['a', 'b', 'c', 'd']
names = sum([['warp_tanh_%s_t%i' % (variables[n],q) for n in range(3)]
for q in range(self.n_terms)],[])
names.append('warp_tanh')
return names
def update_grads(self, Y_untransformed, Kiy):
grad_y = self.fgrad_y(Y_untransformed)
grad_y_psi, grad_psi = self.fgrad_y_psi(Y_untransformed,