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

Neural network kernel that can be used for Automatic Relevance Determination. More...

#include <NeuralNetworkARDKernel.h>

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

List of all members.

Public Member Functions

 NeuralNetworkARDKernel ()
 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; for now, this mostly maps to 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 NeuralNetworkARDKerneldeepCopy (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 ()

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

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.

Detailed Description

Neural network kernel that can be used for Automatic Relevance Determination.

This kernel is designed to be used within a GaussianProcessRegressor. It is similar to the "arcsin" kernel of C.E. Rasmussen's GPML code (see http://www.gaussianprocess.org), but can be used with full Automatic Relevance Determination (ARD). It takes the form:

k(x,y) = sf * asin(2*x*P*y / sqrt((1+2*x*P*x)*(1+2*y*P*y))) * k_kron(x,y)

where sf is softplus(isp_signal_sigma), P is softplus(isp_global_sigma + isp_input_sigma[i])^-2 times the unit matrix, where the x and y vectors on the right-hand-side have an extra bias (1.0) added in front. (Note that if ARD is desired, the number of elements provided for isp_input_sigma must be ONE MORE than the number of inputs, and the first element of the isp_input_sigma vector corresponds to this bias). Also note that in keeping with Rasmussen and Williams, we raise these elements to the -2 power, so these hyperparameters can be interpreted as true length-scales. The last factor 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).

See SquaredExponentialARDKernel for more information about using this kernel within a SummationKernel in order to add IID noise to the examples.

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 80 of file NeuralNetworkARDKernel.h.


Member Typedef Documentation

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 82 of file NeuralNetworkARDKernel.h.


Constructor & Destructor Documentation

PLearn::NeuralNetworkARDKernel::NeuralNetworkARDKernel ( )

Default constructor.

Definition at line 79 of file NeuralNetworkARDKernel.cc.

{ }

Member Function Documentation

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

void PLearn::NeuralNetworkARDKernel::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 94 of file NeuralNetworkARDKernel.cc.

{
    // ### Nothing to add here, simply calls build_
    inherited::build();
    build_();
}
void PLearn::NeuralNetworkARDKernel::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 104 of file NeuralNetworkARDKernel.cc.

{
    // Ensure that we multiply in Kronecker terms
    inherited::m_default_value = 1.0;
}
string PLearn::NeuralNetworkARDKernel::classname ( ) const [virtual]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Compute the Gram Matrix.

Reimplemented from PLearn::KroneckerBaseKernel.

Definition at line 181 of file NeuralNetworkARDKernel.cc.

References DUFF_DOTLOOP, i, j, m, PLearn::TMat< T >::mod(), n, PLASSERT, PLearn::TMat< T >::size(), PLearn::softplus(), and PLearn::sqrt().

{
    PLASSERT( !m_isp_input_sigma.size() || dataInputsize()+1 == 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() + 1);
    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]);
        m_input_sigma[i] *= m_input_sigma[i];
        m_input_sigma[i]  = 2. / 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;
            int  k = n;

            // Handle the bias for x1 and x2
            real sigma     = *p_inpsigma++;
            real dot_x1_x1 = sigma;
            real dot_x2_x2 = sigma;
            real dot_x1_x2 = sigma;

            switch (k % 8) {
            case 0: do {  DUFF_DOTLOOP
            case 7:       DUFF_DOTLOOP
            case 6:       DUFF_DOTLOOP
            case 5:       DUFF_DOTLOOP
            case 4:       DUFF_DOTLOOP
            case 3:       DUFF_DOTLOOP
            case 2:       DUFF_DOTLOOP
            case 1:       DUFF_DOTLOOP  } 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 * asin(dot_x1_x2 / sqrt((1 + dot_x1_x1) * (1 + dot_x2_x2)));
            *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::NeuralNetworkARDKernel::computeGramMatrixDerivative ( Mat KD,
const string &  kernel_param,
real  epsilon = 1e-6 
) const [virtual]

Directly compute the derivative with respect to hyperparameters; for now, this mostly maps to finite differences.

Reimplemented from PLearn::Kernel.

Definition at line 262 of file NeuralNetworkARDKernel.cc.

{
    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);
    }
    // else if (kernel_param == IGS) {
    //     computeGramMatrixDerivNV<
    //         NeuralNetworkARDKernel,
    //         &NeuralNetworkARDKernel::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::NeuralNetworkARDKernel::computeGramMatrixDerivIspInputSigma ( Mat KD,
int  arg 
) const [protected]

Definition at line 336 of file NeuralNetworkARDKernel.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 NeuralNetworkARDKernel.");

    // 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::NeuralNetworkARDKernel::computeGramMatrixDerivIspSignalSigma ( Mat KD) const [protected]

Definition at line 320 of file NeuralNetworkARDKernel.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 NeuralNetworkARDKernel.");
    
    KD << gram_matrix;
    KD *= sigmoid(m_isp_signal_sigma)/softplus(m_isp_signal_sigma);
}

Here is the call graph for this function:

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

Declares the class options.

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 85 of file NeuralNetworkARDKernel.cc.

{
    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}
static const PPath& PLearn::NeuralNetworkARDKernel::declaringFile ( ) [inline, static]

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 117 of file NeuralNetworkARDKernel.h.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

real PLearn::NeuralNetworkARDKernel::derivIspGlobalSigma ( int  i,
int  j,
int  arg,
real  K 
) const [protected]

Derivative function with respect to isp_global_sigma.

Definition at line 303 of file NeuralNetworkARDKernel.cc.

References PLearn::fast_is_equal(), 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:

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

Compute K(x1,x2).

Reimplemented from PLearn::KroneckerBaseKernel.

Definition at line 113 of file NeuralNetworkARDKernel.cc.

References PLearn::TVec< T >::data(), PLearn::fast_is_equal(), i, n, PLASSERT, PLearn::TVec< T >::size(), PLearn::softplus(), and PLearn::sqrt().

{
    PLASSERT( x1.size() == x2.size() );
    PLASSERT( !m_isp_input_sigma.size() || x1.size()+1 == m_isp_input_sigma.size() );

    real gating_term = inherited::evaluate(x1,x2);
    if (fast_is_equal(gating_term, 0.0) || x1.size() == 0)
        return 0.0;
    
    const real* px1 = x1.data();
    const real* px2 = x2.data();
    real sf         = softplus(m_isp_signal_sigma);
    real dot_x1_x1;
    real dot_x2_x2;
    real dot_x1_x2;
    
    if (m_isp_input_sigma.size() > 0) {
        const real* pinpsig = m_isp_input_sigma.data();
        real sigma = softplus(*pinpsig++);
        sigma *= sigma;
        sigma  = 2. / sigma;

        // Handle bias
        dot_x1_x1 = dot_x2_x2 = dot_x1_x2 = sigma;
 
        for (int i=0, n=x1.size() ; i<n ; ++i, ++px1, ++px2) {
            sigma  = softplus(*pinpsig++);
            sigma *= sigma;
            sigma  = 2. / sigma;

            dot_x1_x2 += *px1 * *px2 * sigma;
            dot_x1_x1 += *px1 * *px1 * sigma;
            dot_x2_x2 += *px2 * *px2 * sigma;
        }
    }
    else {
        real global_sigma = softplus(m_isp_global_sigma);
        global_sigma *= global_sigma;
        global_sigma  = 2. / global_sigma;

        // Handle bias for x1 and x2
        dot_x1_x1 = dot_x2_x2 = dot_x1_x2 = 1;
        
        for (int i=0, n=x1.size() ; i<n ; ++i, ++px1, ++px2) {
            dot_x1_x2 += *px1 * *px2;
            dot_x1_x1 += *px1 * *px1;
            dot_x2_x2 += *px2 * *px2;
        }
        dot_x1_x2 *= global_sigma;
        dot_x1_x1 *= global_sigma;
        dot_x2_x2 *= global_sigma;
    }

    // Gate by Kronecker term
    return sf * asin(dot_x1_x2 / sqrt((1 + dot_x1_x1) * (1 + dot_x2_x2))) * gating_term;
}

Here is the call graph for this function:

void PLearn::NeuralNetworkARDKernel::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 294 of file NeuralNetworkARDKernel.cc.

References x.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 76 of file NeuralNetworkARDKernel.cc.

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 393 of file NeuralNetworkARDKernel.cc.


Member Data Documentation

Reimplemented from PLearn::ARDBaseKernel.

Definition at line 117 of file NeuralNetworkARDKernel.h.


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