Changeset 537


Ignore:
Timestamp:
Mar 5, 2006, 10:30:03 AM (16 years ago)
Author:
Peter
Message:

forked tests and fixed bugs

Location:
trunk
Files:
2 added
10 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/lib/classifier/DataLookup1D.cc

    r536 r537  
    22
    33#include <c++_tools/classifier/DataLookup1D.h>
     4#include <c++_tools/classifier/MatrixLookup.h>
    45
    56#include <cassert>
     
    1112  DataLookup1D::DataLookup1D(const DataLookup2D& m, const size_t i,
    1213                             const bool row_vector)
    13     : column_vector_(!row_vector), index_(i), matrix_(&m)
     14    : column_vector_(!row_vector), index_(i), matrix_(&m), owner_(false)
    1415  {
    1516    assert( !column_vector_ || i<m.columns());
     
    1718  }
    1819 
     20  DataLookup1D::DataLookup1D(const size_t size, const double value)
     21    : column_vector_(false), index_(0), owner_(true)
     22  {
     23    matrix_ = new MatrixLookup(1,size,value);
     24  }
     25
     26  DataLookup1D::DataLookup1D(const DataLookup1D& other)
     27    : column_vector_(other.column_vector_), index_(other.index_),
     28      matrix_(other.matrix_), owner_(false)
     29  {
     30  }
     31
    1932  DataLookup1D::~DataLookup1D()
    2033  {
     34    if (owner_)
     35      delete matrix_;
    2136  }
    2237
  • trunk/lib/classifier/DataLookup1D.h

    r536 r537  
    3535
    3636    ///
     37    /// Copy constructor
     38    ///
     39    DataLookup1D(const DataLookup1D&);
     40
     41    ///
     42    /// Construct DataLookup1D that owns its underlying matrix. Object
     43    /// has size @ size and all its element is equal to @a value.
     44    ///
     45    DataLookup1D(const size_t size, const double value=0);
     46
    3747    ///
    3848    ///
    39     ~DataLookup1D();
     49    ///
     50    virtual ~DataLookup1D();
    4051
    4152    ///
     
    6071
    6172  private:
    62     DataLookup1D();
    6373
    6474    const bool column_vector_;
    6575    const size_t index_;
    6676    const DataLookup2D* matrix_;
    67 
     77    const bool owner_;
     78   
    6879  }; 
    6980  ///
  • trunk/lib/classifier/DataLookup2D.cc

    r536 r537  
    1818                             const std::vector<size_t>& row,
    1919                             const std::vector<size_t>& col)
     20    : owner_(false)
    2021  {
    2122    assert(row_index_.empty());
     
    3839                             const std::vector<size_t>& index,
    3940                             const bool row)
     41    : owner_(false)
    4042  {
    4143    if (row){
     
    6163  DataLookup2D::DataLookup2D(const std::vector<size_t>& row,
    6264                             const std::vector<size_t>& col)
    63     : row_index_(row),column_index_(col)
     65    : row_index_(row),column_index_(col), owner_(false)
    6466  {
    6567  }
     
    6870
    6971  DataLookup2D::DataLookup2D(const DataLookup2D& mv)
    70     : row_index_(mv.row_index_),column_index_(mv.column_index_)
     72    : row_index_(mv.row_index_),column_index_(mv.column_index_), owner_(false)
    7173  {
    7274  }
    7375
    7476
     77  DataLookup2D::DataLookup2D(const size_t rows, const size_t columns,
     78                             const bool owner)
     79    : row_index_(std::vector<size_t>(rows,0)),
     80      column_index_(std::vector<size_t>(columns,0)),
     81      owner_(owner)
     82  {
     83  }
     84
    7585}} // of namespace classifier and namespace theplu
  • trunk/lib/classifier/DataLookup2D.h

    r534 r537  
    2222    /// Default constructor.
    2323    ///
    24     inline DataLookup2D(void) {};
     24    inline DataLookup2D(void) : owner_(false){};
    2525
    2626
     
    4747    DataLookup2D(const DataLookup2D&, const std::vector<size_t>& index,
    4848               const bool row);
     49
     50
     51    ///
     52    ///
     53    ///
     54    DataLookup2D(const size_t, const size_t, const bool owner);
    4955
    5056
     
    9197    std::vector<size_t> row_index_;
    9298    std::vector<size_t> column_index_;
    93    
     99    bool owner_;
    94100  }; 
    95101 
  • trunk/lib/classifier/KernelLookup.cc

    r482 r537  
    55
    66
     7#include <cassert>
     8#ifndef NDEBUG
     9#include <algorithm>
     10#endif
    711
    812namespace theplu {
     
    1822  }
    1923 
    20   KernelLookup::KernelLookup(const KernelLookup& kernel,
    21                          const std::vector<size_t>& row,
    22                          const std::vector<size_t>& column)
     24  KernelLookup::KernelLookup(const Kernel& kernel,
     25                             const std::vector<size_t>& row,
     26                             const std::vector<size_t>& column)
     27    : DataLookup2D(row,column), kernel_(&kernel)
     28  {
     29    // Checking that each row index is less than kernel.rows()
     30    assert(row.empty() ||
     31           *(std::max_element(row.begin(),row.end()))<kernel.rows());
     32    // Checking that each column index is less than kernel.column()
     33    assert(column.empty() ||
     34           *(std::max_element(column.begin(),column.end()))<kernel.columns());
     35
     36  }
     37
     38
     39  KernelLookup::KernelLookup(const KernelLookup& kernel,
     40                             const std::vector<size_t>& row,
     41                             const std::vector<size_t>& column)
    2342    : DataLookup2D(kernel,row,column), kernel_(kernel.kernel_)
    2443  {
    2544  }
    2645 
     46
     47  KernelLookup::KernelLookup(const KernelLookup& kernel)
     48    : DataLookup2D(kernel), kernel_(kernel.kernel_)
     49  {
     50  }
     51 
     52
     53  KernelLookup::~KernelLookup(void)
     54  {
     55    if (owner_)
     56      delete kernel_;
     57  }
     58
    2759  const KernelLookup*
    2860  KernelLookup::training_data(const std::vector<size_t>& train) const
  • trunk/lib/classifier/KernelLookup.h

    r536 r537  
    4747   
    4848    ///
     49    /// Copy constructor
     50    ///
     51    KernelLookup(const KernelLookup&);
     52
     53
     54    ///
    4955    /// Contructor building a sub-KernelLookup from a KernelLookup
    5056    /// defined by row index vector and column index vector. The
     
    5864                 const std::vector<size_t>& column);
    5965   
    60     ///
    61     /// Copy constructor
    6266    ///
    63     KernelLookup(const KernelLookup&);
     67    /// @brief Destructor
     68    ///
     69    /// Deletes underlying Kernel if KernelLookup owns it.
     70    ///
     71    virtual ~KernelLookup(void);
    6472
    6573
  • trunk/lib/classifier/MatrixLookup.cc

    r536 r537  
    2929    assert(row.empty() ||
    3030           *(std::max_element(row.begin(),row.end()))<data.rows());
    31     // Checking that each row index is less than data.column()
     31    // Checking that each column index is less than data.column()
    3232    assert(col.empty() ||
    3333           *(std::max_element(col.begin(),col.end()))<data.columns());
     
    9595
    9696
     97  MatrixLookup::MatrixLookup(const size_t rows, const size_t columns,
     98                             const double value)
     99    : DataLookup2D(rows,columns, true)
     100  {
     101    data_ = new gslapi::matrix(1,1,value);
     102  }
     103
     104
     105  MatrixLookup::~MatrixLookup(void)
     106  {
     107    if (owner_)
     108      delete data_;
     109  }
     110
     111
    97112  const MatrixLookup*
    98113  MatrixLookup::training_data(const std::vector<size_t>& i) const
  • trunk/lib/classifier/MatrixLookup.h

    r536 r537  
    9696
    9797    ///
     98    /// Constructor creating a MatrixLookup with @a rows rows, @a
     99    /// columns columns, and all values are set to @a value. Created
     100    /// object owns its underlying matrix.
     101    ///
     102    MatrixLookup(const size_t rows, const size_t columns, const double value=0);
     103
     104    ///
    98105    /// Destructor
    99106    ///
    100     ~MatrixLookup() {};
     107    virtual ~MatrixLookup();
    101108
    102109   
  • trunk/lib/classifier/SVM.cc

    r527 r537  
    7777
    7878    sample_.init(alpha_,tolerance_);
    79     gslapi::vector  E(target_.size());
     79    gslapi::vector  E(target_.size(),0);
    8080    for (size_t i=0; i<E.size(); i++) {
    81       E(i)=0;
    8281      for (size_t j=0; j<E.size(); j++)
    8382        E(i) += kernel_mod(i,j)*target(j)*alpha_(j);
     
    192191     
    193192      // If no violation check among non-support vectors
    194 
    195193      sample_.shuffle();
    196      
    197194      for (size_t i=sample_.nof_sv(); i<sample_.n();i++){
    198         //std::cout << "nr: " << i << std::endl;
    199195        if (target_.binary(sample_(i))){
    200196          if(E(sample_(i)) < E(sample_.value_first()) - 2*tolerance_){
     
    228224    }
    229225   
    230     //std::cout << "Done!" << std::endl;
    231226    // If there is no violation then we should stop training
    232227    return false;
  • trunk/test/Makefile.am

    r533 r537  
    44
    55TESTS = alignment_test averager_test consensus_inputranker_test \
    6   crossvalidation_test ensemble_test inputranker_test \
    7   kernel_test lookup_test matrix_test \
     6  crossvalidation_test data_lookup_1d_test ensemble_test inputranker_test \
     7  kernel_test kernel_lookup_test matrix_test matrix_lookup_test \
    88  ncc_test nni_test pca_test regression_test rnd_test score_test \
    99  statistics_test stl_utility_test svd_test svm_test target_test \
     
    2222consensus_inputranker_test_SOURCES = consensus_inputranker_test.cc
    2323crossvalidation_test_SOURCES = crossvalidation_test.cc
     24data_lookup_1d_test_SOURCES = data_lookup_1d_test.cc
    2425ensemble_test_SOURCES = ensemble_test.cc
    2526inputranker_test_SOURCES = inputranker_test.cc
    2627kernel_test_SOURCES = kernel_test.cc
    27 lookup_test_SOURCES = lookup_test.cc
     28kernel_lookup_test_SOURCES = kernel_lookup_test.cc
    2829matrix_test_SOURCES = matrix_test.cc
     30matrix_lookup_test_SOURCES = matrix_lookup_test.cc
    2931ncc_test_SOURCES = ncc_test.cc
    3032nni_test_SOURCES = nni_test.cc
  • trunk/test/matrix_lookup_test.cc

    r536 r537  
    22
    33#include <c++_tools/gslapi/matrix.h>
    4 #include <c++_tools/classifier/DataLookup1D.h>
    5 #include <c++_tools/classifier/KernelLookup.h>
    6 #include <c++_tools/classifier/Kernel_SEV.h>
    74#include <c++_tools/classifier/MatrixLookup.h>
    8 #include <c++_tools/classifier/PolynomialKernelFunction.h>
    9 
    105
    116#include <fstream>
     
    1611
    1712gslapi::matrix matrix(size_t n);
    18 
    19 bool test_data_lookup1D(std::ostream* error);
    20 bool test_kernel_lookup(std::ostream* error);
    21 bool test_matrix_lookup(std::ostream* error);
    2213
    2314int main(const int argc,const char* argv[])
     
    3324      std::cout << "lookup_test -v : for printing extra information\n";
    3425  }
     26  bool ok = true;
    3527
    36   *error << "Testing Lookup Classes" << std::endl;
    37   bool ok = test_data_lookup1D(error);
    38 
    39   ok = ok && test_matrix_lookup(error);
    40  
    41   ok = ok && test_kernel_lookup(error);
    42 
    43   if (ok)
    44     *error << "Ok." << std::endl;
    45 
    46   if (error!=&std::cerr)
    47     delete error;
    48 
    49   return (ok ? 0 : -1);
    50 }
    51 
    52 gslapi::matrix matrix(size_t n)
    53 {
    54   gslapi::matrix res(n,n);
    55   for (size_t i=0;i<n;i++)
    56     for (size_t j=0;j<n;j++)
    57       res(i,j)=10*i+j;
    58   return res;
    59 }
    60 
    61 bool test_data_lookup1D(std::ostream* error)
    62 {
    63   bool ok =true;
    64   *error << "Testing DataLookup1D" << std::endl;
    65   gslapi::matrix gsl_m1(matrix(5));
    66   std::vector<size_t> index_odd;
    67   index_odd.push_back(1);
    68   index_odd.push_back(3);
    69   std::vector<size_t> index_even;
    70   index_even.push_back(2);
    71   index_even.push_back(0);
    72   index_even.push_back(4);
    73   classifier::MatrixLookup m1(gsl_m1,index_odd,index_even);
    74   *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n"
    75          << "                           const size_t, const bool)...";
    76   classifier::DataLookup1D v1(m1,1);
    77   if (v1.size()!=m1.columns() || v1(0)!=m1(1,0) ||
    78       v1(1)!=m1(1,1) ) {
    79     ok =false;
    80     *error << "\nERROR" << std::endl;
    81     *error << "size: " << v1.size() << " expected " << m1.columns() << "\n"
    82            << "v1(0): " << v1(0) << " expected " << m1(1,0) << "\n"
    83            << "v1(1): " << v1(1) << " expected " << m1(1,1)
    84            << std::endl;
    85   }
    86   else
    87     *error << "Ok" << std::endl;
    88 
    89   *error << "DataLookup1D::DataLookup1D(const MatrixLookup&\n"
    90          << "                           const size_t, const bool)...";
    91   classifier::DataLookup1D v2(m1,1,false);
    92   if (v2.size()!=m1.rows() || v2(0)!=m1(0,1) || v2(1)!=m1(1,1) ) {
    93     ok =false;
    94     *error << "\nERROR\n"
    95            << "size: " << v2.size() << " expected " << m1.rows() << "\n"
    96            << "v2(0): " << v2(0) << " expected " << m1(0,1) << "\n"
    97            << "v2(1): " << v2(1) << " expected " << m1(1,1)
    98            << std::endl;
    99   }
    100   else
    101     *error << "Ok" << std::endl;
    102 
    103   return ok;
    104 }
    105 
    106 bool test_kernel_lookup(std::ostream* error)
    107 {
    108   bool ok =true;
    109   *error << "\nTesting KernelLookup" << std::endl;
    110   gslapi::matrix data_core(1,5);
    111   for (size_t i=0; i<data_core.columns(); i++)
    112     data_core(0,i)=i;
    113   classifier::MatrixLookup data(data_core);
    114   classifier::PolynomialKernelFunction kf;
    115   classifier::Kernel_SEV kernel(data,kf);
    116   *error << "KernelLookup::KernelLookup(const Kernel&)...";
    117   classifier::KernelLookup k1(kernel);
    118   if (k1.rows()!=kernel.rows() || k1.columns()!=kernel.columns()) {
    119     ok =false;
    120     *error <<   "ERROR:" << std::endl;
    121     *error << "Dimensions do not agree." << std::endl;
    122   }
    123   for (size_t i=0; i<k1.rows(); i++)
    124     for (size_t j=0; j<k1.columns(); j++)
    125       if (k1(i,j)!=kernel(i,j)) {
    126         ok =false;
    127         *error << "ERROR:\n"
    128                << "KernelLookup::KernelLookup(const Kernel& data)"
    129                << std::endl;
    130         *error << "k(" << i << "," << j << ") is " << k1(i,j)
    131                << "expected " << kernel(i,j) << std::endl;
    132       }
    133  
    134   std::vector<size_t> index_odd;
    135   index_odd.push_back(1);
    136   index_odd.push_back(3);
    137   std::vector<size_t> index_even;
    138   index_even.push_back(2);
    139   index_even.push_back(0);
    140   index_even.push_back(5);
    141  
    142 
    143 
    144   return ok;
    145 }
    146 
    147 bool test_matrix_lookup(std::ostream* error)
    148 {
    149   bool ok =true;
    15028  *error << "\nTesting MatrixLookup" << std::endl;
    15129  *error << "MatrixLookup::MatrixLookup(const gslapi::matrix& data)...";
     
    234112    *error << "Ok" << std::endl;
    235113
     114  *error << "MatrixLookup::MatrixLookup(const size_t,const size_t,\n"
     115         << "                           const double)...";
     116  classifier::MatrixLookup m7(103,112,12);
     117  if (m7.rows()!=103 || m7.columns()!=112 || m7(0,0)!=12) {
     118    ok =false;
     119    *error << "ERROR:" << std::endl;
     120  }
     121  else
     122    *error << "Ok" << std::endl;
     123
    236124
    237125  *error << "MatrixLookup::training_data(const std::vector<size_t>)...";
     
    257145  delete ValData;
    258146
     147  if (ok)
     148    *error << "Test Ok." << std::endl;
     149  if (error!=&std::cerr)
     150    delete error;
     151  return (ok ? 0 : -1);
     152}
    259153
    260   return ok;
     154gslapi::matrix matrix(size_t n)
     155{
     156  gslapi::matrix res(n,n);
     157  for (size_t i=0;i<n;i++)
     158    for (size_t j=0;j<n;j++)
     159      res(i,j)=10*i+j;
     160  return res;
    261161}
     162
     163
Note: See TracChangeset for help on using the changeset viewer.