input_sensitivity and ard plotting

This commit is contained in:
Max Zwiessele 2014-02-24 14:47:43 +00:00
parent d90d67a8c1
commit b200b9fa90
11 changed files with 108 additions and 83 deletions

View file

@ -83,7 +83,7 @@ class Add(Kern):
from white import White
from rbf import RBF
#from rbf_inv import RBFInv
#from bias import Bias
from bias import Bias
from linear import Linear
#ffrom fixed import Fixed
@ -131,11 +131,11 @@ class Add(Kern):
def gradients_Z_variational(self, dL_dKmm, dL_dpsi0, dL_dpsi1, dL_dpsi2, mu, S, Z):
from white import white
from rbf import rbf
from white import White
from rbf import RBF
#from rbf_inv import rbfinv
#from bias import bias
from linear import linear
from bias import Bias
from linear import Linear
#ffrom fixed import fixed
target = np.zeros(Z.shape)
@ -146,15 +146,15 @@ class Add(Kern):
for p2, is2 in zip(self._parameters_, self.input_slices):
if p2 is p1:
continue
if isinstance(p2, white):
if isinstance(p2, White):
continue
elif isinstance(p2, bias):
elif isinstance(p2, Bias):
eff_dL_dpsi1 += dL_dpsi2.sum(1) * p2.variance * 2.
else:
eff_dL_dpsi1 += dL_dpsi2.sum(1) * p2.psi1(z[:,is2], mu[:,is2], s[:,is2]) * 2.
eff_dL_dpsi1 += dL_dpsi2.sum(1) * p2.psi1(Z[:,is2], mu[:,is2], S[:,is2]) * 2.
target += p1.gradients_z_variational(dL_dkmm, dL_dpsi0, eff_dL_dpsi1, dL_dpsi2, mu[:,is1], s[:,is1], z[:,is1])
target += p1.gradients_z_variational(dL_dKmm, dL_dpsi0, eff_dL_dpsi1, dL_dpsi2, mu[:,is1], S[:,is1], Z[:,is1])
return target
def gradients_muS_variational(self, dL_dKmm, dL_dpsi0, dL_dpsi1, dL_dpsi2, mu, S, Z):
@ -195,6 +195,12 @@ class Add(Kern):
from ..plotting.matplot_dep import kernel_plots
kernel_plots.plot(self,*args)
def input_sensitivity(self):
in_sen = np.zeros((self.input_dim, self.num_params))
for i, [p, i_s] in enumerate(zip(self._parameters_, self.input_slices)):
in_sen[i_s, i] = p.input_sensitivity()
return in_sen
def _getstate(self):
"""
Get the current state of the class,

View file

@ -28,12 +28,12 @@ class Bias(Kern):
self.variance.gradient = dL_dK.sum()
def update_gradients_diag(self, dL_dKdiag, X):
self.variance.gradient = dL_dK.sum()
self.variance.gradient = dL_dKdiag.sum()
def gradients_X(self, dL_dK,X, X2, target):
def gradients_X(self, dL_dK,X, X2):
return np.zeros(X.shape)
def gradients_X_diag(self,dL_dKdiag,X,target):
def gradients_X_diag(self,dL_dKdiag,X):
return np.zeros(X.shape)

View file

@ -61,16 +61,20 @@ class Kern(Parameterized):
def gradients_q_variational(self, dL_dKmm, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, posterior_variational):
raise NotImplementedError
def plot_ARD(self, *args):
"""If an ARD kernel is present, plot a bar representation using matplotlib
See GPy.plotting.matplot_dep.plot_ARD
"""
def plot_ARD(self, *args, **kw):
if "matplotlib" in sys.modules:
from ...plotting.matplot_dep import kernel_plots
self.plot_ARD.__doc__ += kernel_plots.plot_ARD.__doc__
assert "matplotlib" in sys.modules, "matplotlib package has not been imported."
from ...plotting.matplot_dep import kernel_plots
return kernel_plots.plot_ARD(self,*args)
return kernel_plots.plot_ARD(self,*args,**kw)
def input_sensitivity(self):
"""
Returns the sensitivity for each dimension of this kernel.
"""
return np.zeros(self.input_dim)
def __add__(self, other):
""" Overloading of the '+' operator. for more control, see self.add """
return self.add(other)

View file

@ -252,3 +252,6 @@ class Linear(Kern):
return np.dot(ZA, inner).swapaxes(0, 1) # NOTE: self.ZAinner \in [num_inducing x N x input_dim]!
def input_sensitivity(self):
if self.ARD: return self.variances
else: return self.variances.repeat(self.input_dim)

View file

@ -382,3 +382,7 @@ class RBF(Kern):
type_converters=weave.converters.blitz, **self.weave_options)
return mudist, mudist_sq, psi2_exponent, psi2
def input_sensitivity(self):
if self.ARD: return 1./self.lengthscale
else: return (1./self.lengthscale).repeat(self.input_dim)