-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
active.cc
158 lines (134 loc) · 4.44 KB
/
active.cc
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
#include <errno.h>
#include "reductions.h"
#include "rand48.h"
#include "float.h"
#include "vw.h"
#include "active.h"
#include "vw_exception.h"
using namespace LEARNER;
using namespace std;
float get_active_coin_bias(float k, float avg_loss, float g, float c0)
{ float b,sb,rs,sl;
b=(float)(c0*(log(k+1.)+0.0001)/(k+0.0001));
sb=sqrt(b);
avg_loss = min(1.f, max(0.f, avg_loss)); //loss should be in [0,1]
sl=sqrt(avg_loss)+sqrt(avg_loss+g);
if (g<=sb*sl+b)
return 1;
rs = (sl+sqrt(sl*sl+4*g))/(2*g);
return b*rs*rs;
}
float query_decision(active& a, float ec_revert_weight, float k)
{ float bias, avg_loss, weighted_queries;
if (k<=1.)
bias=1.;
else
{ weighted_queries = (float)(a.all->initial_t + a.all->sd->weighted_examples - a.all->sd->weighted_unlabeled_examples);
avg_loss = (float)(a.all->sd->sum_loss/k + sqrt((1.+0.5*log(k))/(weighted_queries+0.0001)));
bias = get_active_coin_bias(k, avg_loss, ec_revert_weight/k, a.active_c0);
}
if(merand48(a.all->random_state) < bias)
return 1.f / bias;
else
return -1.;
}
template <bool is_learn>
void predict_or_learn_simulation(active& a, base_learner& base, example& ec)
{ base.predict(ec);
if (is_learn)
{ vw& all = *a.all;
float k = (float)all.sd->t;
float threshold = 0.f;
ec.confidence = fabsf(ec.pred.scalar - threshold) / base.sensitivity(ec);
float importance = query_decision(a, ec.confidence, k);
if(importance > 0)
{ all.sd->queries += 1;
ec.weight *= importance;
base.learn(ec);
}
else
{ ec.l.simple.label = FLT_MAX;
ec.weight = 0.f;
}
}
}
template <bool is_learn>
void predict_or_learn_active(active& a, base_learner& base, example& ec)
{ if (is_learn)
base.learn(ec);
else
base.predict(ec);
if (ec.l.simple.label == FLT_MAX)
{ float threshold = (a.all->sd->max_label + a.all->sd->min_label) * 0.5f;
ec.confidence = fabsf(ec.pred.scalar - threshold) / base.sensitivity(ec);
}
}
void active_print_result(int f, float res, float weight, v_array<char> tag)
{ if (f >= 0)
{ std::stringstream ss;
char temp[30];
sprintf(temp, "%f", res);
ss << temp;
if(!print_tag(ss, tag))
ss << ' ';
if(weight >= 0)
{ sprintf(temp, " %f", weight);
ss << temp;
}
ss << '\n';
ssize_t len = ss.str().size();
ssize_t t = io_buf::write_file_or_socket(f, ss.str().c_str(), (unsigned int)len);
if (t != len)
cerr << "write error: " << strerror(errno) << endl;
}
}
void output_and_account_example(vw& all, active& a, example& ec)
{ label_data& ld = ec.l.simple;
all.sd->update(ec.test_only, ec.loss, ec.weight, ec.num_features);
if (ld.label != FLT_MAX && !ec.test_only)
all.sd->weighted_labels += ld.label * ec.weight;
all.sd->weighted_unlabeled_examples += ld.label == FLT_MAX ? ec.weight : 0;
float ai=-1;
if(ld.label == FLT_MAX)
ai=query_decision(a, ec.confidence, (float)all.sd->weighted_unlabeled_examples);
all.print(all.raw_prediction, ec.partial_prediction, -1, ec.tag);
for (size_t i = 0; i<all.final_prediction_sink.size(); i++)
{ int f = (int)all.final_prediction_sink[i];
active_print_result(f, ec.pred.scalar, ai, ec.tag);
}
print_update(all, ec);
}
void return_active_example(vw& all, active& a, example& ec)
{ output_and_account_example(all, a, ec);
VW::finish_example(all,&ec);
}
base_learner* active_setup(vw& all)
{ //parse and set arguments
if(missing_option(all, false, "active", "enable active learning")) return nullptr;
new_options(all, "Active Learning options")
("simulation", "active learning simulation mode")
("mellowness", po::value<float>(), "active learning mellowness parameter c_0. Default 8");
add_options(all);
active& data = calloc_or_throw<active>();
data.active_c0 = 8;
data.all=&all;
if (all.vm.count("mellowness"))
data.active_c0 = all.vm["mellowness"].as<float>();
if (count(all.args.begin(), all.args.end(), "--lda") != 0)
{ free(&data);
THROW("error: you can't combine lda and active learning");
}
base_learner* base = setup_base(all);
//Create new learner
learner<active>* l;
if (all.vm.count("simulation"))
l = &init_learner(&data, base, predict_or_learn_simulation<true>,
predict_or_learn_simulation<false>);
else
{ all.active = true;
l = &init_learner(&data, base, predict_or_learn_active<true>,
predict_or_learn_active<false>);
l->set_finish_example(return_active_example);
}
return make_base(*l);
}