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

#include <AdditiveNormalizationKernel.h>

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

List of all members.

Public Member Functions

 AdditiveNormalizationKernel ()
 Default constructor.
 AdditiveNormalizationKernel (Ker the_source, bool remove_bias=false, bool remove_bias_in_evaluate=false, bool double_centering=false)
 Created from an existing kernel.
virtual void build ()
 Simply calls inherited::build() then build_().
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual
AdditiveNormalizationKernel
deepCopy (CopiesMap &copies) const
virtual real evaluate (const Vec &x1, const Vec &x2) const
 Overridden.
virtual real evaluate_i_j (int i, int j) const
 returns evaluate(data(i),data(j))
virtual real evaluate_i_x (int i, const Vec &x, real squared_norm_of_x=-1) const
 Return evaluate(data(i),x).
virtual real evaluate_x_i (const Vec &x, int i, real squared_norm_of_x=-1) const
 returns evaluate(x,data(i)) [default version calls evaluate_i_x if kernel is_symmetric]
virtual real evaluate_i_x_again (int i, const Vec &x, real squared_norm_of_x=-1, bool first_time=false) const
 Return evaluate(data(i),x), where x is the same as in the precedent call to this same function (except if 'first_time' is true).
virtual real evaluate_x_i_again (const Vec &x, int i, real squared_norm_of_x=-1, bool first_time=false) const
virtual void computeGramMatrix (Mat K) const
 Call evaluate_i_j to fill each of the entries (i,j) of symmetric matrix K.
virtual void setDataForKernelMatrix (VMat the_data)
 ** Subclasses may override these methods to provide efficient kernel matrix access **

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

bool data_will_change
bool double_centering
bool remove_bias
bool remove_bias_in_evaluate

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

real computeAverage (const Vec &x, bool on_row, real squared_norm_of_x=-1) const
 Return the average of K(x,x_i) or K(x_i,x), depending on the value of 'on_row' (true or false, respectively).

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares this class' options.

Protected Attributes

Vec average_col
Vec average_row
real total_average
real total_average_unbiased
real avg_evaluate_i_x_again
 The last average computed in evaluate_i_x_again().
real avg_evaluate_x_i_again
 The last average computed in evaluate_x_i_again().
real factor
 A multiplicative factor to scale the result (1 or -1/2).

Private Types

typedef SourceKernel inherited

Private Member Functions

void build_ ()
 This does the actual building.

Private Attributes

Vec all_k_x
 Used to store the values of the source kernel.

Detailed Description

Definition at line 52 of file AdditiveNormalizationKernel.h.


Member Typedef Documentation

Reimplemented from PLearn::SourceKernel.

Definition at line 57 of file AdditiveNormalizationKernel.h.


Constructor & Destructor Documentation

PLearn::AdditiveNormalizationKernel::AdditiveNormalizationKernel ( )

Default constructor.

Definition at line 52 of file AdditiveNormalizationKernel.cc.

PLearn::AdditiveNormalizationKernel::AdditiveNormalizationKernel ( Ker  the_source,
bool  remove_bias = false,
bool  remove_bias_in_evaluate = false,
bool  double_centering = false 
)

Created from an existing kernel.

Definition at line 62 of file AdditiveNormalizationKernel.cc.

    : total_average(0.),
      total_average_unbiased(0.),
      data_will_change(false),
      double_centering(the_double_centering),
      remove_bias(the_remove_bias),
      remove_bias_in_evaluate(the_remove_bias_in_evaluate)
{
    source_kernel = the_source;
    build();
}

Member Function Documentation

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

void PLearn::AdditiveNormalizationKernel::build ( ) [virtual]

Simply calls inherited::build() then build_().

Reimplemented from PLearn::SourceKernel.

Definition at line 131 of file AdditiveNormalizationKernel.cc.

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

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::SourceKernel.

Definition at line 141 of file AdditiveNormalizationKernel.cc.

References double_centering, and factor.

Referenced by build().

{
    // ### This method should do the real building of the object,
    // ### according to set 'options', in *any* situation. 
    // ### Typical situations include:
    // ###  - Initial building of an object from a few user-specified options
    // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
    // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
    // ### You should assume that the parent class' build_() has already been called.
    if (double_centering)
        factor = -0.5;
    else
        factor = 1;
}

Here is the caller graph for this function:

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

real PLearn::AdditiveNormalizationKernel::computeAverage ( const Vec x,
bool  on_row,
real  squared_norm_of_x = -1 
) const [inline, protected]

Return the average of K(x,x_i) or K(x_i,x), depending on the value of 'on_row' (true or false, respectively).

Definition at line 159 of file AdditiveNormalizationKernel.cc.

References all_k_x, PLearn::Kernel::is_symmetric, PLearn::Kernel::n_examples, PLearn::TVec< T >::resize(), PLearn::SourceKernel::source_kernel, and PLearn::sum().

Referenced by evaluate(), evaluate_i_x(), evaluate_i_x_again(), evaluate_x_i(), and evaluate_x_i_again().

                                                                                                        {
    all_k_x.resize(n_examples);
    if (is_symmetric || !on_row) {
        source_kernel->evaluate_all_i_x(x, all_k_x, squared_norm_of_x);
    } else {
        source_kernel->evaluate_all_x_i(x, all_k_x, squared_norm_of_x);
    }
    return sum(all_k_x) / real(n_examples);
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Call evaluate_i_j to fill each of the entries (i,j) of symmetric matrix K.

Reimplemented from PLearn::SourceKernel.

Definition at line 172 of file AdditiveNormalizationKernel.cc.

                                                               {
    // Uses default Kernel implementation.
    Kernel::computeGramMatrix(K);
}
void PLearn::AdditiveNormalizationKernel::declareOptions ( OptionList ol) [static, protected]

Declares this class' options.

Reimplemented from PLearn::SourceKernel.

Definition at line 90 of file AdditiveNormalizationKernel.cc.

References average_col, average_row, PLearn::OptionBase::buildoption, data_will_change, PLearn::declareOption(), PLearn::SourceKernel::declareOptions(), double_centering, PLearn::OptionBase::learntoption, remove_bias, remove_bias_in_evaluate, total_average, and total_average_unbiased.

{
    // Build options.

    declareOption(ol, "double_centering", &AdditiveNormalizationKernel::double_centering, OptionBase::buildoption,
                  "If set to 1, then the resulting kernel will be multiplied by -1/2,\n"
                  "which corresponds to the double-centering formula.");
  
    declareOption(ol, "data_will_change", &AdditiveNormalizationKernel::data_will_change, OptionBase::buildoption,
                  "If set to 1, then the Gram matrix will be always recomputed, even if\n"
                  "it's not completely sure the data has changed.");

    declareOption(ol, "remove_bias", &AdditiveNormalizationKernel::remove_bias, OptionBase::buildoption,
                  "If set to 1, then the bias induced by the K(x_i,x_i) will be removed.\n");

    declareOption(ol, "remove_bias_in_evaluate", &AdditiveNormalizationKernel::remove_bias_in_evaluate, OptionBase::buildoption,
                  "If set to 1, then the bias induced by the K(x_i,x_i) will be removed, but only when\n"
                  "evaluating K(x,y) on test points (you don't need to do this if 'remove_bias' == 1).");

    // Learnt options.

    declareOption(ol, "average_col", &AdditiveNormalizationKernel::average_col, OptionBase::learntoption,
                  "The average of the underlying kernel over each column of the Gram matrix.");

    declareOption(ol, "average_row", &AdditiveNormalizationKernel::average_row, OptionBase::learntoption,
                  "The average of the underlying kernel over each row of the Gram matrix.");

    declareOption(ol, "total_average_unbiased", &AdditiveNormalizationKernel::total_average_unbiased, OptionBase::learntoption,
                  "The average of the underlying kernel over the whole Gram matrix, without\n"
                  "the diagonal terms.");

    declareOption(ol, "total_average", &AdditiveNormalizationKernel::total_average, OptionBase::learntoption,
                  "The average of the underlying kernel over the whole Gram matrix.");

    // Now call the parent class' declareOptions
    inherited::declareOptions(ol);
}

Here is the call graph for this function:

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

Reimplemented from PLearn::SourceKernel.

Definition at line 138 of file AdditiveNormalizationKernel.h.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Overridden.

Reimplemented from PLearn::SourceKernel.

Definition at line 180 of file AdditiveNormalizationKernel.cc.

References computeAverage(), factor, remove_bias, remove_bias_in_evaluate, PLearn::SourceKernel::source_kernel, total_average, and total_average_unbiased.

                                                                             {
    real avg_1 = computeAverage(x1, true);
    real avg_2 = computeAverage(x2, false);
    if (remove_bias || !remove_bias_in_evaluate) {
        // We can use the 'total_average'.
        return factor * (source_kernel->evaluate(x1, x2) - avg_1 - avg_2 + total_average);
    } else {
        // We need to use the 'total_average_unbiased'.
        return factor * (source_kernel->evaluate(x1, x2) - avg_1 - avg_2 + total_average_unbiased);
    }
}

Here is the call graph for this function:

real PLearn::AdditiveNormalizationKernel::evaluate_i_j ( int  i,
int  j 
) const [virtual]

returns evaluate(data(i),data(j))

Reimplemented from PLearn::SourceKernel.

Definition at line 195 of file AdditiveNormalizationKernel.cc.

References average_col, average_row, factor, i, j, PLearn::SourceKernel::source_kernel, and total_average.

                                                                 {
    return factor * (source_kernel->evaluate_i_j(i,j) - average_row[i] - average_col[j] + total_average);
}
real PLearn::AdditiveNormalizationKernel::evaluate_i_x ( int  i,
const Vec x,
real  squared_norm_of_x = -1 
) const [virtual]

Return evaluate(data(i),x).

[squared_norm_of_x is just a hint that may allow to speed up computation if it is already known, but it's optional]

Reimplemented from PLearn::SourceKernel.

Definition at line 202 of file AdditiveNormalizationKernel.cc.

References average_row, computeAverage(), factor, i, PLearn::SourceKernel::source_kernel, and total_average.

                                                                                                {
    return factor * (source_kernel->evaluate_i_x(i, x, squared_norm_of_x)
                     - average_row[i] - computeAverage(x, false, squared_norm_of_x) + total_average);
}

Here is the call graph for this function:

real PLearn::AdditiveNormalizationKernel::evaluate_i_x_again ( int  i,
const Vec x,
real  squared_norm_of_x = -1,
bool  first_time = false 
) const [virtual]

Return evaluate(data(i),x), where x is the same as in the precedent call to this same function (except if 'first_time' is true).

This can be used to speed up successive computations of K(x_i, x) (default version just calls evaluate_i_x).

Reimplemented from PLearn::Kernel.

Definition at line 210 of file AdditiveNormalizationKernel.cc.

References average_row, avg_evaluate_i_x_again, computeAverage(), factor, i, PLearn::SourceKernel::source_kernel, and total_average.

                                                                                                                       {
    if (first_time) {
        avg_evaluate_i_x_again = computeAverage(x, false, squared_norm_of_x);
    }
    return factor * (source_kernel->evaluate_i_x_again(i, x, squared_norm_of_x, first_time)
                     - average_row[i] - avg_evaluate_i_x_again + total_average);
}

Here is the call graph for this function:

real PLearn::AdditiveNormalizationKernel::evaluate_x_i ( const Vec x,
int  i,
real  squared_norm_of_x = -1 
) const [virtual]

returns evaluate(x,data(i)) [default version calls evaluate_i_x if kernel is_symmetric]

Reimplemented from PLearn::SourceKernel.

Definition at line 221 of file AdditiveNormalizationKernel.cc.

References average_col, computeAverage(), factor, i, PLearn::SourceKernel::source_kernel, and total_average.

                                                                                                {
    return factor * (source_kernel->evaluate_x_i(x, i, squared_norm_of_x)
                     - average_col[i] - computeAverage(x, true, squared_norm_of_x) + total_average);
}

Here is the call graph for this function:

real PLearn::AdditiveNormalizationKernel::evaluate_x_i_again ( const Vec x,
int  i,
real  squared_norm_of_x = -1,
bool  first_time = false 
) const [virtual]

Reimplemented from PLearn::Kernel.

Definition at line 229 of file AdditiveNormalizationKernel.cc.

References average_col, avg_evaluate_x_i_again, computeAverage(), factor, i, PLearn::SourceKernel::source_kernel, and total_average.

                                                                                                                       {
    if (first_time) {
        avg_evaluate_x_i_again = computeAverage(x, true, squared_norm_of_x);
    }
    return factor * (source_kernel->evaluate_x_i_again(x, i, squared_norm_of_x, first_time)
                     - average_col[i] - avg_evaluate_x_i_again + total_average);
}

Here is the call graph for this function:

OptionList & PLearn::AdditiveNormalizationKernel::getOptionList ( ) const [virtual]

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 85 of file AdditiveNormalizationKernel.cc.

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::SourceKernel.

Definition at line 240 of file AdditiveNormalizationKernel.cc.

References all_k_x, average_col, average_row, PLearn::deepCopyField(), and PLearn::SourceKernel::makeDeepCopyFromShallowCopy().

Here is the call graph for this function:

void PLearn::AdditiveNormalizationKernel::setDataForKernelMatrix ( VMat  the_data) [virtual]

** Subclasses may override these methods to provide efficient kernel matrix access **

This method sets the data VMat that will be used to define the kernel matrix. It may precompute values from this that may later accelerate the evaluation of a kernel matrix element

Reimplemented from PLearn::SourceKernel.

Definition at line 251 of file AdditiveNormalizationKernel.cc.

References average_col, average_row, PLearn::Kernel::data, data_will_change, PLearn::TVec< T >::fill(), i, PLearn::Kernel::is_symmetric, j, PLearn::TVec< T >::length(), PLearn::VMat::length(), n, remove_bias, PLearn::TVec< T >::resize(), PLearn::SourceKernel::setDataForKernelMatrix(), PLearn::SourceKernel::source_kernel, PLearn::sum(), total_average, and total_average_unbiased.

                                                                      {
    bool there_was_data_and_it_changed = data && !(data->looksTheSameAs(the_data));
    // Set the data for this kernel as well as for the underlying kernel.
    inherited::setDataForKernelMatrix(the_data);
    // Check whether we need to recompute the Gram matrix and its average.
    int n = the_data->length();
    if (   data_will_change
           || average_row.length() != n
           || there_was_data_and_it_changed) {
        // Compute the underlying Gram matrix.
        Mat gram(n, n);
        source_kernel->computeGramMatrix(gram);
        // Compute the row (and column) average.
        average_row.resize(n);
        average_row.fill(0);
        if (is_symmetric) {
            average_col = average_row;
        } else {
            average_col.resize(n);
            average_col.fill(0);
        }
        real k_x_x;
        total_average_unbiased = 0;
        for (int i = 0; i < n; i++) {
            if (is_symmetric) {
                real v;
                k_x_x = gram(i,i);
                if (!remove_bias) {
                    average_row[i] += k_x_x;
                    total_average_unbiased -= k_x_x;
                }
                for (int j = i + 1; j < n; j++) {
                    v = gram(i,j);
                    average_row[i] += v;
                    average_row[j] += v;
                }
            } else {
                for (int j = 0; j < n; j++) {
                    if (!remove_bias || j != i) {
                        average_row[i] += gram(i,j);
                        average_col[i] += gram(j,i);
                        if (j == i) {
                            total_average_unbiased -= gram(i,j);
                        }
                    }
                }
            }
        }
        total_average = sum(average_row);
        if (remove_bias) {
            // The sum is already unbiased.
            total_average_unbiased = total_average;
        } else {
            // At this point, 'total_average_unbiased' = - \sum K(x_i,x_i).
            total_average_unbiased += total_average;
        }
        real n_terms_in_sum;    // The number of terms summed in average_row.
        if (remove_bias) {
            // The diagonal terms were not added.
            n_terms_in_sum = real(n - 1);
        } else {
            n_terms_in_sum = real(n);
        }
        total_average /= real(n * n_terms_in_sum);
        total_average_unbiased /= real(n * (n-1));
        average_row /= n_terms_in_sum;
        if (!is_symmetric) {
            average_col /= n_terms_in_sum;
        }
    }
}

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::SourceKernel.

Definition at line 138 of file AdditiveNormalizationKernel.h.

Used to store the values of the source kernel.

Definition at line 60 of file AdditiveNormalizationKernel.h.

Referenced by computeAverage(), and makeDeepCopyFromShallowCopy().

The last average computed in evaluate_i_x_again().

Definition at line 76 of file AdditiveNormalizationKernel.h.

Referenced by evaluate_i_x_again().

The last average computed in evaluate_x_i_again().

Definition at line 79 of file AdditiveNormalizationKernel.h.

Referenced by evaluate_x_i_again().

Definition at line 90 of file AdditiveNormalizationKernel.h.

Referenced by declareOptions(), and setDataForKernelMatrix().

Definition at line 91 of file AdditiveNormalizationKernel.h.

Referenced by build_(), and declareOptions().

A multiplicative factor to scale the result (1 or -1/2).

Definition at line 82 of file AdditiveNormalizationKernel.h.

Referenced by build_(), evaluate(), evaluate_i_j(), evaluate_i_x(), evaluate_i_x_again(), evaluate_x_i(), and evaluate_x_i_again().

Definition at line 92 of file AdditiveNormalizationKernel.h.

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

Definition at line 93 of file AdditiveNormalizationKernel.h.

Referenced by declareOptions(), and evaluate().

Definition at line 71 of file AdditiveNormalizationKernel.h.

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


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