forked from postgres/postgres
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnodeLimit.c
2853 lines (2254 loc) · 76 KB
/
nodeLimit.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* nodeLimit.c
* Routines to handle limiting of query results where appropriate
*
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/executor/nodeLimit.c
*
*-------------------------------------------------------------------------
*/
/*
* INTERFACE ROUTINES
* ExecLimit - extract a limited range of tuples
* ExecInitLimit - initialize node and subnodes..
* ExecEndLimit - shutdown node and subnodes
*/
#include "postgres.h"
#include "executor/execdebug.h"
#include "executor/executor.h"
#include "executor/nodeLimit.h"
#include "nodes/nodeFuncs.h"
#include "utils/guc.h"
#include "access/tuptoaster.h"
#include "utils/array.h"
#include <sys/time.h>
#include "time.h"
#include "math.h"
char timeBuf[256];
// guc variables
// can be set via "SET VAR = XX" in the psql console
int set_batch_size = DEFAULT_BATCH_SIZE;
int set_iter_num = DEFAULT_ITER_NUM;
double set_learning_rate = DEFAULT_LEARNING_RATE;
double set_decay = DEFAULT_DECAY;
double set_mu = DEFAULT_MU;
char* set_model_name = DEFAULT_MODEL_NAME;
int table_page_number = 0;
int set_class_num = DEFAULT_CLASS_NUM;
// char* table_name = "dflife";
// char* set_table_name = "forest";
char* set_table_name = DEFAULT_TABLE_NAME;
bool set_run_test = false;
int set_block_shuffle = DEFAULT_BLOCK_SHUFFLE;
int set_tuple_shuffle = DEFAULT_TUPLE_SHUFFLE;
bool is_training = true;
bool set_use_malloc = DEFAULT_USE_MALLOC;
// int set_use_test_buffer_num = DEFAULT_USE_TEST_BUFFER_NUM;
SGDTupleDesc* sgd_tupledesc; // also used in nodeSort.c for parsing tuple_slot to double* features
static Model* init_model(int n_features, int max_sparse_count);
static void ExecFreeModel(Model* model);
// static SGDBatchState* init_SGDBatchState(int n_features);
// static SGDTuple* init_SGDTuple(int n_features);
static SortTuple* init_SortTuple(int n_features);
static SGDTupleDesc* init_SGDTupleDesc(int n_features, bool dense, int max_sparse_count);
// static void clear_SGDBatchState(SGDBatchState* batchstate, int n_features);
// static void free_SGDBatchState(SGDBatchState* batchstate);
//static void free_SGDTuple(SGDTuple* sgd_tuple);
static void free_SortTuple(SortTuple* sort_tuple);
static void free_SGDTupleDesc(SGDTupleDesc* sgd_tupledesc);
static char* get_current_time();
// static void compute_tuple_gradient_loss_LR(SGDTuple* tp, Model* model, SGDBatchState* batchstate);
// static void compute_tuple_gradient_loss_SVM(SGDTuple* tp, Model* model, SGDBatchState* batchstate);
// static void compute_tuple_accuracy(Model* model, SGDTuple* tp, TestState* test_state);
// static void update_model(Model* model, SGDBatchState* batchstate);
// static void perform_SGD(Model *model, SGDTuple* sgd_tuple, SGDBatchState* batchstate, int i);
// static void transfer_slot_to_sgd_tuple(TupleTableSlot* slot, SGDTuple* sgd_tuple, SGDTupleDesc* sgd_tupledesc);
//static void fast_transfer_slot_to_sgd_tuple(TupleTableSlot* slot, SGDTuple* sgd_tuple, SGDTupleDesc* sgd_tupledesc);
// static void transfer_slot_to_sgd_tuple_getattr(TupleTableSlot* slot, SGDTuple* sgd_tuple, SGDTupleDesc* sgd_tupledesc);
// static int my_parse_array_no_copy(struct varlena* input, int typesize, char** output);
// static void compute_tuple_loss_LR(SortTuple* tp, Model* model, SGDBatchState* batchstate);
// static void compute_tuple_gradient_LR(SortTuple* tp, Model* model, SGDBatchState* batchstate);
// for selecting the gradient and loss computation function
void (*compute_tuple_gradient) (SortTuple *stup, Model* model) = NULL;
void (*compute_tuple_loss) (SortTuple *stup, Model* model) = NULL;
static char* get_current_time() {
time_t t = time(0);
strftime(timeBuf, 256, "%Y-%m-%d %H:%M:%S", localtime(&t)); //format date and time.
return timeBuf;
}
double diff_timeofday_seconds(struct timeval *start, struct timeval *end) {
double time_use = (end->tv_sec - start->tv_sec) * 1000 + (end->tv_usec - start->tv_usec) / 1000;//微秒
return time_use / 1000; //seconds
}
// static char* get_current_time() {
// time_t t;
// time(&t);
// ctime_r(&t, timeBuf);
// return timeBuf;
// }
// from bismarck
inline double
dot(const double* x, const double* y, const int size) {
double ret = 0.0;
int i;
for(i = size - 1; i >= 0; i--) {
ret += x[i]*y[i];
}
return ret;
}
// for softmax regression
inline double
softmax_dot(const double* w, const int j, const double* x, const int n, const int K) {
double ret = 0.0;
int i;
for(i = 0; i < n; i++) {
double wji = w[K * i + j];
ret += wji * x[i];
}
return ret;
}
// from bismarck
inline double
dot_dss(const double* x, const int* k, const double* v, const int sparseSize) {
double ret = 0.0;
int i;
for(i = sparseSize - 1; i >= 0; i--) {
ret += x[k[i]]*v[i];
}
return ret;
}
// from bismarck
inline void
add_and_scale(double* x, const int size, const double* y, const double c) {
int i;
for(i = size - 1; i >= 0; i--) {
x[i] += y[i]*c;
}
}
// for softmax regression
inline void
softmax_add_and_scale(double* w, const int j, const int n, const double* x, const double c, const int K) {
int i;
for(i = 0; i < n; i++) {
// wjx += x[i] * c
int index = K * i + j;
w[index] += x[i] * c;
}
}
inline void
batch_softmax_add_and_scale(double* w, const int n, const double* batch_w, const double c, const int K) {
int i;
for(i = 0; i < n * K; i++) {
w[i] += batch_w[i] * c;
}
}
inline void
add_c_dss(double* x, const int* k, const int sparseSize, const double c) {
int i;
for(i = sparseSize - 1; i >= 0; i--) {
x[k[i]] += c;
}
}
// from bismarck
inline void
add_and_scale_dss(double* x, const int* k, const double* v, const int sparseSize, const double c) {
int i;
for(i = sparseSize - 1; i >= 0; i--) {
x[k[i]] += v[i]*c;
}
}
// from bismarck
inline void
scale_i(double* x, const int size, const double c) {
int i;
for(i = size - 1; i >= 0; i --) {
x[i] *= c;
}
}
// from bismarck
inline double
norm(const double *x, const int size) {
double norm = 0;
int i;
for(i = size - 1; i >= 0; i --) {
norm += x[i] * x[i];
}
return norm;
}
// from bismarck
inline double
sigma(const double v) {
if (v > 30) { return 1.0 / (1.0 + exp(-v)); }
else { return exp(v) / (1.0 + exp(v)); }
}
// from bismarck
inline void
l1_shrink_mask(double* x, const double u, const int* k, const int sparseSize) {
int i;
for(i = sparseSize-1; i >= 0; i--) {
if (x[k[i]] > u) { x[k[i]] -= u; }
else if (x[k[i]] < -u) { x[k[i]] += u; }
else { x[k[i]] = 0; }
}
}
inline void
l2_shrink_mask_d(double* x, const double u, const int size) {
int i;
for(i = size-1; i >= 0; i--) {
if (x[i] == 0.0) { continue; }
x[i] /= 1 + u;
}
}
// from bismarck
inline void
l1_shrink_mask_d(double* x, const double u, const int size) {
int i;
double xi = 0.0;
for(i = size-1; i >= 0; i--) {
xi = x[i];
if (xi > u) { x[i] -= u; }
else if (xi < -u) { x[i] += u; }
else { x[i] = 0.0; }
}
}
// from bismarck
inline double
log_sum(const double a, const double b) {
return a + log(1.0 + exp(b - a));
}
inline void
my_sparse_add_and_scale_dss(double* w, double* current_batch_gradient, const double c, Model *model) {
// e.g., model->feature_k_non_zeros = [1, 2, 3; 2, 3, 4; 1, 4, 6], model->feature_k_index = 9
int i;
for (i = 0; i < model->feature_k_index; i++) {
int index = model->feature_k_non_zeros[i];
w[index] += current_batch_gradient[index] * c;
current_batch_gradient[index] = 0;
model->feature_k_non_zeros[i] = 0;
}
model->feature_k_index = 0;
}
Model* init_model(int n_features, int max_sparse_count) {
Model* model = (Model *) palloc(sizeof(Model));
model->model_name = set_model_name;
model->total_loss = 0;
model->batch_size = set_batch_size;
model->iter_num = set_iter_num;
model->learning_rate = set_learning_rate;
model->tuple_num = 0;
model->n_features = n_features;
model->decay = set_decay;
model->mu = set_mu;
model->class_num = set_class_num;
model->accuracy = 0;
model->current_batch_num = 0;
// use memorycontext later
if (model->class_num > 2) {
model->w = (double *) palloc0(sizeof(double) * n_features * model->class_num);
model->current_batch_gradient = (double *) palloc0(sizeof(double) * n_features * model->class_num);
}
else {
model->w = (double *) palloc0(sizeof(double) * n_features);
model->current_batch_gradient = (double *) palloc0(sizeof(double) * n_features);
}
//memset(model->w, 0, sizeof(double) * n_features);
// for mini-batch on sparse data
// model->w_old = (double *) palloc0(sizeof(double) * n_features);
model->feature_k_non_zeros = (int *)palloc0(sizeof(int) * max_sparse_count * set_batch_size);
model->feature_k_index = 0;
return model;
}
void ExecFreeModel(Model* model) {
// free(model->gradient);
pfree(model->w);
pfree(model->current_batch_gradient);
//pfree(model->w_old);
pfree(model->feature_k_non_zeros);
pfree(model);
}
/*
static SGDBatchState* init_SGDBatchState(int n_features) {
SGDBatchState* batchstate = (SGDBatchState *) palloc(sizeof(SGDBatchState));
batchstate->gradients = (double *) palloc0(sizeof(double) * n_features);
// int i;
// for (i = 0; i < n_features; i++)
// batchstate->gradients[i] = 0;
batchstate->loss = 0;
batchstate->tuple_num = 0;
return batchstate;
}
static TestState* init_TestState(bool run_test) {
TestState* test_state = NULL;
if (run_test) {
test_state = (TestState *) palloc0(sizeof(TestState));
test_state->test_total_loss = 0;
test_state->test_accuracy = 0;
test_state->right_count = 0;
}
return test_state;
}
static SGDTuple* init_SGDTuple(int n_features) {
SGDTuple* sgd_tuple = (SGDTuple *) palloc(sizeof(SGDTuple));
//sgd_tuple->features = (double *) palloc0(sizeof(double) * n_features);
return sgd_tuple;
}
*/
static SortTuple* init_SortTuple(int n_features) {
SortTuple* sgd_tuple = (SortTuple *) palloc(sizeof(SortTuple));
//sgd_tuple->features = (double *) palloc0(sizeof(double) * n_features);
return sgd_tuple;
}
static void free_SortTuple(SortTuple* sort_tuple) {
pfree(sort_tuple);
}
static SGDTupleDesc* init_SGDTupleDesc(int n_features, bool dense, int max_sparse_count) {
SGDTupleDesc* sgd_tupledesc = (SGDTupleDesc *) palloc(sizeof(SGDTupleDesc));
// sgd_tupledesc->values = (Datum *) palloc0(sizeof(Datum) * col_num);
// sgd_tupledesc->isnulls = (bool *) palloc0(sizeof(bool) * col_num);
// just for dblife:
/*
CREATE TABLE dblife (
did serial,
k integer[],
v double precision[],
label integer);
*/
if (dense == false) {
/* for dblife */
sgd_tupledesc->k_col = 1;
sgd_tupledesc->v_col = 2;
sgd_tupledesc->label_col = 3;
sgd_tupledesc->attr_num = 4;
sgd_tupledesc->max_sparse_count = max_sparse_count;
}
else {
/* for forest */
sgd_tupledesc->k_col = -1; // from 0
sgd_tupledesc->v_col = 1;
sgd_tupledesc->label_col = 2;
sgd_tupledesc->attr_num = 3;
}
sgd_tupledesc->n_features = n_features;
sgd_tupledesc->dense = dense;
return sgd_tupledesc;
}
/*
static void clear_SGDBatchState(SGDBatchState* batchstate, int n_features) {
int i;
for (i = 0; i < n_features; i++)
batchstate->gradients[i] = 0;
batchstate->loss = 0;
batchstate->tuple_num = 0;
}
static void clear_TestState(TestState* test_state) {
test_state->right_count = 0;
test_state->test_accuracy = 0;
test_state->test_total_loss = 0;
}
static void free_SGDBatchState(SGDBatchState* batchstate) {
pfree(batchstate->gradients);
pfree(batchstate);
}
static void free_SGDTuple(SortTuple* sgd_tuple) {
//pfree(sgd_tuple->features);
pfree(sgd_tuple);
}
*/
static void free_SGDTupleDesc(SGDTupleDesc* sgd_tupledesc) {
// pfree(sgd_tupledesc->values);
// pfree(sgd_tupledesc->isnulls);
pfree(sgd_tupledesc);
}
/*
static void free_TestState(TestState* test_state) {
pfree(test_state);
}
*/
inline void
compute_dense_tuple_gradient_LR(SortTuple* tp, Model* model)
{
int y = tp->class_label;
double* x = tp->features_v;
int n = model->n_features;
// compute gradients of the incoming tuple
double wx = dot(model->w, x, n);
double sig = sigma(-wx * y);
double c = model->learning_rate * y * sig; // scale factor
add_and_scale(model->w, n, x, c);
// regularization
double u = model->mu * model->learning_rate;
// l2_shrink_mask_d(model->w, u, n);
l1_shrink_mask_d(model->w, u, n);
}
inline void
batch_compute_dense_tuple_gradient_LR(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0)
add_and_scale(model->w, n, model->current_batch_gradient, 1.0 / model->current_batch_num);
memset(model->current_batch_gradient, 0, sizeof(double) * n);
model->current_batch_num = 0;
return;
}
int y = tp->class_label;
double* x = tp->features_v;
// compute gradients of the incoming tuple
double wx = dot(model->w, x, n);
double sig = sigma(-wx * y);
double c = model->learning_rate * sig * y; // scale factor
// add_and_scale(model->w, n, x, c);
add_and_scale(model->current_batch_gradient, n, x, c);
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
add_and_scale(model->w, n, model->current_batch_gradient, 1.0 / model->current_batch_num);
memset(model->current_batch_gradient, 0, sizeof(double) * n);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
// l2_shrink_mask_d(model->w, u, n);
l1_shrink_mask_d(model->w, u, n);
}
/*
inline void
batch_compute_dense_tuple_gradient_LR(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0) {
memcpy(model->w, model->w_old, sizeof(double) * n);
model->current_batch_num = 0;
}
return;
}
int y = tp->class_label;
double* x = tp->features_v;
// compute gradients of the incoming tuple
double wx = dot(model->w_old, x, n);
double sig = sigma(-wx * y);
double c = model->learning_rate * sig * y / model->batch_size; // scale factor
// add_and_scale(model->w, n, x, c);
add_and_scale(model->w, n, x, c);
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
memcpy(model->w_old, model->w, sizeof(double) * n);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
// l2_shrink_mask_d(model->w, u, n);
l1_shrink_mask_d(model->w, u, n);
}
*/
inline void
compute_sparse_tuple_gradient_LR(SortTuple* tp, Model* model)
{
int y = tp->class_label;
int* k = tp->features_k;
int k_len = tp->k_len;
double* v = tp->features_v;
double* w = model->w;
// grad
double wx = dot_dss(w, k, v, k_len);
double sig = sigma(-wx * y);
double c = model->learning_rate * y * sig; // scale factor
add_and_scale_dss(w, k, v, k_len, c);
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask(w, u, k, k_len);
}
/*
inline void
batch_compute_sparse_tuple_gradient_LR(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0) {
memcpy(model->w, model->w_old, sizeof(double) * n);
model->current_batch_num = 0;
}
return;
}
int y = tp->class_label;
int* k = tp->features_k;
int k_len = tp->k_len;
double* v = tp->features_v;
double* w = model->w_old;
// grad
double wx = dot_dss(w, k, v, k_len);
double sig = sigma(-wx * y);
double c = model->learning_rate * sig * y / model->batch_size; // scale factor
add_and_scale_dss(model->w, k, v, k_len, c);
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
memcpy(model->w_old, model->w, sizeof(double) * n);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask(w, u, k, k_len);
}
*/
/*
inline void
batch_compute_sparse_tuple_gradient_LR(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0)
add_and_scale(model->w, n, model->current_batch_gradient, model->learning_rate / model->current_batch_num);
memset(model->current_batch_gradient, 0, sizeof(double) * n);
model->current_batch_num = 0;
return;
}
int y = tp->class_label;
int* k = tp->features_k;
int k_len = tp->k_len;
double* v = tp->features_v;
double* w = model->w;
// grad
double wx = dot_dss(w, k, v, k_len);
double sig = sigma(-wx * y);
double c = sig * y; // scale factor
add_and_scale_dss(model->current_batch_gradient, k, v, k_len, c);
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
add_and_scale(model->w, n, model->current_batch_gradient, model->learning_rate / model->current_batch_num);
memset(model->current_batch_gradient, 0, sizeof(double) * n);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask(w, u, k, k_len);
}
*/
inline void
batch_compute_sparse_tuple_gradient_LR(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0)
my_sparse_add_and_scale_dss(model->w, model->current_batch_gradient, model->learning_rate / model->current_batch_num, model);
model->current_batch_num = 0;
return;
}
int y = tp->class_label;
int* k = tp->features_k;
int k_len = tp->k_len;
double* v = tp->features_v;
double* w = model->w;
// grad
double wx = dot_dss(w, k, v, k_len);
double sig = sigma(-wx * y);
double c = sig * y; // scale factor
add_and_scale_dss(model->current_batch_gradient, k, v, k_len, c);
// e.g., model->feature_k_non_zeros = [1, 2, 3; 2, 3, 4; 1, 4, 6]
int i;
for (i = 0; i < k_len; i++) {
model->feature_k_non_zeros[model->feature_k_index] = k[i];
model->feature_k_index += 1;
}
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
my_sparse_add_and_scale_dss(model->w, model->current_batch_gradient, model->learning_rate / model->batch_size, model);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask(w, u, k, k_len);
}
inline void
compute_dense_tuple_loss_LR(SortTuple* tp, Model* model)
{
// double* x = tp->features_v;
int y = tp->class_label;
double wx = dot(model->w, tp->features_v, model->n_features);
double _ywx = -y * wx;
if (_ywx >= 35)
model->total_loss += _ywx;
else
model->total_loss += log(1 + exp(_ywx));
// By default, if f(wx) > 0.5, the outcome is positive, or negative otherwise
double f_wx = sigma(wx);
if (f_wx >= 0.5 && y == 1) {
model->accuracy += 1;
}
else if (f_wx < 0.5 && y == -1) {
model->accuracy += 1;
}
}
inline void
compute_sparse_tuple_loss_LR(SortTuple* tp, Model* model)
{
int y = tp->class_label;
double wx = dot_dss(model->w, tp->features_k, tp->features_v, tp->k_len);
double _ywx = -y * wx;
if (_ywx >= 35)
model->total_loss += _ywx;
else
model->total_loss += log(1 + exp(_ywx));
// By default, if f(wx) > 0.5, the outcome is positive, or negative otherwise
double f_wx = sigma(wx);
if (f_wx >= 0.5 && y == 1) {
model->accuracy += 1;
}
else if (f_wx < 0.5 && y == -1) {
model->accuracy += 1;
}
}
inline void
compute_dense_tuple_gradient_SVM(SortTuple* tp, Model* model)
{
int y = tp->class_label;
double* x = tp->features_v;
int n = model->n_features;
double wx = dot(model->w, x, n);
double c = model->learning_rate * y;
// writes
if(1 - y * wx > 0) {
add_and_scale(model->w, n, x, c);
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask_d(model->w, u, n);
}
/*
inline void
batch_compute_dense_tuple_gradient_SVM(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0) {
memcpy(model->w, model->w_old, sizeof(double) * n);
model->current_batch_num = 0;
}
return;
}
int y = tp->class_label;
double* x = tp->features_v;
double wx = dot(model->w_old, x, n);
double c = model->learning_rate * y / model->batch_size;
// writes
if(1 - y * wx > 0) {
add_and_scale(model->w, n, x, c);
}
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
memcpy(model->w_old, model->w, sizeof(double) * n);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask_d(model->w, u, n);
}
*/
inline void
batch_compute_dense_tuple_gradient_SVM(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0)
add_and_scale(model->w, n, model->current_batch_gradient, 1.0 / model->current_batch_num);
memset(model->current_batch_gradient, 0, sizeof(double) * n);
model->current_batch_num = 0;
return;
}
int y = tp->class_label;
double* x = tp->features_v;
double wx = dot(model->w, x, n);
double c = model->learning_rate * y;
// writes
if(1 - y * wx > 0) {
add_and_scale(model->current_batch_gradient, n, x, c);
}
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
add_and_scale(model->w, n, model->current_batch_gradient, 1.0 / model->current_batch_num);
memset(model->current_batch_gradient, 0, sizeof(double) * n);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask_d(model->w, u, n);
}
inline void
compute_sparse_tuple_gradient_SVM(SortTuple* tp, Model* model)
{
int y = tp->class_label;
int* k = tp->features_k;
int k_len = tp->k_len;
double* v = tp->features_v;
// read and prepare
double wx = dot_dss(model->w, k, v, k_len);
double c = model->learning_rate * y;
// writes
if(1 - y * wx > 0) {
add_and_scale_dss(model->w, k, v, k_len, c);
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask(model->w, u, k, k_len);
}
inline void
batch_compute_sparse_tuple_gradient_SVM(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0)
my_sparse_add_and_scale_dss(model->w, model->current_batch_gradient, model->learning_rate / model->current_batch_num, model);
model->current_batch_num = 0;
return;
}
int y = tp->class_label;
int* k = tp->features_k;
int k_len = tp->k_len;
double* v = tp->features_v;
// read and prepare
double wx = dot_dss(model->w, k, v, k_len);
double c = y;
// writes
if(1 - y * wx > 0) {
add_and_scale_dss(model->current_batch_gradient, k, v, k_len, c);
// e.g., model->feature_k_non_zeros = [1, 2, 3; 2, 3, 4; 1, 4, 6]
int i;
for (i = 0; i < k_len; i++) {
model->feature_k_non_zeros[model->feature_k_index] = k[i];
model->feature_k_index += 1;
}
}
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
my_sparse_add_and_scale_dss(model->w, model->current_batch_gradient, model->learning_rate / model->batch_size, model);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask(model->w, u, k, k_len);
}
/*
inline void
batch_compute_sparse_tuple_gradient_SVM(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0)
add_and_scale(model->w, n, model->current_batch_gradient, model->learning_rate / model->current_batch_num);
memset(model->current_batch_gradient, 0, sizeof(double) * n);
model->current_batch_num = 0;
return;
}
int y = tp->class_label;
int* k = tp->features_k;
int k_len = tp->k_len;
double* v = tp->features_v;
// read and prepare
double wx = dot_dss(model->w, k, v, k_len);
double c = y;
// writes
if(1 - y * wx > 0) {
add_and_scale_dss(model->current_batch_gradient, k, v, k_len, c);
}
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
add_and_scale(model->w, n, model->current_batch_gradient, model->learning_rate / model->current_batch_num);
memset(model->current_batch_gradient, 0, sizeof(double) * n);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask(model->w, u, k, k_len);
}
*/
/*
inline void
batch_compute_sparse_tuple_gradient_SVM(SortTuple* tp, Model* model)
{
int n = model->n_features;
if (tp == NULL) {
if (model->current_batch_num > 0) {
memcpy(model->w, model->w_old, sizeof(double) * n);
model->current_batch_num = 0;
}
return;
}
int y = tp->class_label;
int* k = tp->features_k;
int k_len = tp->k_len;
double* v = tp->features_v;
// read and prepare
double wx = dot_dss(model->w_old, k, v, k_len);
double c = model->learning_rate * y / model->batch_size;
// writes
if(1 - y * wx > 0) {
add_and_scale_dss(model->w, k, v, k_len, c);
}
model->current_batch_num += 1;
if (model->current_batch_num == model->batch_size) {
memcpy(model->w_old, model->w, sizeof(double) * n);
model->current_batch_num = 0;
}
// regularization
double u = model->mu * model->learning_rate;
l1_shrink_mask(model->w, u, k, k_len);
}
*/
inline void
compute_dense_tuple_loss_SVM(SortTuple* tp, Model* model)
{
int y = tp->class_label;
double wx = dot(model->w, tp->features_v, model->n_features);
double loss = 1 - y * wx;
model->total_loss += (loss > 0) ? loss : 0;
// if wx >= 0 then the outcome is positive, and negative otherwise.
if (wx >= 0 && y == 1) {
model->accuracy += 1;
}
else if (wx < 0 && y == -1) {
model->accuracy += 1;
}
}
inline void
compute_sparse_tuple_loss_SVM(SortTuple* tp, Model* model)
{
int y = tp->class_label;
double wx = dot_dss(model->w, tp->features_k, tp->features_v, tp->k_len);