-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwice.c
56 lines (44 loc) · 1.07 KB
/
twice.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
#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;
}
int main() {
srand(time(0));
float w = rand_float()*10.0f;
float b = rand_float()*5.0f;
float eps = 1e-3;
float rate = 1e-3;
for (size_t steps = 0; steps < 10000; steps++) {
float c = cost(w, b);
float dw = (cost(w + eps, b) - c)/eps;
float db = (cost(w, b + eps) - c)/eps;
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;
}