35 #define SVM_KER_POLY 2 179 for (
size_t i = krow.size(); i < n; i++)
196 int rowsize = (int)krow.size();
197 krow.end() = krow.begin();
207 params.
all->
trace_message <<
"Internal error at " << __FILE__ <<
":" << __LINE__ << endl;
211 float svi_alpha = model->
alpha[svi];
212 float svi_delta = model->
delta[svi];
213 for (
size_t i = svi; i > 0; --i)
220 model->
alpha[0] = svi_alpha;
221 model->
delta[0] = svi_delta;
223 for (
size_t j = 0; j < n; j++)
229 float kv = e->
krow[svi];
230 for (
size_t i = svi; i > 0; --i) e->
krow[i] = e->
krow[i - 1];
235 float kv = svi_e->
krow[j];
238 for (
size_t i = e->
krow.
size() - 1; i > 0; --i) e->
krow[i] = e->
krow[i - 1];
251 for (
size_t i = 0; i < n; i++)
266 fec = &calloc_or_throw<flat_example>();
271 if (fec->tag_len > 0)
273 fec->tag = calloc_or_throw<char>(fec->tag_len);
274 brw = model_file.
bin_read_fixed((
char*)fec->tag, fec->tag_len *
sizeof(
char),
"");
278 if (fec->fs.size() > 0)
281 size_t len = fs.
size();
282 fs.
values = v_init<feature_value>();
290 fs.
indicies = v_init<feature_index>();
307 if (fec->tag_len > 0)
309 brw = model_file.
bin_write_fixed((
char*)fec->tag, (uint32_t)fec->tag_len *
sizeof(
char));
312 std::cerr << fec->tag_len <<
" " << fec->tag << endl;
316 if (fec->fs.size() > 0)
323 (
char*)fec->fs.indicies.begin(), (uint32_t)fec->fs.indicies.size() *
sizeof(
feature_index));
342 std::stringstream msg;
355 svm_example* tmp = &calloc_or_throw<svm_example>();
369 model_file, (
char*)model->
alpha.
begin(), (uint32_t)model->
num_support *
sizeof(
float),
"", read, msg, text);
373 model_file, (
char*)model->
delta.
begin(), (uint32_t)model->
num_support *
sizeof(
float),
"", read, msg, text);
380 params.
all->
trace_message <<
"Not supporting readable model for kernel svm currently" << endl;
393 if (fs_2.indicies.size() == 0)
397 for (
size_t idx1 = 0, idx2 = 0; idx1 < fs_1.
size() && idx2 < fs_2.size(); idx1++)
399 uint64_t ec1pos = fs_1.
indicies[idx1];
400 uint64_t ec2pos = fs_2.indicies[idx2];
405 while (ec1pos > ec2pos && ++idx2 < fs_2.size()) ec2pos = fs_2.indicies[idx2];
407 if (ec1pos == ec2pos)
412 dotprod += fs_1.
values[idx1] * fs_2.values[idx2];
426 return pow(1 + dotprod, power);
441 return rbf_kernel(fec1, fec2, *((
float*)params));
453 for (
size_t i = 0; i < n; i++) dot_prod += v1[i] * v2[i];
460 for (
size_t i = 0; i < n; i++)
464 if (ec_arr[i]->krow.size() > 0)
476 svm_example* sec = &calloc_or_throw<svm_example>();
479 predict(params, &sec, &score, 1);
495 if ((tmp < model->support_vec[i]->ex.l.simple.weight && model->
delta[i] < 0) || (tmp > 0 && model->
delta[i] > 0))
496 subopt[i] = fabs(model->
delta[i]);
500 if (subopt[i] > max_val)
515 params.all->trace_message <<
"Internal error at " << __FILE__ <<
":" << __LINE__ << endl;
518 for (
size_t i = svi; i < model->
num_support - 1; ++i)
538 for (
size_t i = svi; i < rowsize - 1; i++) e->
krow[i] = e->
krow[i + 1];
561 bool overshoot =
false;
569 float alpha_old = model->
alpha[pos];
570 alphaKi -= model->
alpha[pos] * inprods[pos];
571 model->
alpha[pos] = 0.;
573 float proj = alphaKi * ld.
label;
574 float ai = (params.
lambda - proj) / inprods[pos];
584 float diff = ai - alpha_old;
586 if (fabs(diff) > 1.0e-06)
592 diff = (float)(diff > 0) - (diff < 0);
593 ai = alpha_old + diff;
602 if (fabs(ai) <= 1.0e-10)
605 model->
alpha[pos] = ai;
616 void add_size_t(
size_t& t1,
const size_t& t2) noexcept { t1 += t2; }
618 void add_double(
double& t1,
const double& t2) noexcept { t1 += t2; }
627 for (
size_t i = 0; i < params.
pool_pos; i++)
632 fec = &(params.
pool[i]->
ex);
634 delete params.
pool[i];
642 size_t prev_sum = 0, total_sum = 0;
643 for (
size_t i = 0; i < all.all_reduce->total; i++)
645 if (i <= (all.all_reduce->node - 1))
646 prev_sum += sizes[i];
647 total_sum += sizes[i];
653 queries = calloc_or_throw<char>(total_sum);
656 all_reduce<char, copy_char>(all, queries, total_sum);
660 b->
space.
end() = &queries[total_sum *
sizeof(char)];
665 for (
size_t i = 0; i < params.
pool_size; i++)
669 params.
pool[i] = &calloc_or_throw<svm_example>();
671 train_pool[i] =
true;
684 if (num_read == prev_sum)
686 if (num_read == prev_sum + sizes[all.all_reduce->node])
700 bool* train_pool = calloc_or_throw<bool>(params.
pool_size);
701 for (
size_t i = 0; i < params.
pool_size; i++) train_pool[i] =
false;
703 float* scores = calloc_or_throw<float>(params.
pool_pos);
711 std::multimap<double, size_t> scoremap;
712 for (
size_t i = 0; i < params.
pool_pos; i++)
713 scoremap.insert(std::pair<const double, const size_t>(fabs(scores[i]), i));
715 std::multimap<double, size_t>::iterator iter = scoremap.begin();
720 iter = scoremap.begin();
722 for (
size_t train_size = 1; iter != scoremap.end() && train_size <= params.
subsample; train_size++)
725 train_pool[iter->second] = 1;
731 for (
size_t i = 0; i < params.
pool_pos; i++)
733 float queryp = 2.0f /
750 for (
size_t i = 0; i < params.
pool_pos; i++)
752 delete params.
pool[i];
760 for (
size_t i = 0; i < params.
pool_pos; i++)
770 model_pos =
add(params, params.
pool[i]);
774 model_pos =
add(params, params.
pool[i]);
782 bool overshoot =
update(params, model_pos);
785 double* subopt = calloc_or_throw<double>(model->
num_support);
786 for (
size_t j = 0; j < params.
reprocess; j++)
797 if (subopt[max_pos] > 0)
799 if (!overshoot && max_pos == (
size_t)model_pos && max_pos > 0 && j == 0)
800 params.
all->
trace_message <<
"Shouldn't reprocess right after process!!!" << endl;
821 for (
size_t i = 0; i < params.
pool_pos; i++)
delete params.
pool[i];
838 svm_example* sec = &calloc_or_throw<svm_example>();
841 predict(params, &sec, &score, 1);
869 auto params = scoped_calloc_or_throw<svm_params>();
870 std::string kernel_type;
871 float bandwidth = 1.f;
877 new_options.
add(
make_option(
"ksvm", ksvm).keep().help(
"kernel svm"))
878 .
add(
make_option(
"reprocess", params->reprocess).default_value(1).help(
"number of reprocess steps for LASVM"))
879 .
add(
make_option(
"pool_greedy", params->active_pool_greedy).help(
"use greedy selection on mini pools"))
880 .
add(
make_option(
"para_active", params->para_active).help(
"do parallel active learning"))
881 .
add(
make_option(
"pool_size", params->pool_size).default_value(1).help(
"size of pools for active learning"))
884 .help(
"number of items to subsample from the pool"))
887 .default_value(
"linear")
888 .help(
"type of kernel (rbf or linear (default))"))
889 .
add(
make_option(
"bandwidth", bandwidth).keep().default_value(1.
f).help(
"bandwidth of rbf kernel"))
890 .
add(
make_option(
"degree", degree).keep().default_value(2).help(
"degree of poly kernel"));
899 float loss_parameter = 0.0;
903 params->model = &calloc_or_throw<svm_model>();
904 params->model->num_support = 0;
905 params->maxcache = 1024 * 1024 * 1024;
906 params->loss_sum = 0.;
914 params->active =
true;
916 params->active_c = 1.;
918 params->pool = calloc_or_throw<svm_example*>(params->pool_size);
919 params->pool_pos = 0;
921 if (!options.
was_supplied(
"subsample") && params->para_active)
922 params->subsample = (
size_t)ceil(params->pool_size / all.
all_reduce->
total);
925 if (params->lambda == 0.)
927 params->all->trace_message <<
"Lambda = " << params->lambda << endl;
928 params->all->trace_message <<
"Kernel = " << kernel_type << endl;
930 if (kernel_type.compare(
"rbf") == 0)
933 params->all->trace_message <<
"bandwidth = " << bandwidth << endl;
934 params->kernel_params = &calloc_or_throw<double>();
935 *((
float*)params->kernel_params) = bandwidth;
937 else if (kernel_type.compare(
"poly") == 0)
940 params->all->trace_message <<
"degree = " << degree << endl;
941 params->kernel_params = &calloc_or_throw<int>();
942 *((
int*)params->kernel_params) = degree;
947 params->all->weights.stride_shift(0);
float poly_kernel(const flat_example *fec1, const flat_example *fec2, int power)
void resize(size_t length)
LEARNER::base_learner * kernel_svm_setup(options_i &options, vw &all)
static size_t num_kernel_evals
std::shared_ptr< rand_state > _random_state
void save_load_svm_model(svm_params ¶ms, io_buf &model_file, bool read, bool text)
void init_svm_example(flat_example *fec)
v_array< feature_index > indicies
the core definition of a set of features.
float dense_dot(float *v1, v_array< float > v2, size_t n)
base_learner * make_base(learner< T, E > &base)
v_array< feature_value > values
virtual void add_and_parse(const option_group_definition &group)=0
void set_save_load(void(*sl)(T &, io_buf &, bool, bool))
void free_flatten_example(flat_example *fec)
void learn(svm_params ¶ms, single_learner &, example &ec)
void sync_queries(vw &all, svm_params ¶ms, bool *train_pool)
std::shared_ptr< rand_state > get_random_state()
static int trim_cache(svm_params ¶ms)
size_t bin_read_fixed(char *data, size_t len, const char *read_message)
void copy_char(char &c1, const char &c2) noexcept
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)
bool update(svm_params ¶ms, size_t pos)
void push_back(const T &new_ele)
int save_load_flat_example(io_buf &model_file, bool read, flat_example *&fec)
void add_size_t(size_t &t1, const size_t &t2) noexcept
static int make_hot_sv(svm_params ¶ms, size_t svi)
virtual bool was_supplied(const std::string &key)=0
void predict(svm_params ¶ms, svm_example **ec_arr, float *scores, size_t n)
float kernel_function(const flat_example *fec1, const flat_example *fec2, void *params, size_t kernel_type)
float linear_kernel(const flat_example *fec1, const flat_example *fec2)
size_t suboptimality(svm_model *model, double *subopt)
void free_svm_model(svm_model *model)
void save_load(svm_params ¶ms, io_buf &model_file, bool read, bool text)
v_array< svm_example * > support_vec
option_group_definition & add(T &&op)
int compute_kernels(svm_params ¶ms)
int add(svm_params ¶ms, svm_example *fec)
flat_example * flatten_sort_example(vw &all, example *ec)
void add_double(double &t1, const double &t2) noexcept
typed_option< T > make_option(std::string name, T &location)
size_t bin_write_fixed(const char *data, size_t len)
void train(svm_params ¶ms)
loss_function * getLossFunction(vw &all, std::string funcName, float function_parameter)
float rbf_kernel(const flat_example *fec1, const flat_example *fec2, float bandwidth)
size_t bin_text_read_write_fixed(io_buf &io, char *data, size_t len, const char *read_message, bool read, std::stringstream &msg, bool text)
static size_t num_cache_evals