29 free(subsample_order);
37 std::cout <<
"label " << ld.
label <<
" is not in {1," << o.
k <<
"} This won't work right." << std::endl;
42 size_t prediction = ld.
label;
46 float weight_temp = ec.
weight;
54 if (l == ld.
label - 1)
71 template <
bool is_learn,
bool pr
int_all,
bool scores,
bool probabilities>
75 if (mc_label_data.
label == 0 || (mc_label_data.
label > o.
k && mc_label_data.
label != (uint32_t)-1))
76 std::cout <<
"label " << mc_label_data.
label <<
" is not in {1," << o.
k <<
"} This won't work right." << std::endl;
78 std::stringstream outputStringStream;
79 uint32_t prediction = 1;
84 ec.
l.
simple = {FLT_MAX, 0.f, 0.f};
86 for (uint32_t i = 2; i <= o.
k; i++)
95 for (uint32_t i = 1; i <= o.
k; i++)
105 outputStringStream <<
"1:" << o.
pred[0].
scalar;
106 for (uint32_t i = 2; i <= o.
k; i++) outputStringStream <<
' ' << i <<
':' << o.
pred[i - 1].
scalar;
112 scores_array.
clear();
119 for (uint32_t i = 0; i < o.
k; i++)
124 float inv_sum_prob = 1.f / sum_prob;
125 for (uint32_t i = 0; i < o.
k; i++) ec.
pred.
scalars[i] *= inv_sum_prob;
131 ec.
l.
multi = mc_label_data;
135 template <
bool probabilities>
145 float multiclass_log_loss = 999;
146 float correct_class_prob = 0;
151 if (correct_class_prob > 0)
152 multiclass_log_loss = -log(correct_class_prob) * ec.
weight;
161 uint32_t prediction = 0;
162 for (uint32_t i = 1; i < o.
k; i++)
166 float zero_one_loss = 0;
168 zero_one_loss = ec.
weight;
171 std::ostringstream outputStringStream;
172 for (uint32_t i = 0; i < o.
k; i++)
175 outputStringStream <<
' ';
179 outputStringStream << std::string(ss.
begin, ss.
end - ss.
begin);
182 outputStringStream << i + 1;
205 auto data = scoped_calloc_or_throw<oaa>();
206 bool probabilities =
false;
209 new_options.
add(
make_option(
"oaa", data->k).keep().help(
"One-against-all multiclass with <k> labels"))
211 .help(
"subsample this number of negative examples when learning"))
212 .
add(
make_option(
"probabilities", probabilities).help(
"predict probabilites of all classes"))
213 .
add(
make_option(
"scores", scores).help(
"output raw scores per class"));
220 THROW(
"error: you have " << all.
sd->
ldict->
getK() <<
" named labels; use that as the argument to oaa")
223 data->pred = calloc_or_throw<polyprediction>(data->k);
224 data->subsample_order =
nullptr;
225 data->subsample_id = 0;
226 if (data->num_subsample > 0)
228 if (data->num_subsample >= data->k)
230 data->num_subsample = 0;
231 all.
trace_message <<
"oaa is turning off subsampling because your parameter >= K" << std::endl;
235 data->subsample_order = calloc_or_throw<uint32_t>(data->k);
236 for (
size_t i = 0; i < data->k; i++) data->subsample_order[i] = (uint32_t)i;
237 for (
size_t i = 0; i < data->k; i++)
239 size_t j = (size_t)(all.
get_random_state()->get_and_update_random() * (float)(data->k - i)) + i;
240 uint32_t tmp = data->subsample_order[i];
241 data->subsample_order[i] = data->subsample_order[j];
242 data->subsample_order[j] = tmp;
247 oaa* data_ptr = data.get();
250 if (probabilities || scores)
256 if (loss_function_type !=
"logistic")
257 all.
trace_message <<
"WARNING: --probabilities should be used only with --loss_function=logistic" << std::endl;
bool report_multiclass_log_loss
void(* delete_prediction)(void *)
void predict_or_learn(oaa &o, LEARNER::single_learner &base, example &ec)
void set_learn(void(*u)(T &, L &, E &))
LEARNER::base_learner * oaa_setup(options_i &options, vw &all)
void finish_example_scores(vw &all, oaa &o, example &ec)
v_array< int > final_prediction_sink
double holdout_multiclass_log_loss
base_learner * make_base(learner< T, E > &base)
virtual void add_and_parse(const option_group_definition &group)=0
void print_update_with_probability(vw &all, example &ec, uint32_t pred)
#define add_passthrough_feature(ec, i, x)
void print_update_with_score(vw &all, example &ec, uint32_t pred)
std::shared_ptr< rand_state > get_random_state()
single_learner * as_singleline(learner< T, E > *l)
double multiclass_log_loss
MULTICLASS::label_t multi
void set_finish_example(void(*f)(vw &all, T &, E &))
void push_back(const T &new_ele)
void delete_scalars(void *v)
virtual bool was_supplied(const std::string &key)=0
virtual std::string getType()=0
void(* print_text)(int, std::string, v_array< char >)
void finish_example(vw &, example &)
void update(bool test_example, bool labeled_example, float loss, float weight, size_t num_features)
option_group_definition & add(T &&op)
int add(svm_params ¶ms, svm_example *fec)
typed_option< T > make_option(std::string name, T &location)
uint32_t * subsample_order
learner< T, E > & init_multiclass_learner(free_ptr< T > &dat, L *base, void(*learn)(T &, L &, E &), void(*predict)(T &, L &, E &), parser *p, size_t ws, prediction_type::prediction_type_t pred_type=prediction_type::multiclass)
void multipredict(E &ec, size_t lo, size_t count, polyprediction *pred, bool finalize_predictions)
uint64_t get(substring &s)
LEARNER::base_learner * setup_base(options_i &options, vw &all)
void update(E &ec, size_t i=0)
void learn(E &ec, size_t i=0)
void learn_randomized(oaa &o, LEARNER::single_learner &base, example &ec)