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