Changeset 163


Ignore:
Timestamp:
Sep 22, 2004, 7:14:06 PM (19 years ago)
Author:
Peter
Message:

Design of Kernel class modified

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/GaussianKernelFunction.cc

    r67 r163  
    77// Thep C++ Tools
    88#include "GaussianKernelFunction.h"
     9#include "matrix.h"
    910#include "vector.h"
    1011
     
    3334}
    3435
     36
     37const theplu::gslapi::matrix&
     38GaussianKernelFunction::operator()(theplu::gslapi::matrix& k,
     39                                   const theplu::gslapi::matrix& data) const
     40{
     41  theplu::gslapi::matrix data_transposed(data);
     42  data_transposed.transpose();
     43  k = data_transposed*data;
     44  // Working with the linear linear kernel in order to avoid copying
     45  // row vectors from data matrix (many times) and calculate the
     46  // kernel element in a more transparent way.
     47  for (size_t i=0; i<k.rows()-1; i++)
     48    for (size_t j=i+1; j<k.columns(); j++)
     49      k(i,j)=k(j,i)=exp(-(k(i,i)+k(j,j)-2*k(i,j))/(2*sigma_));
     50  for (size_t i=0; i<k.rows(); i++)
     51    k(i,i)=1;
     52  return k;
     53}
     54
    3555}} // of namespace cpptools and namespace theplu
  • trunk/src/GaussianKernelFunction.h

    r67 r163  
    1515namespace cpptools {
    1616
    17   /**
    18      Class calculating one element in the kernel matrix using the
    19      Gaussian kernel.
    20   */
     17  ///
     18  /// Class for Gaussian kernel calculations.
     19  ///
    2120
    2221 
     
    2524   
    2625  public:
    27     ///Constructor taking the sigma_ , i.e. the width of the Gaussian,as
     26    ///
     27    ///Constructor taking the sigma_ , i.e. the width of the Gaussian,as
    2828    ///input. Default is sigma_ = 1.
    2929    ///
    3030    GaussianKernelFunction(double = 1);
    3131   
    32     ///Destructor
     32    ///
     33    ///Destructor
    3334    ///
    3435       virtual ~GaussianKernelFunction(void) {};
    3536   
    36     ///returning the scalar product of two vectors in feature space using the
    37     ///Gaussian kernel. @return \f$ exp((x - y)^{2}/\sigma^2) \f$ \n
     37    ///
     38    /// returning the scalar product of two vectors in feature space using the
     39    /// Gaussian kernel. @return \f$ exp((x - y)^{2}/\sigma^2) \f$ \n
    3840    ///
    3941    inline double operator()(const gslapi::vector& a1,
     
    4951                      const gslapi::vector& w2) const;
    5052         
    51   private:
     53    ///
     54    /// @kernel matrix
     55    ///
     56    const gslapi::matrix& operator()(theplu::gslapi::matrix& kernel,
     57                                     const theplu::gslapi::matrix& data) const;
     58 
     59  private:
    5260    double sigma_;
    5361
  • trunk/src/Kernel.cc

    r67 r163  
    1111
    1212Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf)
    13   : k_(data.rows(),data.rows())
     13  : k_(data.columns(),data.columns())
    1414{
    15   for(u_int i=0;i<data.rows();i++)
    16     for(u_int j=0;j<i+1;j++)
    17       k_(i,j)=kf(data[i],data[j]);
    18  
    19   // Copy lower triangle to upper triangle of Kernel matrix
    20   for(u_int i=0;i<data.rows()-1;i++)
    21     for(u_int j=i+1;j<data.rows();j++)
    22       k_(i,j)=k_(j,i);
     15  k_ = kf(k_,data);
    2316}
    2417
    2518Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf,
    2619               const gslapi::matrix& weight)
    27   : k_(data.rows(),data.rows())
     20  : k_(data.columns(),data.columns())
    2821{
    29   for(u_int i=0;i<data.rows();i++)
    30     for(u_int j=0;j<i+1;j++)
    31       k_(i,j)=kf(data[i],data[j],weight[i],weight[j]);
     22  for(u_int i=0;i<data.columns();i++)
     23    for(u_int j=i;j<data.columns();j++)
     24      k_(i,j)=k_(j,i)=kf(data[i],data[j],weight[i],weight[j]);
    3225 
    33   // Copy lower triangle to upper triangle of Kernel matrix
    34   for(u_int i=0;i<data.rows()-1;i++)
    35     for(u_int j=i+1;j<data.rows();j++)
    36       k_(i,j)=k_(j,i);
    3726}
    3827
  • trunk/src/KernelFunction.h

    r99 r163  
    88namespace gslapi {
    99  class vector;
     10  class matrix;
    1011}
    1112
     
    1314
    1415  ///
    15   ///   Virtual Class calculating one element in the kernel matrix
    16   ///   (i.e. the scalar product in feature space) from the two
    17   ///   corresponding vector in the data matrix.
     16  ///   Virtual Class calculating kernel matrix.
    1817  ///
    1918  class KernelFunction
     
    3938                              const gslapi::vector&) const = 0;
    4039   
    41   }; // class KernelFunction
     40    virtual const theplu::gslapi::matrix& operator()
     41      (theplu::gslapi::matrix& k, const gslapi::matrix& data) const = 0;
     42 
     43  }; // class KernelFunction
    4244
    4345}} // of namespace cpptools and namespace theplu
  • trunk/src/PolynomialKernelFunction.cc

    r67 r163  
    66// Thep C++ Tools
    77#include "PolynomialKernelFunction.h"
     8#include "matrix.h"
    89#include "vector.h"
    910
     
    2122{
    2223  if(order_>1)
    23     return pow(a1*a2,order_);
     24    return pow(1+a1*a2,order_);
    2425  return a1*a2;
    2526}
     
    4344}
    4445
     46const theplu::gslapi::matrix&
     47PolynomialKernelFunction::operator()(theplu::gslapi::matrix& kernel,
     48                                     const theplu::gslapi::matrix& data) const
     49{
     50  theplu::gslapi::matrix data_transposed(data.transpose());
     51  kernel = data_transposed*data;
     52  if (order_>1)
     53    for (size_t i=0; i<kernel.rows(); i++)
     54      for (size_t j=i; j<kernel.rows(); j++)
     55        kernel(i,j)=kernel(j,i)=pow(1+kernel(i,j),order_);
     56  return kernel;
     57}
    4558}} // of namespace cpptools and namespace theplu
  • trunk/src/PolynomialKernelFunction.h

    r100 r163  
    1414namespace cpptools {
    1515
    16   ///Class calculating one element in the kernel matrix using the polynomial
    17   ///kernel.
     16  ///
     17  /// Class for polynomial kernel calculations
    1818  ///
    1919
     
    2323   
    2424  public:
    25     ///Constructor taking the order of the polynomial as input. Default is
     25    ///
     26    ///Constructor taking the order of the polynomial as input. Default is
    2627    ///order=1 yielding the linear kernel function.
    2728    ///
     
    3334    virtual ~PolynomialKernelFunction(void) {};
    3435   
     36    ///
    3537    ///returning the scalar product of two vectors in feature space using the
    3638    ///polynomial kernel. @return If order is larger than one: \f$ (1+x \cdot
     
    3941    double operator()(const gslapi::vector&, const gslapi::vector&) const;
    4042
     43    ///
    4144    ///returning the scalar product of two vectors in feature space using the
    4245    ///polynomial kernel with weights.
     
    4548                      const gslapi::vector&, const gslapi::vector&) const;
    4649   
     50    ///
     51    /// @kernel matrix
     52    ///
     53    const gslapi::matrix& operator()(theplu::gslapi::matrix& kernel,
     54                                     const theplu::gslapi::matrix& data) const;
     55
    4756  private:
    4857    int order_;
Note: See TracChangeset for help on using the changeset viewer.