11 #define B_SEARCH_MAX_ITER 20 67 float maxw = std::min(fhat / sens, FLT_MAX);
69 if (maxw * fhat * fhat <= delta)
72 float l = 0, u = maxw, w, v;
77 v = w * (fhat * fhat - (fhat - sens * w) * (fhat - sens * w)) - delta;
82 if (fabs(v) <= tol || u - l <= tol)
89 template <
bool is_learn,
bool is_simulation>
91 float& score,
float& partial_prediction,
bool query_this_label,
bool& query_needed)
103 if (query_this_label)
120 cerr <<
"warning: cost " << cost <<
" outside of cost range [" << cs_a.
cost_min <<
", " << cs_a.
cost_max 128 base.
learn(ec, i - 1);
130 else if (!is_simulation)
133 query_needed = query_this_label;
145 float& min_pred,
float& max_pred,
bool& is_range_large)
152 if (cs_a.
t <= 1 || std::isnan(sens) || std::isinf(sens))
156 is_range_large =
true;
158 cerr <<
" find_cost_rangeA: i=" << i <<
" pp=" << ec.
partial_prediction <<
" sens=" << sens <<
" eta=" << eta
159 <<
" [" << min_pred <<
", " << max_pred <<
"] = " << (max_pred - min_pred) << endl;
168 is_range_large = (max_pred - min_pred > eta);
170 cerr <<
" find_cost_rangeB: i=" << i <<
" pp=" << ec.
partial_prediction <<
" sens=" << sens <<
" eta=" << eta
171 <<
" [" << min_pred <<
", " << max_pred <<
"] = " << (max_pred - min_pred) << endl;
175 template <
bool is_learn,
bool is_simulation>
185 std::stringstream filename;
189 cerr << endl <<
"Number of examples with at least one query = " << cs_a.
num_any_queries;
194 cerr << endl <<
"examples with " << i <<
" labels queried = " << cs_a.
examples_by_queries[i];
207 cerr << endl << endl;
213 uint32_t prediction = 1;
214 float score = FLT_MAX;
217 float min_max_cost = FLT_MAX;
218 float t = (float)cs_a.
t;
219 float t_prev = t - 1.f;
222 float delta = cs_a.
c0 * log((
float)(cs_a.
num_classes * std::max(t_prev, 1.f))) *
225 if (ld.
costs.size() > 0)
230 lq_data f = {0.0, 0.0, 0, 0, 0, cl};
233 uint32_t n_overlapped = 0;
237 min_max_cost = std::min(min_max_cost, lqd.
max_pred);
259 bool query = (n_overlapped > 1);
266 inner_loop<is_learn, is_simulation>(cs_a, base, ec, lqd.
cl.
class_index, lqd.
cl.
x, prediction, score,
270 if (cs_a.print_debug_stuff)
271 cerr <<
"label=" << lqd.
cl.
class_index <<
" x=" << lqd.
cl.
x <<
" prediction=" << prediction
274 << lqd.
min_pred <<
", " << lqd.
max_pred <<
"] vs delta=" << delta <<
" n_overlapped=" << n_overlapped
275 <<
" is_baseline=" << cs_a.is_baseline << endl;
297 bool temp2 =
false, temp3 =
false;
300 inner_loop<false, is_simulation>(cs_a, base, ec, i, FLT_MAX, prediction, score, temp, temp2, temp3);
312 auto data = scoped_calloc_or_throw<cs_active>();
314 bool simulation =
false;
318 .
add(
make_option(
"cs_active", data->num_classes).keep().help(
"Cost-sensitive active learning with <k> costs"))
319 .
add(
make_option(
"simulation", simulation).help(
"cost-sensitive active learning simulation mode"))
320 .
add(
make_option(
"baseline", data->is_baseline).help(
"cost-sensitive active learning baseline"))
323 .help(
"cost-sensitive active learning use domination. Default 1"))
324 .
add(
make_option(
"mellowness", data->c0).default_value(0.1
f).help(
"mellowness parameter c_0. Default 0.1."))
327 .help(
"parameter controlling the threshold for per-label cost uncertainty. Default 0.5."))
328 .
add(
make_option(
"max_labels", data->max_labels).default_value(-1).help(
"maximum number of label queries."))
329 .
add(
make_option(
"min_labels", data->min_labels).default_value(-1).help(
"minimum number of label queries."))
330 .
add(
make_option(
"cost_max", data->cost_max).default_value(1.
f).help(
"cost upper bound. Default 1."))
331 .
add(
make_option(
"cost_min", data->cost_min).default_value(0.
f).help(
"cost lower bound. Default 0."))
333 .
add(
make_option(
"csa_debug", data->print_debug_stuff).help(
"print debug stuff for cs_active"));
336 data->use_domination =
true;
338 data->use_domination =
false;
347 if (loss_function_type !=
"squared")
348 THROW(
"error: you can't use non-squared loss with cs_active");
351 THROW(
"error: you can't combine lda and active learning");
354 THROW(
"error: you can't use --cs_active and --active at the same time");
357 THROW(
"error: you can't use --cs_active and --active_cover at the same time");
360 THROW(
"error: you can't use --cs_active and --csoaa at the same time");
363 all.
trace_message <<
"WARNING: --cs_active should be used with --adax" << endl;
368 for (uint32_t i = 0; i < data->num_classes + 1; i++) data->examples_by_queries.push_back(0);
void predict(E &ec, size_t i=0)
LEARNER::base_learner * cost_sensitive
void find_cost_range(cs_active &cs_a, single_learner &base, example &ec, uint32_t i, float delta, float eta, float &min_pred, float &max_pred, bool &is_range_large)
LEARNER::base_learner * l
size_t overlapped_and_range_small
base_learner * make_base(learner< T, E > &base)
virtual void add_and_parse(const option_group_definition &group)=0
void save_predictor(vw &all, std::string reg_name)
void finish_example(vw &all, csoaa &, example &ec)
#define add_passthrough_feature(ec, i, x)
single_learner * as_singleline(learner< T, E > *l)
void(* set_minmax)(shared_data *sd, float label)
void set_finish_example(void(*f)(vw &all, T &, E &))
float binarySearch(float fhat, float delta, float sens, float tol)
learner< T, E > & init_learner(free_ptr< T > &dat, L *base, void(*learn)(T &, L &, E &), void(*predict)(T &, L &, E &), size_t ws, prediction_type::prediction_type_t pred_type)
void push_back(const T &new_ele)
v_array< lq_data > query_data
void finish_example(vw &all, cs_active &cs_a, example &ec)
virtual bool was_supplied(const std::string &key)=0
virtual std::string getType()=0
base_learner * cs_active_setup(options_i &options, vw &all)
#define B_SEARCH_MAX_ITER
void inner_loop(cs_active &cs_a, single_learner &base, example &ec, uint32_t i, float cost, uint32_t &prediction, float &score, float &partial_prediction, bool query_this_label, bool &query_needed)
float sensitivity(example &ec, size_t i=0)
option_group_definition & add(T &&op)
int add(svm_params ¶ms, svm_example *fec)
void predict_or_learn(cs_active &cs_a, single_learner &base, example &ec)
typed_option< T > make_option(std::string name, T &location)
v_array< size_t > examples_by_queries
size_t labels_outside_range
COST_SENSITIVE::wclass & cl
LEARNER::base_learner * setup_base(options_i &options, vw &all)
void learn(E &ec, size_t i=0)
std::string final_regressor_name