Changeset 1206


Ignore:
Timestamp:
Mar 5, 2008, 6:56:01 PM (16 years ago)
Author:
Peter
Message:

fixes #345

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/ensemble_test.cc

    r1161 r1206  
    3232#include "yat/classifier/Kernel_MEV.h"
    3333#include "yat/classifier/MatrixLookup.h"
     34#include "yat/classifier/MatrixLookupWeighted.h"
    3435#include "yat/classifier/NCC.h"
    3536#include "yat/classifier/PolynomialKernelFunction.h"
     
    8788    *error << "build ensemble" << std::endl;
    8889    ensemble.build();
     90    std::vector<std::vector<statistics::Averager> > result;
     91    ensemble.predict(data, result);
     92  }
     93
     94  {
     95    *error << "create ensemble of ncc" << std::endl;
     96    classifier::MatrixLookupWeighted data_weighted(data);
     97    classifier::NCC<statistics::EuclideanDistance> ncc;
     98    classifier::CrossValidationSampler sampler(target,3,3);
     99    classifier::SubsetGenerator<classifier::MatrixLookupWeighted>
     100      subdata(sampler,data_weighted);
     101    classifier::EnsembleBuilder<classifier::SupervisedClassifier,
     102      classifier::MatrixLookupWeighted> ensemble(ncc, data_weighted, sampler);
     103    *error << "build ensemble" << std::endl;
     104    ensemble.build();
     105    std::vector<std::vector<statistics::Averager> > result;
     106    ensemble.predict(data_weighted, result);
    89107  }
    90108
     
    103121  *error << "build ensemble" << std::endl;
    104122  ensemble.build();
     123  std::vector<std::vector<statistics::Averager> > result;
     124  ensemble.predict(kernel_lookup, result);
    105125 
    106126  utility::Vector out(target.size(),0);
  • trunk/yat/classifier/EnsembleBuilder.h

    r1157 r1206  
    115115    SubsetGenerator<Data>* subset_;
    116116    std::vector<Classifier*> classifier_;
     117    KernelLookup test_data(const KernelLookup&, size_t k);
     118    MatrixLookup test_data(const MatrixLookup&, size_t k);
     119    MatrixLookupWeighted test_data(const MatrixLookupWeighted&, size_t k);
    117120    std::vector<std::vector<statistics::Averager> > validation_result_;
    118121
     
    169172
    170173  template <class C, class D>
    171   u_long EnsembleBuilder<C, D>::size(void) const
    172   {
    173     return classifier_.size();
    174   }
    175 
    176 
    177   template <class C, class D>
    178174  void EnsembleBuilder<C, D>::predict
    179175  (const D& data, std::vector<std::vector<statistics::Averager> >& result)
     
    187183
    188184    for(u_long k=0;k<subset_->size();++k) {       
    189       const D* sub_data =
    190         data.selected(subset_->training_features(k));
    191       assert(sub_data);
    192       classifier(k).predict(*sub_data,prediction);
    193       delete sub_data;
     185      D sub_data =  test_data(data, k);
     186      classifier(k).predict(sub_data,prediction);
    194187    }
    195188
     
    200193
    201194 
     195  template <class C, class D>
     196  u_long EnsembleBuilder<C, D>::size(void) const
     197  {
     198    return classifier_.size();
     199  }
     200
     201
     202  template <class C, class D>
     203  MatrixLookup EnsembleBuilder<C, D>::test_data(const MatrixLookup& data,
     204                                                size_t k)
     205  {
     206    return MatrixLookup(data, subset_->training_features(k), true);
     207  }
     208 
     209
     210  template <class C, class D>
     211  MatrixLookupWeighted
     212  EnsembleBuilder<C, D>::test_data(const MatrixLookupWeighted& data, size_t k)
     213  {
     214    return MatrixLookupWeighted(data, subset_->training_features(k), true);
     215  }
     216 
     217
     218  template <class C, class D>
     219  KernelLookup
     220  EnsembleBuilder<C, D>::test_data(const KernelLookup& kernel, size_t k)
     221  {
     222    // weighted case
     223    if (kernel.weighted()){
     224      assert(false);
     225      // no feature selection
     226      if (kernel.data_weighted().rows()==subset_->training_features(k).size())
     227        return KernelLookup(kernel, subset_->training_index(k), true);
     228      MatrixLookupWeighted mlw = test_data(kernel.data_weighted(), k);
     229      return subset_->training_data(k).test_kernel(mlw);
     230
     231    }
     232    // unweighted case
     233
     234    // no feature selection
     235    if (kernel.data().rows()==subset_->training_features(k).size())
     236      return KernelLookup(kernel, subset_->training_index(k), true);
     237   
     238    // feature selection
     239    return subset_->training_data(k).test_kernel(test_data(kernel.data(),k));
     240  }
     241 
     242
    202243  template <class C, class D>
    203244  const std::vector<std::vector<statistics::Averager> >&
  • trunk/yat/classifier/KernelLookup.cc

    r1201 r1206  
    131131
    132132
    133   utility::SmartPtr<const MatrixLookup> KernelLookup::data(void) const
    134   {
    135     return utility::SmartPtr<const MatrixLookup>
    136       (new MatrixLookup(kernel_->data(), column_index_, false));
    137   }
    138 
    139 
    140   utility::SmartPtr<const MatrixLookupWeighted>
    141   KernelLookup::data_weighted(void) const
    142   {
    143     return utility::SmartPtr<const MatrixLookupWeighted>
    144       (new MatrixLookupWeighted(kernel_->data_weighted(),column_index_,false));
     133  MatrixLookup KernelLookup::data(void) const
     134  {
     135    assert(!weighted());
     136    return MatrixLookup(kernel_->data(), column_index_, false);
     137  }
     138
     139
     140  MatrixLookupWeighted KernelLookup::data_weighted(void) const
     141  {
     142    assert(weighted());
     143    return MatrixLookupWeighted(kernel_->data_weighted(),column_index_,false);
    145144  }
    146145
     
    184183
    185184
    186   utility::SmartPtr<const KernelLookup>
    187   KernelLookup::selected(const utility::Index& inputs) const
     185  KernelLookup KernelLookup::selected(const utility::Index& inputs) const
    188186  {
    189187    const Kernel* kernel;
    190188    if (kernel_->weighted()){
    191       utility::SmartPtr<const MatrixLookupWeighted> ml = data_weighted();
    192189      const MatrixLookupWeighted* ms =
    193         new MatrixLookupWeighted(*ml,inputs,true);
    194       kernel = kernel_->make_kernel(*ms, false);
     190        new MatrixLookupWeighted(data_weighted(),inputs,true);
     191      kernel = kernel_->make_kernel(*ms, true);
    195192    }
    196193    else {
    197       utility::SmartPtr<const MatrixLookup> m = data();
    198194      // matrix with selected features
    199       const MatrixLookup* ms = new MatrixLookup(*m,inputs,true);
     195      const MatrixLookup* ms = new MatrixLookup(data(),inputs,true);
    200196      kernel = kernel_->make_kernel(*ms,true);
    201197    }
    202     return utility::SmartPtr<const KernelLookup>
    203       (new KernelLookup(*kernel, true));
    204   }
    205 
    206 
    207   utility::SmartPtr<const KernelLookup>
    208   KernelLookup::test_kernel(const MatrixLookup& data) const
     198    return KernelLookup(*kernel, true);
     199  }
     200
     201
     202  KernelLookup KernelLookup::test_kernel(const MatrixLookup& data) const
    209203  {
    210204    if (!weighted()){
     
    240234        kernel_->make_kernel(*tmp, true);
    241235
    242       return utility::SmartPtr<const KernelLookup>
    243         (new KernelLookup(*kernel, utility::Index(row_index),
    244                           utility::Index(column_index), true));
     236      return KernelLookup(*kernel, utility::Index(row_index),
     237                          utility::Index(column_index), true);
    245238    }
    246239
     
    282275
    283276
    284     return utility::SmartPtr<const KernelLookup>
    285       (new KernelLookup(*kernel, row_index_,
    286                         utility::Index(column_index), true));
    287   }
    288 
    289 
    290 
    291   utility::SmartPtr<const KernelLookup>
    292   KernelLookup::test_kernel(const MatrixLookupWeighted& data) const
     277    return KernelLookup(*kernel, row_index_,
     278                        utility::Index(column_index), true);
     279  }
     280
     281
     282
     283  KernelLookup KernelLookup::test_kernel(const MatrixLookupWeighted& data) const
    293284  {
    294285    utility::Matrix* data_all =
     
    331322    const Kernel* kernel =
    332323      kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true));
    333     return utility::SmartPtr<const KernelLookup>
    334       (new KernelLookup(*kernel, row_index_,
    335                         utility::Index(column_index), true));
     324    return KernelLookup(*kernel, row_index_,
     325                        utility::Index(column_index), true);
    336326  }
    337327
  • trunk/yat/classifier/KernelLookup.h

    r1201 r1206  
    4949  /// not contain any data or values, but rather is a lookup into a
    5050  /// Kernel object. Each row and each column corresponds to a row and
    51   /// a column in the Kernel, respectively. This design allow for fast
     51  /// a column in the Kernel, respectively. This design allows for fast
    5252  /// creation of sub-kernels, which is a common operation in most
    5353  /// traning/validation procedures.
    5454  ///
    55   /// A KernelLookup can be created directly from a Kernel or from an
    56   /// other KernelLookup. In the latter case, the resulting
     55  /// A KernelLookup can be created directly from a Kernel or from
     56  /// another KernelLookup. In the latter case, the resulting
    5757  /// KernelLookup is looking directly into the underlying Kernel to
    5858  /// avoid multiple lookups.
     
    154154
    155155    ///
    156     /// Constructor taking the column (default) or row index vector as
     156    /// Constructor taking the column (default) or row index as
    157157    /// input. If @a row is false the created KernelLookup will have
    158158    /// equally many rows as @a kernel.
     
    165165    /// undefined.
    166166    ///
    167     KernelLookup(const KernelLookup& kernel, const utility::Index&,
     167    KernelLookup(const KernelLookup& kl, const utility::Index&,
    168168                 const bool row=false);
    169169
     
    211211    /// \throw if KernelLookup is weighted
    212212    ///
    213     utility::SmartPtr<const MatrixLookup> data(void) const;
     213    MatrixLookup data(void) const;
    214214
    215215    ///
     
    221221    /// \throw if KernelLookup is unweighted
    222222    ///
    223     utility::SmartPtr<const MatrixLookupWeighted> data_weighted(void) const;
     223    MatrixLookupWeighted data_weighted(void) const;
    224224
    225225    /**
     
    268268       KernelLookup.
    269269    */
    270     utility::SmartPtr<const KernelLookup>
    271     selected(const utility::Index& index) const;
     270    KernelLookup selected(const utility::Index& index) const;
    272271   
    273272    /**
     
    281280       this was built from.
    282281    */
    283     utility::SmartPtr<const KernelLookup>
    284     test_kernel(const MatrixLookup& data) const;
     282    KernelLookup test_kernel(const MatrixLookup& data) const;
    285283
    286284    /**
     
    293291       between the passed @a data and the internal underlying data @a
    294292       this was built from.
    295    
    296        @note Returns a dynamically allocated KernelLookup, which has
    297        to be deleted by the caller to avoid memory leaks.
    298     */
    299     utility::SmartPtr<const KernelLookup>
    300     test_kernel(const MatrixLookupWeighted& data) const;
     293    */
     294    KernelLookup test_kernel(const MatrixLookupWeighted& data) const;
    301295
    302296    /**
  • trunk/yat/classifier/SubsetGenerator.h

    r1201 r1206  
    202202  void SubsetGenerator<T>::build(const MatrixLookup& ml)
    203203  {
     204    if (!f_selector_)// no feature selection
     205      features_.push_back(utility::Index(ml.rows()));
     206
    204207    for (size_t k=0; k<size(); k++){
    205208      training_target_.push_back(Target(target(),training_index(k)));
     
    217220        delete train_data_all_feat;
    218221      }
    219       else // no feature selection
    220         features_.push_back(utility::Index(ml.rows()));
    221 
    222222     
    223223      // Dynamically allocated. Must be deleted in destructor.
     
    234234  void SubsetGenerator<T>::build(const MatrixLookupWeighted& ml)
    235235  {
     236    if (!f_selector_)// no feature selection
     237      features_.push_back(utility::Index(ml.rows()));
     238
    236239    for (u_long k=0; k<size(); k++){
    237240      training_target_.push_back(Target(target(),training_index(k)));
     
    247250        delete train_data_all_feat;
    248251      }
    249       else // no feature selection
    250         features_.push_back(utility::Index(ml.rows()));
    251252
    252253
     
    268269      if (f_selector_){
    269270        if (kernel.weighted()){
    270           utility::SmartPtr<const MatrixLookupWeighted> ml=
    271             kernel.data_weighted();
    272           f_selector_->update(MatrixLookupWeighted(*ml,training_index(k),false),
     271          MatrixLookupWeighted ml = kernel.data_weighted();
     272          f_selector_->update(MatrixLookupWeighted(ml,training_index(k),false),
    273273                              training_target(k));
    274274        }
    275275        else {
    276           utility::SmartPtr<const MatrixLookup> ml=kernel.data();
    277           f_selector_->update(MatrixLookup(*ml,training_index(k), false),
     276          MatrixLookup ml=kernel.data();
     277          f_selector_->update(MatrixLookup(ml,training_index(k), false),
    278278                              training_target(k));
    279279        }
    280280        features_.push_back(f_selector_->features());
    281         utility::SmartPtr<const KernelLookup> kl =
    282           kernel.selected(features_.back());
     281        KernelLookup kl = kernel.selected(features_.back());
    283282        // Dynamically allocated. Must be deleted in destructor.
    284         training_data_.push_back(new KernelLookup(*kl,training_index(k),
     283        training_data_.push_back(new KernelLookup(kl,training_index(k),
    285284                                                  training_index(k)));
    286         validation_data_.push_back(new KernelLookup(*kl, training_index(k),
     285        validation_data_.push_back(new KernelLookup(kl, training_index(k),
    287286                                                    validation_index(k)));
    288287      }
     
    296295     
    297296    }
     297    if (!f_selector_){
     298      if (kernel.weighted())
     299        features_.push_back(utility::Index(kernel.data_weighted().rows()));
     300      else
     301        features_.push_back(utility::Index(kernel.data().rows()));
     302    }
    298303  }
    299304
     
    325330  SubsetGenerator<T>::training_features(size_t i) const
    326331  {
     332    utility::yat_assert<std::runtime_error>(features_.size(),
     333                                           "SubsetGenerator::training_features");
    327334    return f_selector_ ? features_[i] : features_[0];
    328335  }
Note: See TracChangeset for help on using the changeset viewer.