PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Private Types | Private Member Functions
PLearn::Matern1ARDKernel Class Reference

Matern kernel with nu=1/2 that can be used for Automatic Relevance Determination. More...

#include <Matern1ARDKernel.h>

Inheritance diagram for PLearn::Matern1ARDKernel:
Inheritance graph
[legend]
Collaboration diagram for PLearn::Matern1ARDKernel:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Matern1ARDKernel ()
 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 OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual Matern1ARDKerneldeepCopy (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 PPathdeclaringFile ()

Public Attributes

real m_isp_persistence
 Inverse softplus of the O-U persistence parameter.

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 derivIspPersistence (int i, int j, int arg, real K) const
 Compute derivative w.r.t. isp_persistence.
void computeGramMatrixDerivIspSignalSigma (Mat &KD) const
 Compute derivative w.r.t. isp_signal_sigma for WHOLE MATRIX.
void computeGramMatrixDerivIspInputSigma (Mat &KD, int arg) const
 Compute derivative w.r.t. isp_input_sigma[arg] for WHOLE MATRIX.

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.

Detailed Description

Matern kernel with nu=1/2 that can be used for Automatic Relevance Determination.

With nu=1/2, the Matern kernel corresponds to the Ornstein-Uhlenbeck process. This function is specified as:

k(x,y) = (sf / (2*a)) * exp(-a sum_i |x_i - y_i|/w_i) * k_kron(x,y)

where sf = softplus(isp_signal_sigma), a = softplus(isp_persistence), w_i = 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 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 69 of file Matern1ARDKernel.h.


Member Typedef Documentation

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 71 of file Matern1ARDKernel.h.


Constructor & Destructor Documentation

PLearn::Matern1ARDKernel::Matern1ARDKernel ( )

Default constructor.

Definition at line 69 of file Matern1ARDKernel.cc.

    : m_isp_persistence(pl_log(exp(1.0) - 1.)) // inverse-softplus(1.0)
{ }

Member Function Documentation

string PLearn::Matern1ARDKernel::_classname_ ( ) [static]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

OptionList & PLearn::Matern1ARDKernel::_getOptionList_ ( ) [static]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

RemoteMethodMap & PLearn::Matern1ARDKernel::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

bool PLearn::Matern1ARDKernel::_isa_ ( const Object o) [static]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

Object * PLearn::Matern1ARDKernel::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

StaticInitializer Matern1ARDKernel::_static_initializer_ & PLearn::Matern1ARDKernel::_static_initialize_ ( ) [static]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

void PLearn::Matern1ARDKernel::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 92 of file Matern1ARDKernel.cc.

References PLearn::ARDBaseKernel::build(), and build_().

{
    // ### Nothing to add here, simply calls build_
    inherited::build();
    build_();
}

Here is the call graph for this function:

void PLearn::Matern1ARDKernel::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 102 of file Matern1ARDKernel.cc.

References PLearn::KroneckerBaseKernel::m_default_value.

Referenced by build().

{
    // Ensure that we multiply in Kronecker terms
    inherited::m_default_value = 1.0;
}

Here is the caller graph for this function:

string PLearn::Matern1ARDKernel::classname ( ) const [virtual]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

void PLearn::Matern1ARDKernel::computeGramMatrix ( Mat  K) const [virtual]

Compute the Gram Matrix.

Reimplemented from PLearn::KroneckerBaseKernel.

Definition at line 156 of file Matern1ARDKernel.cc.

References PLearn::Kernel::cache_gram_matrix, PLearn::KroneckerBaseKernel::computeGramMatrix(), PLearn::TVec< T >::data(), PLearn::Kernel::data, PLearn::Kernel::dataInputsize(), PLearn::diff(), PLearn::exp(), PLearn::TVec< T >::fill(), PLearn::Kernel::gram_matrix, PLearn::Kernel::gram_matrix_is_cached, i, j, PLearn::VMat::length(), m, PLearn::MemoryCachedKernel::m_data_cache, PLearn::ARDBaseKernel::m_input_sigma, PLearn::ARDBaseKernel::m_isp_global_sigma, PLearn::ARDBaseKernel::m_isp_input_sigma, m_isp_persistence, PLearn::ARDBaseKernel::m_isp_signal_sigma, PLearn::TMat< T >::mod(), n, PLASSERT, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::TMat< T >::size(), PLearn::TVec< T >::size(), PLearn::softplus(), and PLearn::KroneckerBaseKernel::softplusFloor().

{
    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);
    real persistence = softplus(m_isp_persistence);
    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 += fabs(diff) / *p_inpsigma++;
            //     }
            real diff;
            switch (k % 8) {
            case 0: do { diff = *x1++ - *x2++; sum_wt += fabs(diff) / *p_inpsigma++;
            case 7:      diff = *x1++ - *x2++; sum_wt += fabs(diff) / *p_inpsigma++;
            case 6:      diff = *x1++ - *x2++; sum_wt += fabs(diff) / *p_inpsigma++;
            case 5:      diff = *x1++ - *x2++; sum_wt += fabs(diff) / *p_inpsigma++;
            case 4:      diff = *x1++ - *x2++; sum_wt += fabs(diff) / *p_inpsigma++;
            case 3:      diff = *x1++ - *x2++; sum_wt += fabs(diff) / *p_inpsigma++;
            case 2:      diff = *x1++ - *x2++; sum_wt += fabs(diff) / *p_inpsigma++;
            case 1:      diff = *x1++ - *x2++; sum_wt += fabs(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 / (2.*persistence) * exp(-persistence * sum_wt);
            *Kij++ = Kij_cur;
        }
    }
    if (cache_gram_matrix) {
        gram_matrix.resize(l,l);
        gram_matrix << K;
        gram_matrix_is_cached = true;
    }
}

Here is the call graph for this function:

void PLearn::Matern1ARDKernel::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 238 of file Matern1ARDKernel.cc.

References PLearn::Kernel::computeGramMatrixDerivative(), and computeGramMatrixDerivIspSignalSigma().

{
    static const string ISS("isp_signal_sigma");
    static const string IGS("isp_global_sigma");
    static const string IIS("isp_input_sigma[");
    static const string IPe("isp_persistence");

    if (kernel_param == ISS) {
        computeGramMatrixDerivIspSignalSigma(KD);
        
        // computeGramMatrixDerivNV<
        //     Matern1ARDKernel,
        //     &Matern1ARDKernel::derivIspSignalSigma>(KD, this, -1);
    }
    /*
    else if (kernel_param == IGS) {
        computeGramMatrixDerivNV<
            Matern1ARDKernel,
            &Matern1ARDKernel::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);
}

Here is the call graph for this function:

void PLearn::Matern1ARDKernel::computeGramMatrixDerivIspInputSigma ( Mat KD,
int  arg 
) const [protected]

Compute derivative w.r.t. isp_input_sigma[arg] for WHOLE MATRIX.

Definition at line 328 of file Matern1ARDKernel.cc.

References PLearn::TMat< T >::data(), PLearn::Kernel::data, PLearn::diff(), PLearn::Kernel::gram_matrix, i, j, PLearn::TMat< T >::length(), PLearn::VMat::length(), PLearn::MemoryCachedKernel::m_data_cache, PLearn::ARDBaseKernel::m_input_sigma, PLearn::ARDBaseKernel::m_isp_global_sigma, PLearn::ARDBaseKernel::m_isp_input_sigma, PLearn::TMat< T >::mod(), PLASSERT_MSG, PLearn::TMat< T >::resize(), PLearn::sigmoid(), and PLearn::TMat< T >::width().

{
    // 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 Matern1ARDKernel.");

    // 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;
        }
    }
}

Here is the call graph for this function:

void PLearn::Matern1ARDKernel::computeGramMatrixDerivIspSignalSigma ( Mat KD) const [protected]

Compute derivative w.r.t. isp_signal_sigma for WHOLE MATRIX.

Definition at line 312 of file Matern1ARDKernel.cc.

References PLearn::Kernel::data, PLearn::Kernel::gram_matrix, PLearn::TMat< T >::length(), PLearn::VMat::length(), PLearn::ARDBaseKernel::m_isp_signal_sigma, PLASSERT_MSG, PLearn::TMat< T >::resize(), PLearn::sigmoid(), PLearn::softplus(), and PLearn::TMat< T >::width().

Referenced by computeGramMatrixDerivative().

{
    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 Matern1ARDKernel.");
    
    KD << gram_matrix;
    KD *= sigmoid(m_isp_signal_sigma)/softplus(m_isp_signal_sigma);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::Matern1ARDKernel::declareOptions ( OptionList ol) [static, protected]

Declares the class options.

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file Matern1ARDKernel.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::ARDBaseKernel::declareOptions(), and m_isp_persistence.

{
    declareOption(
        ol, "isp_persistence",
        &Matern1ARDKernel::m_isp_persistence,
        OptionBase::buildoption,
        "Inverse softplus of the O-U persistence parameter.  Default value =\n"
        "isp(1.0).");
    
    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::Matern1ARDKernel::declaringFile ( ) [inline, static]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 108 of file Matern1ARDKernel.h.

:
Matern1ARDKernel * PLearn::Matern1ARDKernel::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

real PLearn::Matern1ARDKernel::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 Matern1ARDKernel.cc.

References PLearn::fast_is_equal(), PLearn::ARDBaseKernel::m_isp_global_sigma, PLearn::ARDBaseKernel::m_isp_signal_sigma, pl_log, PLearn::sigmoid(), and PLearn::softplus().

{
    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.
}

Here is the call graph for this function:

void PLearn::Matern1ARDKernel::derivIspPersistence ( int  i,
int  j,
int  arg,
real  K 
) const [protected]

Compute derivative w.r.t. isp_persistence.

real PLearn::Matern1ARDKernel::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 Matern1ARDKernel.cc.

References PLearn::ARDBaseKernel::m_isp_signal_sigma, PLearn::sigmoid(), and PLearn::softplus().

{
    // (No longer used; see computeGramMatrixDerivIspInputSigma below)
    return K*sigmoid(m_isp_signal_sigma)/softplus(m_isp_signal_sigma);
}

Here is the call graph for this function:

real PLearn::Matern1ARDKernel::evaluate ( const Vec x1,
const Vec x2 
) const [virtual]

Compute K(x1,x2).

Reimplemented from PLearn::KroneckerBaseKernel.

Definition at line 111 of file Matern1ARDKernel.cc.

References PLearn::TVec< T >::data(), PLearn::diff(), PLearn::KroneckerBaseKernel::evaluate(), PLearn::exp(), PLearn::fast_is_equal(), i, PLearn::ARDBaseKernel::m_isp_global_sigma, PLearn::ARDBaseKernel::m_isp_input_sigma, m_isp_persistence, PLearn::ARDBaseKernel::m_isp_signal_sigma, 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) /
            (2*softplus(m_isp_persistence)) * gating_term;
    
    const real* px1 = x1.data();
    const real* px2 = x2.data();
    real sf         = softplus(m_isp_signal_sigma);
    real persistence= softplus(m_isp_persistence);
    real expval     = 0.0;

    // Case where we have real ARD
    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 absdiff = fabs(diff);
            expval      += absdiff / softplus(m_isp_global_sigma + *pinpsig++);
        }
    }
    // No ARD
    else {
        real global_sigma = softplus(m_isp_global_sigma);
        for (int i=0, n=x1.size() ; i<n ; ++i) {
            real diff    = *px1++ - *px2++;
            real absdiff = fabs(diff);
            expval      += absdiff / global_sigma;
        }
    }

    // Gate by Kronecker term
    return sf / (2. * persistence) * exp(-persistence * expval) * gating_term;
}

Here is the call graph for this function:

void PLearn::Matern1ARDKernel::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 Matern1ARDKernel.cc.

References PLearn::Kernel::k_xi_x, and x.

{
    evaluateAllIXNV<Matern1ARDKernel>(x, k_xi_x, istart);
}
OptionList & PLearn::Matern1ARDKernel::getOptionList ( ) const [virtual]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

OptionMap & PLearn::Matern1ARDKernel::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

RemoteMethodMap & PLearn::Matern1ARDKernel::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 66 of file Matern1ARDKernel.cc.

void PLearn::Matern1ARDKernel::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 385 of file Matern1ARDKernel.cc.

References PLearn::ARDBaseKernel::makeDeepCopyFromShallowCopy().

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 108 of file Matern1ARDKernel.h.

Inverse softplus of the O-U persistence parameter.

Default value = isp(1.0).

Definition at line 78 of file Matern1ARDKernel.h.

Referenced by computeGramMatrix(), declareOptions(), and evaluate().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines