-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalexnet.cc
322 lines (243 loc) · 7.21 KB
/
alexnet.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
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
#include <math.h>
#include "singa/model/feed_forward_net.h"
#include "singa/model/optimizer.h"
#include "singa/model/metric.h"
#include "singa/utils/channel.h"
#include "singa/utils/string.h"
#include "singa/core/initialize_static_ctors.h" // added by inkoziev
using namespace singa;
// currently supports 'cudnn' and 'singacpp'
#ifdef USE_CUDNN
const std::string engine = "cudnn";
#else
const std::string engine = "singacpp";
#endif // USE_CUDNN
static LayerConf GenOutputDenseConf(string name, int num_output, float std, float wd)
{
LayerConf conf;
conf.set_name(name);
//conf.set_type("singacpp_dense");
conf.set_type("singacpp_dense");
DenseConf *dense = conf.mutable_dense_conf();
dense->set_num_output(num_output);
ParamSpec *wspec = conf.add_param();
wspec->set_name(name + "_weight");
wspec->set_decay_mult(wd);
auto wfill = wspec->mutable_filler();
wfill->set_type("Gaussian");
wfill->set_std(std);
ParamSpec *bspec = conf.add_param();
bspec->set_name(name + "_bias");
bspec->set_lr_mult(2);
bspec->set_decay_mult(0);
return conf;
}
static LayerConf GenHiddenDenseConf(string name, int num_output, float std, float wd)
{
LayerConf conf;
conf.set_name(name);
conf.set_type("singacpp_dense");
DenseConf *dense = conf.mutable_dense_conf();
dense->set_num_output(num_output);
ParamSpec *wspec = conf.add_param();
wspec->set_name(name + "_weight");
wspec->set_decay_mult(wd);
auto wfill = wspec->mutable_filler();
wfill->set_type("Gaussian");
wfill->set_std(std);
ParamSpec *bspec = conf.add_param();
bspec->set_name(name + "_bias");
bspec->set_lr_mult(2);
bspec->set_decay_mult(0);
return conf;
}
static LayerConf GenReLUConf(string name)
{
LayerConf conf;
conf.set_name(name);
conf.set_type(engine + "_relu");
return conf;
}
static LayerConf GenSigmoidConf(string name)
{
LayerConf conf;
conf.set_name(name);
conf.set_type(engine + "_sigmoid");
return conf;
}
static void split(const string &s, const char* delim, vector<string> & v)
{
v.clear();
// to avoid modifying original string
// first duplicate the original string and return a char pointer then free the memory
char * dup = strdup(s.c_str());
char * token = strtok(dup, delim);
while (token != NULL) {
v.push_back(string(token));
// the call is treated as a subsequent calls to strtok:
// the function continues from where it left in previous invocation
token = strtok(NULL, delim);
}
free(dup);
return;
}
static Tensor load_tensor_from_csv(const std::string & data_folder, const char * filename, singa::DataType data_type)
{
LOG(INFO) << "Loading dataset from " << filename;
size_t nb_row = 0;
size_t nb_col = 0;
vector<string> tx;
std::string line;
std::ifstream rdr(data_folder + filename);
int max_rows = 10000000;
while (std::getline(rdr, line))
{
nb_row++;
if (nb_col == 0)
{
split(line, "\t", tx);
nb_col = tx.size();
}
if (nb_row >= max_rows) break;
}
size_t nb_cells = nb_row*nb_col;
int idata = 0;
rdr = std::ifstream(data_folder + filename);
int row_count = 0;
if (data_type == kFloat32)
{
float * data = new float[nb_cells];
while (std::getline(rdr, line))
{
split(line, "\t", tx);
for (int icol = 0; icol < nb_col; ++icol)
{
data[idata++] = atof(tx[icol].c_str());
}
row_count++;
if (row_count >= max_rows) break;
}
Shape shape = Shape{ nb_row, nb_col };
Tensor t = Tensor(shape, data_type);
t.CopyDataFromHostPtr(data, nb_cells);
delete[] data;
return t;
}
else if (data_type == kInt)
{
int * data = new int[nb_cells];
while (std::getline(rdr, line))
{
split(line, "\t", tx);
for (int icol = 0; icol < nb_col; ++icol)
{
int y = (int)atof(tx[icol].c_str());
data[idata++] = y;
}
row_count++;
if (row_count >= max_rows) break;
}
Shape shape = Shape{ nb_row, nb_col };
Tensor t = Tensor(shape, data_type);
t.CopyDataFromHostPtr(data, nb_cells);
delete[] data;
return t;
}
}
static FeedForwardNet create_net(size_t input_size)
{
FeedForwardNet net;
Shape s{ input_size };
net.Add(GenHiddenDenseConf("dense1", input_size, 1.0, 1.0), &s);
net.Add(GenSigmoidConf("dense1_a"));
net.Add(GenOutputDenseConf("dense_output", 2, 1.0, 1.0));
net.Add(GenSigmoidConf("dense2_a"));
return net;
}
static FeedForwardNet create_net_relu(size_t input_size)
{
FeedForwardNet net;
Shape s{ input_size };
net.Add(GenHiddenDenseConf("relu1", input_size, 1.0, 1.0), &s);
net.Add(GenReLUConf("relu1_a"));
net.Add(GenHiddenDenseConf("relu2", input_size/2, 1.0, 1.0));
net.Add(GenReLUConf("relu2_a"));
net.Add(GenHiddenDenseConf("relu3", input_size/3, 1.0, 1.0));
net.Add(GenReLUConf("relu3_a"));
net.Add(GenOutputDenseConf("dense_output", 2, 1.0, 1.0));
net.Add(GenSigmoidConf("dense2_a"));
return net;
}
static void Train(int num_epoch, const std::string & data_dir)
{
Tensor train_x = load_tensor_from_csv(data_dir, "X_train.csv", kFloat32);
Tensor train_y = load_tensor_from_csv(data_dir, "y_train.csv", kInt);
Tensor val_x = load_tensor_from_csv(data_dir, "X_val.csv", kFloat32);
Tensor val_y = load_tensor_from_csv(data_dir, "y_val.csv", kInt);
size_t nsamples = train_x.shape(0);
CHECK_EQ(train_x.shape(0), train_y.shape(0));
CHECK_EQ(val_x.shape(0), val_y.shape(0));
LOG(INFO) << "Training samples = " << train_y.shape(0)
<< ", Test samples = " << val_y.shape(0);
auto net = create_net(train_x.shape(1));
//auto net = create_net_relu(train_x.shape(1));
SGD sgd;
OptimizerConf opt_conf;
opt_conf.set_momentum(0.9);
auto reg = opt_conf.mutable_regularizer();
reg->set_coefficient(0.0000);
sgd.Setup(opt_conf);
sgd.SetLearningRateGenerator([](int step) {
return 0.5 * exp(-step/10.0);
});
/*
AdaGrad sgd;
OptimizerConf opt_conf;
opt_conf.set_momentum(0.9);
//auto reg = opt_conf.mutable_regularizer();
//reg->set_coefficient(0.0000);
sgd.Setup(opt_conf);
sgd.SetLearningRateGenerator([](int step) {
return 0.1;
});
*/
SoftmaxCrossEntropy loss;
//MSE loss;
Accuracy acc;
net.Compile(true, &sgd, &loss, &acc);
#ifdef USE_CUDNN
auto dev = std::make_shared<CudaGPU>();
net.ToDevice(dev);
train_x.ToDevice(dev);
train_y.ToDevice(dev);
test_x.ToDevice(dev);
test_y.ToDevice(dev);
#endif // USE_CUDNN
LOG(INFO) << "Start training";
size_t batch_size = 128;
net.Train(batch_size, num_epoch, train_x, train_y, val_x, val_y);
LOG(INFO) << "End training";
LOG(INFO) << "Start evaluating";
Tensor holdout_x = load_tensor_from_csv(data_dir, "X_holdout.csv", kFloat32);
Tensor holdout_y = load_tensor_from_csv(data_dir, "y_holdout.csv", kInt);
std::pair<Tensor, Tensor> holdout_res = net.Evaluate(holdout_x, holdout_y, 256);
float h_loss = Sum(holdout_res.first) / holdout_y.Size();
float h_accuracy = Sum(holdout_res.second) / holdout_y.Size();
std::cout << "Holdout loss=" << h_loss << " accuracy=" << h_accuracy << std::endl;
return;
}
int main(int argc, char **argv)
{
singa::InitChannel(nullptr);
//singa::initialize_static_ctors();
std::vector<std::string> rl = singa::GetRegisteredLayers();
int pos = singa::ArgPos(argc, argv, "-epoch");
int nEpoch = 1;
if (pos != -1) nEpoch = atoi(argv[pos + 1]);
pos = singa::ArgPos(argc, argv, "-data");
int nb_epochs = 50;
std::string data_folder("e:/polygon/WordRepresentations/data/");
Train(nb_epochs, data_folder);
LOG(INFO) << "Alldone.";
return 0;
}