-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient.c
More file actions
55 lines (43 loc) · 970 Bytes
/
gradient.c
File metadata and controls
55 lines (43 loc) · 970 Bytes
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, 2},
{2, 4},
{3, 6},
{4, 8},
};
#define train_count (sizeof(train)/sizeof(train[0]))
float rand_float(){
return (float) rand() / (float) RAND_MAX;
}
float cost_function(float w){
float result = 0.0f;
for (size_t i = 0; i < train_count; i++){
float x = train[i][0];
float y = x * w;
float distance = y - train[i][0];
result += distance * distance;
}
result /= train_count;
return result;
}
float derivative(float w){
float result = 0.0f;
for (size_t i = 0; i < train_count; i++){
float x = train[i][0];
float y = x * w;
float distance = y - train[i][0];
result += -2 * distance
}
result /= train_count;
}
float gradient_step(w, eps){
w = w - (eps * derivative(w));
return w;
}
float gradient_descent()
int main(){
return 0;
}