Ignore:
Timestamp:
Oct 10, 2007, 6:49:39 PM (14 years ago)
Author:
Peter
Message:

Fixed so NBC and SVM are throwing when unexpected DataLookup2D is
apssed to make_classifier or predict.

Speeding up NBC::predict by separating weighted code from
unweighted. Also fixed some bugs in NBC.

File:
1 edited

Legend:

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

    r950 r959  
    116116  {
    117117    SVM* sc=0;
    118     const KernelLookup& kernel = dynamic_cast<const KernelLookup&>(data);
    119     assert(data.rows()==data.columns());
    120     assert(data.columns()==target.size());
    121     sc = new SVM(kernel,target);
    122    
    123     //Copy those variables possible to modify from outside
    124     // Peter, in particular C
     118    try {
     119      const KernelLookup& kernel = dynamic_cast<const KernelLookup&>(data);
     120      assert(data.rows()==data.columns());
     121      assert(data.columns()==target.size());
     122      sc = new SVM(kernel,target);
     123      //Copy those variables possible to modify from outside
     124      // Peter, in particular C
     125    }
     126    catch (std::bad_cast& e) {
     127      std::string str =
     128        "Error in SVM::make_classifier: DataLookup2D of unexpected class.";
     129      throw std::runtime_error(str);
     130    }
    125131 
    126132    return sc;
     
    139145  void SVM::predict(const DataLookup2D& input, utility::matrix& prediction) const
    140146  {
    141     const KernelLookup& input_kernel = dynamic_cast<const KernelLookup&>(input);
    142 
    143     assert(input.rows()==alpha_.size());
    144     prediction.clone(utility::matrix(2,input.columns(),0));
    145     for (size_t i = 0; i<input.columns(); i++){
    146       for (size_t j = 0; j<input.rows(); j++){
    147         prediction(0,i) += target(j)*alpha_(j)*input_kernel(j,i);
    148         assert(target(j));
    149       }
    150       prediction(0,i) = margin_ * (prediction(0,i) + bias_);
    151     }
    152 
    153     for (size_t i = 0; i<prediction.columns(); i++)
    154       prediction(1,i) = -prediction(0,i);
     147    try {
     148      const KernelLookup& input_kernel =dynamic_cast<const KernelLookup&>(input);
     149      assert(input.rows()==alpha_.size());
     150      prediction.clone(utility::matrix(2,input.columns(),0));
     151      for (size_t i = 0; i<input.columns(); i++){
     152        for (size_t j = 0; j<input.rows(); j++){
     153          prediction(0,i) += target(j)*alpha_(j)*input_kernel(j,i);
     154          assert(target(j));
     155        }
     156        prediction(0,i) = margin_ * (prediction(0,i) + bias_);
     157      }
     158     
     159      for (size_t i = 0; i<prediction.columns(); i++)
     160        prediction(1,i) = -prediction(0,i);
     161    }
     162    catch (std::bad_cast& e) {
     163      std::string str =
     164        "Error in SVM::predict: DataLookup2D of unexpected class.";
     165      throw std::runtime_error(str);
     166    }
    155167   
    156     assert(prediction(0,0));
    157168  }
    158169
Note: See TracChangeset for help on using the changeset viewer.