-
Notifications
You must be signed in to change notification settings - Fork 0
/
SGD.c
158 lines (130 loc) · 5.14 KB
/
SGD.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
void new_func2() {
}
void new_func() {
//hey i will put my new codes here.
}
//average SGD implemented by Julius 2014.08.27
void LLC_SGD(double *w, double *x, double *centers, int *knn_idx, int knn, int d) {
int i, j, iter=0, iter0=-1;
float GAMMA = 2, adaGAMMA=GAMMA;
memset(w, 0.01, knn*sizeof(double));
//minimizing 0.5*||x - w*centers||2,2 + 0.5*lambda*||w||2,2
srand(time(NULL));
float past_grad[64] = {0};
//w[0] = w[1] = 0.25;
init_w(w, knn);
eval_obj_iter = 0;
while(iter < knn*30) {
for(i=0; i<BATCH_SIZE; i++)
batch_idx[i] = rand()%d; //[0, 127]
adaGAMMA = GAMMA*pow(1+GAMMA*BETA_SGD*(iter0+1), -0.75);
//adaGAMMA = GAMMA;
if(iter > knn*8)
iter0++;
//if(iter0 == 0)
// printf("============Start averaging!!============\n");
//printf("adaGAMMA[%d]: %f\n", iter, adaGAMMA);
float wcenters[BATCH_SIZE]={0}, sub_grad=0;
//mini-batch SGD, if BATCH_SIZE = 1, then it is shrunk to SGD
if(BATCH_SIZE > 1) { //mini-batch SGD
for(j=0; j<BATCH_SIZE; j++) {
int idx = batch_idx[j];
for(i=0; i<knn; i++)
wcenters[j] += w[i]*centers[knn_idx[i]*d+idx];
wcenters[j] = x[idx] - wcenters[j];
}
for(i=0; i<knn; i++) {
sub_grad=0;
for(j=0; j<BATCH_SIZE; j++) {
int idx = batch_idx[j];
sub_grad += wcenters[j]*centers[knn_idx[i]*d+idx];
}
sub_grad /= BATCH_SIZE; //average over mini-batch gradients
sub_grad += BETA_SGD*w[i];
sub_grad *= adaGAMMA;
if(abs_float(sub_grad) < 5e-4) //neglect too small update
continue;
w[i] += (sub_grad);
w[i] = (w[i] > 1) ? 1: w[i];
w[i] = (w[i] < -1) ? -1: w[i];
}
}
else { //SGD
int idx = batch_idx[0];
for(i=0; i<knn; i++)
wcenters[0] += w[i]*centers[knn_idx[i]*d+idx];
for(i=0; i<knn; i++) {
sub_grad = (x[idx] - wcenters[0])*centers[knn_idx[i]*d+idx]; //calculate sub_grad[0~127]
sub_grad += BETA_SGD*w[i];
sub_grad *= adaGAMMA;
if(abs_float(sub_grad) < 5e-4) //neglect too small update
continue;
double lower_bound, upper_bound;
double tentaive_w = w[i] + sub_grad;
if(w[i] > 0) {
lower_bound = 0; //avg_w - 2*avg < x < avg_w + 2*avg
upper_bound = 2*w[i];
}
else {
lower_bound = 2*w[i];
upper_bound = 0;
}
w[i] += (sub_grad);
w[i] = (tentaive_w > upper_bound) ? upper_bound: tentaive_w;
w[i] = (tentaive_w < lower_bound) ? lower_bound: tentaive_w;
w[i] = (w[i] > 1) ? 1: w[i];
w[i] = (w[i] < -1) ? -1: w[i];
}
}
if(iter0 == 0) {
for(i=0; i<knn; i++)
avg_w[i] = w[i];
}
if(iter0 >= 0) {
for(i=0; i<knn; i++) {
double past_w = avg_w[i];
avg_w[i] = w[i] + (double)iter0/(iter0+1)*(avg_w[i]-w[i]);
past_grad[i] = avg_w[i] - past_w;
double tentaive_w = avg_w[i] + 2*past_grad[i]; //momentum is used
double lower_bound, upper_bound;
if(avg_w[i] > 0) {
lower_bound = -2*avg_w[i]; //avg_w - 3*avg < x < avg_w + 3*avg
upper_bound = 4*avg_w[i];
}
else {
lower_bound = 4*avg_w[i];
upper_bound = -2*avg_w[i];
}
avg_w[i] = (tentaive_w > upper_bound) ? upper_bound: tentaive_w;
avg_w[i] = (tentaive_w < lower_bound) ? lower_bound: tentaive_w;
avg_w[i] = (avg_w[i] > 1) ? 1: avg_w[i];
avg_w[i] = (avg_w[i] < -1) ? -1: avg_w[i];
//printf("[%d](w, grad) = (%f, %f)\n", i, avg_w[i], past_grad[i]);
}
#ifdef SGD_DEBUGGING
eval_obj(avg_w, x, centers, knn_idx, knn, d);
#endif
}
//norm_w(w, d);
else {
#ifdef SGD_DEBUGGING
eval_obj(w, x, centers, knn_idx, knn, d);
#endif
}
iter++;
}
double sum=0;
for(i=0; i<knn; i++)
sum += avg_w[i];
for(i=0; i<knn; i++) {
avg_w[i] /= sum;
if(abs_double(avg_w[i]) < TOLERANCE) //cut-off small values
avg_w[i] = 0;
}
#ifdef SGD_DEBUGGING
printf("LLC_SGD w: (");
for(i=0; i<knn; i++)
printf("%f ", avg_w[i]);
printf(")\n");
#endif
}