Changeset 1126 for trunk/yat


Ignore:
Timestamp:
Feb 22, 2008, 11:31:39 PM (13 years ago)
Author:
Peter
Message:

working on ticket:234

Location:
trunk/yat
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/KernelLookup.cc

    r1121 r1126  
    2929
    3030#include <cassert>
    31 #ifndef NDEBUG
    32 #include <algorithm>
    33 #endif
    3431
    3532namespace theplu {
     
    3835
    3936  KernelLookup::KernelLookup(const Kernel& kernel, const bool own)
    40     : DataLookup2D(own), kernel_(&kernel)
    41   {
    42     column_index_.reserve(kernel.size());
    43     for(size_t i=0; i<kernel.size(); i++)
    44       column_index_.push_back(i);
    45     row_index_=column_index_;
     37    : column_index_(utility::Index(kernel.size())),
     38      kernel_(utility::SmartPtr<const Kernel>(&kernel, own)),
     39      row_index_(utility::Index(kernel.size()))
     40  {
    4641  }
    4742
    4843
    4944  KernelLookup::KernelLookup(const Kernel& kernel,
    50                              const std::vector<size_t>& row,
    51                              const std::vector<size_t>& column,
     45                             const utility::Index& row,
     46                             const utility::Index& column,
    5247                             const bool owner)
    53     : DataLookup2D(row,column,owner), kernel_(&kernel)
    54   {
    55     // Checking that each row index is less than kernel.rows()
    56     assert(row.empty() ||
    57            *(std::max_element(row.begin(),row.end()))<kernel_->size());
    58     // Checking that each column index is less than kernel.column()
    59     assert(column.empty() ||
    60            *(std::max_element(column.begin(),column.end()))<kernel_->size());
     48    : column_index_(column),
     49      kernel_(utility::SmartPtr<const Kernel>(&kernel, false)),
     50      row_index_(row)
     51  {
    6152  }
    6253
    6354
    6455  KernelLookup::KernelLookup(const KernelLookup& other,
    65                              const std::vector<size_t>& row,
    66                              const std::vector<size_t>& column)
    67     : DataLookup2D(other,row,column), kernel_(other.kernel_)
    68   {
    69     ref_count_=other.ref_count_;
    70     if (ref_count_)
    71       ++(*ref_count_);
     56                             const utility::Index& row,
     57                             const utility::Index& column)
     58    : column_index_(utility::Index(other.column_index_, column)),
     59      kernel_(other.kernel_),
     60      row_index_(utility::Index(other.row_index_, row))
     61  {
    7262  }
    7363 
    7464
    7565  KernelLookup::KernelLookup(const KernelLookup& other)
    76     : DataLookup2D(other), kernel_(other.kernel_)
    77   {
    78     // Checking that no index is out of range
    79     assert(row_index_.empty() ||
    80            *(max_element(row_index_.begin(), row_index_.end()))<
    81            kernel_->size());
    82     assert(column_index_.empty() ||
    83            *(max_element(column_index_.begin(), column_index_.end()))<
    84            kernel_->size());
    85     ref_count_=other.ref_count_;
    86     if (ref_count_)
    87       ++(*ref_count_);
     66    : column_index_(other.column_index_),
     67      kernel_(other.kernel_),
     68      row_index_(other.row_index_)
     69  {
    8870  }
    8971 
    9072
    9173  KernelLookup::KernelLookup(const KernelLookup& other,
    92                              const std::vector<size_t>& index,
     74                             const utility::Index& index,
    9375                             const bool row)
    94     : DataLookup2D(other,index,row), kernel_(other.kernel_)
    95   {
    96     assert(kernel_->size());
    97 
    98     // Checking that no index is out of range
    99     assert(row_index_.empty() ||
    100            *(max_element(row_index_.begin(), row_index_.end()))<
    101            kernel_->size());
    102     assert(column_index_.empty() ||
    103            *(max_element(column_index_.begin(), column_index_.end()))<
    104            kernel_->size());
    105     ref_count_=other.ref_count_;
    106     if (ref_count_)
    107       ++(*ref_count_);
     76    : kernel_(other.kernel_)
     77  {
     78    if (row) {
     79      row_index_ = utility::Index(other.row_index_, index);
     80      column_index_ = other.column_index_;
     81    }
     82    else {
     83      row_index_ = other.row_index_;
     84      column_index_ = utility::Index(other.column_index_, index);
     85    }
    10886  }
    10987 
     
    11189  KernelLookup::~KernelLookup(void)
    11290  {
    113     if (ref_count_)
    114       if (!--(*ref_count_))
    115         delete kernel_;
    11691  }
    11792
     
    138113
    139114
     115  size_t KernelLookup::columns(void) const
     116  {
     117    return column_index_.size();
     118  }
     119
     120
    140121  const DataLookup2D* KernelLookup::data(void) const
    141122  {
    142     return kernel_->data().training_data(column_index_);
     123    return kernel_->data().training_data(column_index_.vector());
    143124  }
    144125
     
    176157
    177158
     159  size_t KernelLookup::rows(void) const
     160  {
     161    return row_index_.size();
     162  }
     163
     164
    178165  const KernelLookup*
    179   KernelLookup::selected(const std::vector<size_t>& inputs) const
     166  KernelLookup::selected(const utility::Index& inputs) const
    180167  {
    181168    const Kernel* kernel;
     
    185172      assert(ml);
    186173      const MatrixLookupWeighted* ms =
    187         new MatrixLookupWeighted(*ml,inputs,true);
     174        new MatrixLookupWeighted(*ml,inputs.vector(),true);
    188175      kernel = kernel_->make_kernel(*ms, false);
    189176    }
     
    193180      assert(m);
    194181      // matrix with selected features
    195       const MatrixLookup* ms = new MatrixLookup(*m,inputs,true);
     182      const MatrixLookup* ms = new MatrixLookup(*m,inputs.vector(),true);
    196183      kernel = kernel_->make_kernel(*ms,true);
    197184    }
     
    235222        kernel_->make_kernel(*tmp, true);
    236223
    237       return new KernelLookup(*kernel, row_index, column_index, true);
     224      return new KernelLookup(*kernel, utility::Index(row_index),
     225                              utility::Index(column_index), true);
    238226    }
    239227
     
    273261                                                         *weight_all, true);
    274262    const Kernel* kernel = kernel_->make_kernel(*tmp, true);
    275     return new KernelLookup(*kernel, row_index_, column_index, true);
     263    return new KernelLookup(*kernel, row_index_, utility::Index(column_index),
     264                            true);
    276265  }
    277266
     
    323312    const Kernel* kernel =
    324313      kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true));
    325     return new KernelLookup(*kernel, row_index_, column_index, true);
     314    return new KernelLookup(*kernel, row_index_, utility::Index(column_index),
     315                            true);
    326316  }
    327317
     
    330320  KernelLookup::training_data(const std::vector<size_t>& train) const
    331321  {
    332     return new KernelLookup(*this,train,train);
     322    return new KernelLookup(*this,utility::Index(train),utility::Index(train));
    333323  }
    334324
     
    338328                                const std::vector<size_t>& validation) const
    339329  {
    340     return new KernelLookup(*this,train,validation);
     330    return new KernelLookup(*this,utility::Index(train),
     331                            utility::Index(validation));
    341332  }
    342333
  • trunk/yat/classifier/KernelLookup.h

    r1125 r1126  
    3131#include "MatrixLookup.h"
    3232#include "yat/utility/Container2DIterator.h"
     33#include "yat/utility/Index.h"
    3334#include "yat/utility/iterator_traits.h"
     35#include "yat/utility/SmartPtr.h"
    3436#include "yat/utility/StrideIterator.h"
    35 
    36 
    37 #include <vector>
    3837
    3938namespace theplu {
    4039namespace yat {
     40  namespace utility{
     41    class Index;
     42  }
    4143namespace classifier {
    4244
     
    6668  /// constructors and assignments.
    6769  ///
    68   class KernelLookup : public DataLookup2D
     70  class KernelLookup
    6971  {
    7072
     
    116118    /// undefined.
    117119    ///
    118     /// @note For training usage row index shall always be equal to
    119     /// column index.
    120     ///
    121     KernelLookup(const Kernel& kernel, const std::vector<size_t>& row,
    122                  const std::vector<size_t>& column, const bool owner=false);
     120    /// @note For training usage row Index shall always be equal to
     121    /// column Index.
     122    ///
     123    KernelLookup(const Kernel& kernel, const utility::Index& row,
     124                 const utility::Index& column, const bool owner=false);
    123125
    124126    ///
     
    138140    ///
    139141    /// Contructor building a sub-KernelLookup from a KernelLookup
    140     /// defined by row index vector and column index vector. In the
     142    /// defined by row index and column index. In the
    141143    /// created Lookup the element in the \f$ i \f$ th row in the
    142144    /// \f$ j \f$ th column is identical to the element in row row[i] and
     
    151153    /// column index.
    152154    ///
    153     KernelLookup(const KernelLookup& kl, const std::vector<size_t>& row,
    154                  const std::vector<size_t>& column);
    155 
    156     ///
    157     /// Constructor taking the column (default) or row index vector as
     155    KernelLookup(const KernelLookup& kl, const utility::Index& row,
     156                 const utility::Index& column);
     157
     158    ///
     159    /// Constructor taking the column (default) or row Index as
    158160    /// input. If @a row is false the created KernelLookup will have
    159161    /// equally many rows as @a kernel.
     
    166168    /// undefined.
    167169    ///
    168     KernelLookup(const KernelLookup& kernel, const std::vector<size_t>&,
     170    KernelLookup(const KernelLookup& kernel, const utility::Index&,
    169171                 const bool row=false);
    170172
     
    198200     */
    199201    const_row_iterator begin_row(size_t) const;
     202
     203    /**
     204       \return Number of columns
     205    */
     206    size_t columns(void) const;
    200207
    201208    ///
     
    245252
    246253    /**
     254       \return Number of columns
     255    */
     256    size_t rows(void) const;
     257
     258    /**
    247259       Each element in returned KernelLookup is calculated using only
    248260       selected features (defined by @a index). Each element
     
    253265       to be deleted by the caller to avoid memory leaks.
    254266    */
    255     const KernelLookup* selected(const std::vector<size_t>& index) const;
     267    const KernelLookup* selected(const utility::Index& index) const;
    256268   
    257269    /**
     
    326338    const KernelLookup& operator=(const KernelLookup&);
    327339
    328     const Kernel* kernel_;
     340    utility::Index column_index_;
     341    utility::SmartPtr<const Kernel> kernel_;
     342    utility::Index row_index_;
    329343   
    330344  }; // class KernelLookup
  • trunk/yat/utility/Index.cc

    r1106 r1126  
    4747
    4848
     49  Index::Index(const std::vector<size_t>& vec)
     50    : index_(SmartPtr<const std::vector<size_t> >(new std::vector<size_t>(vec)))
     51  {}
     52
     53
    4954  Index::Index(const Index& a, const Index& b)
    5055  {
     
    6368
    6469
     70  const std::vector<size_t>& Index::vector(void) const
     71  {
     72    return *index_;
     73  }
     74
     75
    6576  const size_t& Index::operator[](size_t i) const
    6677  {
  • trunk/yat/utility/Index.h

    r1106 r1126  
    6363
    6464    /**
    65        \brief access operator
    66     */
    67     const size_t& operator[](size_t) const;
     65       \brief Constructor
     66
     67       \a vec is copied
     68     */
     69    explicit Index(const std::vector<size_t>& vec);
    6870
    6971    /**
     
    7173    */
    7274    size_t size(void) const;
     75
     76    /**
     77       \brief underlying std::vector
     78    */
     79    const std::vector<size_t>& vector(void) const;
     80
     81    /**
     82       \brief access operator
     83    */
     84    const size_t& operator[](size_t) const;
    7385
    7486  private:
Note: See TracChangeset for help on using the changeset viewer.