Changeset 659


Ignore:
Timestamp:
Sep 26, 2006, 3:44:24 PM (17 years ago)
Author:
Peter
Message:

fixes #147 and #136

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

Legend:

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

    r658 r659  
    7070
    7171  DataLookup2D::DataLookup2D(const std::vector<size_t>& row,
    72                              const std::vector<size_t>& col)
     72                             const std::vector<size_t>& col,
     73                             const bool own)
    7374    : row_index_(row),column_index_(col), ref_count_(NULL)
    7475  {
     76    if (own)
     77      ref_count_ = new u_int(1);
    7578  }
    7679
  • trunk/c++_tools/classifier/DataLookup2D.h

    r658 r659  
    3939    ///
    4040    DataLookup2D(const std::vector<size_t>& row,
    41                  const std::vector<size_t>& column);
     41                 const std::vector<size_t>& column,
     42                 const bool own=false);
    4243
    4344    ///
  • trunk/c++_tools/classifier/EnsembleBuilder.cc

    r635 r659  
    5252    size_t k=0;
    5353    utility::matrix prediction; 
    54    
    55 
    5654    try {
    5755      const KernelLookup& kernel = dynamic_cast<const KernelLookup&>(data);
    5856      while(subset_.more()) {
    59         classifier(k++).predict(KernelLookup(kernel,
    60                                              subset_.training_index(),
    61                                              true),
    62                                 prediction);
     57        KernelLookup kernel_peter(kernel,subset_.training_index(),true);
     58        classifier(k++).predict(kernel_peter,prediction);
     59
    6360        for(size_t i=0; i<prediction.rows();i++)
    6461          for(size_t j=0; j<prediction.columns();j++)
  • trunk/c++_tools/classifier/Kernel.cc

    r658 r659  
    1515  Kernel::Kernel(const MatrixLookup& data, const KernelFunction& kf,
    1616                 const bool own)
    17     : data_(&data), data_w_(0), kf_(&kf), data_owner_(own),
    18       weight_owner_(false)
     17    : data_(&data), data_w_(0), kf_(&kf), ref_count_w_(NULL)
    1918  {
     19    if (own)
     20      ref_count_ = new u_int(1);
     21    else
     22      ref_count_ = NULL;
    2023  }
    2124
     
    2326  Kernel::Kernel(const MatrixLookupWeighted& data, const KernelFunction& kf,
    2427                 const bool own)
    25     : data_(&data), data_w_(&data), kf_(&kf), data_owner_(own),
    26       weight_owner_(own)
     28    : data_(&data), data_w_(&data), kf_(&kf)
    2729  {
     30    if (own){
     31      ref_count_ = new u_int(1);
     32      ref_count_w_ = new u_int(1);
     33    }
     34    else {
     35      ref_count_ = NULL;
     36      ref_count_w_ = NULL;
     37    }
    2838  }
    2939
    3040
    3141  Kernel::Kernel(const Kernel& other, const std::vector<size_t>& index)
    32     : kf_(other.kf_), data_owner_(true)
     42    : kf_(other.kf_)
    3343  {
    3444    data_ = other.data_->selected(index);
     45    ref_count_ = new u_int(1);
     46   
    3547    if (other.data_w_){
    3648      data_w_ = other.data_w_->selected(index);
    37       weight_owner_=true;
     49      ref_count_w_ = new u_int(1);
    3850    }
    3951    else{
    4052      data_w_=NULL;
    41       weight_owner_=false;
     53      ref_count_w_ = NULL;
    4254    }
    4355
     
    4658  Kernel::~Kernel()
    4759  {
    48     if (data_owner_)
    49       delete data_;
    50      
    51     if (weight_owner_)
    52       if (data_w_)
     60    if (ref_count_)
     61      if (!--(*ref_count_))
     62        delete data_;
     63
     64    if (ref_count_w_)
     65      if (!--(*ref_count_w_))
    5366        delete data_w_;
    54       else
    55         std::cerr << "Error in Kernel implementation: probably a constructor"
    56                   << std::endl;
    57    
     67
    5868  }
    5969
  • trunk/c++_tools/classifier/Kernel.h

    r658 r659  
    148148
    149149  protected:
    150     /// underlyung data
     150    /// underlying data
    151151    const DataLookup2D* data_;
    152152    /// same as data_ if weifghted otherwise a NULL pointer
     
    154154    /// type of Kernel Function e.g. Gaussian (aka RBF)
    155155    const KernelFunction* kf_;
    156     /// if true we own data and will delete it in destructor
    157     const bool data_owner_;
    158     /// if true we own data_w and will delete it in destructor
    159     bool weight_owner_;
     156
     157    ///
     158    /// poiter telling how many owners to underlying data
     159    /// (data_). NULL if this is not an owner.
     160    ///
     161    u_int* ref_count_;
     162
     163    ///
     164    /// poiter telling how many owners to underlying weights
     165    /// (data_w_). NULL if this is not an owner.
     166    ///
     167    u_int* ref_count_w_;
    160168
    161169  private:
  • trunk/c++_tools/classifier/KernelLookup.cc

    r658 r659  
    1616
    1717  KernelLookup::KernelLookup(const Kernel& kernel, const bool own)
    18     : DataLookup2D(), kernel_(&kernel)
    19   {
    20     if (own)
    21       ref_count_ = new u_int(1);
    22 
     18    : DataLookup2D(own), kernel_(&kernel)
     19  {
    2320    column_index_.reserve(kernel.size());
    2421    for(size_t i=0; i<kernel.size(); i++)
     
    3128                             const std::vector<size_t>& column,
    3229                             const bool owner)
    33     : DataLookup2D(row,column), kernel_(&kernel)
    34   {
    35     if (owner)
    36       ref_count_ = new u_int(1);
    37 
     30    : DataLookup2D(row,column,owner), kernel_(&kernel)
     31  {
    3832    // Checking that each row index is less than kernel.rows()
    3933    assert(row.empty() ||
     
    4236    assert(column.empty() ||
    4337           *(std::max_element(column.begin(),column.end()))<kernel_->size());
    44 
    4538  }
    4639
     
    7063    if (ref_count_)
    7164      ++(*ref_count_);
    72 
    7365  }
    7466 
     
    7971    : DataLookup2D(other,index,row), kernel_(other.kernel_)
    8072  {
     73    assert(kernel_->size());
     74
    8175    // Checking that no index is out of range
    8276    assert(row_index_.empty() ||
     
    124118  const KernelLookup* KernelLookup::test_kernel(const MatrixLookup& data) const
    125119  {
     120
    126121    assert(data.rows()==kernel_->data().rows());
    127122    if (!weighted()){
    128123      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]);
     124        new utility::matrix(data.rows(), row_index_.size()+data.columns());
     125
     126      for (size_t i=0; i<data_all->rows(); ++i) {
     127
     128        // first some columns from data in kernel_
     129        for (size_t j=0; j<row_index_.size(); ++j){
     130          (*data_all)(i,j) = kernel_->data()(i,row_index_[j]);
     131        }
     132       
    134133        // last columns are equal to new data
    135         for (size_t j=0;j<data.columns(); ++j)
     134        for (size_t j=0;j<data.columns(); ++j){
    136135          (*data_all)(i,j+row_index_.size()) = data(i,j);
     136        }
    137137      }
    138138      std::vector<size_t> column_index;
     
    140140      for (size_t i=0;i<data.columns(); ++i)
    141141        column_index.push_back(i+row_index_.size());
     142
     143      std::vector<size_t> row_index;
     144      row_index.reserve(row_index_.size());
     145      for (size_t i=0;i<row_index_.size(); ++i)
     146        row_index.push_back(i);
     147
     148      const MatrixLookup* tmp = new MatrixLookup(*data_all, true);
     149
    142150      const Kernel* kernel =
    143         kernel_->make_kernel(MatrixLookup(*data_all, true), true);
    144       return new KernelLookup(*kernel, row_index_, column_index, true);
     151        kernel_->make_kernel(*tmp, true);
     152
     153      return new KernelLookup(*kernel, row_index, column_index, true);
    145154    }
    146155
     
    155164
    156165    for (size_t i=0; i<data.rows(); ++i){
    157       // first columns are equal to data in kernel_
     166
     167      // first some columns from data in kernel_
    158168      for (size_t j=0; j<row_index_.size(); ++j){
    159         (*data_all)(i,j) = kernel_data.data(i,row_index_[j]);
     169        (*data_all)(i,j) = kernel_data.data(i,row_index_[j]); 
    160170        (*weight_all)(i,j) = kernel_data.weight(i,row_index_[j]);
    161171      }
     172
    162173      // last columns are equal to new data
    163174      for (size_t j=0;j<data.columns(); ++j){
     
    169180    for (size_t i=0;i<data.columns(); ++i)
    170181      column_index.push_back(i+row_index_.size());
    171     const Kernel* kernel =
    172       kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true));
     182
     183    std::vector<size_t> row_index;
     184    row_index.reserve(row_index_.size());
     185    for (size_t i=0;i<row_index_.size(); ++i)
     186      row_index.push_back(i);
     187
     188    MatrixLookupWeighted* tmp = new MatrixLookupWeighted(*data_all,
     189                                                         *weight_all, true);
     190    const Kernel* kernel = kernel_->make_kernel(*tmp, true);
    173191    return new KernelLookup(*kernel, row_index_, column_index, true);
    174192  }
  • trunk/c++_tools/classifier/KernelLookup.h

    r658 r659  
    217217
    218218    ///
    219     /// Weighted version of element function. Using weights @a w all
    220     /// identical to unity results in same as using the unweighted
    221     /// version above.
     219    /// Function to calculate a new Kernel element using the
     220    /// underlying KernelFunction. The value is calulated between @a
     221    /// vec and the data vector of the \f$ i \f$ th sample, in other
     222    /// words, the sample corresponding to the \f$ i \f$ th row or
     223    /// \f$ i \f$ th column. In case KernelLookup is a sub-Kernel and not
     224    /// symmetric, the kernel value is calculated between @a vec and
     225    /// the data vector corresponding to \f$ i \f$ th row.
    222226    ///
    223227    inline double element(const DataLookupWeighted1D& vec, const size_t i) const
     
    240244    inline bool weighted(void) const { return kernel_->weighted(); }
    241245
     246    inline const Kernel* kernel(void) const { return kernel_; }
     247   
    242248  private:
    243249    const KernelLookup& operator=(const KernelLookup&);
  • trunk/c++_tools/classifier/SVM.cc

    r640 r659  
    9595    const KernelLookup& input_kernel = dynamic_cast<const KernelLookup&>(input);
    9696
    97     const KernelLookup* kernel_pointer;
    98     kernel_pointer = &input_kernel;
    99 
    10097    assert(input.rows()==alpha_.size());
    10198    prediction = utility::matrix(2,input.columns(),0);
    10299    for (size_t i = 0; i<input.columns(); i++){
    103100      for (size_t j = 0; j<input.rows(); j++){
    104         prediction(0,i) += target(j)*alpha_(j)*(*kernel_pointer)(j,i);
     101        prediction(0,i) += target(j)*alpha_(j)*input_kernel(j,i);
    105102        assert(target(j));
    106103      }
Note: See TracChangeset for help on using the changeset viewer.