-
Notifications
You must be signed in to change notification settings - Fork 0
/
twice.c
55 lines (47 loc) · 1.08 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float train[][2] = {
{0, 0},
{1, 10},
{2, 20},
{3, 30},
{4, 40}
};
#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;
printf("%f\n", cost(w, b));
for (size_t i = 0; i < 10000; ++i) {
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, weight = %f, bias = %f\n", cost(w, b), w, b);
}
printf("---------------\n");
printf("w = %f, b = %f\n", w, b);
return 0;
}