Changeset 559 for trunk/lib/classifier/SVM.cc
- Timestamp:
- Mar 11, 2006, 11:21:27 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/classifier/SVM.cc
r552 r559 35 35 tolerance_(0.00000001) 36 36 { 37 #ifndef NDEBUG 38 for (size_t i=0; i<alpha_.size(); i++) 39 for (size_t j=0; j<alpha_.size(); j++) 40 assert(kernel(i,j)==kernel(j,i)); 41 for (size_t i=0; i<alpha_.size(); i++) 42 for (size_t j=0; j<alpha_.size(); j++) 43 assert((*kernel_)(i,j)==(*kernel_)(j,i)); 44 for (size_t i = 0; i<kernel_->rows(); i++) 45 for (size_t j = 0; j<kernel_->columns(); j++) 46 if (std::isnan((*kernel_)(i,j))) 47 std::cerr << "SVM: Found nan in kernel: " << i << " " 48 << j << std::endl; 49 #endif 50 37 51 } 38 52 … … 84 98 const Target& target) const 85 99 { 100 assert(data.rows()==data.columns()); 101 assert(data.columns()==target.size()); 86 102 // Peter, should check success of dynamic_cast 87 103 const KernelLookup& tmp = dynamic_cast<const KernelLookup&>(data); … … 92 108 sc = new SVM(tmp,target); 93 109 110 94 111 //Copy those variables possible to modify from outside 95 112 return sc; … … 99 116 { 100 117 // Peter, should check success of dynamic_cast 101 const KernelLookup* input_kernel= &dynamic_cast<const KernelLookup&>(input); 102 118 const KernelLookup input_kernel = dynamic_cast<const KernelLookup&>(input); 119 120 const KernelLookup* kernel_pointer; 103 121 if (ranker_) {// feature selection 104 122 std::vector<size_t> index; … … 106 124 for (size_t i=0; i<nof_inputs_; i++) 107 125 index.push_back(ranker_->id(i)); 108 input_kernel = input_kernel->selected(index); 109 } 126 kernel_pointer = input_kernel.selected(index); 127 } 128 else 129 kernel_pointer = &input_kernel; 110 130 111 131 assert(input.rows()==alpha_.size()); 112 132 prediction = gslapi::matrix(2,input.columns(),0); 113 133 for (size_t i = 0; i<input.columns(); i++){ 114 for (size_t j = 0; j<input.rows(); j++) 115 prediction(0,i) += target(j)*alpha_(i)*(*input_kernel)(j,i); 134 for (size_t j = 0; j<input.rows(); j++){ 135 prediction(0,i) += target(j)*alpha_(j)*(*kernel_pointer)(j,i); 136 assert(target(j)); 137 } 116 138 prediction(0,i) += bias_; 117 139 } … … 119 141 for (size_t i = 0; i<prediction.columns(); i++) 120 142 prediction(1,i) = -prediction(0,i); 143 144 if (ranker_) 145 delete kernel_pointer; 146 assert(prediction(0,0)); 121 147 } 122 148 … … 150 176 for (size_t j=0; j<E.size(); j++) 151 177 E(i) += kernel_mod(i,j)*target(j)*alpha_(j); 152 E(i) =E(i)-target(i);178 E(i)-=target(i); 153 179 } 154 180 assert(target_.size()==E.size()); … … 170 196 double alpha_old1=alpha_(sample_.value_first()); 171 197 double alpha_old2=alpha_(sample_.value_second()); 172 173 198 alpha_new2 = ( alpha_(sample_.value_second()) + 174 199 target(sample_.value_second())* … … 179 204 else if (alpha_new2<u) 180 205 alpha_new2 = u; 181 182 206 183 207 // Updating the alphas … … 254 278 assert(alpha_(sample_.value_second())>tolerance_); 255 279 256 257 280 if (E(sample_.value_second()) - E(sample_.value_first()) > 2*tolerance_){ 258 281 return true; … … 279 302 // if no support vectors - special case 280 303 else{ 304 // to avoid getting stuck we shuffle 305 sample_.shuffle(); 281 306 for (size_t i=0; i<sample_.n(); i++) { 282 307 if (target_.binary(sample_(i))){
Note: See TracChangeset
for help on using the changeset viewer.