This repository was archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregress.cpp
More file actions
230 lines (210 loc) · 8.25 KB
/
regress.cpp
File metadata and controls
230 lines (210 loc) · 8.25 KB
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
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
constexpr int model_count = 1000;
// Hyperparameters
constexpr float huber_loss_threashold = 10;
constexpr float z_score_trimming_threashold = 2;
constexpr float epsilon = 0.49;
constexpr float learning_rate = 0.01;
constexpr int batch_size = 128;
constexpr int max_iter = 10000;
constexpr int sample_size = 1024;
constexpr int dimension = 6;
int main(void) {
srand(clock());
// Read training data
FILE* f = fopen("in.txt", "r");
float* const _X = new float[sample_size * dimension * model_count];
float* const _y = new float[sample_size * model_count];
for (int i = 0; i < sample_size; i++) {
_X[i * dimension] = 1;
for (int j = 1; j < dimension; j++) {
fscanf(f, "%f", _X + i * dimension + j);
}
fscanf(f, "%f", _y + i);
}
fclose(f);
for (int i = 0; i < model_count; i++) {
memcpy(_X + i * sample_size * dimension, _X, sample_size * dimension * sizeof(float));
memcpy(_y + i * sample_size, _y, sample_size * sizeof(float));
}
float* const _w = new float[dimension * model_count];
// Start timing
clock_t clk = clock();
for (int model_id = 0; model_id < model_count; model_id++) {
float* const X = _X + model_id * sample_size * dimension;
float* const y = _y + model_id * sample_size;
float* const w = _w + model_id * dimension;
// Initialize weights
for (int i = 0; i < dimension; i++) {
w[i] = 1;
}
int indices[batch_size];
int indices_copy[batch_size];
float residuals[batch_size];
float residuals_copy[batch_size];
float gradient[dimension];
float prev_loss = 0;
for (int _ = -1; _ < max_iter; _++) {
// Sample with replacement
for (int i = 0; i < batch_size; i++) {
indices[i] = ((float)rand()) / RAND_MAX * sample_size;
}
for (int i = 0; i < batch_size; i++) {
float* const x = X + indices[i] * dimension;
float residual = -y[indices[i]];
for (int j = 0; j < dimension; j++) {
residual += x[j] * w[j];
}
residuals[i] = residual;
}
// Merge sort residuals and permute the indices accordingly
for (int i = 1; i < batch_size; i += 2) {
if (residuals[i] < residuals[i - 1]) {
const float tmp_float = residuals[i];
residuals[i] = residuals[i - 1];
residuals[i - 1] = tmp_float;
const int tmp_int = indices[i];
indices[i] = indices[i - 1];
indices[i - 1] = tmp_int;
}
}
for (int stride = 2; stride < batch_size; stride *= 2) {
for (int i = 0; i < batch_size; i += stride * 2) {
int j = i;
const int j_end = i + stride;
if (j_end >= batch_size) {
break;
}
int k = j_end;
const int k_end = ((i + stride * 2) > batch_size) ? batch_size : (i + stride * 2);
int l = i;
while (j != j_end && k != k_end) {
if (residuals[j] < residuals[k]) {
residuals_copy[l] = residuals[j];
indices_copy[l] = indices[j];
j++;
}
else {
residuals_copy[l] = residuals[k];
indices_copy[l] = indices[k];
k++;
}
l++;
}
if (j == j_end) {
for (j = i; j < l; j++) {
residuals[j] = residuals_copy[j];
indices[j] = indices_copy[j];
}
}
else {
for (k = j_end - 1;k >= j; k--) {
residuals[k + k_end - j_end] = residuals[k];
indices[k + k_end - j_end] = indices[k];
}
for (k = i; k < l; k++) {
residuals[k] = residuals_copy[k];
indices[k] = indices_copy[k];
}
}
}
}
// Epsilon-trimming
int index_low = 0;
float abs_residual_low = std::abs(residuals[0]);
int index_high = batch_size - 1;
float abs_residual_high = std::abs(residuals[batch_size - 1]);
for (int i = 0; i < (int)(batch_size * epsilon); i++) {
if (abs_residual_low < abs_residual_high) {
index_high--;
abs_residual_high = std::abs(residuals[index_high]);
}
else {
index_low++;
abs_residual_low = std::abs(residuals[index_low]);
}
}
// Z-score trimming
while (true) {
float mean = 0;
for (int i = index_low; i <= index_high; i++) {
mean += residuals[i];
}
mean /= (index_high - index_low);
float stdev = 0;
for (int i = index_low; i <= index_high; i++) {
const float diff = residuals[i] - mean;
stdev += diff * diff;
}
stdev = sqrt(stdev / (index_high - index_low));
bool flag_converged = true;
const float threashold_low = mean - stdev * z_score_trimming_threashold;
while (residuals[index_low] < threashold_low) {
index_low++;
flag_converged = false;
}
const float threashold_high = mean + stdev * z_score_trimming_threashold;
while (residuals[index_high] > threashold_high) {
index_high--;
flag_converged = false;
}
if (flag_converged) {
break;
}
}
// Huber loss
float loss = 0;
for (int i = 0; i < dimension; i++) {
gradient[i] = 0;
}
for (int i = index_low; i <= index_high; i++) {
float* const x = X + indices[i] * dimension;
const float residual = residuals[i];
const float abs_residual = std::abs(residual);
if (abs_residual <= huber_loss_threashold) {
loss += residual * residual / 2;
for (int j = 0; j < dimension; j++) {
gradient[j] += residual * x[j];
}
}
else {
loss += abs_residual * huber_loss_threashold - huber_loss_threashold * huber_loss_threashold / 2;
for (int j = 0; j < dimension; j++) {
gradient[j] += ((residual > 0) - (residual < 0)) * x[j] * huber_loss_threashold;
}
}
}
for (int i = 0; i < dimension; i++) {
gradient[i] /= batch_size;
}
loss /= batch_size;
// Update weights
for (int i = 0; i < dimension; i++) {
w[i] -= learning_rate * gradient[i];
}
// Check convergence
if (std::abs((loss - prev_loss) / prev_loss) < 1e-4) {
//break;
}
prev_loss = loss;
}
}
clk = clock() - clk;
printf("C++ running time:\t%.3fms\n", (double)clk / CLOCKS_PER_SEC * 1000);
// Write the trained weights
f = fopen("out.txt", "w");
fprintf(f, "%f", _w[(model_count - 1) * dimension]);
for (int i = 1; i < dimension; i++) {
fprintf(f, " %f", _w[(model_count - 1) * dimension + i]);
}
fclose(f);
// Free resources
delete[] _X;
delete[] _y;
delete[] _w;
return 0;
}