-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlearnfast.pde
183 lines (141 loc) · 4.37 KB
/
learnfast.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* This teaches the traffic light colors to the MLP, then
* outputs the weights for use on an Arduino.
*/
boolean pause = false;
boolean learningMode = false;
boolean learnRed = false;
boolean learnAmber = false;
boolean learnGreen = false;
boolean learnOther = false;
int r;
int g;
int b;
Neural network;
void setup() {
println("Configuring neural network...");
network = new Neural(3,6,4);
network.setLearningRate(0.5);
println("Inputs =", network.getNoOfInputNodes(), " Hidden = ", network.getNoOfHiddenNodes(), " Outputs = ", network.getNoOfOutputNodes());
network.setBiasInputToHidden(0.35);
network.setBiasHiddenToOutput(0.60);
/***********************************
**
** TEACH THE BRAIN !!!
**
**********************************/
network.turnLearningOn();
println("Neural network is learning...");
for (int loop = 0; loop < 90000; ++loop) {
// Enter your RGB values here
teachRed(149, 56, 61);
teachAmber(123, 77, 61);
teachGreen(67, 100, 90);
teachOther(92, 90, 82);
teachOther(92, 90, 75);
teachOther(73, 93, 89);
//teachOther(152, 167, 161);
}
network.turnLearningOff();
/***********************************
**
** END OF TEACHING !!!
**
**********************************/
println("Input-to-hidden node weights");
network.displayInputToHiddenWeightsCurrent();
println();
println("Hidden-to-output node weights");
network.displayHiddenToOutputWeightsCurrent();
println();
println("Arduino sketch code:");
println();
for (int x = 0; x < network.getNoOfInputNodes(); ++x) {
println(" // For Input Node " + x + ": ");
for (int y = 0; y < network.getNoOfHiddenNodes(); ++y) {
println(" network.setInputToHiddenWeight(", x,",", y,",", network.inputToHiddenWeights[x][y],");");
//print(inputToHiddenWeights[x][y], " ");
}
println();
}
for (int x = 0; x < network.getNoOfHiddenNodes(); ++x) {
println(" //For Hidden Node " + x + ": ");
for (int y = 0; y < network.getNoOfOutputNodes(); ++y) {
println(" network.setHiddenToOutputWeight(", x,",", y,",", network.hiddenToOutputWeights[x][y],");");
//print(hiddenToOutputWeights[x][y], " ");
}
println();
}
println("Neural network is ready");
}
void draw() {
// No code required here
}
void teachRed(int r, int g, int b) {
float newR, newG, newB;
newR = (randomise(r) / 255.0);
newG = (randomise(g) / 255.0);
newB = (randomise(b) / 255.0);
//println("Red:", newR, newG, newB);
network.setInputNode(0, newR);
network.setInputNode(1, newG);
network.setInputNode(2, newB);
network.setOutputNodeDesired(0, 0.99);
network.setOutputNodeDesired(1, 0.01);
network.setOutputNodeDesired(2, 0.01);
network.setOutputNodeDesired(3, 0.01);
network.calculateOutput();
}
void teachAmber(int r, int g, int b) {
float newR, newG, newB;
newR = (randomise(r) / 255.0);
newG = (randomise(g) / 255.0);
newB = (randomise(b) / 255.0);
//println("Amber:", newR, newG, newB);
network.setInputNode(0, newR);
network.setInputNode(1, newG);
network.setInputNode(2, newB);
network.setOutputNodeDesired(0, 0.01);
network.setOutputNodeDesired(1, 0.99);
network.setOutputNodeDesired(2, 0.01);
network.setOutputNodeDesired(3, 0.01);
network.calculateOutput();
}
void teachGreen(int r, int g, int b) {
float newR, newG, newB;
newR = (randomise(r) / 255.0);
newG = (randomise(g) / 255.0);
newB = (randomise(b) / 255.0);
network.setInputNode(0, newR);
network.setInputNode(1, newG);
network.setInputNode(2, newB);
network.setOutputNodeDesired(0, 0.01);
network.setOutputNodeDesired(1, 0.01);
network.setOutputNodeDesired(2, 0.99);
network.setOutputNodeDesired(3, 0.01);
network.calculateOutput();
}
void teachOther(int r, int g, int b) {
float newR, newG, newB;
newR = (randomise(r) / 255.0);
newG = (randomise(g) / 255.0);
newB = (randomise(b) / 255.0);
network.setInputNode(0, newR);
network.setInputNode(1, newG);
network.setInputNode(2, newB);
network.setOutputNodeDesired(0, 0.01);
network.setOutputNodeDesired(1, 0.01);
network.setOutputNodeDesired(2, 0.01);
network.setOutputNodeDesired(3, 0.99);
network.calculateOutput();
}
int randomise(int value) {
value += random(-4, 5);
if (value > 255) {
value = 255;
}
if (value < 0 ) {
value = 0;
}
return value;
}