Repository navigation
Expand file tree
/
Copy pathfactor_graph.cpp
More file actions
368 lines (323 loc) · 13.5 KB
/
Copy pathfactor_graph.cpp
File metadata and controls
368 lines (323 loc) · 13.5 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <iostream>
#include "io/binary_parser.h"
#include "dstruct/factor_graph/factor_graph.h"
#include "dstruct/factor_graph/factor.h"
#include <fstream>
// 64-bit big endian to little endian
# define bswap_64(x) \
((((x) & 0xff00000000000000ull) >> 56) \
| (((x) & 0x00ff000000000000ull) >> 40) \
| (((x) & 0x0000ff0000000000ull) >> 24) \
| (((x) & 0x000000ff00000000ull) >> 8) \
| (((x) & 0x00000000ff000000ull) << 8) \
| (((x) & 0x0000000000ff0000ull) << 24) \
| (((x) & 0x000000000000ff00ull) << 40) \
| (((x) & 0x00000000000000ffull) << 56))
bool dd::FactorGraph::is_usable(){
return this->sorted && this->safety_check_passed;
}
dd::FactorGraph::FactorGraph(long _n_var, long _n_factor, long _n_weight, long _n_edge) :
n_var(_n_var), n_factor(_n_factor), n_weight(_n_weight), n_edge(_n_edge),
c_nvar(0), c_nfactor(0), c_nweight(0), n_evid(0), n_query(0),
variables(new Variable[_n_var]),
factors(new Factor[_n_factor]),
weights(new Weight[_n_weight]),
compact_factors(new CompactFactor[_n_edge]),
compact_factors_weightids(new int[_n_edge]),
factor_ids(new long[_n_edge]),
vifs(new VariableInFactor[_n_edge]),
infrs(new InferenceResult(_n_var, _n_weight)),
sorted(false),
safety_check_passed(false),
old_weight_values(NULL) {}
void dd::FactorGraph::copy_from(const FactorGraph * const p_other_fg){
// copy each member from the given graph
memcpy(variables, p_other_fg->variables, sizeof(Variable)*n_var);
memcpy(factors, p_other_fg->factors, sizeof(Factor)*n_factor);
memcpy(weights, p_other_fg->weights, sizeof(Weight)*n_weight);
memcpy(factor_ids, p_other_fg->factor_ids, sizeof(long)*n_edge);
memcpy(vifs, p_other_fg->vifs, sizeof(VariableInFactor)*n_edge);
memcpy(compact_factors, p_other_fg->compact_factors, sizeof(CompactFactor)*n_edge);
memcpy(compact_factors_weightids, p_other_fg->compact_factors_weightids, sizeof(int)*n_edge);
if(p_other_fg->old_weight_values != NULL){
if(old_weight_values == NULL){
old_weight_values = new float[n_weight];
}
memcpy(old_weight_values, p_other_fg->old_weight_values, sizeof(float)*n_weight);
}
c_nvar = p_other_fg->c_nvar;
c_nfactor = p_other_fg->c_nfactor;
c_nweight = p_other_fg->c_nweight;
c_edge = p_other_fg->c_edge;
sorted = p_other_fg->sorted;
safety_check_passed = p_other_fg->safety_check_passed;
infrs->init(variables, weights);
infrs->ntallies = p_other_fg->infrs->ntallies;
infrs->multinomial_tallies = new int[p_other_fg->infrs->ntallies];
for(long i=0;i<infrs->ntallies;i++){
infrs->multinomial_tallies[i] = p_other_fg->infrs->multinomial_tallies[i];
}
}
long dd::FactorGraph::get_multinomial_weight_id(const VariableValue *assignments, const CompactFactor& fs, long vid, long proposal) {
/**
* The weight ids are aligned in a continuous region according
* to the numerical order of variable values.
* Say for two variables v1, v2, v3, with cardinality d. The numerical value is
* v1 * d^2 + v2 * d + v3.
*/
long weight_offset = 0;
// for each variable in the factor
for (long i = fs.n_start_i_vif; i < fs.n_start_i_vif + fs.n_variables; i++) {
const VariableInFactor & vif = vifs[i];
if (vif.vid == vid) {
weight_offset = weight_offset * (variables[vif.vid].upper_bound+1) + proposal;
} else {
weight_offset = weight_offset * (variables[vif.vid].upper_bound+1) + assignments[vif.vid];
}
}
long base_offset = &fs - compact_factors; // note c++ will auto scale by sizeof(CompactFactor)
return *(compact_factors_weightids + base_offset) + weight_offset;
}
void dd::FactorGraph::update_weight(const Variable & variable){
// corresponding factors and weights in a continous region
CompactFactor * const fs = compact_factors + variable.n_start_i_factors;
const int * const ws = compact_factors_weightids + variable.n_start_i_factors;
// for each factor
for(long i=0;i<variable.n_factors;i++){
// boolean variable
if (variable.domain_type == DTYPE_BOOLEAN) {
// only update weight when it is not fixed
if(infrs->weights_isfixed[ws[i]] == false){
// stochastic gradient ascent
// increment weight with stepsize * gradient of weight
// gradient of weight = E[f|D] - E[f], where D is evidence variables,
// f is the factor function, E[] is expectation. Expectation is calculated
// using a sample of the variable.
infrs->weight_values[ws[i]] +=
stepsize * (this->template potential<false>(fs[i]) - this->template potential<true>(fs[i]));
}
} else if (variable.domain_type == DTYPE_MULTINOMIAL) {
// two weights need to be updated
// sample with evidence fixed, I0, with corresponding weight w1
// sample without evidence unfixed, I1, with corresponding weight w2
// gradient of wd0 = f(I0) - I(w1==w2)f(I1)
// gradient of wd1 = I(w1==w2)f(I0) - f(I1)
long wid1 = get_multinomial_weight_id(infrs->assignments_evid, fs[i], -1, -1);
long wid2 = get_multinomial_weight_id(infrs->assignments_free, fs[i], -1, -1);
int equal = (wid1 == wid2);
if(infrs->weights_isfixed[wid1] == false){
infrs->weight_values[wid1] +=
stepsize * (this->template potential<false>(fs[i]) - equal * this->template potential<true>(fs[i]));
}
if(infrs->weights_isfixed[wid2] == false){
infrs->weight_values[wid2] +=
stepsize * (equal * this->template potential<false>(fs[i]) - this->template potential<true>(fs[i]));
}
}
}
}
void dd::FactorGraph::load(const CmdParser & cmd, const bool is_quiet, int inc){
// get factor graph file names from command line arguments
std::string filename_edges;
std::string filename_factors;
std::string filename_variables;
std::string filename_weights;
if (inc) {
filename_weights = cmd.original_folder->getValue() + "/graph.weights";
filename_variables = cmd.original_folder->getValue() + "/graph.variables";
filename_factors = cmd.original_folder->getValue() + "/graph.factors";
filename_edges = cmd.original_folder->getValue() + "/graph.edges";
} else {
filename_weights = cmd.weight_file->getValue();
filename_variables = cmd.variable_file->getValue();
filename_factors = cmd.factor_file->getValue();
filename_edges = cmd.edge_file->getValue();
}
// load variables
long long n_loaded = read_variables(filename_variables, *this);
if(cmd.delta_folder->getValue() != ""){
std::cout << "Loading delta..." << std::endl;
std::cout << cmd.delta_folder->getValue() + "/graph.variables" << std::endl;
n_loaded += read_variables(cmd.delta_folder->getValue() + "/graph.variables", *this);
}
assert(n_loaded == n_var);
if (!is_quiet) {
std::cout << "LOADED VARIABLES: #" << n_loaded << std::endl;
std::cout << " N_QUERY: #" << n_query << std::endl;
std::cout << " N_EVID : #" << n_evid << std::endl;
}
// load factors
n_loaded = read_factors(filename_factors, *this);
if(cmd.delta_folder->getValue() != ""){
std::cout << "Loading delta..." << cmd.delta_folder->getValue() + "/graph.factors" << std::endl;
n_loaded += read_factors(cmd.delta_folder->getValue() + "/graph.factors", *this);
}
assert(n_loaded == n_factor);
if (!is_quiet) {
std::cout << "LOADED FACTORS: #" << n_loaded << std::endl;
}
// load weights
n_loaded = read_weights(filename_weights, *this);
if(cmd.delta_folder->getValue() != ""){
std::cout << "Loading delta..." << std::endl;
n_loaded += read_weights(cmd.delta_folder->getValue() + "/graph.weights", *this);
}
assert(n_loaded == n_weight);
if (!is_quiet) {
std::cout << "LOADED WEIGHTS: #" << n_loaded << std::endl;
}
// sort the above components
// NOTE This is very important, as read_edges assume variables,
// factors and weights are ordered so that their id is the index
// where they are stored in the array
this->sort_by_id();
// load edges
n_loaded = read_edges(filename_edges, *this);
if(cmd.delta_folder->getValue() != ""){
std::cout << "Loading delta..." << std::endl;
n_loaded += read_edges(cmd.delta_folder->getValue() + "/graph.edges", *this);
}
if (!is_quiet) {
std::cout << "LOADED EDGES: #" << n_loaded << std::endl;
}
// load active variables
std::string active_vars = cmd.original_folder->getValue() + "/active.variables";
long long active_id;
std::ifstream file;
file.open(active_vars.c_str(), ios::in | ios::binary);
while (file.good()) {
if(!file.read((char *)&active_id, 8)) break;
active_id = bswap_64(active_id);
this->variables[active_id].isactive = true;
}
// a slow, but good enough algorithm for connected components
std::string useful_training = cmd.original_folder->getValue() + "/mat_components_hasevids";
std::ofstream fout2(useful_training.c_str());
long long component_id = -1;
for(long long vid=0;vid < n_var;vid++){
const Variable & var = this->variables[vid];
if(var.component_id != -1) continue;
component_id ++;
bool isuseful_for_training = false;
std::vector<long long> vars_to_work_on;
vars_to_work_on.push_back(vid);
long long var_to_work_on;
while(vars_to_work_on.size() != 0){
var_to_work_on = vars_to_work_on.back();
vars_to_work_on.pop_back();
this->variables[var_to_work_on].component_id = component_id;
if(this->variables[var_to_work_on].is_evid){
isuseful_for_training = true;
}
for(long long fid : this->variables[var_to_work_on].tmp_factor_ids){
const Factor & factor = this->factors[fid];
for(const VariableInFactor & next_var : factor.tmp_variables){
if(this->variables[next_var.vid].component_id != -1){
assert(this->variables[next_var.vid].component_id == component_id);
}else{
this->variables[next_var.vid].component_id = component_id;
vars_to_work_on.push_back(next_var.vid);
}
}
}
}
fout2 << component_id << " " << isuseful_for_training << std::endl;
}
std::string component_file = cmd.original_folder->getValue() + "/mat_active_components";
std::ofstream fout(component_file.c_str());
for(long long vid=0;vid < n_var;vid++){
const Variable & var = this->variables[vid];
assert(var.component_id != -1);
if(var.isactive){
fout << var.id << " " << var.component_id << std::endl;
}
}
// construct edge-based store
this->organize_graph_by_edge();
this->safety_check();
assert(this->is_usable() == true);
if(inc == 2){
std::cout << "LOADING PREVIOUS WEIGHT..." << std::endl;
this->old_weight_values = new float[n_weight];
std::ifstream fin(cmd.original_folder->getValue() + "/inference_result.out.weights.text");
long long wid;
float weight;
while(fin >> wid >> weight){
this->weights[wid].weight = weight;
this->old_weight_values[wid] = weight;
}
fin.close();
}
}
// sort according to id
template <class OBJTOSORT>
class idsorter : public std::binary_function<OBJTOSORT, OBJTOSORT, bool>{
public:
inline bool operator()(const OBJTOSORT & left, const OBJTOSORT & right){
return left.id < right.id;
}
};
void dd::FactorGraph::sort_by_id() {
// sort variables, factors, and weights by id
std::sort(&variables[0], &variables[n_var], idsorter<Variable>());
std::sort(&factors[0], &factors[n_factor], idsorter<Factor>());
std::sort(&weights[0], &weights[n_weight], idsorter<Weight>());
this->sorted = true;
infrs->init(variables, weights);
}
bool dd::compare_position(const VariableInFactor& x, const VariableInFactor& y) {
return x.n_position < y.n_position;
}
void dd::FactorGraph::organize_graph_by_edge() {
// number of edges
c_edge = 0;
// put variables into the edge-based variable array vifs
for(long i=0;i<n_factor;i++){
Factor & factor = factors[i];
factor.n_start_i_vif = c_edge;
// sort variables in factor by position in factor
std::sort(factor.tmp_variables.begin(), factor.tmp_variables.end(), dd::compare_position);
for(const VariableInFactor & vif : factor.tmp_variables){
vifs[c_edge] = vif;
c_edge ++;
}
}
c_edge = 0;
long ntallies = 0;
// for each variable, put the factors into factor_dups
for(long i=0;i<n_var;i++){
Variable & variable = variables[i];
variable.n_factors = variable.tmp_factor_ids.size(); // no edge count any more
variable.n_start_i_factors = c_edge;
if(variable.domain_type == DTYPE_MULTINOMIAL){
variable.n_start_i_tally = ntallies;
ntallies += variable.upper_bound - variable.lower_bound + 1;
}
for(const long & fid : variable.tmp_factor_ids){
factor_ids[c_edge] = fid;
compact_factors[c_edge].id = factors[fid].id;
compact_factors[c_edge].func_id = factors[fid].func_id;
compact_factors[c_edge].n_variables = factors[fid].n_variables;
compact_factors[c_edge].n_start_i_vif = factors[fid].n_start_i_vif;
compact_factors_weightids[c_edge] = factors[fid].weight_id;
c_edge ++;
}
}
}
void dd::FactorGraph::safety_check(){
// check whether variables, factors, and weights are stored
// in the order of their id
long s = n_var;
for(long i=0;i<s;i++){
assert(this->variables[i].id == i);
}
s = n_factor;
for(long i=0;i<s;i++){
assert(this->factors[i].id == i);
}
s = n_weight;
for(long i=0;i<s;i++){
assert(this->weights[i].id == i);
}
this->safety_check_passed = true;
}