-
Notifications
You must be signed in to change notification settings - Fork 23
/
lr_sgd.cpp
371 lines (332 loc) · 13 KB
/
lr_sgd.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// L1-regularized logistic regression implementation using stochastic gradient descent
// (c) Tim Nugent
#include <iostream>
#include <fstream>
#include <iomanip>
#include <random>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <sstream>
#include <numeric>
#include <algorithm>
#include <map>
using namespace std;
vector<string> split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
void usage(const char* prog){
cout << "Read training data then classify test data using logistic regression:\nUsage:\n" << prog << " [options] [training_data]" << endl << endl;
cout << "Options:" << endl;
cout << "-s <int> Shuffle dataset after each iteration. default 1" << endl;
cout << "-i <int> Maximum iterations. default 50000" << endl;
cout << "-e <float> Convergence rate. default 0.005" << endl;
cout << "-a <float> Learning rate. default 0.001" << endl;
cout << "-l <float> L1 regularization weight. default 0.0001" << endl;
cout << "-m <file> Read weights from file" << endl;
cout << "-o <file> Write weights to file" << endl;
cout << "-t <file> Test file to classify" << endl;
cout << "-p <file> Write predictions to file" << endl;
cout << "-r Randomise weights between -1 and 1, otherwise 0" << endl;
cout << "-v Verbose." << endl << endl;
}
double vecnorm(map<int,double>& w1, map<int,double>& w2){
double sum = 0.0;
for(auto it = w1.begin(); it != w1.end(); it++){
double minus = w1[it->first] - w2[it->first];
double r = minus * minus;
sum += r;
}
return sqrt(sum);
}
double l1norm(map<int,double>& weights){
double sum = 0.0;
for(auto it = weights.begin(); it != weights.end(); it++){
sum += fabs(it->second);
}
return sum;
}
double sigmoid(double x){
static double overflow = 20.0;
if (x > overflow) x = overflow;
if (x < -overflow) x = -overflow;
return 1.0/(1.0 + exp(-x));
}
double classify(map<int,double>& features, map<int,double>& weights){
double logit = 0.0;
for(auto it = features.begin(); it != features.end(); it++){
if(it->first != 0){
logit += it->second * weights[it->first];
}
}
return sigmoid(logit);
}
int main(int argc, const char* argv[]){
// Learning rate
double alpha = 0.001;
// L1 penalty weight
double l1 = 0.0001;
// Max iterations
unsigned int maxit = 50000;
// Shuffle data set
int shuf = 1;
// Convergence threshold
double eps = 0.005;
// Verbose
int verbose = 0;
// Randomise weights
int randw = 0;
// Read model file
string model_in = "";
// Write model file
string model_out = "";
// Test file
string test_file = "";
// Predictions file
string predict_file = "";
if(argc < 2){
usage(argv[0]);
return(1);
}else{
cout << "# called with: ";
for(int i = 0; i < argc; i++){
cout << argv[i] << " ";
if(string(argv[i]) == "-a" && i < argc-1){
alpha = atof(argv[i+1]);
}
if(string(argv[i]) == "-m" && i < argc-1){
model_in = string(argv[i+1]);
}
if(string(argv[i]) == "-o" && i < argc-1){
model_out = string(argv[i+1]);
}
if(string(argv[i]) == "-t" && i < argc-1){
test_file = string(argv[i+1]);
}
if(string(argv[i]) == "-p" && i < argc-1){
predict_file = string(argv[i+1]);
}
if(string(argv[i]) == "-s" && i < argc-1){
shuf = atoi(argv[i+1]);
}
if(string(argv[i]) == "-i" && i < argc-1){
maxit = atoi(argv[i+1]);
}
if(string(argv[i]) == "-e" && i < argc-1){
eps = atof(argv[i+1]);
}
if(string(argv[i]) == "-l" && i < argc-1){
l1 = atof(argv[i+1]);
}
if(string(argv[i]) == "-v"){
verbose = 1;
}
if(string(argv[i]) == "-r"){
randw = 1;
}
if(string(argv[i]) == "-h"){
usage(argv[0]);
return(1);
}
}
cout << endl;
}
if(!model_in.length()){
cout << "# learning rate: " << alpha << endl;
cout << "# convergence rate: " << eps << endl;
cout << "# l1 penalty weight: " << l1 << endl;
cout << "# max. iterations: " << maxit << endl;
cout << "# training data: " << argv[argc-1] << endl;
if(model_out.length()) cout << "# model output: " << model_out << endl;
}
if(model_in.length()) cout << "# model input: " << model_in << endl;
if(test_file.length()) cout << "# test data: " << test_file << endl;
if(predict_file.length()) cout << "# predictions: " << predict_file << endl;
vector<map<int,double> > data;
map<int,double> weights;
map<int,double> total_l1;
random_device rd;
mt19937 g(rd());
ifstream fin;
string line;
// Read weights from model file, if provided
if(model_in.length()){
fin.open(model_in.c_str());
while (getline(fin, line)){
if(line.length()){
if(line[0] != '#' && line[0] != ' '){
vector<string> tokens = split(line,' ');
if(tokens.size() == 2){
weights[atoi(tokens[0].c_str())] = atof(tokens[1].c_str());
}
}
}
}
if(!weights.size()){
cout << "# failed to read weights from file!" << endl;
fin.close();
exit(-1);
}fin.close();
}
// If no weights file provided, read training file and calculate weights
if(!weights.size()){
fin.open(argv[argc-1]);
while (getline(fin, line)){
if(line.length()){
if(line[0] != '#' && line[0] != ' '){
vector<string> tokens = split(line,' ');
map<int,double> example;
if(atoi(tokens[0].c_str()) == 1){
example[0] = 1;
}else{
example[0] = 0;
}
for(unsigned int i = 1; i < tokens.size(); i++){
//if(strstr (tokens[i],"#") == NULL){
vector<string> feat_val = split(tokens[i],':');
if(feat_val.size() == 2){
example[atoi(feat_val[0].c_str())] = atof(feat_val[1].c_str());
if(randw){
weights[atoi(feat_val[0].c_str())] = -1.0+2.0*(double)rd()/rd.max();
}else{
weights[atoi(feat_val[0].c_str())] = 0.0;
}
total_l1[atoi(feat_val[0].c_str())] = 0.0;
}
//}
}
data.push_back(example);
//if(verbose) cout << "read example " << data.size() << " - found " << example.size()-1 << " features." << endl;
}
}
}
fin.close();
cout << "# training examples: " << data.size() << endl;
cout << "# features: " << weights.size() << endl;
double mu = 0.0;
double norm = 1.0;
unsigned int n = 0;
vector<int> index(data.size());
iota(index.begin(),index.end(),0);
cout << "# stochastic gradient descent" << endl;
while(norm > eps){
map<int,double> old_weights(weights);
if(shuf) shuffle(index.begin(),index.end(),g);
for (unsigned int i = 0; i < data.size(); i++){
mu += (l1*alpha);
int label = data[index[i]][0];
double predicted = classify(data[index[i]],weights);
for(auto it = data[index[i]].begin(); it != data[index[i]].end(); it++){
if(it->first != 0){
weights[it->first] += alpha * (label - predicted) * it->second;
if(l1){
// Cumulative L1-regularization
// Tsuruoka, Y., Tsujii, J., and Ananiadou, S., 2009
// http://aclweb.org/anthology/P/P09/P09-1054.pdf
double z = weights[it->first];
if(weights[it->first] > 0.0){
weights[it->first] = max(0.0,(double)(weights[it->first] - (mu + total_l1[it->first])));
}else if(weights[it->first] < 0.0){
weights[it->first] = min(0.0,(double)(weights[it->first] + (mu - total_l1[it->first])));
}
total_l1[it->first] += (weights[it->first] - z);
}
}
}
}
norm = vecnorm(weights,old_weights);
if(n && n % 100 == 0){
double l1n = l1norm(weights);
printf("# convergence: %1.4f l1-norm: %1.4e iterations: %i\n",norm,l1n,n);
}
if(++n > maxit){
break;
}
}
unsigned int sparsity = 0;
for(auto it = weights.begin(); it != weights.end(); it++){
if(it->second != 0) sparsity++;
}
printf("# sparsity: %1.4f (%i/%i)\n",(double)sparsity/weights.size(),sparsity,(int)weights.size());
if(model_out.length()){
ofstream outfile;
outfile.open(model_out.c_str());
for(auto it = weights.begin(); it != weights.end(); it++){
outfile << it->first << " " << it->second << endl;
}
outfile.close();
cout << "# written weights to file " << model_out << endl;
}
}
// If a test file is provided, classify it using either weights from
// the provided weights file, or those just calculated from training
if(test_file.length()){
ofstream outfile;
if(predict_file.length()){
outfile.open(predict_file.c_str());
}
cout << "# classifying" << endl;
double tp = 0.0, fp = 0.0, tn = 0.0, fn = 0.0;
fin.open(test_file.c_str());
while (getline(fin, line)){
if(line.length()){
if(line[0] != '#' && line[0] != ' '){
vector<string> tokens = split(line,' ');
map<int,double> example;
int label = atoi(tokens[0].c_str());
for(unsigned int i = 1; i < tokens.size(); i++){
vector<string> feat_val = split(tokens[i],':');
example[atoi(feat_val[0].c_str())] = atof(feat_val[1].c_str());
}
double predicted = classify(example,weights);
if(verbose){
if(label > 0){
printf("label: +%i : prediction: %1.3f",label,predicted);
}else{
printf("label: %i : prediction: %1.3f",label,predicted);
}
}
if(predict_file.length()){
if(predicted >= 0.5){
outfile << "1" << endl;
}else{
outfile << "0" << endl;
}
}
if(((label == -1 || label == 0) && predicted < 0.5) || (label == 1 && predicted >= 0.5)){
if(label == 1){tp++;}else{tn++;}
if(verbose) cout << "\tcorrect" << endl;
}else{
if(label == 1){fn++;}else{fp++;}
if(verbose) cout << "\tincorrect" << endl;
}
}
}
}
fin.close();
printf ("# accuracy: %1.4f (%i/%i)\n",((tp+tn)/(tp+tn+fp+fn)),(int)(tp+tn),(int)(tp+tn+fp+fn));
printf ("# precision: %1.4f\n",tp/(tp+fp));
printf ("# recall: %1.4f\n",tp/(tp+fn));
printf ("# mcc: %1.4f\n",((tp*tn)-(fp*fn))/sqrt((tp+fp)*(tp+fn)*(tn+fp)*(tn+fn)));
printf ("# tp: %i\n",(int)tp);
printf ("# tn: %i\n",(int)tn);
printf ("# fp: %i\n",(int)fp);
printf ("# fn: %i\n",(int)fn);
if(predict_file.length()){
cout << "# written predictions to file " << predict_file << endl;
outfile.close();
}
}
return(0);
}