-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtwice.c
79 lines (62 loc) · 1.48 KB
/
gtwice.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float train[][2] = {
{0, 0},
{1, 2},
{2, 4},
{3, 6},
{4, 8},
};
// y = x*w+b;
#define train_count (sizeof(train)/sizeof(train[0]))
float rand_float(void){
return (float) rand()/ (float) RAND_MAX;
}
float cost(float w, float b){
float result = 0.0f;
for (size_t i = 0; i < train_count; i++) {
float x = train[i][0];
float y = x*w + b;
float d = y - train[i][1];
result += d*d;
}
result /= train_count;
return result;
}
float wcost(float w, float b){
float result = 0.f;
size_t n = train_count;
for(size_t i = 0; i < n; i++){
float y = train[i][1];
float x = train[i][0];
result += 2*(x*w + b - y)*x;
}
return result /= n;
}
float dcost(float w, float b){
float result = 0.f;
size_t n = train_count;
for(size_t i = 0; i < n; i++){
float y = train[i][1];
float x = train[i][0];
result += 2*(x*w + b - y);
}
return result /= n;
}
int main() {
srand(time(0));
float w = rand_float()*10.0f;
float b = rand_float()*5.0f;
float rate = 1e-3;
for (size_t steps = 0; steps < 5000; steps++) {
float dw = wcost(w, b);
float db = dcost(w, b);
w -= rate * dw;
b -= rate * db;
printf("cost = %f, w = %f, b = %f\n", cost(w, b), w, b);
}
printf("-----------------------\n");
printf("w = %f, b = %f\n", w, b);
return 0;
}