Changeset 1127 for trunk/yat/classifier
- Timestamp:
- Feb 22, 2008, 11:46:59 PM (16 years ago)
- Location:
- trunk/yat/classifier
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/classifier/KernelLookup.cc
r1126 r1127 29 29 30 30 #include <cassert> 31 #ifndef NDEBUG 32 #include <algorithm> 33 #endif 31 34 32 35 namespace theplu { … … 35 38 36 39 KernelLookup::KernelLookup(const Kernel& kernel, const bool own) 37 : column_index_(utility::Index(kernel.size())), 38 kernel_(utility::SmartPtr<const Kernel>(&kernel, own)), 39 row_index_(utility::Index(kernel.size())) 40 { 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_; 41 46 } 42 47 43 48 44 49 KernelLookup::KernelLookup(const Kernel& kernel, 45 const utility::Index& row,46 const utility::Index& column,50 const std::vector<size_t>& row, 51 const std::vector<size_t>& column, 47 52 const bool owner) 48 : column_index_(column), 49 kernel_(utility::SmartPtr<const Kernel>(&kernel, false)), 50 row_index_(row) 51 { 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()); 52 61 } 53 62 54 63 55 64 KernelLookup::KernelLookup(const KernelLookup& other, 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 { 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_); 62 72 } 63 73 64 74 65 75 KernelLookup::KernelLookup(const KernelLookup& other) 66 : column_index_(other.column_index_), 67 kernel_(other.kernel_), 68 row_index_(other.row_index_) 69 { 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_); 70 88 } 71 89 72 90 73 91 KernelLookup::KernelLookup(const KernelLookup& other, 74 const utility::Index& index,92 const std::vector<size_t>& index, 75 93 const bool row) 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 } 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_); 86 108 } 87 109 … … 89 111 KernelLookup::~KernelLookup(void) 90 112 { 113 if (ref_count_) 114 if (!--(*ref_count_)) 115 delete kernel_; 91 116 } 92 117 … … 113 138 114 139 115 size_t KernelLookup::columns(void) const116 {117 return column_index_.size();118 }119 120 121 140 const DataLookup2D* KernelLookup::data(void) const 122 141 { 123 return kernel_->data().training_data(column_index_ .vector());142 return kernel_->data().training_data(column_index_); 124 143 } 125 144 … … 157 176 158 177 159 size_t KernelLookup::rows(void) const160 {161 return row_index_.size();162 }163 164 165 178 const KernelLookup* 166 KernelLookup::selected(const utility::Index& inputs) const179 KernelLookup::selected(const std::vector<size_t>& inputs) const 167 180 { 168 181 const Kernel* kernel; … … 172 185 assert(ml); 173 186 const MatrixLookupWeighted* ms = 174 new MatrixLookupWeighted(*ml,inputs .vector(),true);187 new MatrixLookupWeighted(*ml,inputs,true); 175 188 kernel = kernel_->make_kernel(*ms, false); 176 189 } … … 180 193 assert(m); 181 194 // matrix with selected features 182 const MatrixLookup* ms = new MatrixLookup(*m,inputs .vector(),true);195 const MatrixLookup* ms = new MatrixLookup(*m,inputs,true); 183 196 kernel = kernel_->make_kernel(*ms,true); 184 197 } … … 222 235 kernel_->make_kernel(*tmp, true); 223 236 224 return new KernelLookup(*kernel, utility::Index(row_index), 225 utility::Index(column_index), true); 237 return new KernelLookup(*kernel, row_index, column_index, true); 226 238 } 227 239 … … 261 273 *weight_all, true); 262 274 const Kernel* kernel = kernel_->make_kernel(*tmp, true); 263 return new KernelLookup(*kernel, row_index_, utility::Index(column_index), 264 true); 275 return new KernelLookup(*kernel, row_index_, column_index, true); 265 276 } 266 277 … … 312 323 const Kernel* kernel = 313 324 kernel_->make_kernel(MatrixLookupWeighted(*data_all, *weight_all, true)); 314 return new KernelLookup(*kernel, row_index_, utility::Index(column_index), 315 true); 325 return new KernelLookup(*kernel, row_index_, column_index, true); 316 326 } 317 327 … … 320 330 KernelLookup::training_data(const std::vector<size_t>& train) const 321 331 { 322 return new KernelLookup(*this, utility::Index(train),utility::Index(train));332 return new KernelLookup(*this,train,train); 323 333 } 324 334 … … 328 338 const std::vector<size_t>& validation) const 329 339 { 330 return new KernelLookup(*this,utility::Index(train), 331 utility::Index(validation)); 340 return new KernelLookup(*this,train,validation); 332 341 } 333 342 -
trunk/yat/classifier/KernelLookup.h
r1126 r1127 31 31 #include "MatrixLookup.h" 32 32 #include "yat/utility/Container2DIterator.h" 33 #include "yat/utility/Index.h"34 33 #include "yat/utility/iterator_traits.h" 35 #include "yat/utility/SmartPtr.h"36 34 #include "yat/utility/StrideIterator.h" 35 36 37 #include <vector> 37 38 38 39 namespace theplu { 39 40 namespace yat { 40 namespace utility{41 class Index;42 }43 41 namespace classifier { 44 42 … … 68 66 /// constructors and assignments. 69 67 /// 70 class KernelLookup 68 class KernelLookup : public DataLookup2D 71 69 { 72 70 … … 118 116 /// undefined. 119 117 /// 120 /// @note For training usage row Index shall always be equal to121 /// column Index.122 /// 123 KernelLookup(const Kernel& kernel, const utility::Index& row,124 const utility::Index& column, const bool owner=false);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); 125 123 126 124 /// … … 140 138 /// 141 139 /// Contructor building a sub-KernelLookup from a KernelLookup 142 /// defined by row index and column index. In the140 /// defined by row index vector and column index vector. In the 143 141 /// created Lookup the element in the \f$ i \f$ th row in the 144 142 /// \f$ j \f$ th column is identical to the element in row row[i] and … … 153 151 /// column index. 154 152 /// 155 KernelLookup(const KernelLookup& kl, const utility::Index& row,156 const utility::Index& column);157 158 /// 159 /// Constructor taking the column (default) or row Indexas153 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 160 158 /// input. If @a row is false the created KernelLookup will have 161 159 /// equally many rows as @a kernel. … … 168 166 /// undefined. 169 167 /// 170 KernelLookup(const KernelLookup& kernel, const utility::Index&,168 KernelLookup(const KernelLookup& kernel, const std::vector<size_t>&, 171 169 const bool row=false); 172 170 … … 200 198 */ 201 199 const_row_iterator begin_row(size_t) const; 202 203 /**204 \return Number of columns205 */206 size_t columns(void) const;207 200 208 201 /// … … 252 245 253 246 /** 254 \return Number of columns255 */256 size_t rows(void) const;257 258 /**259 247 Each element in returned KernelLookup is calculated using only 260 248 selected features (defined by @a index). Each element … … 265 253 to be deleted by the caller to avoid memory leaks. 266 254 */ 267 const KernelLookup* selected(const utility::Index& index) const;255 const KernelLookup* selected(const std::vector<size_t>& index) const; 268 256 269 257 /** … … 338 326 const KernelLookup& operator=(const KernelLookup&); 339 327 340 utility::Index column_index_; 341 utility::SmartPtr<const Kernel> kernel_; 342 utility::Index row_index_; 328 const Kernel* kernel_; 343 329 344 330 }; // class KernelLookup
Note: See TracChangeset
for help on using the changeset viewer.