Changeset 1086 for trunk/yat/classifier


Ignore:
Timestamp:
Feb 14, 2008, 6:43:10 AM (13 years ago)
Author:
Peter
Message:

fixes #310

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/SubsetGenerator.h

    r1079 r1086  
    3333#include "Target.h"
    3434#include "Sampler.h"
    35 
     35#include "yat/utility/yat_assert.h"
    3636
    3737#include <algorithm>
     
    6262    SubsetGenerator(const Sampler& sampler, const T& data);
    6363
    64 
    6564    ///
    6665    /// @brief Constructor
     
    137136
    138137  private:
     138    void build(const MatrixLookup&);
     139    void build(const MatrixLookupWeighted&);
     140    void build(const KernelLookup&);
     141
    139142    SubsetGenerator(const SubsetGenerator&);
    140143    const SubsetGenerator& operator=(const SubsetGenerator&) const;
     
    147150    std::vector<const T*> validation_data_;
    148151    std::vector<Target> validation_target_;
    149     const bool weighted_;
    150152
    151153  };
     
    156158  template<typename T>
    157159  SubsetGenerator<T>::SubsetGenerator(const Sampler& sampler,
    158                                    const T& data)
    159     : f_selector_(NULL), sampler_(sampler), weighted_(false)
     160                                      const T& data)
     161    : f_selector_(NULL), sampler_(sampler)
    160162  {
    161     assert(target().size()==data.columns());
     163    utility::yat_assert<std::runtime_error>(target().size()==data.columns());
    162164
    163165    training_data_.reserve(sampler_.size());
     
    172174      validation_target_.push_back(Target(target(),
    173175                                          sampler.validation_index(i)));
    174       assert(training_data_.size()==i+1);
    175       assert(training_target_.size()==i+1);
    176       assert(validation_data_.size()==i+1);
    177       assert(validation_target_.size()==i+1);
     176      utility::yat_assert<std::runtime_error>(training_data_.size()==i+1);
     177      utility::yat_assert<std::runtime_error>(training_target_.size()==i+1);
     178      utility::yat_assert<std::runtime_error>(validation_data_.size()==i+1);
     179      utility::yat_assert<std::runtime_error>(validation_target_.size()==i+1);
    178180    }
    179181
     
    185187      features_[0].push_back(i);
    186188
    187     assert(training_data_.size()==size());
    188     assert(training_target_.size()==size());
    189     assert(validation_data_.size()==size());
    190     assert(validation_target_.size()==size());
     189    utility::yat_assert<std::runtime_error>(training_data_.size()==size());
     190    utility::yat_assert<std::runtime_error>(training_target_.size()==size());
     191    utility::yat_assert<std::runtime_error>(validation_data_.size()==size());
     192    utility::yat_assert<std::runtime_error>(validation_target_.size()==size());
    191193  }
    192194
     
    196198                                   const T& data,
    197199                                   FeatureSelector& fs)
    198     : f_selector_(&fs), sampler_(sampler), weighted_(false)
     200    : f_selector_(&fs), sampler_(sampler)
    199201  {
    200     assert(target().size()==data.columns());
    201 
     202    utility::yat_assert<std::runtime_error>(target().size()==data.columns());
    202203    features_.reserve(size());
    203204    training_data_.reserve(size());
    204205    validation_data_.reserve(size());
    205 
    206     // Taking care of three different case.
    207     // We start with the case of MatrixLookup
    208     const MatrixLookup* ml = dynamic_cast<const MatrixLookup*>(&data);
    209     if (ml){
    210       for (size_t k=0; k<size(); k++){
    211      
    212         training_target_.push_back(Target(target(),training_index(k)));
    213         validation_target_.push_back(Target(target(),validation_index(k)));
    214         // training data with no feature selection
    215         const MatrixLookup* train_data_all_feat =
    216           ml->training_data(training_index(k));
    217         // use these data to create feature selection
    218         assert(train_data_all_feat);
    219         f_selector_->update(*train_data_all_feat, training_target(k));
    220         // get features
    221         features_.push_back(f_selector_->features());
    222         assert(train_data_all_feat);
    223         delete train_data_all_feat;
    224        
    225         // Dynamically allocated. Must be deleted in destructor.
    226         training_data_.push_back(new MatrixLookup(*ml,features_.back(),
    227                                                   training_index(k)));
    228         validation_data_.push_back(new MatrixLookup(*ml,features_.back(),
    229                                                     validation_index(k)));     
    230       }
    231     }
    232     else {
    233       // Second the case of MatrixLookupWeighted
    234       const MatrixLookupWeighted* ml =
    235         dynamic_cast<const MatrixLookupWeighted*>(&data);
    236       if (ml){       
    237         for (u_long k=0; k<size(); k++){
    238           training_target_.push_back(Target(target(),training_index(k)));
    239           validation_target_.push_back(Target(target(),validation_index(k)));
    240           // training data with no feature selection
    241           const MatrixLookupWeighted* train_data_all_feat =
    242             ml->training_data(training_index(k));
    243           // use these data to create feature selection
    244           f_selector_->update(*train_data_all_feat, training_target(k));
    245           // get features
    246           features_.push_back(f_selector_->features());
    247           delete train_data_all_feat;
    248          
    249           // Dynamically allocated. Must be deleted in destructor.
    250           training_data_.push_back(new MatrixLookupWeighted(*ml,
    251                                                             features_.back(),
    252                                                             training_index(k)
    253                                                             ));
    254           validation_data_.push_back(new MatrixLookupWeighted(*ml,
    255                                                               features_.back(),
    256                                                               validation_index(k)
    257                                                               ));     
    258         }
    259       }
    260       else {
    261         // Third the case of MatrixLookupWeighted
    262         const KernelLookup* kernel = dynamic_cast<const KernelLookup*>(&data);
    263         if (kernel){
    264           for (u_long k=0; k<size(); k++){
    265             training_target_.push_back(Target(target(),training_index(k)));
    266             validation_target_.push_back(Target(target(),validation_index(k)));
    267             const T* matrix = kernel->data();
    268             // dynamically allocated must be deleted
    269             const T* training_matrix =
    270               matrix->training_data(training_index(k));
    271             if (matrix->weighted()){
    272               const MatrixLookupWeighted& ml =
    273                 dynamic_cast<const MatrixLookupWeighted&>(*matrix);
    274               f_selector_->update(MatrixLookupWeighted(ml,training_index(k),false),
    275                                   training_target(k));
    276             }
    277             else {
    278               const MatrixLookup& ml =
    279                 dynamic_cast<const MatrixLookup&>(*matrix);
    280               f_selector_->update(MatrixLookup(ml,training_index(k), false),
    281                                   training_target(k));
    282             }
    283             std::vector<size_t> dummie=f_selector_->features();
    284             features_.push_back(dummie);
    285             //features_.push_back(f_selector_->features());
    286             assert(kernel);
    287             const KernelLookup* kl = kernel->selected(features_.back());
    288             assert(training_matrix);
    289             delete training_matrix;
    290                      
    291             // Dynamically allocated. Must be deleted in destructor.
    292             training_data_.push_back(kl->training_data(training_index(k)));
    293             validation_data_.push_back(kl->validation_data(training_index(k),
    294                                                            validation_index(k)));
    295             assert(kl);
    296             delete kl;
    297           }
    298         }
    299         else {
    300         std::cerr << "Sorry, your type of T ("
    301                   << typeid(data).name() << ")\nis not supported in "
    302                   << "SubsetGenerator with\nFeatureSelection\n";
    303         exit(-1);
    304         }
    305       }
    306     }
    307     assert(training_data_.size()==size());
    308     assert(training_target_.size()==size());
    309     assert(validation_data_.size()==size());
    310     assert(validation_target_.size()==size());
     206    build(data);
     207    utility::yat_assert<std::runtime_error>(training_data_.size()==size());
     208    utility::yat_assert<std::runtime_error>(training_target_.size()==size());
     209    utility::yat_assert<std::runtime_error>(validation_data_.size()==size());
     210    utility::yat_assert<std::runtime_error>(validation_target_.size()==size());
    311211  }
    312212
     
    315215  SubsetGenerator<T>::~SubsetGenerator()
    316216  {
    317     assert(training_data_.size()==validation_data_.size());
     217    utility::yat_assert<std::runtime_error>(training_data_.size()==validation_data_.size());
    318218    for (size_t i=0; i<training_data_.size(); i++)
    319219      delete training_data_[i];
     
    324224
    325225  template<typename T>
     226  void SubsetGenerator<T>::build(const MatrixLookup& ml)
     227  {
     228    for (size_t k=0; k<size(); k++){
     229      training_target_.push_back(Target(target(),training_index(k)));
     230      validation_target_.push_back(Target(target(),validation_index(k)));
     231      // training data with no feature selection
     232      const MatrixLookup* train_data_all_feat =
     233        ml.training_data(training_index(k));
     234      // use these data to create feature selection
     235      utility::yat_assert<std::runtime_error>(train_data_all_feat);
     236      f_selector_->update(*train_data_all_feat, training_target(k));
     237        // get features
     238      features_.push_back(f_selector_->features());
     239      utility::yat_assert<std::runtime_error>(train_data_all_feat);
     240      delete train_data_all_feat;
     241     
     242      // Dynamically allocated. Must be deleted in destructor.
     243      training_data_.push_back(new MatrixLookup(ml,features_.back(),
     244                                                training_index(k)));
     245      validation_data_.push_back(new MatrixLookup(ml,features_.back(),
     246                                                  validation_index(k)));     
     247    }
     248
     249  }
     250
     251
     252  template<typename T>
     253  void SubsetGenerator<T>::build(const MatrixLookupWeighted& ml)
     254  {
     255    for (u_long k=0; k<size(); k++){
     256      training_target_.push_back(Target(target(),training_index(k)));
     257      validation_target_.push_back(Target(target(),validation_index(k)));
     258      // training data with no feature selection
     259      const MatrixLookupWeighted* train_data_all_feat =
     260        ml.training_data(training_index(k));
     261      // use these data to create feature selection
     262      f_selector_->update(*train_data_all_feat, training_target(k));
     263      // get features
     264      features_.push_back(f_selector_->features());
     265      delete train_data_all_feat;
     266     
     267      // Dynamically allocated. Must be deleted in destructor.
     268      training_data_.push_back(new MatrixLookupWeighted(ml, features_.back(),
     269                                                        training_index(k)));
     270      validation_data_.push_back(new MatrixLookupWeighted(ml, features_.back(),
     271                                                          validation_index(k)));
     272    }
     273  }
     274
     275  template<typename T>
     276  void SubsetGenerator<T>::build(const KernelLookup& kernel)
     277  {
     278    for (u_long k=0; k<size(); k++){
     279      training_target_.push_back(Target(target(),training_index(k)));
     280      validation_target_.push_back(Target(target(),validation_index(k)));
     281      const DataLookup2D* matrix = kernel.data();
     282      // dynamically allocated must be deleted
     283      const DataLookup2D* training_matrix =
     284        matrix->training_data(training_index(k));
     285      if (matrix->weighted()){
     286        const MatrixLookupWeighted& ml =
     287          dynamic_cast<const MatrixLookupWeighted&>(*matrix);
     288        f_selector_->update(MatrixLookupWeighted(ml,training_index(k),false),
     289                            training_target(k));
     290      }
     291      else {
     292        const MatrixLookup& ml =
     293          dynamic_cast<const MatrixLookup&>(*matrix);
     294        f_selector_->update(MatrixLookup(ml,training_index(k), false),
     295                            training_target(k));
     296      }
     297      std::vector<size_t> dummie=f_selector_->features();
     298      features_.push_back(dummie);
     299      //features_.push_back(f_selector_->features());
     300      const KernelLookup* kl = kernel.selected(features_.back());
     301      utility::yat_assert<std::runtime_error>(training_matrix);
     302      delete training_matrix;
     303     
     304      // Dynamically allocated. Must be deleted in destructor.
     305      training_data_.push_back(kl->training_data(training_index(k)));
     306      validation_data_.push_back(kl->validation_data(training_index(k),
     307                                                     validation_index(k)));
     308      utility::yat_assert<std::runtime_error>(kl);
     309      delete kl;
     310    }
     311  }
     312
     313
     314  template<typename T>
    326315  u_long SubsetGenerator<T>::size(void) const
    327316  {
Note: See TracChangeset for help on using the changeset viewer.