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
PLearn::ThresholdedKernel Class Reference

#include <ThresholdedKernel.h>

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

List of all members.

Public Member Functions

 ThresholdedKernel ()
 Default constructor.
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 ThresholdedKerneldeepCopy (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
 Overridden for a more efficient implementation when the underlying kernel has cached its Gram matrix.
virtual void setDataForKernelMatrix (VMat the_data)
 Overridden to precompute nearest neighbors in the dataset.

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

int knn
real knn_approximation
int max_size_for_full_gram
string method
real threshold

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

virtual void thresholdGramMatrix (const Mat &K) const
 Replace all elements in the Gram matrix K by 'threshold' when they meet the thresholding condition defined by the thresholding method.
void evaluate_random_k_x_i (const Vec &x, const Vec &k_x_xi) const
 Fill 'k_x_xi' with values of the source kernel evaluated at points xi sampled randomly from the dataset.

Static Protected Member Functions

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

Protected Attributes

bool knn_approx
 Set to 'true' at build time iff we use the knn approximation, i.e.
int knn_sub
 Number of actual nearest neighbors to consider (will be less than 'knn' when using the knn approximation, otherwise will be equal to 'knn').
int n_approx
 Set to the number of points to sample when using the knn approximation.
Vec knn_kernel_values
 Element i is equal to K(x_i, n_knn(x_i)) with K the source kernel.
Vec k_x_xi
 Used to store kernel values.
Mat k_x_xi_mat
 Points to the same data as k_x_xi;.
real k_x_threshold
 The value K(x, n_knn(x)).

Private Types

typedef SourceKernel inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 51 of file ThresholdedKernel.h.


Member Typedef Documentation

Reimplemented from PLearn::SourceKernel.

Definition at line 56 of file ThresholdedKernel.h.


Constructor & Destructor Documentation

PLearn::ThresholdedKernel::ThresholdedKernel ( )

Default constructor.

Definition at line 53 of file ThresholdedKernel.cc.

                                    :
    knn(2),
    knn_approximation(0),
    max_size_for_full_gram(5000),
    method("knn"),
    threshold(0)
{
}

Member Function Documentation

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

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

Reimplemented from PLearn::SourceKernel.

Definition at line 104 of file ThresholdedKernel.cc.

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

Here is the call graph for this function:

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

This does the actual building.

Reimplemented from PLearn::SourceKernel.

Definition at line 113 of file ThresholdedKernel.cc.

References PLearn::fast_exact_is_equal(), knn_approx, knn_approximation, n_approx, PLASSERT, PLERROR, and PLearn::SourceKernel::source_kernel.

Referenced by build().

{
    if (source_kernel && !source_kernel->is_symmetric)
        PLERROR("In ThresholdedKernel::build_ - The source kernel must currently "
                "be symmetric");
    PLASSERT( knn_approximation >= 0);
    knn_approx = !fast_exact_is_equal(knn_approximation, 0);
    if (knn_approx) {
        if (knn_approximation > 1)
            n_approx = int(round(knn_approximation));
        // Otherwise, n_approx is set in setDataForKernelMatrix.
    } else
        n_approx = -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Overridden for a more efficient implementation when the underlying kernel has cached its Gram matrix.

Reimplemented from PLearn::SourceKernel.

Definition at line 131 of file ThresholdedKernel.cc.

References PLearn::Kernel::cache_gram_matrix, PLearn::Kernel::gram_matrix, PLearn::Kernel::gram_matrix_is_cached, PLearn::TMat< T >::length(), PLearn::TMat< T >::resize(), PLearn::SourceKernel::source_kernel, and thresholdGramMatrix().

                                                     {
    if (cache_gram_matrix && gram_matrix_is_cached) {
        K << gram_matrix;
        return;
    }
    source_kernel->computeGramMatrix(K);
    thresholdGramMatrix(K);
    if (cache_gram_matrix) {
        int l = K.length();
        gram_matrix.resize(l,l);
        gram_matrix << K;
        gram_matrix_is_cached = true;
    }
}

Here is the call graph for this function:

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

Declares this class' options.

Reimplemented from PLearn::SourceKernel.

Definition at line 70 of file ThresholdedKernel.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::SourceKernel::declareOptions(), knn, knn_approximation, max_size_for_full_gram, method, and threshold.

{
    declareOption(ol, "method", &ThresholdedKernel::method, OptionBase::buildoption,
                  "Which method is used to threshold the underlying kernel:\n"
                  " - 'knn' : if y is such that K(x,y) is strictly less than K(x,n_k(x)) where n_k(x)\n"
                  "           is the k-th neighbor of x as given by K, and K(x,y) < K(n_k(y), y), then\n"
                  "           K(x,y) is thresholded\n");

    declareOption(ol, "threshold", &ThresholdedKernel::threshold, OptionBase::buildoption,
                  "The value returned when K(x,y) is thresholded.");

    declareOption(ol, "knn", &ThresholdedKernel::knn, OptionBase::buildoption,
                  "When 'method' is 'knn', this is 'k' in n_k(x) (x will be counted if in data matrix).");

    declareOption(ol, "knn_approximation", &ThresholdedKernel::knn_approximation,
                                           OptionBase::buildoption,
                  "When 'method' is 'knn', this option can take several values:\n"
                  " - 0          : it is ignored, the 'knn' nearest neighbors are computed normally\n"
                  " - p > 1      : the value of K(x,n_k(x)) is estimated by computing K(x,y) for p\n"
                  "                values of y taken randomly in the dataset (the same value of y\n"
                  "                may be taken more than once)\n"
                  " - 0 < f <= 1 : same as above, with p = f * n (n = dataset size)");

    declareOption(ol, "max_size_for_full_gram", &ThresholdedKernel::max_size_for_full_gram, OptionBase::buildoption,
                  "When the dataset has more than 'max_size_for_full_gram' samples, the full Gram\n"
                  "matrix will not be computed in memory (less efficient, but scales better).");

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

Here is the call graph for this function:

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

Reimplemented from PLearn::SourceKernel.

Definition at line 144 of file ThresholdedKernel.h.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Overridden.

Reimplemented from PLearn::SourceKernel.

Definition at line 149 of file ThresholdedKernel.cc.

References evaluate_random_k_x_i(), k_x_xi, k_x_xi_mat, knn_approx, knn_sub, method, MISSING_VALUE, PLearn::negateElements(), PLearn::partialSortRows(), PLERROR, PLearn::SourceKernel::source_kernel, and threshold.

                                                                   {
    real k_x1_x2 = source_kernel->evaluate(x1, x2);
    if (method == "knn") {
        if (knn_approx)
            evaluate_random_k_x_i(x1, k_x_xi);
        else
            source_kernel->evaluate_all_x_i(x1, k_x_xi);
        negateElements(k_x_xi);
        partialSortRows(k_x_xi_mat, knn_sub);
        if (k_x1_x2 >= - k_x_xi[knn_sub-1])
            return k_x1_x2;
        if (knn_approx)
            evaluate_random_k_x_i(x2, k_x_xi);
        else
            source_kernel->evaluate_all_i_x(k_x_xi, x2);
        partialSortRows(k_x_xi_mat, knn_sub);
        negateElements(k_x_xi);
        if (k_x1_x2 >= -k_x_xi[knn_sub-1])
            return k_x1_x2;
        return threshold;
    }
    PLERROR("ThresholdedKernel::evaluate: unsupported method '%s'", method.c_str());
    return MISSING_VALUE;
}

Here is the call graph for this function:

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

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

Reimplemented from PLearn::SourceKernel.

Definition at line 177 of file ThresholdedKernel.cc.

References knn_kernel_values, method, MISSING_VALUE, PLERROR, PLearn::SourceKernel::source_kernel, and threshold.

                                                       {
    real k_i_j = source_kernel->evaluate_i_j(i, j);
    if (method == "knn") {
        if (k_i_j >= knn_kernel_values[i] || k_i_j >= knn_kernel_values[j])
            return k_i_j;
        else
            return threshold;
    }
    PLERROR("ThresholdedKernel::evaluate_i_j: unsupported method '%s'", method.c_str());
    return MISSING_VALUE;
}
real PLearn::ThresholdedKernel::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 192 of file ThresholdedKernel.cc.

                                                                                      {
    // Default = uses the Kernel implementation.
    // Alternative = return source_kernel->evaluate_i_x(i,x,squared_norm_of_x);
    return Kernel::evaluate_i_x(i, x, squared_norm_of_x);
}
real PLearn::ThresholdedKernel::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 201 of file ThresholdedKernel.cc.

References evaluate_random_k_x_i(), k_x_threshold, k_x_xi, k_x_xi_mat, knn_approx, knn_kernel_values, knn_sub, method, MISSING_VALUE, PLearn::negateElements(), PLearn::partialSortRows(), PLERROR, PLearn::SourceKernel::source_kernel, and threshold.

                                                                                                             {
    if (method == "knn") {
        if (first_time) {
            if (knn_approx)
                evaluate_random_k_x_i(x, k_x_xi);
            else
                source_kernel->evaluate_all_i_x(x, k_x_xi);
            negateElements(k_x_xi);
            partialSortRows(k_x_xi_mat, knn_sub);
            k_x_threshold = - k_x_xi[knn_sub - 1];
        }
        real k_i_x = source_kernel->evaluate_i_x_again(i, x, squared_norm_of_x, first_time);
        if (k_i_x >= k_x_threshold || k_i_x >= knn_kernel_values[i])
            return k_i_x;
        else
            return threshold;
    }
    PLERROR("ThresholdedKernel::evaluate_i_x_again: unsupported method '%s'", method.c_str());
    return MISSING_VALUE;
}

Here is the call graph for this function:

void PLearn::ThresholdedKernel::evaluate_random_k_x_i ( const Vec x,
const Vec k_x_xi 
) const [protected]

Fill 'k_x_xi' with values of the source kernel evaluated at points xi sampled randomly from the dataset.

Definition at line 226 of file ThresholdedKernel.cc.

References PLearn::PRandom::common(), i, j, PLearn::TVec< T >::length(), PLearn::Kernel::n_examples, and PLearn::SourceKernel::source_kernel.

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

{
    PP<PRandom> random = PRandom::common(false); // PRandom with fixed seed.
    int k = k_x_xi.length();
    for (int j = 0; j < k; j++) {
        int i = random->uniform_multinomial_sample(n_examples);
        k_x_xi[j] = source_kernel->evaluate_x_i(x,i);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

real PLearn::ThresholdedKernel::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 240 of file ThresholdedKernel.cc.

                                                                                      {
    return Kernel::evaluate_x_i(x, i, squared_norm_of_x);
}
real PLearn::ThresholdedKernel::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 247 of file ThresholdedKernel.cc.

References evaluate_random_k_x_i(), k_x_threshold, k_x_xi, k_x_xi_mat, knn_approx, knn_kernel_values, knn_sub, method, MISSING_VALUE, PLearn::negateElements(), PLearn::partialSortRows(), PLearn::SourceKernel::source_kernel, and threshold.

                                                                                                             {
    if (method == "knn") {
        if (first_time) {
            if (knn_approx)
                evaluate_random_k_x_i(x, k_x_xi);
            else
                source_kernel->evaluate_all_x_i(x, k_x_xi);
            negateElements(k_x_xi);
            partialSortRows(k_x_xi_mat, knn_sub);
            k_x_threshold = - k_x_xi[knn_sub - 1];
        }
        real k_x_i = source_kernel->evaluate_x_i_again(x, i, squared_norm_of_x, first_time);
        if (k_x_i >= k_x_threshold || k_x_i >= knn_kernel_values[i])
            return k_x_i;
        else
            return threshold;
    }
    return MISSING_VALUE;
}

Here is the call graph for this function:

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Reimplemented from PLearn::SourceKernel.

Definition at line 65 of file ThresholdedKernel.cc.

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

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::SourceKernel.

Definition at line 270 of file ThresholdedKernel.cc.

References PLearn::deepCopyField(), k_x_xi, k_x_xi_mat, knn_kernel_values, and PLearn::SourceKernel::makeDeepCopyFromShallowCopy().

Here is the call graph for this function:

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

Overridden to precompute nearest neighbors in the dataset.

Reimplemented from PLearn::SourceKernel.

Definition at line 281 of file ThresholdedKernel.cc.

References PLearn::TMat< T >::appendRow(), PLearn::Kernel::cache_gram_matrix, PLearn::PRandom::common(), PLearn::fast_exact_is_equal(), PLearn::TVec< T >::find(), PLearn::Kernel::gram_matrix, PLearn::Kernel::gram_matrix_is_cached, PLearn::TVec< T >::hasMissing(), i, j, k_x_xi, k_x_xi_mat, knn, knn_approx, knn_approximation, knn_kernel_values, knn_sub, PLearn::TVec< T >::length(), PLearn::TMat< T >::length(), PLearn::VMat::length(), max_size_for_full_gram, method, n, n_approx, PLearn::negateElements(), PLearn::partialSortRows(), PLASSERT, PLERROR, PLWARNING, PLearn::Kernel::report_progress, PLearn::TMat< T >::resize(), PLearn::TVec< T >::resize(), PLearn::SourceKernel::setDataForKernelMatrix(), PLearn::SourceKernel::source_kernel, PLearn::Kernel::sparse_gram_matrix, PLearn::Kernel::sparse_gram_matrix_is_cached, threshold, thresholdGramMatrix(), and PLearn::TVec< T >::toMat().

                                                            {
    inherited::setDataForKernelMatrix(the_data);
    int n = the_data->length();
    PP<ProgressBar> pb;
    knn_sub = knn_approx ? int(round(knn * real(n_approx) / real(n)))
                             : knn;
    if (knn_sub <= 0)
        PLERROR("In ThresholdedKernel::setDataForKernelMatrix - Not "
                "enough neighbors considered");
    if (method == "knn") {
        knn_kernel_values.resize(n);
        if (knn_approx) {
            if (knn_approximation <= 1) {
                n_approx = int(round(knn_approximation * n));
                PLASSERT( n_approx >= 1 );
            } else {
                int k_int = int(round(knn_approximation));
                if (k_int > n)
                    PLERROR("In ThresholdedKernel::setDataForKernelMatrix - "
                            "'knn_approximation' (%d) cannot be more than the "
                            " number of data points (%d)", k_int, n);
            }
        }
        if (knn_sub > n)
            PLERROR("In ThresholdedKernel::setDataForKernelMatrix - The number"
                    " of nearest neighbors to compute (%d) must be less than "
                    "the length of the dataset (%d)", knn_sub, n);
        if (n <= max_size_for_full_gram && !knn_approx) {
            // Can afford to store the Gram matrix in memory.
            gram_matrix.resize(n,n);
            source_kernel->computeGramMatrix(gram_matrix);
            if (report_progress)
                pb = new ProgressBar("Finding nearest neighbors", n);
            Mat sorted_k_i(n, 1);
            for (int i = 0; i < n; i++) {
                sorted_k_i << gram_matrix(i);
                negateElements(sorted_k_i);       // For sorting.
                partialSortRows(sorted_k_i, knn);
                knn_kernel_values[i] = - sorted_k_i(knn - 1, 0);
                if (report_progress)
                    pb->update(i+1);
            }
            if (cache_gram_matrix) {
                // Since we have the Gram matrix at hand, we may cache it now.
                thresholdGramMatrix(gram_matrix);
                gram_matrix_is_cached = true;
            } else
                // Free memory.
                gram_matrix = Mat();
        } else {
            // Computing the whole Gram matrix will probably not fit in memory,
            // or we do not even want / afford to compute it.
            if (cache_gram_matrix) {
                // We will cache the sparse Gram matrix.
                sparse_gram_matrix.resize(n);
                for (int i = 0; i < n; i++)
                    sparse_gram_matrix[i].resize(0,2);
            }
            if (report_progress)
                pb = new ProgressBar("Computing Gram matrix of source kernel and "
                                     "finding nearest neighbors", n);
            int n_used = knn_approx ? n_approx : n;
            Mat k_i_mat(n_used, 1);
            Vec k_i(n_used);
            Vec row(2);
            TVec<int> neighb_i, neighb_j;
            PP<PRandom> random = PRandom::common(false); // Has fixed seed.
            for (int i = 0; i < n; i++) {
                if (knn_approx) {
                    for (int j = 0; j < n_approx; j++) {
                        int k = random->uniform_multinomial_sample(n);
                        k_i[j] = source_kernel->evaluate_i_j(i,k);
                    }
                } else {
                    for (int j = 0; j < n; j++)
                        k_i[j] = source_kernel->evaluate_i_j(i,j);
                }
                k_i_mat << k_i;
                negateElements(k_i_mat);  // For sorting.
                partialSortRows(k_i_mat, knn_sub);
                knn_kernel_values[i] = - k_i_mat(knn_sub - 1, 0);
                if (report_progress)
                    pb->update(i+1);
                if (cache_gram_matrix) {
                    if (knn_approx)
                        PLERROR("In ThresholdedKernel::setDataForKernelMatrix "
                                "- Cannot currently cache the Gram matrix when"
                                " using the knn approximation");
                    PLASSERT( !knn_approx );
                    // Let us cache the sparse Gram matrix.
                    if (!fast_exact_is_equal(threshold, 0))
                        PLWARNING("In ThresholdedKernel::setDataForKernelMatrix - The sparse "
                                  "Gram matrix will be cached based on a non-zero threshold");
                    real k_min = knn_kernel_values[i];
                    Mat& g_i = sparse_gram_matrix[i];
                    int ki = g_i.length();
                    neighb_i.resize(ki);
                    for (int j = 0; j < ki; j++)
                        neighb_i[j] = int(g_i(j,0));
                    for (int j = 0; j < n; j++)
                        if (k_i[j] >= k_min && neighb_i.find(j) == -1) {
                            row[0] = j;
                            row[1] = k_i[j];
                            g_i.appendRow(row);
                            Mat& g_j = sparse_gram_matrix[j];
                            int kj = g_j.length();
                            bool already_there = false;
                            for (int l = 0; l < kj; l++)
                                if (i == int(g_j(0,1))) {
                                    already_there = true;
                                    break;
                                }
                            if (!already_there) {
                                row[0] = i;
                                g_j.appendRow(row);
                            }
                        }
                    sparse_gram_matrix_is_cached = true;
                }
            }
        }
    }
    k_x_xi.resize(knn_approx ? n_approx : n);
    k_x_xi_mat = k_x_xi.toMat(k_x_xi.length(), 1);
    PLASSERT( !knn_kernel_values.hasMissing() );
}

Here is the call graph for this function:

void PLearn::ThresholdedKernel::thresholdGramMatrix ( const Mat K) const [protected, virtual]

Replace all elements in the Gram matrix K by 'threshold' when they meet the thresholding condition defined by the thresholding method.

Definition at line 411 of file ThresholdedKernel.cc.

References i, j, knn_kernel_values, PLearn::TMat< T >::length(), method, n, PLERROR, PLearn::Kernel::report_progress, threshold, and PLearn::TMat< T >::width().

Referenced by computeGramMatrix(), and setDataForKernelMatrix().

                                                              {
    PP<ProgressBar> pb;
    int n = K.length();
    if (K.width() != n)
        PLERROR("In ThresholdedKernel::thresholdGramMatrix - A square matrix is expected");
    if (report_progress)
        pb = new ProgressBar("Thresholding Gram matrix", n);
    if (method == "knn") {
        for (int i = 0; i < n; i++) {
            real* K_i = K[i];
            real knn_kernel_values_i = knn_kernel_values[i];
            for (int j = 0; j < n; j++, K_i++)
                if (*K_i < knn_kernel_values_i && *K_i < knn_kernel_values[j])
                    *K_i = threshold;
            if (report_progress)
                pb->update(i+1);
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from PLearn::SourceKernel.

Definition at line 144 of file ThresholdedKernel.h.

The value K(x, n_knn(x)).

Definition at line 86 of file ThresholdedKernel.h.

Referenced by evaluate_i_x_again(), and evaluate_x_i_again().

Used to store kernel values.

Definition at line 80 of file ThresholdedKernel.h.

Referenced by evaluate(), evaluate_i_x_again(), evaluate_x_i_again(), makeDeepCopyFromShallowCopy(), and setDataForKernelMatrix().

Points to the same data as k_x_xi;.

Definition at line 83 of file ThresholdedKernel.h.

Referenced by evaluate(), evaluate_i_x_again(), evaluate_x_i_again(), makeDeepCopyFromShallowCopy(), and setDataForKernelMatrix().

Definition at line 94 of file ThresholdedKernel.h.

Referenced by declareOptions(), and setDataForKernelMatrix().

Set to 'true' at build time iff we use the knn approximation, i.e.

knn_approximation > 0.

Definition at line 62 of file ThresholdedKernel.h.

Referenced by build_(), evaluate(), evaluate_i_x_again(), evaluate_x_i_again(), and setDataForKernelMatrix().

Definition at line 95 of file ThresholdedKernel.h.

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

Element i is equal to K(x_i, n_knn(x_i)) with K the source kernel.

Definition at line 77 of file ThresholdedKernel.h.

Referenced by evaluate_i_j(), evaluate_i_x_again(), evaluate_x_i_again(), makeDeepCopyFromShallowCopy(), setDataForKernelMatrix(), and thresholdGramMatrix().

Number of actual nearest neighbors to consider (will be less than 'knn' when using the knn approximation, otherwise will be equal to 'knn').

Definition at line 66 of file ThresholdedKernel.h.

Referenced by evaluate(), evaluate_i_x_again(), evaluate_x_i_again(), and setDataForKernelMatrix().

Definition at line 96 of file ThresholdedKernel.h.

Referenced by declareOptions(), and setDataForKernelMatrix().

Set to the number of points to sample when using the knn approximation.

When it is not the case, it is set to -1.

Definition at line 70 of file ThresholdedKernel.h.

Referenced by build_(), and setDataForKernelMatrix().


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