Changeset 307


Ignore:
Timestamp:
May 3, 2005, 3:28:29 PM (16 years ago)
Author:
Peter
Message:

changed names of Kernels and made them work

Location:
trunk
Files:
5 edited
4 moved

Legend:

Unmodified
Added
Removed
  • trunk/lib/svm/Kernel_MEV.cc

    r306 r307  
    11// $Id$
    22
    3 #include <c++_tools/svm/Kernel.h>
     3#include <c++_tools/svm/Kernel_MEV.h>
    44
    55#include <c++_tools/svm/KernelFunction.h>
     
    1010namespace svm { 
    1111
    12 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf)
    13   : k_(data.columns(),data.columns()), kf_(&kf), weighted_(false)
     12Kernel_MEV::Kernel_MEV(const gslapi::matrix& data, const KernelFunction& kf)
     13  : data_(data), kf_(&kf)
    1414{
    15   k_ = kf(k_,data);
    16   kf_=0;
    17 }
    18 
    19 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf,
    20                const gslapi::matrix& weight)
    21   : k_(data.columns(),data.columns()), kf_(&kf), weighted_(true)
    22 {
    23   for(u_int i=0;i<data.columns();i++)
    24     for(u_int j=i;j<data.columns();j++)
    25       k_(i,j)=k_(j,i)=kf(data[i],data[j],weight[i],weight[j]);
    26  
    2715}
    2816
    2917
    30 Kernel::~Kernel(void)
     18Kernel_MEV::~Kernel_MEV(void)
    3119{
    3220
    3321
     22double Kernel_MEV::operator()(size_t row, size_t column) const
     23{
     24  return (*kf_)(data_.TEMP_col_return(row),data_.TEMP_col_return(column));
     25}
     26
     27
    3428}} // of namespace svm and namespace theplu
  • trunk/lib/svm/Kernel_MEV.h

    r306 r307  
    11// $Id$
    22
    3 #ifndef _theplu_svm_kernel_
    4 #define _theplu_svm_kernel_
     3#ifndef _theplu_svm_kernel_mev
     4#define _theplu_svm_kernel_mev
    55
    66#include <c++_tools/gslapi/matrix.h>
     
    1212
    1313  ///
    14   ///   Class calculating the \f$NxN\f$ kernel matrix from the \f$MxN\f$
    15   ///   data matrix using KernelFunction. Each column in the data matrix
    16   ///   corresponds to one sample.
     14  ///   Class taking care of the \f$NxN\f$ kernel matrix, where
     15  ///   \f$N\f$ is number of samples. Type of Kernel is defined by a
     16  ///   KernelFunction. This Memory Efficient Versions (SEV) does not
     17  ///   store the kernel matrix in memory, but calculates each element
     18  ///   when it is needed.
    1719  ///   
     20  ///   @see also Kernel_SEV
    1821  ///
    19   class Kernel
     22  class Kernel_MEV
    2023  {
    2124   
    2225  public:
    2326   
     27    Kernel_MEV();
     28
    2429    ///
    2530    ///   Constructor taking the data matrix and KernelFunction as
    26     ///   input. @note Can not handle NaNs. When dealing with missing values,
    27     ///   use constructor taking a weight matrix.
    28     Kernel(const gslapi::matrix&, const KernelFunction&);
     31    ///   input.Each column in the data matrix corresponds to one
     32    ///   sample. @note Can not handle NaNs.
     33    ///
     34    Kernel_MEV(const gslapi::matrix&, const KernelFunction&);
    2935   
    3036    ///
    31     ///   Constructor taking the data matrix, the KernelFunction and a weight
    32     ///   matrix as input.
    33     Kernel(const gslapi::matrix&, const KernelFunction&, const gslapi::matrix&);
     37    ///   Constructor taking the \a data matrix, the KernelFunction and a
     38    ///   \a weight matrix as input. Each column in the data matrix
     39    ///   corresponds to one sample.
     40    ///   @todo
     41    Kernel_MEV(const gslapi::matrix& data, const KernelFunction&,
     42               const gslapi::matrix& weight);
    3443
    3544    ///
    3645    ///   Destructor
    37     virtual ~Kernel(void);
     46    ///
     47    virtual ~Kernel_MEV(void);
    3848
    39     ///   @return Kernel matrix
     49    ///
     50    /// @return element at position (\a row, \a column) of the Kernel
     51    /// matrix
    4052    ///
    41     const gslapi::matrix& get() const {return k_;}
     53    virtual double operator()(size_t row,size_t column) const;
     54
     55    ///
     56    /// @brief number of samples
     57    ///
     58    inline size_t size(void) const { return data_.columns(); }
    4259   
    43   private:
    44     gslapi::matrix k_;
     60  protected:
     61    const gslapi::matrix& data_;
    4562    const KernelFunction* kf_;
    46     bool weighted_;
    4763
    48   }; // class Kernel
     64  }; // class Kernel_MEV
    4965
    5066}} // of namespace svm and namespace theplu
  • trunk/lib/svm/Kernel_SEV.cc

    r306 r307  
    11// $Id$
    22
    3 #include <c++_tools/svm/Kernel.h>
     3#include <c++_tools/svm/Kernel_SEV.h>
    44
    55#include <c++_tools/svm/KernelFunction.h>
     6#include <c++_tools/svm/Kernel_MEV.h>
    67#include <c++_tools/gslapi/matrix.h>
    78#include <c++_tools/gslapi/vector.h>
     
    1011namespace svm { 
    1112
    12 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf)
    13   : k_(data.columns(),data.columns()), kf_(&kf), weighted_(false)
     13Kernel_SEV::Kernel_SEV(const gslapi::matrix& data, const KernelFunction& kf)
     14  : Kernel_MEV(data,kf)
    1415{
    15   k_ = kf(k_,data);
    16   kf_=0;
     16  kernel_ = gslapi::matrix(data.columns(),data.columns());
     17  for (size_t i=0; i<kernel_.rows(); i++)
     18    for (size_t j=i; j<kernel_.columns(); j++)
     19      kernel_(i,j) = kernel_(j,i) =
     20        (*kf_)(data_.TEMP_col_return(i),data_.TEMP_col_return(j));
     21
    1722}
    1823
    19 Kernel::Kernel(const gslapi::matrix& data, const KernelFunction& kf,
    20                const gslapi::matrix& weight)
    21   : k_(data.columns(),data.columns()), kf_(&kf), weighted_(true)
    22 {
    23   for(u_int i=0;i<data.columns();i++)
    24     for(u_int j=i;j<data.columns();j++)
    25       k_(i,j)=k_(j,i)=kf(data[i],data[j],weight[i],weight[j]);
    26  
    27 }
    28 
    29 
    30 Kernel::~Kernel(void)
     24Kernel_SEV::~Kernel_SEV(void)
    3125{
    3226
  • trunk/lib/svm/Kernel_SEV.h

    r306 r307  
    11// $Id$
    22
    3 #ifndef _theplu_svm_kernel_
    4 #define _theplu_svm_kernel_
     3#ifndef _theplu_svm_kernel_sev_
     4#define _theplu_svm_kernel_sev_
    55
    66#include <c++_tools/gslapi/matrix.h>
     7#include <c++_tools/svm/Kernel_MEV.h>
     8
    79
    810namespace theplu {
     
    1214
    1315  ///
    14   ///   Class calculating the \f$NxN\f$ kernel matrix from the \f$MxN\f$
    15   ///   data matrix using KernelFunction. Each column in the data matrix
    16   ///   corresponds to one sample.
     16  ///   Class taking care of the \f$NxN\f$ kernel matrix, where
     17  ///   \f$N\f$ is number of samples. Type of Kernel is defined by a
     18  ///   KernelFunction. This Speed Efficient Versions (SEV) calculated
     19  ///   the kernel matrix once and the kernel is stored in memory.
    1720  ///   
     21  ///   @see also Kernel_MEV
    1822  ///
    19   class Kernel
     23  class Kernel_SEV : public Kernel_MEV
    2024  {
    2125   
     
    2630    ///   input. @note Can not handle NaNs. When dealing with missing values,
    2731    ///   use constructor taking a weight matrix.
    28     Kernel(const gslapi::matrix&, const KernelFunction&);
     32    Kernel_SEV(const gslapi::matrix&, const KernelFunction&);
    2933   
    30     ///
    31     ///   Constructor taking the data matrix, the KernelFunction and a weight
    32     ///   matrix as input.
    33     Kernel(const gslapi::matrix&, const KernelFunction&, const gslapi::matrix&);
     34    Kernel_SEV(const Kernel_SEV&);
    3435
    3536    ///
    3637    ///   Destructor
    37     virtual ~Kernel(void);
     38    ///
     39    virtual ~Kernel_SEV(void);
    3840
    39     ///   @return Kernel matrix
     41    ///
     42    /// @return element at position (\a row, \a column) of the Kernel
     43    /// matrix
    4044    ///
    41     const gslapi::matrix& get() const {return k_;}
     45    inline double operator()(size_t row,size_t column) const
     46    { return kernel_(row,column); }
    4247   
    4348  private:
    44     gslapi::matrix k_;
    45     const KernelFunction* kf_;
    46     bool weighted_;
     49    gslapi::matrix kernel_;
    4750
    48   }; // class Kernel
     51  }; // class Kernel_SEV
    4952
    5053}} // of namespace svm and namespace theplu
  • trunk/lib/svm/Makefile.am

    r306 r307  
    1010libsvm_la_SOURCES = \
    1111  ConsensusInputRanker.cc CrossValidation.cc GaussianKernelFunction.cc \
    12   InputRanker.cc Kernel_mev.cc Kernel_sev.cc \
     12  InputRanker.cc Kernel_MEV.cc Kernel_SEV.cc \
    1313  PolynomialKernelFunction.cc  SVM.cc
  • trunk/lib/svm/SVM.cc

    r301 r307  
    44#include <c++_tools/svm/SVM.h>
    55
     6#include <c++_tools/svm/Kernel_MEV.h>
    67#include <c++_tools/gslapi/matrix.h>
    78#include <c++_tools/gslapi/vector.h>
     
    1819namespace svm { 
    1920
    20   SVM::SVM(const Kernel& kernel,
     21  SVM::SVM(const Kernel_MEV& kernel,
    2122           const gslapi::vector& target,
    2223           const std::vector<size_t>& train_set)
     
    105106    gslapi::vector target_train(train_set_.size());
    106107       
     108    // copy submatrix to train on. Peter, might be unnecessary.
    107109    for (unsigned int i=0; i<train_set_.size(); i++) {
    108110      target_train(i) = target_(train_set_[i]);
    109111      for (unsigned int j=0; j<train_set_.size(); j++)
    110         kernel_train(i,j) = kernel_.get()(train_set_[i],train_set_[j]);
     112        kernel_train(i,j) = kernel_(train_set_[i],train_set_[j]);
    111113    }
    112114    // Modify the diagonal of the kernel matrix (soft margin)
  • trunk/lib/svm/SVM.h

    r295 r307  
    44#define _theplu_svm_svm_
    55
    6 #include <c++_tools/svm/Kernel.h>
     6#include <c++_tools/svm/Kernel_MEV.h>
    77#include <c++_tools/gslapi/vector.h>
    88
     
    2929    /// Constructor taking the kernel matrix and the target vector as input
    3030    ///
    31     SVM(const Kernel&,
     31    SVM(const Kernel_MEV&,
    3232        const gslapi::vector&,
    3333        const std::vector<size_t>& = std::vector<size_t>());
     
    5555    ///
    5656    /// @return output
    57     ///
    58     inline theplu::gslapi::vector output(void)
    59     {return kernel_.get() * alpha_.mul(target_)+
    60        theplu::gslapi::vector(alpha_.size(),bias_);}
     57    /// @todo
     58    theplu::gslapi::vector output(void) const;
    6159
    6260    ///
     
    8583    double bias_;
    8684    double c_;
    87     Kernel kernel_; // Peter, const ref?
     85    const Kernel_MEV kernel_;
    8886    unsigned long int max_epochs_;
    89     gslapi::vector target_; // Peter, const ref?
     87    const gslapi::vector& target_;
    9088    bool trained_;
    9189    std::vector<size_t> train_set_;
  • trunk/test/kernel_test.cc

    r301 r307  
    77#include <c++_tools/gslapi/vector.h>
    88#include <c++_tools/svm/PolynomialKernelFunction.h>
    9 #include <c++_tools/svm/Kernel.h>
     9#include <c++_tools/svm/Kernel_MEV.h>
     10#include <c++_tools/svm/Kernel_SEV.h>
    1011
    1112#include <cmath>
     
    1718using namespace theplu;
    1819
     20bool test_MEV(const gslapi::matrix& data, const svm::KernelFunction* kf,
     21              const gslapi::matrix& control, const double error_bound)
     22{
     23  svm::Kernel_MEV kernel(data,*kf);
     24  for(u_int i=0;i<control.rows();i++)
     25    for(u_int j=0;j<control.columns();j++)
     26      if (fabs(kernel(i,j)-control(i,j))>error_bound)
     27        return false;
     28
     29  return true;
     30}
     31
     32bool test_SEV(const gslapi::matrix& data, const svm::KernelFunction* kf,
     33              const gslapi::matrix& control, const double error_bound)
     34{
     35  svm::Kernel_SEV kernel(data,*kf);
     36  for(u_int i=0;i<control.rows();i++)
     37    for(u_int j=0;j<control.columns();j++)
     38      if (fabs(kernel(i,j)-control(i,j))>error_bound)
     39        return false;
     40
     41  return true;
     42}
     43
     44
    1945int main()
    2046
    2147
    22  
    23   bool ok = true;
     48  bool ok = true;
     49  // Peter, the hardcoded number below should be changed.
     50  double error_bound = 1e-8;
    2451  std::ifstream is("data/nm_data_centralized.txt");
    2552  gslapi::matrix transposed_data(is);
    2653  is.close();
    27   // Because how the kernel is treated is changed, I have to transpose the data
     54  // Because how the kernel is treated is changed, data must be transposed.
    2855  gslapi::matrix data=transposed_data;
    2956
     
    3158  gslapi::matrix kernel_matlab(is);
    3259  is.close();
    33   svm::KernelFunction* kf =
    34     new svm::PolynomialKernelFunction();
    35   svm::Kernel kernel(data,*kf);
    36   gslapi::matrix diff(kernel.get()-kernel_matlab);
    37   double tmp=0;
    38   for(u_int i=0;i<diff.rows();i++)
    39     for(u_int j=0;j<diff.rows();j++)
    40       tmp+=fabs(diff(i,j)/kernel_matlab(i,j));
    41   delete kf;
    42   if (tmp > diff.rows()*diff.rows()/100000)
    43     ok = false;
     60  svm::KernelFunction* kf = new svm::PolynomialKernelFunction();
     61  ok = (ok && test_MEV(data,kf,kernel_matlab,error_bound)
     62        & test_SEV(data,kf,kernel_matlab,error_bound));
     63  delete kf;
    4464 
    4565  is.open("data/nm_kernel2.txt");
     
    4767  is.close();
    4868  kf = new svm::PolynomialKernelFunction(2);
    49   svm::Kernel kernel2(data,*kf);
    50   diff = kernel2.get()-kernel_matlab2;
    51   tmp=0;
    52   for(u_int i=0;i<diff.rows();i++)
    53     for(u_int j=0;j<diff.rows();j++)
    54       tmp+=fabs(diff(i,j)/kernel_matlab2(i,j));
    55   if (tmp > diff.rows()*diff.rows()/100)
    56     ok = false;
    57 
     69  ok = (ok && test_MEV(data,kf,kernel_matlab2,error_bound)
     70        & test_SEV(data,kf,kernel_matlab2,error_bound));
     71 
    5872  if (ok=true) 
    5973    return 0;
  • trunk/test/svm_test.cc

    r301 r307  
    44#include <c++_tools/gslapi/vector.h>
    55#include <c++_tools/svm/SVM.h>
    6 #include <c++_tools/svm/Kernel.h>
     6#include <c++_tools/svm/Kernel_SEV.h>
    77#include <c++_tools/svm/PolynomialKernelFunction.h>
    88
     
    6464  svm::KernelFunction* kf =
    6565    new svm::PolynomialKernelFunction();
    66   svm::Kernel kernel(data,*kf);
     66  svm::Kernel_SEV kernel(data,*kf);
    6767  //theplu::gslapi::matrix m=kernel.get();
    6868  //std::cout << "kernel:\n" << m << "\n";
Note: See TracChangeset for help on using the changeset viewer.