-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwice.c
More file actions
75 lines (65 loc) · 1.35 KB
/
twice.c
File metadata and controls
75 lines (65 loc) · 1.35 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float train[][2] = {
{0, 0},
{1, 2},
{2, 4},
{3, 6},
{4, 8},
};
#define train_count (sizeof(train)/sizeof(train[0]))
float rand_float(void)
{
return (float) rand()/ (float) RAND_MAX;
}
// x1, x2, x3, ..., b
// w1, w2, w3, ...
// y = x1*w1 + x2*w2 + x3*w3 + ... + b
float cost(float w)
{
float result = 0.0f;
size_t n = train_count;
for (size_t i = 0; i < n; ++i) {
float x = train[i][0];
float y = x*w;
float d = y - train[i][1];
result += d*d;
}
result /= n;
return result;
}
float dcost(float w)
{
float result = 0.0f;
size_t n = train_count;
for (size_t i = 0; i < n; ++i) {
float x = train[i][0];
float y = train[i][1];
result += 2*(x*w - y)*x;
}
result /= n;
return result;
}
int main()
{
// srand(time(0));
srand(69);
float w = rand_float()*10.0f;
float rate = 1e-1;
printf("cost = %f, w = %f\n", cost(w), w);
for (size_t i = 0; i < 50; ++i) {
#if 0
float eps = 1e-3;
float c = cost(w);
float dw = (cost(w + eps) - c)/eps;;
#else
float dw = dcost(w);
#endif
w -= rate*dw;
printf("cost = %f, w = %f\n", cost(w), w);
}
printf("------------------------------\n");
printf("w = %f\n", w);
return 0;
}