blob: 582b8d5283a69006e1129a955f8764fc123c2a86 (
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
62
63
64
|
#include "simple_label.h"
#include <float.h>
#include "parser.h"
#include "vw.h"
using namespace LEARNER;
namespace ALINK {
const int autoconstant = 524267083;
struct autolink {
uint32_t d;
uint32_t stride;
};
template <bool is_learn>
void predict_or_learn(autolink* b, learner& base, example* ec)
{
base.predict(ec);
float base_pred = ec->final_prediction;
// 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) };
ec->atomics[autolink_namespace].push_back(f);
sum_sq += base_pred*base_pred;
base_pred *= ec->final_prediction;
}
ec->total_sum_feat_sq += sum_sq;
// apply predict or learn
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* setup(vw& all, std::vector<std::string>&opts, po::variables_map& vm, po::variables_map& vm_file)
{
autolink* data = (autolink*)calloc(1,sizeof(autolink));
data->d = (uint32_t)vm["autolink"].as<size_t>();
data->stride = all.reg.stride;
if (!vm_file.count("autolink"))
{
std::stringstream ss;
ss << " --autolink " << data->d << " ";
all.options_from_file.append(ss.str());
}
learner* ret = new learner(data, all.l);
ret->set_learn<autolink, predict_or_learn<true> >();
ret->set_predict<autolink, predict_or_learn<false> >();
return ret;
}
}
|