forked from blei-lab/deep-exponential-families
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdef_layer.hpp
278 lines (241 loc) · 7.93 KB
/
def_layer.hpp
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
#ifndef DEF_LAYER_HPP
#define DEF_LAYER_HPP
#include <gsl/gsl_rng.h>
#include "bbvi.hpp"
#include "optimizer.hpp"
#include "optimizer.hpp"
#include "serialization.hpp"
#include "def_data.hpp"
// stuff that can't be put to the property tree
// but useful for initialization
struct DEFInitializer {
gsl_rng* rng;
shared_ptr<DEFData> def_data;
DEFInitializer() {
}
private:
// make the copy constructor and the = private to prevent serialization
DEFInitializer(const DEFInitializer& other) {
}
DEFInitializer& operator=(DEFInitializer& other) {
return *this;
}
};
class DEFLayer {
public:
virtual double compute_log_p(double z, double param) = 0;
// TODO: Clean up these similar functions
shared_ptr<arma::mat> log_p_matrix(shared_ptr<arma::mat> w,
shared_ptr<arma::mat> z,
shared_ptr<arma::mat> z_higher,
shared_ptr<arma::mat> z_bias) {
arma::mat param = (*w) * (*z_higher);
if (z_bias != NULL) {
assert(z_bias->n_cols == 1);
param.each_col() += z_bias->col(0);
}
shared_ptr<arma::mat> log_p( new arma::mat(z->n_rows, z->n_cols) );
for(arma::uword j=0; j<z->n_cols; ++j) {
for(arma::uword i=0; i<z->n_rows; ++i) {
// TODO inline?
(*log_p)(i, j) = compute_log_p((*z)(i, j), param(i, j));
}
}
return log_p;
}
};
class DEFPriorLayer {
public:
virtual double compute_log_p(double z) = 0;
shared_ptr<arma::mat> log_p_matrix(shared_ptr<arma::mat> z) {
shared_ptr<arma::mat> log_p( new arma::mat(z->n_rows, z->n_cols) );
for(arma::uword j=0; j<z->n_cols; ++j) {
for(arma::uword i=0; i<z->n_rows; ++i) {
(*log_p)(i, j) = compute_log_p((*z)(i, j));
}
}
return log_p;
}
};
class InferenceFactorizedLayer {
public:
typedef function<double(double z, arma::uword i, arma::uword j)> ScoreFunction;
private:
vector<ScoreFunction> score_funcs;
vector<Serializable<arma::mat>* > param_matrices;
vector<Optimizer> optimizers;
protected:
int threads;
const pt::ptree options;
arma::uword n_examples;
ExampleIds all_examples;
public:
template<class Archive>
void serialize(Archive& ar, const unsigned int) {
ar & (*const_cast<pt::ptree*>(&options));
init();
ar & optimizers;
ar & param_matrices;
}
InferenceFactorizedLayer() {
}
InferenceFactorizedLayer(const pt::ptree& options)
: options( options ) {
init();
}
void init() {
n_examples = options.get<int>("n_examples");
all_examples.clear();
for(arma::uword j=0; j<n_examples; ++j)
all_examples.push_back(j);
threads = options.get<int>("threads");
}
virtual ~InferenceFactorizedLayer() {}
virtual double compute_log_q(double z, arma::uword i, arma::uword j) = 0;
virtual double sample(gsl_rng* rng, arma::uword i, arma::uword j) = 0;
virtual double mean(arma::uword i, arma::uword j) = 0;
virtual void copy_params(InferenceFactorizedLayer* other) = 0;
// truncate the parameters
virtual void truncate(const ExampleIds& example_ids) = 0;
virtual void truncate() = 0;
void register_param(Serializable<arma::mat>* param_mat, ScoreFunction score_func, bool deserialize) {
if (!deserialize) {
assert(param_mat->n_cols == n_examples);
optimizers.emplace_back(options, param_mat);
param_matrices.push_back(param_mat);
}
score_funcs.push_back(score_func);
}
// z: layer-size x num-examples
shared_ptr<arma::cube> grad_lq_matrix(shared_ptr<arma::mat> z,
const ExampleIds& example_ids) {
size_t n_params = param_matrices.size();
shared_ptr<arma::cube> grad_lp( new arma::cube(z->n_rows, z->n_cols, n_params) );
arma::uword ind = 0;
for(auto j : example_ids) {
for(arma::uword i=0; i<z->n_rows; ++i) {
for(size_t k=0; k<score_funcs.size(); ++k) {
(*grad_lp)(i, ind, k) = score_funcs[k]((*z)(i,ind), i, j);
}
}
++ind;
}
return grad_lp;
}
shared_ptr<arma::cube> grad_lq_matrix(shared_ptr<arma::mat> z) {
return grad_lq_matrix(z, all_examples);
}
shared_ptr<arma::mat> log_q_matrix(shared_ptr<arma::mat> z,
const ExampleIds& example_ids) {
shared_ptr<arma::mat> log_q( new arma::mat(z->n_rows, z->n_cols) );
arma::uword ind = 0;
for(auto j : example_ids) {
for(arma::uword i=0; i<z->n_rows; ++i) {
// TODO inline?
(*log_q)(i, ind) = compute_log_q((*z)(i, ind), i, j);
}
++ind;
}
return log_q;
}
shared_ptr<arma::mat> log_q_matrix(shared_ptr<arma::mat> z) {
return log_q_matrix(z, all_examples);
}
shared_ptr<arma::mat> sample_matrix(gsl_rng* rng, const ExampleIds& example_ids) {
auto n_rows = param_matrices[0]->n_rows;
shared_ptr<arma::mat> sample_mat( new arma::mat(n_rows, example_ids.size()) );
arma::uword ind = 0;
for(auto j : example_ids) {
for(arma::uword i=0; i<n_rows; ++i) {
// TODO inline?
(*sample_mat)(i, ind) = sample(rng, i, j);
}
++ind;
}
return sample_mat;
}
shared_ptr<arma::mat> sample_matrix(gsl_rng* rng) {
return sample_matrix(rng, all_examples);
}
shared_ptr<arma::mat> mean_matrix(const ExampleIds& example_ids) {
auto n_rows = param_matrices[0]->n_rows;
auto n_cols = example_ids.size();
shared_ptr<arma::mat> mean_mat( new arma::mat(n_rows, n_cols) );
arma::uword ind = 0;
for(auto j : example_ids) {
for(arma::uword i=0; i<n_rows; ++i) {
// TODO inline?
(*mean_mat)(i, ind) = mean(i, j);
}
++ind;
}
return mean_mat;
}
shared_ptr<arma::mat> mean_matrix() {
return mean_matrix(all_examples);
}
BBVIStats update(const VecOfCube& score_q,
const VecOfMat& log_p,
const VecOfMat& log_q,
const ExampleIds& example_ids) {
BBVIStats stats;
auto n_params = param_matrices.size();
for(arma::uword k=0; k<n_params; ++k) {
// This is inefficent just pass and index to bbvi
VecOfMat score_k;
for(auto c : score_q)
score_k.emplace_back( new arma::mat(c->slice(k)) );
BBVIStats stats_k;
auto grad_k = grad_bbvi_factorized(options, score_k, log_p, log_q, stats_k, threads);
stats += stats_k;
optimizers[k].update(*grad_k, example_ids);
}
stats /= (n_params+0.0);
return stats;
}
BBVIStats update(const VecOfCube& score_q,
const VecOfMat& log_p,
const VecOfMat& log_q) {
return update(score_q, log_p, log_q, all_examples);
}
// save params in binary format, save at least max_examples
// columns for each parameter matrix
void save_params(FILE* ofile, int max_examples) {
if (max_examples > (int) n_examples)
max_examples = n_examples;
//cout << " max_examples" << max_examples << endl;
for(auto w : param_matrices) {
arma::mat sub_w = w->cols(0, max_examples-1);
LOG(trace) << "save one param matrix";
save_mat(ofile, sub_w);
}
}
void save_params(FILE* ofile) {
save_params(ofile, n_examples);
}
// load params
void load_params(FILE* ifile, int max_examples) {
if ((max_examples < 0) || (max_examples > (int) n_examples))
max_examples = n_examples;
for(auto w : param_matrices) {
// we only load the whole matrics here
LOG(trace) << "load params " << "max_examples=" << max_examples
<< " cols(w)=" << w->n_cols;
assert(max_examples == (int) w->n_cols);
LOG(trace) << "load one param matrix";
load_mat(ifile, *w);
}
}
void load_params(FILE* ifile) {
load_params(ifile, n_examples);
}
// check the parameter matrix
void check_params(int m=4) {
for (size_t k=0; k<param_matrices.size(); ++k) {
LOG(debug) << "param matrix " << k << "\n"
<< param_matrices[k]->submat(0, 0, m-1, m-1);
}
}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(InferenceFactorizedLayer)
#endif