-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcls_boosting_second.h
executable file
·160 lines (142 loc) · 3.43 KB
/
cls_boosting_second.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "lib_import.h"
class cls_boosting_adaptive_second
:public cls_boosting
{
public:
cls_boosting_adaptive_second()
:cls_boosting()
{
;
}
virtual void train_example( cls_training_set & ts_in )
{
if (ts_in.get_output_classes_count() != 2)
throw cls_exception_not_sense("cls_boosting_first::train_example() - the clser only can train two classes.");
cls_training_set ts(ts_in.get_input_size(),ts_in.get_output_size());
trans_training_set(ts_in,ts);
vector<double> weights;
weights.resize(ts.get_size());
int size = weights.size();
fill(weights.begin(),weights.end(),1.0/size);
vector<pair<cls_classifier*,double> >::iterator itor;
for(itor = weakers.begin();itor != weakers.end();itor++)
{
vector<bool> bs(size);
fill(bs.begin(),bs.end(),false);
int n_times = 0;
switch(itor->first->get_classifier_type())
{
case en_clser_ann:
n_times = 50;
break;
case en_clser_bagging:
n_times = 10;
break;
case en_clser_bayes:
n_times = 1;
break;
case en_clser_boosting:
n_times = 10;
break;
case en_clser_hmm:
n_times = 1;
break;
case en_clser_linear:
n_times = 50;
break;
case en_clser_neaest:
n_times = 1;
break;
case en_clser_svm:
n_times = 1;
break;
default:
throw cls_exception_not_found("cls_boosting_first::train_example() - the classifier can't be found,\n");
}
while(n_times--)
{
itor->first->train_example(ts);
}
itor->second = 0.0;
ts.initialize();
while(!ts.epoch_over())
{
get_next_pair_for_ts(ts,a,b);
try
{
if (itor->first->get_output_data(a) != b)
{
bs[ts.get_count()-1] = true;
itor->second += weights[ts.get_count()-1];
}
}
catch (cls_exception &exc)
{
try
{
if (itor->first->get_output_index(a) != ts.get_example_id(ts.get_count()-1))
{
bs[ts.get_count()-1] = true;
itor->second += weights[ts.get_count()-1];
}
}
catch (cls_exception &exc)
{
throw ;
}
}
}
itor->second /= size;
for(int cnt=0;cnt<size;cnt++)
{
if (bs[cnt] == false)
{
weights[cnt] = weights[cnt] * (itor->second + 0.001) / (1 - itor->second + 0.001);
}
else
{
weights[cnt] = weights[cnt] *(1 - itor->second + 0.001)/(itor->second + 0.001);
}
}
vector<double>::iterator itor;
double d_sum = 0.001;
for(itor=weights.begin();itor !=weights.end();itor++)
{
d_sum += (*itor);
}
for(itor=weights.begin();itor != weights.end();itor++)
{
(*itor) /= d_sum;
}
}
}
virtual int get_output_index(const vector<double> & input )
{
double d_sign = 0.0;
vector<pair<cls_classifier*,double> >::iterator itor;
for(itor = weakers.begin();itor != weakers.end();itor++)
{
try
{
int idx = itor->first->get_output_index(input);
d_sign += ((idx==1)?-1:1) * log((1 - itor->second + 0.001)/(itor->second + 0.001));
}
catch (cls_exception &exc)
{
try
{
int idx = get_index_for_output(itor->first->get_output_data(input));
d_sign += ((idx==1)?-1:1) * log((1 - itor->second + 0.001)/(itor->second + 0.001));
}
catch (cls_exception &exc)
{
cout<<exc.display()<<endl;
}
}
}
if (d_sign >= 0)
return 0;
else
return 1;
}
};