forked from liuzhiqiangruc/dml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregress.c
320 lines (310 loc) · 8.36 KB
/
regress.c
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
/* ========================================================
* Copyright (C) 2016 All rights reserved.
*
* filename : regress.c
* author : [email protected]
* date : 2016-01-08
* info : regression framework implementation
* ======================================================== */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "str.h"
#include "hash.h"
#include "newton.h"
#include "regress.h"
#define REG_LINE_LEN 0x100000 // 1MByte
static int load_train_ds(REG * reg, Hash * hs){
FILE * fp = NULL;
if (NULL == (fp = fopen(reg->p.in_file, "r"))){
fprintf(stderr, "can not open file \"%s\"\n", reg->p.in_file);
return -1;
}
reg->train_ds = (RDS*)malloc(sizeof(RDS));
if (!reg->train_ds){
goto train_ds_failed;
}
memset(reg->train_ds, 0, sizeof(RDS));
char buffer[REG_LINE_LEN] = {'\0'};
int col_cnt = 0, tf_cnt = 0, row = 0;
char *token = NULL, *string = buffer;
while (NULL != fgets(buffer, REG_LINE_LEN, fp)){
string = trim(buffer, 3);
token = strsep(&string, "\t");
token = strsep(&string, "\t");
tf_cnt += atoi(token);
while (NULL != (token = strsep(&string, "\t"))){
hash_add(hs, token);
if (reg->p.binary == 0){
strsep(&string, "\t");
}
}
row += 1;
}
col_cnt = hash_cnt(hs);
reg->c = col_cnt;
reg->id_map = (char(*)[RKL])malloc(sizeof(char[RKL]) * col_cnt);
if (!reg->id_map){
goto id_map_failed;
}
memset(reg->id_map, 0, sizeof(char[RKL]) * col_cnt);
reg->x = (double*)malloc(sizeof(double) * reg->c);
if (!reg->x){
goto x_failed;
}
memset(reg->x, 0, sizeof(double) * reg->c);
reg->train_ds->r = row;
reg->train_ds->y = (double*)malloc(sizeof(double) * row);
if (!reg->train_ds->y){
goto train_y_failed;
}
reg->train_ds->l = (int*)malloc(sizeof(int) * row);
if (!reg->train_ds->l){
goto train_l_failed;
}
reg->train_ds->ids = (int*)malloc(sizeof(int) * tf_cnt);
if (!reg->train_ds->ids){
goto train_ids_failed;
}
if (0 == reg->p.binary){
reg->train_ds->val = (double*)malloc(sizeof(double) * tf_cnt);
if(!reg->train_ds->val){
goto train_val_failed;
}
}
rewind(fp);
tf_cnt = row = 0;
while (NULL != fgets(buffer, REG_LINE_LEN, fp)){
string = trim(buffer, 3);
token = strsep(&string, "\t");
reg->train_ds->y[row] = atof(token);
token = strsep(&string, "\t");
reg->train_ds->l[row] = atoi(token);
while (NULL != (token = strsep(&string, "\t"))){
int id = hash_find(hs, token);
reg->train_ds->ids[tf_cnt] = id;
strncpy(reg->id_map[id], token, RKL - 1);
if (reg->p.binary == 0){
token = strsep(&string, "\t");
reg->train_ds->val[tf_cnt] = atof(token);
}
tf_cnt += 1;
}
row += 1;
}
fclose(fp);
return 0;
train_val_failed:
free(reg->train_ds->ids);
train_ids_failed:
free(reg->train_ds->l);
train_l_failed:
free(reg->train_ds->y);
train_y_failed:
free(reg->x);
x_failed:
free(reg->id_map);
id_map_failed:
free(reg->train_ds);
train_ds_failed:
return -1;
}
static int load_test_ds(REG * reg, Hash * hs){
FILE * fp = NULL;
if (NULL == (fp = fopen(reg->p.te_file, "r"))){
return -1;
}
char buffer[REG_LINE_LEN] = {'\0'};
char *string = NULL, *token = NULL;
int tf_cnt = 0, row = 0;
while (NULL != fgets(buffer, REG_LINE_LEN, fp)){
string = trim(buffer, 3);
token = strsep(&string, "\t");
token = strsep(&string, "\t");
int tlen = 0;
while (NULL != (token = strsep(&string, "\t"))){
if (-1 != hash_find(hs, token)){
tlen += 1;
}
if (0 == reg->p.binary){
strsep(&string, "\t");
}
}
if (tlen > 0){
tf_cnt += tlen;
row += 1;
}
}
if (row == 0){
reg->test_ds = NULL;
goto empty_test;
}
reg->test_ds = (RDS*)malloc(sizeof(RDS));
if (!reg->test_ds){
goto ds_failed;
}
memset(reg->test_ds, 0, sizeof(RDS));
reg->test_ds->r = row;
reg->test_ds->y = (double*)malloc(sizeof(double) * row);
if (!reg->test_ds->y){
goto test_y_failed;
}
memset(reg->test_ds->y, 0, sizeof(double) * row);
reg->test_ds->l = (int*)malloc(sizeof(int) * row);
if (!reg->test_ds->l){
goto test_l_failed;
}
memset(reg->test_ds->l, 0, sizeof(int) * row);
reg->test_ds->ids = (int*)malloc(sizeof(int) * tf_cnt);
if (!reg->test_ds->ids){
goto test_ids_failed;
}
memset(reg->test_ds->ids, 0, sizeof(int) * tf_cnt);
if (0 == reg->p.binary){
reg->test_ds->val = (double *)malloc(sizeof(double) * tf_cnt);
if (!reg->test_ds->val){
goto test_val_failed;
}
memset(reg->test_ds->val, 0, sizeof(double) * tf_cnt);
}
rewind(fp);
tf_cnt = row = 0;
while (NULL != fgets(buffer, REG_LINE_LEN, fp)){
string = trim(buffer, 3);
token = strsep(&string, "\t");
reg->test_ds->y[row] = atof(token);
token = strsep(&string, "\t");
int tlen = 0;
int id = -1;
while (NULL != (token = strsep(&string, "\t"))){
if (-1 != (id = hash_find(hs, token))){
tlen += 1;
reg->test_ds->ids[tf_cnt] = id;
}
if (0 == reg->p.binary){
token = strsep(&string, "\t");
if (id != -1){
reg->test_ds->val[tf_cnt] = atof(token);
}
}
if (id != -1){
tf_cnt += 1;
}
}
if (tlen > 0){
reg->test_ds->l[row] = tlen;
row += 1;
}
}
fclose(fp);
empty_test:
return 0;
test_val_failed:
free(reg->test_ds->ids);
test_ids_failed:
free(reg->test_ds->l);
test_l_failed:
free(reg->test_ds->y);
test_y_failed:
free(reg->test_ds);
ds_failed:
return -1;
}
static void free_ds(RDS * ds){
if (ds){
if (ds->l){
free(ds->l);
ds->l = NULL;
}
if (ds->y){
free(ds->y);
ds->y = NULL;
}
if (ds->ids){
free(ds->ids);
ds->ids = NULL;
}
if (ds->val){
free(ds->val);
ds->val = NULL;
}
free(ds);
}
}
REG * create_model(EVAL_FN eval_fn, GRAD_FN grad_fn, REPO_FN repo_fn){
if (!eval_fn || !grad_fn){
return NULL;
}
REG * reg = (REG*)malloc(sizeof(REG));
if (!reg){
return NULL;
}
memset(reg, 0, sizeof(REG));
reg->eval_fn = eval_fn;
reg->grad_fn = grad_fn;
reg->repo_fn = repo_fn;
return reg;
}
int init_model(REG * reg){
Hash * hs = hash_create(1<<20, STRING);
if (0 != load_train_ds(reg, hs)){
hash_free(hs); hs = NULL;
return -1;
}
if (NULL != reg->p.te_file){
load_test_ds(reg, hs);
}
hash_free(hs); hs = NULL;
return 0;
}
int learn_model(REG * reg){
NT_METHOD method;
if (reg->p.method == 1){
method = OWLQN;
}
else if(reg->p.method == 2) {
method = LBFGS;
}
else{
return -1;
}
newton(reg->eval_fn, reg->grad_fn, reg->repo_fn, method, reg, 5, reg->p.niters, reg->c, reg->x);
return 0;
}
void save_model(REG * reg, int n){
FILE * fp = NULL;
char out_file[512] = {'\0'};
if (n < reg->p.niters){
sprintf(out_file, "%s/%d_coe", reg->p.out_dir, n);
}
else{
sprintf(out_file, "%s/%s_coe", reg->p.out_dir, "f");
}
if (NULL == (fp = fopen(out_file, "w"))){
fprintf(stderr, "can not open file \"%s\"\n", out_file);
return;
}
for (int i = 0; i < reg->c; i++){
fprintf(fp, "%s\t%.10f\n", reg->id_map[i], reg->x[i]);
}
fclose(fp);
}
void free_model(REG * reg){
if (reg->train_ds){
free_ds(reg->train_ds);
reg->train_ds = NULL;
}
if (reg->test_ds){
free(reg->test_ds);
reg->test_ds = NULL;
}
if (reg->x){
free(reg->x);
reg->x = NULL;
}
if (reg->id_map){
free(reg->id_map);
reg->id_map = NULL;
}
free(reg);
}