blob: 44fc05ec52a30c55add2068d8bf0577534bfeba0 (
plain)
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
|
#include "reductions.h"
#include "simple_label.h"
namespace ALINK {
const int autoconstant = 524267083;
struct autolink {
uint32_t d; // degree of the polynomial
uint32_t stride_shift;
};
template <bool is_learn>
void predict_or_learn(autolink& b, LEARNER::base_learner& base, example& ec)
{
base.predict(ec);
float base_pred = ec.pred.scalar;
// add features of label
ec.indices.push_back(autolink_namespace);
float sum_sq = 0;
for (size_t i = 0; i < b.d; i++)
if (base_pred != 0.)
{
feature f = { base_pred, (uint32_t) (autoconstant + (i << b.stride_shift)) };
ec.atomics[autolink_namespace].push_back(f);
sum_sq += base_pred*base_pred;
base_pred *= ec.pred.scalar;
}
ec.total_sum_feat_sq += sum_sq;
if (is_learn)
base.learn(ec);
else
base.predict(ec);
ec.atomics[autolink_namespace].erase();
ec.indices.pop();
ec.total_sum_feat_sq -= sum_sq;
}
LEARNER::base_learner* setup(vw& all, po::variables_map& vm)
{
po::options_description opts("Autolink options");
opts.add_options()
("autolink", po::value<size_t>(), "create link function with polynomial d");
vm = add_options(all,opts);
if(!vm.count("autolink"))
return NULL;
autolink& data = calloc_or_die<autolink>();
data.d = (uint32_t)vm["autolink"].as<size_t>();
data.stride_shift = all.reg.stride_shift;
*all.file_options << " --autolink " << data.d;
LEARNER::base_learner* base = setup_base(all,vm);
LEARNER::learner<autolink>& ret = init_learner(&data, base, predict_or_learn<true>,
predict_or_learn<false>);
return make_base(ret);
}
}
|