-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN.pde
87 lines (67 loc) · 1.66 KB
/
NN.pde
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
80
81
82
83
84
85
86
87
float target(float HD, float LD)
{
if(LD >= 56 && (HD >= -75))
{
return 1;
}
else
{
return 0;
}
}
float sigmoid(float x)
{
return 1/(1+pow(2.71828182846, -x));
}
float learning_rate = 0.2;
float w1 = random(1)*.2-.1;
float w2 = random(1)*.2-.1;
float b = random(1)*.2-.1;
ArrayList<float[]> dataList = new ArrayList<float[]>();
void giveInput(){
float[] point = {heightDifference, lengthDifference, target(heightDifference, lengthDifference)};
dataList.add(point);
float output = train();
print(output + "\n");
print("\n");
jump_or_not(output);
}
float train(){
for(int i = 0; i < 5000; i++){
//Geef input
float[] point = dataList.get(round(random(0,dataList.size() - 1)));
float target = point[2];
//Maak (training) voorspelling
float z = w1 * point[0] + w2 * point[1] + b;
float pred = sigmoid(z);
//Bereken cost & back-propagate
float cost = pow((pred - target),2);
//Derivatives berekeningen:
float dcost_dpred = 2 * (pred - target);
float dpred_dz = sigmoid(z) * (1-sigmoid(z));
float dz_dw1 = point[0];
float dz_dw2 = point[1];
float dz_db = 1;
float dcost_dw1 = dcost_dpred * dpred_dz * dz_dw1;
float dcost_dw2 = dcost_dpred * dpred_dz * dz_dw2;
float dcost_db = dcost_dpred * dpred_dz * dz_db;
w1 -= learning_rate * dcost_dw1;
w2 -= learning_rate * dcost_dw2;
b -= learning_rate * dcost_db;
}
float z = w1 * heightDifference + w2 * lengthDifference + b;
float pred = sigmoid(z);
return pred;
}
void jump_or_not(float output)
{
if(output > 0.5)
{
//doe niks
}
else
{
velocityY = 0;
accelerationY -= jumpforce;
}
}