Changeset 658 for trunk/c++_tools


Ignore:
Timestamp:
Sep 25, 2006, 4:12:03 AM (17 years ago)
Author:
Peter
Message:

added function in KernelLookup? to create a KernelLookup? from inner data and outer (passed) data.

Location:
trunk/c++_tools/classifier
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/c++_tools/classifier/DataLookup2D.cc

    r640 r658  
    1515
    1616
    17   DataLookup2D::DataLookup2D()
     17  DataLookup2D::DataLookup2D(const bool own)
    1818    : ref_count_(NULL)
    1919  {
     20    if (own)
     21      ref_count_ = new u_int(1);
    2022  }
    2123
  • trunk/c++_tools/classifier/DataLookup2D.h

    r656 r658  
    3131    /// Default constructor.
    3232    ///
    33     DataLookup2D(void);
     33    DataLookup2D(const bool own=false);
    3434
    3535
  • trunk/c++_tools/classifier/Kernel.cc

    r640 r658  
    1313namespace classifier {
    1414
    15   Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf)
    16     : data_(&data), data_w_(0), kf_(&kf), data_owner_(false),
     15  Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf,
     16                 const bool own)
     17    : data_(&data), data_w_(0), kf_(&kf), data_owner_(own),
    1718      weight_owner_(false)
    1819  {
     
    2021
    2122
    22   Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf)
    23     : data_(&data), data_w_(&data), kf_(&kf), data_owner_(false),
    24       weight_owner_(false)
     23  Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
     24                 const bool own)
     25    : data_(&data), data_w_(&data), kf_(&kf), data_owner_(own),
     26      weight_owner_(own)
    2527  {
    2628  }
  • trunk/c++_tools/classifier/Kernel.h

    r648 r658  
    4747    /// @note Can not handle NaNs.
    4848    ///
    49     Kernel(const MatrixLookup& data, const KernelFunction& kf);
     49    Kernel(const MatrixLookup& data, const KernelFunction& kf,
     50           const bool own=false);
    5051
    5152    ///
     
    5859    /// @note Can not handle NaNs.
    5960    ///
    60     Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf);
     61    Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
     62           const bool own=false);
    6163
    6264    ///
     
    105107
    106108    ///
     109    /// An interface for making new classifier objects. This function
     110    /// allows for specification at run-time of which kernel to
     111    /// instatiate (see 'Prototype' in Design Patterns).
     112    ///
     113    /// @Note Returns a dynamically allocated Kernel, which has
     114    /// to be deleted by the caller to avoid memory leaks.
     115    ///
     116    virtual const Kernel* make_kernel(const MatrixLookup&, const bool) const=0;
     117
     118
     119    ///
     120    /// An interface for making new classifier objects. This function
     121    /// allows for specification at run-time of which kernel to
     122    /// instatiate (see 'Prototype' in Design Patterns).
     123    ///
     124    /// @Note Returns a dynamically allocated Kernel, which has
     125    /// to be deleted by the caller to avoid memory leaks.
     126    ///
     127    virtual const Kernel* make_kernel(const MatrixLookupWeighted&,
     128                                      const bool own=false) const=0;
     129
     130
     131    ///
    107132    /// Created Kernel is built from selected features in data. The
    108133    /// @a index corresponds to which rows in data to use for the
     
    114139    /// to be deleted by the caller to avoid memory leaks.
    115140    ///
     141    /// @todo remove this function
    116142    virtual const Kernel* selected(const std::vector<size_t>& index) const=0;
    117143
  • trunk/c++_tools/classifier/KernelLookup.cc

    r640 r658  
    33#include <c++_tools/classifier/KernelLookup.h>
    44#include <c++_tools/classifier/DataLookup2D.h>
     5#include <c++_tools/classifier/MatrixLookup.h>
     6#include <c++_tools/classifier/MatrixLookupWeighted.h>
    57
    68
     
    105107
    106108  const KernelLookup*
    107   KernelLookup::training_data(const std::vector<size_t>& features,
    108                               const std::vector<size_t>& train) const
    109   {
    110     const Kernel* kernel = kernel_->selected(features);
    111     return new KernelLookup(*kernel, train, train, true);
    112   }
    113 
    114 
    115   const KernelLookup*
    116109  KernelLookup::validation_data(const std::vector<size_t>& train,
    117110                                const std::vector<size_t>& validation) const
     
    121114
    122115
    123   const KernelLookup*
    124   KernelLookup::validation_data(const std::vector<size_t>& features,
    125                                 const std::vector<size_t>& train,
    126                                 const std::vector<size_t>& validation) const
    127   {
    128     const Kernel* kernel = kernel_->selected(features);
    129     return new KernelLookup(*kernel, train, validation, true);
    130   }
    131 
    132 
    133116  const KernelLookup*
    134117  KernelLookup::selected(const std::vector<size_t>& inputs) const
     
    138121  }
    139122
     123
     124  const KernelLookup* KernelLookup::test_kernel(const MatrixLookup& data) const
     125  {
     126    assert(data.rows()==kernel_->data().rows());
     127    if (!weighted()){
     128      utility::matrix* data_all =
     129        new utility::matrix(data.rows(), rows()+data.columns());
     130      for (size_t i=0; i<data.rows(); ++i){
     131        // first columns are equal to data in kernel_
     132        for (size_t j=0; j<row_index_.size(); ++j)
     133          (*data_all)(i,j) = kernel_->data()(i,row_index_[j]);
     134        // last columns are equal to new data
     135        for (size_t j=0;j<data.columns(); ++j)
     136          (*data_all)(i,j+row_index_.size()) = data(i,j);
     137      }
     138      std::vector<size_t> column_index;
     139      column_index.reserve(data.columns());
     140      for (size_t i=0;i<data.columns(); ++i)
     141        column_index.push_back(i+row_index_.size());
     142      const Kernel* kernel =
     143        kernel_->make_kernel(MatrixLookup(*data_all, true), true);
     144      return new KernelLookup(*kernel, row_index_, column_index, true);
     145    }
     146
     147    // kernel_ holds MatrixLookupWeighted, hence new Kernel also
     148    // should hold a MatrixLookupweighted.
     149    utility::matrix* data_all =
     150      new utility::matrix(data.rows(), rows()+data.columns());
     151    utility::matrix* weight_all =
     152      new utility::matrix(data.rows(), rows()+data.columns(), 1.0);
     153    const MatrixLookupWeighted& kernel_data =
     154      dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
     155
     156    for (size_t i=0; i<data.rows(); ++i){
     157      // first columns are equal to data in kernel_
     158      for (size_t j=0; j<row_index_.size(); ++j){
     159        (*data_all)(i,j) = kernel_data.data(i,row_index_[j]);
     160        (*weight_all)(i,j) = kernel_data.weight(i,row_index_[j]);
     161      }
     162      // last columns are equal to new data
     163      for (size_t j=0;j<data.columns(); ++j){
     164        (*data_all)(i,j+row_index_.size()) = data(i,j);
     165      }
     166    }
     167    std::vector<size_t> column_index;
     168    column_index.reserve(data.columns());
     169    for (size_t i=0;i<data.columns(); ++i)
     170      column_index.push_back(i+row_index_.size());
     171    const Kernel* kernel =
     172      kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true));
     173    return new KernelLookup(*kernel, row_index_, column_index, true);
     174  }
     175
     176
     177
     178  const KernelLookup*
     179  KernelLookup::test_kernel(const MatrixLookupWeighted& data) const
     180  {
     181    utility::matrix* data_all =
     182      new utility::matrix(data.rows(), rows()+data.columns());
     183    utility::matrix* weight_all =
     184      new utility::matrix(data.rows(), rows()+data.columns(), 1.0);
     185
     186    if (weighted()){
     187      const MatrixLookupWeighted& kernel_data =
     188        dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
     189   
     190      for (size_t i=0; i<data.rows(); ++i){
     191        // first columns are equal to data in kernel_
     192        for (size_t j=0; j<row_index_.size(); ++j){
     193          (*data_all)(i,j) = kernel_data.data(i,row_index_[j]);
     194          (*weight_all)(i,j) = kernel_data.weight(i,row_index_[j]);
     195        }
     196      }
     197    }
     198    else {
     199
     200        dynamic_cast<const MatrixLookupWeighted&>(kernel_->data());
     201   
     202      for (size_t i=0; i<data.rows(); ++i){
     203        // first columns are equal to data in kernel_
     204        for (size_t j=0; j<row_index_.size(); ++j)
     205          (*data_all)(i,j) = kernel_->data()(i,row_index_[j]);
     206      }
     207    }
     208
     209    // last columns are equal to new data
     210    for (size_t i=0; i<data.rows(); ++i){
     211      for (size_t j=0;j<data.columns(); ++j){
     212        (*data_all)(i,j+row_index_.size()) = data.data(i,j);
     213        (*weight_all)(i,j+row_index_.size()) = data.weight(i,j);
     214      }
     215    }
     216   
     217    std::vector<size_t> column_index;
     218    column_index.reserve(data.columns());
     219    for (size_t i=0;i<data.columns(); ++i)
     220      column_index.push_back(i+row_index_.size());
     221    const Kernel* kernel =
     222      kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true));
     223    return new KernelLookup(*kernel, row_index_, column_index, true);
     224  }
     225
    140226}} // of namespace classifier and namespace theplu
  • trunk/c++_tools/classifier/KernelLookup.h

    r656 r658  
    139139
    140140    ///
    141     /// @return a sub-kernel of kernel calculated using data defined by
    142     /// @a features. Each row and each columns corresponds to a traing
    143     /// sample defined by @a train.
    144     ///
    145     /// @return pointer to dynamically allocated sub-Lookup of the KernelLookup
    146     ///
    147     /// @Note Returns a dynamically allocated DataLookup2D, which has
    148     /// to be deleted by the caller to avoid memory leaks.
    149     ///
    150     const KernelLookup* training_data(const std::vector<size_t>& features,
    151                                       const std::vector<size_t>& train) const;
    152 
    153 
    154     ///
    155141    /// In returned kernel each row corresponds to a training sample
    156142    /// and each column corresponds to a validation sample. The
     
    168154
    169155
    170     ///
    171     /// In returned kernel each row corresponds to a training sample
    172     /// and each column corresponds to a validation sample. The kernel
    173     /// is based on the features defined by @a features.
    174     ///
    175     /// @Note Returns a dynamically allocated DataLookup2D, which has
    176     /// to be deleted by the caller to avoid memory leaks.
    177     ///
    178     const KernelLookup*
    179     validation_data(const std::vector<size_t>& features,
    180                     const std::vector<size_t>& train,
    181                     const std::vector<size_t>& validation) const;
     156    /**
     157       This function is useful when predicting on a independent data
     158       set using a kernel-based classifier. In returned KernelLookup
     159       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
     160       data. Row \f$ i \f$ in returned KernelLookup corresponds to
     161       same sample as row \f$ i \f$ in @a this. In other words, this
     162       function returns a KernelLookup containing the kernel elements
     163       between the passed @a data and the internal underlying data @a
     164       this was built from.
     165   
     166       @Note Returns a dynamically allocated DataLookup2D, which has
     167       to be deleted by the caller to avoid memory leaks.
     168    */
     169    const KernelLookup* test_kernel(const MatrixLookup& data) const;
     170
     171
     172    /**
     173       This function is useful when predicting on a independent data
     174       set using a kernel-based classifier. In returned KernelLookup
     175       column \f$ i \f$ corresponds to column \f$ i \f$ in @a
     176       data. Row \f$ i \f$ in returned KernelLookup corresponds to
     177       same sample as row \f$ i \f$ in @a this. In other words, this
     178       function returns a KernelLookup containing the kernel elements
     179       between the passed @a data and the internal underlying data @a
     180       this was built from.
     181   
     182       @Note Returns a dynamically allocated DataLookup2D, which has
     183       to be deleted by the caller to avoid memory leaks.
     184    */
     185    const KernelLookup* test_kernel(const MatrixLookupWeighted& data) const;
    182186
    183187
  • trunk/c++_tools/classifier/Kernel_MEV.cc

    r628 r658  
    99namespace classifier { 
    1010
    11   Kernel_MEV::Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf)
    12     : Kernel(data,kf)
     11  Kernel_MEV::Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf,
     12                         const bool own)
     13    : Kernel(data,kf,own)
    1314  {
    1415  }
     
    1617
    1718  Kernel_MEV::Kernel_MEV(const MatrixLookupWeighted& data,
    18                          const KernelFunction& kf)
    19     : Kernel(data,kf)
     19                         const KernelFunction& kf, const bool own)
     20    : Kernel(data,kf,own)
    2021  {
    2122  }
     
    2627    : Kernel(kernel,index)
    2728  {
     29  }
     30
     31
     32  const Kernel_MEV* Kernel_MEV::make_kernel(const MatrixLookup& data,
     33                                            const bool own) const
     34  {
     35    return new Kernel_MEV(data, *kf_, own);
     36  }
     37
     38
     39  const Kernel_MEV* Kernel_MEV::make_kernel(const MatrixLookupWeighted& data,
     40                                            const bool own) const
     41  {
     42    return new Kernel_MEV(data, *kf_, own);
    2843  }
    2944
  • trunk/c++_tools/classifier/Kernel_MEV.h

    r651 r658  
    3434    /// sample. @note Can not handle NaNs.
    3535    ///
    36     Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf);
     36    Kernel_MEV(const MatrixLookup& data, const KernelFunction& kf,
     37               const bool own=false);
    3738
    3839
     
    4243    /// sample. @note Can not handle NaNs.
    4344    ///
    44     Kernel_MEV(const MatrixLookupWeighted& data, const KernelFunction& kf);
     45    Kernel_MEV(const MatrixLookupWeighted& data, const KernelFunction& kf,
     46               const bool own=false);
    4547
    4648
     
    5254
    5355
    54     ///
    55     /// Destructor
    56     ///
    57     inline virtual ~Kernel_MEV(void) {};
    58 
    5956    ///
    6057    /// @return Element at position (\a row, \a column) of the Kernel
     
    6360    double operator()(const size_t row, const size_t column) const;
    6461   
     62    ///
     63    /// An interface for making new classifier objects. This function
     64    /// allows for specification at run-time of which kernel to
     65    /// instatiate (see 'Prototype' in Design Patterns).
     66    ///
     67    /// @Note Returns a dynamically allocated Kernel, which has
     68    /// to be deleted by the caller to avoid memory leaks.
     69    ///
     70    const Kernel_MEV* make_kernel(const MatrixLookup&,
     71                                  const bool own=false) const;
     72
     73
     74    ///
     75    /// An interface for making new classifier objects. This function
     76    /// allows for specification at run-time of which kernel to
     77    /// instatiate (see 'Prototype' in Design Patterns).
     78    ///
     79    /// @Note Returns a dynamically allocated Kernel, which has
     80    /// to be deleted by the caller to avoid memory leaks.
     81    ///
     82    const Kernel_MEV* make_kernel(const MatrixLookupWeighted&,
     83                                  const bool own=false) const;
     84
     85
    6586    ///
    6687    /// @see Kernel_MEV(const Kernel_MEV&, const std::vector<size_t>&);
  • trunk/c++_tools/classifier/Kernel_SEV.cc

    r628 r658  
    1414
    1515
    16   Kernel_SEV::Kernel_SEV(const MatrixLookup& data, const KernelFunction& kf)
    17     : Kernel(data,kf)
     16  Kernel_SEV::Kernel_SEV(const MatrixLookup& data, const KernelFunction& kf,
     17                         const bool own)
     18    : Kernel(data,kf, own)
    1819  {
    1920    build_kernel();
     
    2223
    2324  Kernel_SEV::Kernel_SEV(const MatrixLookupWeighted& data,
    24                          const KernelFunction& kf)
    25     : Kernel(data,kf)
     25                         const KernelFunction& kf, const bool own)
     26    : Kernel(data,kf, own)
    2627  {
    2728    kernel_matrix_ = utility::matrix(data_->columns(),data_->columns());
     
    5253
    5354
     55  double Kernel_SEV::operator()(const size_t row,const size_t column) const
     56  {
     57    return kernel_matrix_(row,column);
     58  }
     59 
     60
     61  const Kernel_SEV* Kernel_SEV::make_kernel(const MatrixLookup& data,
     62                                            const bool own) const
     63  {
     64    return new Kernel_SEV(data, *kf_, own);
     65  }
     66
     67
     68  const Kernel_SEV* Kernel_SEV::make_kernel(const MatrixLookupWeighted& data,
     69                            const bool own) const
     70  {
     71    return new Kernel_SEV(data, *kf_, own);
     72  }
     73
     74
    5475  const Kernel* Kernel_SEV::selected(const std::vector<size_t>& index) const
    5576  {
  • trunk/c++_tools/classifier/Kernel_SEV.h

    r651 r658  
    3434    /// use KernelWeighted_SEV instead.
    3535    ///
    36     Kernel_SEV(const MatrixLookup&, const KernelFunction&);
     36    Kernel_SEV(const MatrixLookup&, const KernelFunction&,const bool own=false);
    3737   
    3838    ///
     
    4141    /// use KernelWeighted_SEV instead.
    4242    ///
    43     Kernel_SEV(const MatrixLookupWeighted&, const KernelFunction&);
     43    Kernel_SEV(const MatrixLookupWeighted&, const KernelFunction&,
     44               const bool own=false);
    4445   
    4546    ///
     
    4849    Kernel_SEV(const Kernel_SEV& kernel, const std::vector<size_t>& index);
    4950
     51
     52    ///
     53    /// An interface for making new classifier objects. This function
     54    /// allows for specification at run-time of which kernel to
     55    /// instatiate (see 'Prototype' in Design Patterns).
     56    ///
     57    /// @Note Returns a dynamically allocated Kernel, which has
     58    /// to be deleted by the caller to avoid memory leaks.
     59    ///
     60    const Kernel_SEV* make_kernel(const MatrixLookup&,
     61                                  const bool own=false) const;
     62
     63
     64    ///
     65    /// An interface for making new classifier objects. This function
     66    /// allows for specification at run-time of which kernel to
     67    /// instatiate (see 'Prototype' in Design Patterns).
     68    ///
     69    /// @Note Returns a dynamically allocated Kernel, which has
     70    /// to be deleted by the caller to avoid memory leaks.
     71    ///
     72    const Kernel_SEV* make_kernel(const MatrixLookupWeighted&,
     73                                  const bool own=false) const;
     74
     75
    5076    ///
    5177    /// @return element at position (\a row, \a column) in the Kernel
    5278    /// matrix
    5379    ///
    54     inline double operator()(const size_t row,const size_t column) const
    55     { return kernel_matrix_(row,column); }
     80    double operator()(const size_t row,const size_t column) const;
     81
    5682
    5783    ///
  • trunk/c++_tools/classifier/MatrixLookup.cc

    r656 r658  
    1414namespace classifier {
    1515
    16   MatrixLookup::MatrixLookup(const utility::matrix& data)
    17     : DataLookup2D(), data_(&data)
     16  MatrixLookup::MatrixLookup(const utility::matrix& data, const bool own)
     17    : DataLookup2D(own), data_(&data)
    1818  {
    1919    for(size_t i=0;i<(*data_).rows();i++)
  • trunk/c++_tools/classifier/MatrixLookup.h

    r657 r658  
    3030  ///
    3131  /// There is a possibility to set the MatrixLookup as owner of the
    32   /// underlying matrix. In that case the underlying matrix will be
    33   /// destroyed in the destructor. Consequently, the underlying matrix
    34   /// must have been dynamically allocated and no other MatrixLookup
    35   /// can own the Kernel.
     32  /// underlying matrix.
    3633  ///
    3734  class MatrixLookup : public DataLookup2D
     
    4340    ///
    4441    /// Constructor creating a lookup into the entire @a matrix.
     42    /// @param own if true MatrixLookup owns its underlying @a matrix
    4543    ///
    4644    /// @note If @a matrix goes out of scope or is deleted, the
     
    4846    /// undefined.
    4947    ///
    50     explicit MatrixLookup(const utility::matrix& matrix);
     48    MatrixLookup(const utility::matrix& matrix, const bool own=false);
    5149
    5250    ///
  • trunk/c++_tools/classifier/MatrixLookupWeighted.cc

    r656 r658  
    1515
    1616  MatrixLookupWeighted::MatrixLookupWeighted(const utility::matrix& data,
    17                                              const utility::matrix& weights)
    18     : DataLookup2D(), data_(&data), weights_(&weights), ref_count_weights_(NULL)
     17                                             const utility::matrix& weights,
     18                                             const bool own)
     19    : DataLookup2D(own), data_(&data), weights_(&weights),
     20      ref_count_weights_(NULL)
    1921  {
    2022    assert(data.rows()==weights.rows());
  • trunk/c++_tools/classifier/MatrixLookupWeighted.h

    r656 r658  
    4444    ///
    4545    MatrixLookupWeighted(const utility::matrix& matrix,
    46                          const utility::matrix& weights);
     46                         const utility::matrix& weights,
     47                         const bool owner=false);
    4748
    4849   
Note: See TracChangeset for help on using the changeset viewer.