forked from poweic/libdnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnn-train.cpp
199 lines (148 loc) · 5.96 KB
/
dnn-train.cpp
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
#include <iostream>
#include <string>
#include <dnn.h>
#include <dnn-utility.h>
#include <cmdparser.h>
#include <rbm.h>
#include <batch.h>
using namespace std;
size_t dnn_predict(const DNN& dnn, DataSet& data, ERROR_MEASURE errorMeasure);
void dnn_train(DNN& dnn, DataSet& train, DataSet& valid, size_t batchSize, ERROR_MEASURE errorMeasure);
bool isEoutStopDecrease(const std::vector<size_t> Eout, size_t epoch, size_t nNonIncEpoch);
int main (int argc, char* argv[]) {
CmdParser cmd(argc, argv);
cmd.add("training_set_file")
.add("model_in")
.add("model_out", false);
cmd.addGroup("Feature options:")
.add("--input-dim", "specify the input dimension (dimension of feature).\n"
"0 for auto detection.")
.add("--normalize", "Feature normalization: \n"
"0 -- Do not normalize.\n"
"1 -- Rescale each dimension to [0, 1] respectively.\n"
"2 -- Normalize to standard score. z = (x-u)/sigma .", "0")
.add("--nf", "Load pre-computed statistics from file", "")
.add("--base", "Label id starts from 0 or 1 ?", "0");
cmd.addGroup("Training options: ")
.add("-v", "ratio of training set to validation set (split automatically)", "5")
.add("--max-epoch", "number of maximum epochs", "100000")
.add("--min-acc", "Specify the minimum cross-validation accuracy", "0.5")
.add("--learning-rate", "learning rate in back-propagation", "0.1")
.add("--variance", "the variance of normal distribution when initializing the weights", "0.01")
.add("--batch-size", "number of data per mini-batch", "32")
.add("--type", "choose one of the following:\n"
"0 -- classfication\n"
"1 -- regression", "0");
cmd.addGroup("Hardward options:")
.add("--cache", "specify cache size (in MB) in GPU used by cuda matrix.", "16");
cmd.addGroup("Example usage: dnn-train data/train3.dat --nodes=16-8");
if (!cmd.isOptionLegal())
cmd.showUsageAndExit();
string train_fn = cmd[1];
string model_in = cmd[2];
string model_out = cmd[3];
size_t input_dim = cmd["--input-dim"];
NormType n_type = (NormType) (int) cmd["--normalize"];
string n_filename = cmd["--nf"];
int base = cmd["--base"];
int ratio = cmd["-v"];
size_t batchSize = cmd["--batch-size"];
float learningRate = cmd["--learning-rate"];
float variance = cmd["--variance"];
float minValidAcc = cmd["--min-acc"];
size_t maxEpoch = cmd["--max-epoch"];
size_t cache_size = cmd["--cache"];
CudaMemManager<float>::setCacheSize(cache_size);
// Set configurations
Config config;
config.variance = variance;
config.learningRate = learningRate;
config.minValidAccuracy = minValidAcc;
config.maxEpoch = maxEpoch;
// Load model
DNN dnn(model_in);
dnn.setConfig(config);
// Load data
DataSet data(train_fn, input_dim, base);
// data.loadPrecomputedStatistics(n_filename);
data.setNormType(n_type);
data.showSummary();
DataSet train, valid;
DataSet::split(data, train, valid, ratio);
config.print();
// Start Training
ERROR_MEASURE err = CROSS_ENTROPY;
dnn_train(dnn, train, valid, batchSize, err);
// Save the model
if (model_out.empty())
model_out = train_fn.substr(train_fn.find_last_of('/') + 1) + ".model";
dnn.save(model_out);
return 0;
}
void dnn_train(DNN& dnn, DataSet& train, DataSet& valid, size_t batchSize, ERROR_MEASURE errorMeasure) {
printf("Training...\n");
perf::Timer timer;
timer.start();
vector<mat> O(dnn.getNLayer());
size_t Ein = 1;
size_t MAX_EPOCH = dnn.getConfig().maxEpoch, epoch;
std::vector<size_t> Eout;
float lr = dnn.getConfig().learningRate / batchSize;
size_t nTrain = train.size(),
nValid = valid.size();
mat fout;
printf("._______._________________________._________________________.\n"
"| | | |\n"
"| | In-Sample | Out-of-Sample |\n"
"| Epoch |__________.______________|__________.______________|\n"
"| | | | | |\n"
"| | Accuracy | # of correct | Accuracy | # of correct |\n"
"|_______|__________|______________|__________|______________|\n");
for (epoch=0; epoch<MAX_EPOCH; ++epoch) {
Batches batches(batchSize, nTrain);
for (Batches::iterator itr = batches.begin(); itr != batches.end(); ++itr) {
// Copy a batch of data from host to device
auto data = train[itr];
dnn.feedForward(fout, data.x);
mat error = getError( data.y, fout, errorMeasure);
dnn.backPropagate(error, data.x, fout, lr);
}
Ein = dnn_predict(dnn, train, errorMeasure);
Eout.push_back(dnn_predict(dnn, valid, errorMeasure));
float trainAcc = 1.0f - (float) Ein / nTrain;
if (trainAcc < 0) {
cout << "."; cout.flush();
continue;
}
float validAcc = 1.0f - (float) Eout[epoch] / nValid;
printf("|%4lu | %.2f %% | %7lu | %.2f %% | %7lu |\n",
epoch, trainAcc * 100, nTrain - Ein, validAcc * 100, nValid - Eout[epoch]);
if (validAcc > dnn.getConfig().minValidAccuracy && isEoutStopDecrease(Eout, epoch, dnn.getConfig().nNonIncEpoch))
break;
dnn.adjustLearningRate(trainAcc);
}
// Show Summary
printf("\n%ld epochs in total\n", epoch);
timer.elapsed();
printf("[ In-Sample ] ");
showAccuracy(Ein, train.size());
printf("[ Out-of-Sample ] ");
showAccuracy(Eout.back(), valid.size());
}
size_t dnn_predict(const DNN& dnn, DataSet& data, ERROR_MEASURE errorMeasure) {
size_t nError = 0;
Batches batches(2048, data.size());
for (Batches::iterator itr = batches.begin(); itr != batches.end(); ++itr) {
auto d = data[itr];
mat prob = dnn.feedForward(d.x);
nError += zeroOneError(prob, d.y, errorMeasure);
}
return nError;
}
bool isEoutStopDecrease(const std::vector<size_t> Eout, size_t epoch, size_t nNonIncEpoch) {
for (size_t i=0; i<nNonIncEpoch; ++i) {
if (epoch - i > 0 && Eout[epoch] > Eout[epoch - i])
return false;
}
return true;
}