PLearn 0.1
|
Squared-Exponential kernel that can be used for Automatic Relevance Determination. More...
#include <SquaredExponentialARDKernel.h>
Public Member Functions | |
SquaredExponentialARDKernel () | |
Default constructor. | |
virtual real | evaluate (const Vec &x1, const Vec &x2) const |
Compute K(x1,x2). | |
virtual void | computeGramMatrix (Mat K) const |
Compute the Gram Matrix. | |
virtual void | computeGramMatrixDerivative (Mat &KD, const string &kernel_param, real epsilon=1e-6) const |
Directly compute the derivative with respect to hyperparameters (Faster than finite differences...) | |
virtual void | evaluate_all_i_x (const Vec &x, const Vec &k_xi_x, real squared_norm_of_x=-1, int istart=0) const |
Fill k_xi_x with K(x_i, x), for all i from istart to istart + k_xi_x.length() - 1. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual SquaredExponentialARDKernel * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Post-constructor. | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Transforms a shallow copy into a deep copy. | |
Static Public Member Functions | |
static string | _classname_ () |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
real | derivIspSignalSigma (int i, int j, int arg, real K) const |
Derivative function with respect to isp_signal_sigma. | |
real | derivIspGlobalSigma (int i, int j, int arg, real K) const |
Derivative function with respect to isp_global_sigma. | |
void | computeGramMatrixDerivIspSignalSigma (Mat &KD) const |
void | computeGramMatrixDerivIspInputSigma (Mat &KD, int arg) const |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares the class options. | |
Private Types | |
typedef ARDBaseKernel | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Squared-Exponential kernel that can be used for Automatic Relevance Determination.
This is a variant of the GaussianKernel (a.k.a. Radial Basis Function) that provides a different length-scale parameter for each input variable. When used in conjunction with GaussianProcessRegressor, this kernel may be used for Automatic Relevance Determination (ARD), a procedure wherein the significance of each input variable for the prediction task is found automatically through numerical optimization.
Similar to C.E. Rasmussen's GPML code (see http://www.gaussianprocess.org), this kernel function is specified as:
k(x,y) = sf * exp(- 0.5 * (sum_i (x_i - y_i)^2 / w_i)) * k_kron(x,y)
where sf is softplus(isp_signal_sigma), w_i is softplus(isp_global_sigma + isp_input_sigma[i]), and k_kron(x,y) is the result of the KroneckerBaseKernel evaluation, or 1.0 if there are no Kronecker terms. Note that since the Kronecker terms are incorporated multiplicatively, the very presence of the term associated to this kernel can be gated by the value of some input variable(s) (that are incorporated within one or more Kronecker terms).
Note that contrarily to previous versions that incorporated IID noise and Kronecker terms ADDITIVELY, this version does not add any noise at all (and as explained above incorporates the Kronecker terms multiplicatively). For best results, especially with moderately noisy data, IT IS IMPERATIVE to use whis kernel within a SummationKernel in conjunction with an IIDNoiseKernel, as follows (e.g. within a GaussianProcessRegressor):
kernel = SummationKernel(terms = [ SquaredExponentialARDKernel(), IIDNoiseKernel() ] )
Note that to make its operations more robust when used with unconstrained optimization of hyperparameters, all hyperparameters of this kernel are specified in the inverse softplus domain. See IIDNoiseKernel for more explanations.
Definition at line 86 of file SquaredExponentialARDKernel.h.
typedef ARDBaseKernel PLearn::SquaredExponentialARDKernel::inherited [private] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 88 of file SquaredExponentialARDKernel.h.
PLearn::SquaredExponentialARDKernel::SquaredExponentialARDKernel | ( | ) |
string PLearn::SquaredExponentialARDKernel::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
OptionList & PLearn::SquaredExponentialARDKernel::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
RemoteMethodMap & PLearn::SquaredExponentialARDKernel::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
Object * PLearn::SquaredExponentialARDKernel::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
StaticInitializer SquaredExponentialARDKernel::_static_initializer_ & PLearn::SquaredExponentialARDKernel::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
void PLearn::SquaredExponentialARDKernel::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 100 of file SquaredExponentialARDKernel.cc.
{ // ### Nothing to add here, simply calls build_ inherited::build(); build_(); }
void PLearn::SquaredExponentialARDKernel::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 110 of file SquaredExponentialARDKernel.cc.
{ // Ensure that we multiply in Kronecker terms inherited::m_default_value = 1.0; }
string PLearn::SquaredExponentialARDKernel::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
void PLearn::SquaredExponentialARDKernel::computeGramMatrix | ( | Mat | K | ) | const [virtual] |
Compute the Gram Matrix.
Reimplemented from PLearn::KroneckerBaseKernel.
Definition at line 160 of file SquaredExponentialARDKernel.cc.
References PLearn::diff(), PLearn::exp(), i, j, m, PLearn::TMat< T >::mod(), n, PLASSERT, PLearn::TMat< T >::size(), and PLearn::softplus().
{ PLASSERT( !m_isp_input_sigma.size() || dataInputsize() == m_isp_input_sigma.size() ); PLASSERT( K.size() == 0 || m_data_cache.size() > 0 ); // Ensure data cached OK // Compute Kronecker gram matrix inherited::computeGramMatrix(K); // Precompute some terms. Make sure that the input sigmas don't get too // small real sf = softplus(m_isp_signal_sigma); m_input_sigma.resize(dataInputsize()); softplusFloor(m_isp_global_sigma, 1e-6); m_input_sigma.fill(m_isp_global_sigma); // Still in ISP domain for (int i=0, n=m_input_sigma.size() ; i<n ; ++i) { if (m_isp_input_sigma.size() > 0) { softplusFloor(m_isp_input_sigma[i], 1e-6); m_input_sigma[i] += m_isp_input_sigma[i]; } m_input_sigma[i] = softplus(m_input_sigma[i]); } // Compute Gram Matrix int l = data->length(); int m = K.mod(); int n = dataInputsize(); int cache_mod = m_data_cache.mod(); real *data_start = &m_data_cache(0,0); real *Ki = K[0]; // Start of current row real *Kij; // Current element along row real *input_sigma_data = m_input_sigma.data(); real *xi = data_start; for (int i=0 ; i<l ; ++i, xi += cache_mod, Ki+=m) { Kij = Ki; real *xj = data_start; for (int j=0; j<=i; ++j, xj += cache_mod) { // Kernel evaluation per se real *x1 = xi; real *x2 = xj; real *p_inpsigma = input_sigma_data; real sum_wt = 0.0; int k = n; // Use Duff's device to unroll the following loop: // while (k--) { // real diff = *x1++ - *x2++; // sum_wt += (diff * diff) / *p_inpsigma++; // } real diff; switch (k % 8) { case 0: do { diff = *x1++ - *x2++; sum_wt += (diff*diff) / *p_inpsigma++; case 7: diff = *x1++ - *x2++; sum_wt += (diff*diff) / *p_inpsigma++; case 6: diff = *x1++ - *x2++; sum_wt += (diff*diff) / *p_inpsigma++; case 5: diff = *x1++ - *x2++; sum_wt += (diff*diff) / *p_inpsigma++; case 4: diff = *x1++ - *x2++; sum_wt += (diff*diff) / *p_inpsigma++; case 3: diff = *x1++ - *x2++; sum_wt += (diff*diff) / *p_inpsigma++; case 2: diff = *x1++ - *x2++; sum_wt += (diff*diff) / *p_inpsigma++; case 1: diff = *x1++ - *x2++; sum_wt += (diff*diff) / *p_inpsigma++; } while((k -= 8) > 0); } // Multiplicatively update kernel matrix (already pre-filled with // Kronecker terms, or 1.0 if no Kronecker terms, as per build_). real Kij_cur = *Kij * sf * exp(-0.5 * sum_wt); *Kij++ = Kij_cur; } } if (cache_gram_matrix) { gram_matrix.resize(l,l); gram_matrix << K; gram_matrix_is_cached = true; } }
void PLearn::SquaredExponentialARDKernel::computeGramMatrixDerivative | ( | Mat & | KD, |
const string & | kernel_param, | ||
real | epsilon = 1e-6 |
||
) | const [virtual] |
Directly compute the derivative with respect to hyperparameters (Faster than finite differences...)
Reimplemented from PLearn::Kernel.
Definition at line 241 of file SquaredExponentialARDKernel.cc.
References derivIspGlobalSigma(), PLASSERT, PLearn::string_begins_with(), and PLearn::tolong().
{ static const string ISS("isp_signal_sigma"); static const string IGS("isp_global_sigma"); static const string IIS("isp_input_sigma["); if (kernel_param == ISS) { computeGramMatrixDerivIspSignalSigma(KD); // computeGramMatrixDerivNV< // SquaredExponentialARDKernel, // &SquaredExponentialARDKernel::derivIspSignalSigma>(KD, this, -1); } else if (kernel_param == IGS) { computeGramMatrixDerivNV< SquaredExponentialARDKernel, &SquaredExponentialARDKernel::derivIspGlobalSigma>(KD, this, -1); } else if (string_begins_with(kernel_param, IIS) && kernel_param[kernel_param.size()-1] == ']') { int arg = tolong(kernel_param.substr( IIS.size(), kernel_param.size() - IIS.size() - 1)); PLASSERT( arg < m_isp_input_sigma.size() ); computeGramMatrixDerivIspInputSigma(KD, arg); } else inherited::computeGramMatrixDerivative(KD, kernel_param, epsilon); }
void PLearn::SquaredExponentialARDKernel::computeGramMatrixDerivIspInputSigma | ( | Mat & | KD, |
int | arg | ||
) | const [protected] |
Definition at line 328 of file SquaredExponentialARDKernel.cc.
References PLearn::TMat< T >::data(), PLearn::diff(), i, j, PLearn::TMat< T >::mod(), PLASSERT_MSG, PLearn::TMat< T >::resize(), and PLearn::sigmoid().
{ // Precompute some terms real input_sigma_arg = m_input_sigma[arg]; real input_sigma_sq = input_sigma_arg * input_sigma_arg; real input_sigmoid = sigmoid(m_isp_global_sigma + m_isp_input_sigma[arg]); // Compute Gram Matrix derivative w.r.t. isp_input_sigma[arg] int l = data->length(); PLASSERT_MSG( gram_matrix.width() == l && gram_matrix.length() == l, "To compute the derivative with respect to 'isp_input_sigma[i]', the\n" "Gram matrix must be precomputed and cached in SquaredExponentialARDKernel."); // Variables that walk over the data matrix int cache_mod = m_data_cache.mod(); real *data_start = &m_data_cache(0,0); real *xi = data_start+arg; // Iterator on data rows // Variables that walk over the gram cache int gram_cache_mod = gram_matrix.mod(); real *gram_cache_row = gram_matrix.data(); real *gram_cache_cur; // Variables that walk over the kernel derivative matrix (KD) KD.resize(l,l); real* KDi = KD.data(); // Start of row i real* KDij; // Current element on row i int KD_mod = KD.mod(); // Iterate on rows of derivative matrix for (int i=0 ; i<l ; ++i, xi += cache_mod, KDi += KD_mod, gram_cache_row += gram_cache_mod) { KDij = KDi; real *xj = data_start+arg; // Inner iterator on data rows gram_cache_cur = gram_cache_row; // Iterate on columns of derivative matrix for (int j=0 ; j <= i ; ++j, xj += cache_mod, ++gram_cache_cur) { real diff = *xi - *xj; real sq_diff = diff * diff; real KD_cur = 0.5 * *gram_cache_cur * input_sigmoid * sq_diff / input_sigma_sq; // Set into derivative matrix *KDij++ = KD_cur; } } }
void PLearn::SquaredExponentialARDKernel::computeGramMatrixDerivIspSignalSigma | ( | Mat & | KD | ) | const [protected] |
Definition at line 312 of file SquaredExponentialARDKernel.cc.
References PLASSERT_MSG, PLearn::TMat< T >::resize(), PLearn::sigmoid(), and PLearn::softplus().
{ int l = data->length(); KD.resize(l,l); PLASSERT_MSG( gram_matrix.width() == l && gram_matrix.length() == l, "To compute the derivative with respect to 'isp_signal_sigma', the\n" "Gram matrix must be precomputed and cached in SquaredExponentialARDKernel."); KD << gram_matrix; KD *= sigmoid(m_isp_signal_sigma)/softplus(m_isp_signal_sigma); }
void PLearn::SquaredExponentialARDKernel::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 91 of file SquaredExponentialARDKernel.cc.
{ // Now call the parent class' declareOptions inherited::declareOptions(ol); }
static const PPath& PLearn::SquaredExponentialARDKernel::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 123 of file SquaredExponentialARDKernel.h.
:
SquaredExponentialARDKernel * PLearn::SquaredExponentialARDKernel::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
real PLearn::SquaredExponentialARDKernel::derivIspGlobalSigma | ( | int | i, |
int | j, | ||
int | arg, | ||
real | K | ||
) | const [protected] |
Derivative function with respect to isp_global_sigma.
Definition at line 295 of file SquaredExponentialARDKernel.cc.
References PLearn::fast_is_equal(), pl_log, PLearn::sigmoid(), and PLearn::softplus().
Referenced by computeGramMatrixDerivative().
{ if (fast_is_equal(K,0.)) return 0.; // The norm term inside the exponential may be accessed as Log(K/sf) real inner = pl_log(K / softplus(m_isp_signal_sigma)); return - K * inner * sigmoid(m_isp_global_sigma) / softplus(m_isp_global_sigma); // Note: in the above expression for 'inner' there is the implicit // assumption that the input_sigma[i] are zero, which allows the // sigmoid/softplus term to be factored out of the norm summation. }
real PLearn::SquaredExponentialARDKernel::derivIspSignalSigma | ( | int | i, |
int | j, | ||
int | arg, | ||
real | K | ||
) | const [protected] |
Derivative function with respect to isp_signal_sigma.
Definition at line 286 of file SquaredExponentialARDKernel.cc.
References PLearn::sigmoid(), and PLearn::softplus().
{ // (No longer used; see computeGramMatrixDerivIspInputSigma below) return K*sigmoid(m_isp_signal_sigma)/softplus(m_isp_signal_sigma); }
real PLearn::SquaredExponentialARDKernel::evaluate | ( | const Vec & | x1, |
const Vec & | x2 | ||
) | const [virtual] |
Compute K(x1,x2).
Reimplemented from PLearn::KroneckerBaseKernel.
Definition at line 119 of file SquaredExponentialARDKernel.cc.
References PLearn::TVec< T >::data(), PLearn::diff(), PLearn::exp(), PLearn::fast_is_equal(), i, n, PLASSERT, PLearn::TVec< T >::size(), and PLearn::softplus().
{ PLASSERT( x1.size() == x2.size() ); PLASSERT( !m_isp_input_sigma.size() || x1.size() == m_isp_input_sigma.size() ); real gating_term = inherited::evaluate(x1,x2); if (fast_is_equal(gating_term, 0.0)) return 0.0; if (x1.size() == 0) return softplus(m_isp_signal_sigma) * gating_term; const real* px1 = x1.data(); const real* px2 = x2.data(); real sf = softplus(m_isp_signal_sigma); real expval = 0.0; if (m_isp_input_sigma.size() > 0) { const real* pinpsig = m_isp_input_sigma.data(); for (int i=0, n=x1.size() ; i<n ; ++i) { real diff = *px1++ - *px2++; real sqdiff = diff * diff; expval += sqdiff / softplus(m_isp_global_sigma + *pinpsig++); } } else { real global_sigma = softplus(m_isp_global_sigma); for (int i=0, n=x1.size() ; i<n ; ++i) { real diff = *px1++ - *px2++; real sqdiff = diff * diff; expval += sqdiff / global_sigma; } } // Gate by Kronecker term return sf * exp(-0.5 * expval) * gating_term; }
void PLearn::SquaredExponentialARDKernel::evaluate_all_i_x | ( | const Vec & | x, |
const Vec & | k_xi_x, | ||
real | squared_norm_of_x = -1 , |
||
int | istart = 0 |
||
) | const [virtual] |
Fill k_xi_x with K(x_i, x), for all i from istart to istart + k_xi_x.length() - 1.
Reimplemented from PLearn::Kernel.
Definition at line 277 of file SquaredExponentialARDKernel.cc.
References x.
OptionList & PLearn::SquaredExponentialARDKernel::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
OptionMap & PLearn::SquaredExponentialARDKernel::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
RemoteMethodMap & PLearn::SquaredExponentialARDKernel::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 82 of file SquaredExponentialARDKernel.cc.
void PLearn::SquaredExponentialARDKernel::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 385 of file SquaredExponentialARDKernel.cc.
{ inherited::makeDeepCopyFromShallowCopy(copies); }
Reimplemented from PLearn::ARDBaseKernel.
Definition at line 123 of file SquaredExponentialARDKernel.h.