Example #1
0
 void train(unsigned n_boosting_item, double sigma_noise = 0.1)
 {
     MatrixXd    sample_weight(1, dataset.rows());
     sample_weight = MatrixXd::Ones(1, dataset.rows());
     sample_weight /= sample_weight.sum();
     
     while (n_boosting_item --> 0)
     {
         WLMS* new_boosting_item = new WLMS(dataset, label, sample_weight, sigma_noise);
         double error_rate = 1e-5;
         for(auto i=0; i<dataset.rows(); ++i)
         {
             if (label(i) * new_boosting_item->infer(dataset.row(i)) < 0)
             {
                 error_rate += sample_weight(i);
             }
         }
         
         double update_rate = log((1-error_rate)/error_rate);
         boosting_item.push_back(make_pair(new_boosting_item, update_rate));
         
         for(auto i=0; i<dataset.rows(); ++i)
         {
             sample_weight(i) *= exp(-update_rate * label(i) * new_boosting_item->infer(dataset.row(i)));
         }
         sample_weight /= sample_weight.sum();
     }
 }
Example #2
0
void ClassificationCriterion::init(Mat _y,
                                   Mat _sample_weight,
                                   double _weight_n_samples,
                                   vector<int>& _samples,
                                   int _start,
                                   int _end)
{
    y = _y;
    sample_weight = _sample_weight;
    weighted_n_samples = _weight_n_samples;
    samples = _samples;
    start = _start;
    end = _end;

    // Find how many classes in y
    std::set<double> unique;
    for (int i = 0; i < y.rows; i++)
    {
        if (unique.find(y.at<double>(i)) == unique.end())
            unique.insert(y.at<double>(i));
    }
    n_classes = unique.size();

    // Initialize
    label_count_total.resize(n_classes);
    label_count_left.resize(n_classes);
    label_count_right.resize(n_classes);

    double _weighted_n_node_samples = 0.0;
    double w = 1.0;
    int index;
    for (int i = start; i < end; i++)
    {
        index = samples.at(i);

        if (sample_weight.total() == 0)
        {
            w = sample_weight(index);
        }

        // Get count of every class
        int c = (int)y.at<double>(index, 0);
        label_count_total.at(c) += w;

        _weighted_n_node_samples += w;
    }
    weighted_n_node_samples = _weighted_n_node_samples;
    reset();
}