-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
marginal.cc
161 lines (146 loc) · 4.7 KB
/
marginal.cc
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<unordered_map>
#include "reductions.h"
using namespace std;
namespace MARGINAL {
typedef pair<double,double> marginal;
struct data
{
float initial_numerator;
float initial_denominator;
bool id_features[256];
feature temp[256];//temporary storage when reducing.
audit_strings_ptr asp[256];
unordered_map<uint64_t, marginal > marginals;
vw* all;
};
template <bool is_learn>
void predict_or_learn(data& sm, LEARNER::base_learner& base, example& ec)
{
uint64_t mask = sm.all->weights.mask();
for (example::iterator i = ec.begin(); i!= ec.end(); ++i)
{
namespace_index n = i.index();
if (sm.id_features[n])
{
features& f = *i;
if (f.size() != 2)
{
cout << "warning: id feature namespace has " << f.size() << " features. Should have a constant then the id" << endl;
continue;
}
features::iterator i = f.begin();
float first_value = i.value();
float second_value = (++i).value();
uint64_t second_index = i.index() & mask;
if (first_value != 1. || second_value != 1.)
{
cout << "warning: bad id features, must have value 1." << endl;
continue;
}
uint64_t key = second_index + ec.ft_offset;
if (sm.marginals.find(key) == sm.marginals.end())//need to initialize things.
sm.marginals.insert(make_pair(key,make_pair(sm.initial_numerator, sm.initial_denominator)));
f.begin().value() = (feature_value)(sm.marginals[key].first / sm.marginals[key].second);
sm.temp[n]={second_value,second_index};
if (!f.space_names.empty())
sm.asp[n]=f.space_names[1];
f.truncate_to(1);
}
}
if (is_learn)
base.learn(ec);
else
base.predict(ec);
for (example::iterator i = ec.begin(); i!= ec.end(); ++i)
{
namespace_index n = i.index();
if (sm.id_features[n])
{
features& f = *i;
if (f.size() != 1)
cout << "warning: id feature namespace has " << f.size() << " features. Should have a constant then the id" << endl;
else //do unsubstitution dance
{
f.begin().value() = 1.;
f.push_back(sm.temp[n].x, sm.temp[n].weight_index);
if (!f.space_names.empty())
f.space_names.push_back(sm.asp[n]);
if (is_learn)
{
uint64_t second_index = (++(f.begin())).index();
uint64_t key = second_index + ec.ft_offset;
sm.marginals[key].first += ec.l.simple.label * ec.weight;
sm.marginals[key].second += ec.weight;
}
}
}
}
}
void finish(data& sm) { sm.marginals.~unordered_map(); }
void save_load(data& sm, io_buf& io, bool read, bool text)
{
uint64_t stride_shift = sm.all->weights.stride_shift();
if (io.files.size() == 0)
return;
stringstream msg;
uint64_t total_size;
if (!read)
{
total_size = (uint64_t)sm.marginals.size();
msg << "marginals size = " << total_size << "\n";
}
bin_text_read_write_fixed_validated(io, (char*)&total_size, sizeof(total_size), "", read, msg, text);
auto iter = sm.marginals.begin();
for (size_t i = 0; i < total_size; ++i)
{
uint64_t index;
if (!read)
{
index = iter->first >> stride_shift;
msg << index << ":";
}
bin_text_read_write_fixed(io, (char*)&index, sizeof(index), "", read, msg, text);
double numerator;
if (!read)
{
numerator = iter->second.first;
msg << numerator << ":";
}
bin_text_read_write_fixed(io, (char*)&numerator, sizeof(numerator), "", read, msg, text);
double denominator;
if (!read)
{
denominator = iter->second.second;
msg << denominator << "\n";
}
bin_text_read_write_fixed(io, (char*)&denominator, sizeof(denominator), "", read, msg, text);
if (read)
sm.marginals.insert(make_pair(index << stride_shift,make_pair(numerator,denominator)));
else
++iter;
}
}
}
using namespace MARGINAL;
LEARNER::base_learner* marginal_setup(vw& all)
{ if (missing_option<string, true>(all, "marginal", "substitute marginal label estimates for ids"))
return nullptr;
new_options(all)
("initial_denominator", po::value<float>()->default_value(1.f), "initial default value")
("initial_numerator", po::value<float>()->default_value(0.5f), "initial default value");
add_options(all);
data& d = calloc_or_throw<data>();
d.initial_numerator = all.vm["initial_numerator"].as<float>();
d.initial_denominator = all.vm["initial_denominator"].as<float>();
d.all = &all;
string s = (string)all.vm["marginal"].as<string>();
for (size_t u = 0; u < 256; u++)
if (s.find((char)u) != string::npos)
d.id_features[u] = true;
new(&d.marginals)unordered_map<uint64_t,marginal>();
LEARNER::learner<data>& ret =
init_learner(&d, setup_base(all), predict_or_learn<true>, predict_or_learn<false>);
ret.set_finish(finish);
ret.set_save_load(save_load);
return make_base(ret);
}