-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdef_data.hpp
163 lines (125 loc) · 3.55 KB
/
def_data.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
#pragma once
#include "utils.hpp"
#include <gsl/gsl_sf_gamma.h>
// a general data base class
class DEFData {
public:
// sp_mat || mat
virtual string get_data_type() = 0;
virtual shared_ptr<arma::sp_mat> get_sp_mat() {
throw runtime_error("get_sp_mat() not implemented in DEFData");
return NULL;
}
virtual shared_ptr<arma::mat> get_mat() {
throw runtime_error("get_mat() not implemented in DEFData");
return NULL;
}
// returns NULL by default, not NULL means there is a
// training/testing split
virtual shared_ptr<arma::mat> get_train_filter() {
return NULL;
}
virtual int n_examples() = 0;
virtual int n_dim_y() = 0;
virtual shared_ptr<DEFData> transpose() const = 0;
virtual void transform(std::function<double(double)> func) = 0;
};
// sparse-text | test-text
shared_ptr<DEFData> build_def_data(const string& data_type,
const pt::ptree& options,
const string& fname);
shared_ptr<arma::sp_mat> read_text_data(const string& fname, int max_examples);
class SparseTextData : public DEFData {
private:
pt::ptree options;
shared_ptr<arma::sp_mat> data;
SparseTextData() {}
public:
virtual string get_data_type() {
return "sp_mat";
}
virtual shared_ptr<arma::sp_mat> get_sp_mat() {
return data;
}
virtual int n_examples() {
return data->n_cols;
}
virtual int n_dim_y() {
return data->n_rows;
}
virtual shared_ptr<DEFData> transpose() const;
SparseTextData(const pt::ptree& options, const string& fname)
: options(options){
auto max_examples = options.get<int>("max_examples");
auto exp_fname = expand_environment_variables(fname);
data = read_text_data(exp_fname, max_examples);
}
// NOTE: this function is slow
shared_ptr<arma::sp_mat>
slice_data(const ExampleIds& example_ids) {
shared_ptr<arma::sp_mat> batch(new arma::sp_mat(data->n_rows,
example_ids.size()));
for(size_t i=0; i<example_ids.size(); ++i)
batch->col(i) = data->col(example_ids[i]);
return batch;
}
void transform(std::function<double(double)> func) {
for(auto it=data->begin(); it!=data->end(); ++it) {
*it = func(*it);
}
}
};
class MaskedTextData : public DEFData {
private:
pt::ptree options;
shared_ptr<arma::sp_mat> data;
shared_ptr<arma::mat> test_filter, train_filter;
MaskedTextData() {}
public:
virtual string get_data_type() {
return "sp_mat";
}
virtual shared_ptr<arma::sp_mat> get_sp_mat() {
return data;
}
virtual int n_examples() {
return data->n_cols;
}
virtual int n_dim_y() {
return data->n_rows;
}
virtual shared_ptr<DEFData> transpose() const;
virtual shared_ptr<arma::mat> get_train_filter() {
return train_filter;
}
MaskedTextData(const pt::ptree& options, const string& fname);
void transform(std::function<double(double)> func) {
for(auto it=data->begin(); it!=data->end(); ++it) {
*it = func(*it);
}
}
};
class DenseData : public DEFData {
private:
pt::ptree options;
shared_ptr<arma::mat> data;
DenseData() {}
public:
virtual string get_data_type() {
return "mat";
}
virtual shared_ptr<arma::mat> get_mat() {
return data;
}
virtual int n_examples() {
return data->n_cols;
}
virtual int n_dim_y() {
return data->n_rows;
}
virtual shared_ptr<DEFData> transpose() const;
DenseData(const pt::ptree& options, const string& fname);
void transform(std::function<double(double)> func) {
data->transform(func);
}
};