-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatypes.hpp
339 lines (251 loc) · 9.22 KB
/
datatypes.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
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
#ifndef mdi_datatypes_hpp
#define mdi_datatypes_hpp
#include <memory>
#include "cuda/cudasampler.hpp"
#include "shared.hpp"
#include "stats.hpp"
class sampler {
public:
virtual ~sampler() {};
// cluster allocations have been altered from i to changed[i], by
// default we assume that all parameters are Gibbs sampled during
// the next call to @sampleParams
virtual void swap(const std::vector<int> &changed) { };
virtual void sampleParams (const Eigen::Ref<const Eigen::VectorXi> &alloc) = 0;
// code for calculating the log-likelihoods of any given item being
// assigned to each cluster, given current cluster parameters. it's
// in a functor so that calculations can be reused appropriately
struct item {
virtual ~item() {}
virtual Eigen::VectorXf operator()(int i) const = 0;
};
virtual item* newItemSampler() const = 0;
virtual void sampleFeatureSelection(const Eigen::Ref<const Eigen::VectorXi> &alloc) { }
virtual Eigen::VectorXi featureState() const { return Eigen::VectorXi::Zero(0); }
#ifndef NOCUDA
/** sample the cluster parameters given the specified cluster
* allocations. note that GPU version of @alloc will be maintained
* outside this API
*/
virtual void cudaSampleParameters(const Eigen::Ref<const Eigen::VectorXi> &alloc) = 0;
/** accumulate the log-probability of cluster associations into
* cuda::datatype::myprobs(), @cudaSampleParameters will have been
* called prior to this
*/
virtual void cudaAccumAllocProbs() = 0;
#endif
};
class datatype {
public:
virtual ~datatype() {};
virtual std::vector<std::string> items() const = 0;
virtual std::vector<std::string> features() const = 0;
virtual sampler* newSampler(int nclus, class cuda::sampler *gpu) const = 0;
};
template<class T, T fn(const std::string&)>
class csvdatastore : public datatype {
protected:
// _rownames and _colnames must be before _data as they're referred to in its construction
std::vector<std::string> _rownames, _colnames;
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> _data;
public:
csvdatastore(const char *path)
: _data(loadNamedRectCsv(path, _colnames, _rownames, fn).transpose()) {}
const Eigen::MatrixXf & rawdata() const { return _data; }
std::vector<std::string> items() const { return _rownames; }
std::vector<std::string> features() const { return std::vector<std::string>(); }
};
class gaussianSampler : public sampler {
int _nclus;
const Eigen::MatrixXf _data;
// @_mu are the means, @_tau are the precisions (i.e. 1/sqrt(_tau) = sd)
Eigen::MatrixXf _mu, _tau;
Eigen::VectorXi _featurestate;
class Item : public item {
const gaussianSampler *s;
public:
Item(const gaussianSampler *s) : s(s) {}
Eigen::VectorXf operator()(int item) const;
};
Eigen::ArrayXf logprobItemClus(int item, int clus) const;
public:
gaussianSampler(class cuda::sampler *gpu, const Eigen::MatrixXf &data, int nclus);
int nitems() const { return _data.cols(); }
int nfeatures() const { return _data.rows(); }
int nclus() const { return _nclus; }
// not private for testing purposes
std::vector<runningstats<> > accumState(const Eigen::Ref<const Eigen::VectorXi> &alloc);
void debug_setMuTau(Eigen::MatrixXf mu, Eigen::MatrixXf tau) {
_mu = mu;
_tau = tau;
}
void sampleParams (const Eigen::Ref<const Eigen::VectorXi> &alloc);
Item* newItemSampler() const {
return new Item(this);
}
void sampleFeatureSelection(const Eigen::Ref<const Eigen::VectorXi> &alloc);
Eigen::VectorXi featureState() const { return _featurestate; }
#ifndef NOCUDA
private:
cuda::gaussian _gpu;
public:
void cudaSampleParameters(const Eigen::Ref<const Eigen::VectorXi> &alloc) { _gpu.sampleParameters(); }
void cudaAccumAllocProbs() { _gpu.accumAllocProbs(); }
#endif
};
class gaussianDatatype : public csvdatastore<float, stof_strict> {
public:
gaussianDatatype(const char *path) : csvdatastore(path) {}
std::vector<std::string> features() const { return _colnames; }
gaussianSampler* newSampler(int nclus, class cuda::sampler *gpu) const {
return new gaussianSampler(gpu, _data, nclus);
}
};
class bagofwordsSampler : public sampler {
int _nclus;
const Eigen::MatrixXi &_data;
Eigen::ArrayXXf _lpar;
class Item : public item {
const bagofwordsSampler *s;
public:
Item(const bagofwordsSampler *s) : s(s) {}
Eigen::VectorXf operator()(int item) const;
};
public:
bagofwordsSampler(class cuda::sampler *gpu, const Eigen::MatrixXi &data, int nclus);
int nitems() const { return _data.cols(); }
int nfeatures() const { return _data.rows(); }
int nclus() const { return _nclus; }
void sampleParams (const Eigen::Ref<const Eigen::VectorXi> &alloc);
Item* newItemSampler() const {
return new Item(this);
}
#ifndef NOCUDA
private:
cuda::bagofwords _gpu;
public:
void cudaSampleParameters(const Eigen::Ref<const Eigen::VectorXi> &alloc) { _gpu.sampleParameters(); }
void cudaAccumAllocProbs() { _gpu.accumAllocProbs(); }
#endif
};
class bagofwordsDatatype : public csvdatastore<int, stoi_strict> {
public:
bagofwordsDatatype(const char *path) : csvdatastore(path) {}
bagofwordsSampler* newSampler(int nclus, class cuda::sampler *gpu) const {
return new bagofwordsSampler(gpu, _data, nclus);
}
};
class multinomSampler : public sampler {
int _nclus;
const Eigen::MatrixXi &_data;
std::vector<Eigen::ArrayXXf> _lpar;
class Item : public item {
const multinomSampler *s;
public:
Item(const multinomSampler *s) : s(s) {}
Eigen::VectorXf operator()(int item) const;
};
public:
multinomSampler(class cuda::sampler *gpu, const Eigen::MatrixXi &data,
const std::vector<int> &levels, int nclus);
int nitems() const { return _data.cols(); }
int nfeatures() const { return _data.rows(); }
int nclus() const { return _nclus; }
void sampleParams (const Eigen::Ref<const Eigen::VectorXi> &alloc);
Item* newItemSampler() const {
return new Item(this);
}
#ifndef NOCUDA
private:
cuda::multinomial _gpu;
public:
void cudaSampleParameters(const Eigen::Ref<const Eigen::VectorXi> &alloc) { _gpu.sampleParameters(); }
void cudaAccumAllocProbs() { _gpu.accumAllocProbs(); }
#endif
};
class multinomDatatype : public datatype {
// _rownames and _colnames must be before _data as they're referred to in its construction
std::vector<std::string> _rownames, _colnames;
std::vector<int> _levels;
Eigen::MatrixXi _data;
public:
multinomDatatype(const char *path);
std::vector<std::string> items() const { return _rownames; }
std::vector<std::string> features() const { return _colnames; }
multinomSampler* newSampler(int nclus, class cuda::sampler *gpu) const {
return new multinomSampler(gpu, _data, _levels, nclus);
}
};
/* We refer to @sf, @sn and @l throughout as the hyperparameters of a GP.
* These are the standard deviation of the function, the additive noise and
* characteristic length scale of a squared exponential covariance function.
*
* Often we have @pf, @pn, @pl, which refers to the "precision" of the
* function, noise and length scales (i.e. @pf = 1/@sf^2)
*/
class gpHypers {
gamma_distribution<double> _pf, _pn, _pl;
public:
gpHypers () : _pf(2.0, 0.5), _pn(2.0, 0.1), _pl(2.0, 0.5) { }
gamma_distribution<double> pf() const { return _pf; }
gamma_distribution<double> pn() const { return _pn; }
gamma_distribution<double> pl() const { return _pl; }
};
class gp {
double _pf, _pn, _pl;
public:
gp(double pf, double pn, double pl) : _pf(pf), _pn(pn), _pl(pl) {}
gp(const gpHypers &prior) { sampleFromPrior(prior); }
double pf() const { return _pf; }
double pn() const { return _pn; }
double pl() const { return _pl; }
void pf(double x) { _pf = x; }
void pn(double x) { _pn = x; }
void pl(double x) { _pl = x; }
Eigen::MatrixXd covarianceMatrix (const Eigen::VectorXd &time) const;
void sampleFromPrior (const gpHypers &prior);
Eigen::VectorXd sampleFunction (const Eigen::VectorXd &time);
};
class gpSampler : public sampler {
int _nclus;
const Eigen::MatrixXf &_data;
const Eigen::VectorXd &_time;
std::vector<gp> _gp;
gpHypers _prior;
// our sampled functions
Eigen::MatrixXf _sampfn;
class Item : public item {
const gpSampler *s;
Eigen::ArrayXf prob0, prob1;
public:
Item(const gpSampler *s);
Eigen::VectorXf operator()(int item) const;
};
public:
gpSampler(class cuda::sampler *gpu, const Eigen::MatrixXf &data,
const Eigen::VectorXd &time, int nclus);
int nitems() const { return _data.cols(); }
int nfeatures() const { return _data.rows(); }
int nclus() const { return _nclus; }
void swap(const std::vector<int> &changed);
void sampleParams (const Eigen::Ref<const Eigen::VectorXi> &alloc);
Item* newItemSampler() const {
return new Item(this);
}
#ifndef NOCUDA
private:
cuda::gaussianprocess _gpu;
public:
void cudaSampleParameters(const Eigen::Ref<const Eigen::VectorXi> &alloc);
void cudaAccumAllocProbs() { _gpu.accumAllocProbs(); }
#endif
};
class gpDatatype : public csvdatastore<float, stof_strict> {
Eigen::VectorXd _time;
public:
gpDatatype(const char *path);
gpSampler* newSampler(int nclus, class cuda::sampler *gpu) const {
return new gpSampler(gpu, _data, _time, nclus);
}
};
#endif // #ifndef mdi_datatypes_hpp