-
Notifications
You must be signed in to change notification settings - Fork 28
/
Neuroduino.cpp
231 lines (187 loc) · 5.3 KB
/
Neuroduino.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Neuroduino.c
*
* Arduino Neural Network Library
* Adapted & simplified from:
* http://www.neural-networks-at-your-fingertips.com/bpn.html
*
* Implements a simple feedforward perceptron network
*
* Created by Ted Hayes on 4/15/10.
* Copyright 2010 Limina.Studio. All rights reserved.
*
*/
#include "WProgram.h"
#include "Neuroduino.h"
Neuroduino::Neuroduino(){};
Neuroduino::Neuroduino(int nodeArray[], int numLayers, double eta = 0.1, double theta = 0.0, boolean debug = false) {
// Constructor
_debug = debug;
trace("Neuroduino::Neuroduino\n");
_numLayers = numLayers;
_output = (int*) calloc(nodeArray[_numLayers-1], sizeof(int));
// allocate structs & initialize network
int i, j;
_net.Layer = (LAYER**) calloc(_numLayers, sizeof(LAYER*));
for (i=0; i < _numLayers; i++) {
// 2D array of the network's layers
//_output[i] = 0;
_net.Layer[i] = (LAYER*) malloc(sizeof(LAYER));
_net.Layer[i]->Units = nodeArray[i];
_net.Layer[i]->Output = (int*) calloc(nodeArray[i], sizeof(int));
// 2D array of weights for each pair of nodes between layers
_net.Layer[i]->Weight = (VAL**) calloc(nodeArray[i]+1, sizeof(VAL*));
if (i!=0) {
for (j=0; j<nodeArray[i]; j++) {
// init array for incoming connections to node j of layer i
_net.Layer[i]->Weight[j] = (VAL*) calloc(nodeArray[i-1], sizeof(VAL));
}
}
}
_net.InputLayer = _net.Layer[0];
_net.OutputLayer = _net.Layer[_numLayers-1];
_net.Eta = eta; // learning rate
_net.Theta = theta; // threshold
}
/********* UTILITIES *********/
//just for testing alternative ways of storing the values... (for now is the same: DOUBLE)
VAL Neuroduino::doubleToVal(double dValue) {
return (VAL) dValue;
}
double Neuroduino::randomEqualDouble(double Low, double High) {
return ((double) rand() / RAND_MAX) * (High-Low) + Low;
}
void Neuroduino::printNet(){
if (_debug) {
int l,i,j;
//Serial.print("weights of layer 0 test: ");
//Serial.println(_net.Layer[0]->Weight[0][0], DEC);
for (l=1; l<_numLayers; l++) {
Serial.print("Layer[");
Serial.print(l, DEC);
Serial.println("]:");
Serial.print("\t");
for (i=0; i<_net.Layer[l]->Units; i++) {
Serial.print(i,DEC);
Serial.print("\t");
}
Serial.println();
for (i=0; i<_net.Layer[l]->Units; i++) {
// cycle through units of current layer
//Serial.print(" Unit ");
Serial.print(i,DEC);
Serial.print(":\t");
for (j=0; j<_net.Layer[l-1]->Units; j++) {
// cycle through units of "leftmost" layer
Serial.print(_net.Layer[l]->Weight[i][j], 3);
trace("\t");
}
trace("\n");
}
trace("\n");
}
trace("\n");
}
}
// free memory check
// from: http://forum.pololu.com/viewtopic.php?f=10&t=989#p4218
int Neuroduino::get_free_memory(){
int free_memory;
if((int)__brkval == 0)
free_memory = ((int)&free_memory) - ((int)&__bss_end);
else
free_memory = ((int)&free_memory) - ((int)__brkval);
return free_memory;
}
/********* PRIVATE *********/
void Neuroduino::randomizeWeights() {
int l,i,j;
double temp;
for (l=1; l<_numLayers; l++) {
for (i=0; i<_net.Layer[l]->Units; i++) {
for (j=0; j<_net.Layer[l-1]->Units; j++) {
_net.Layer[l]->Weight[i][j] = doubleToVal(randomEqualDouble(-0.5, 0.5));
}
}
}
}
void Neuroduino::setInput(int inputs[]){
int i;
for (i=0; i<_net.InputLayer->Units; i++) {
_net.InputLayer->Output[i] = inputs[i];
}
}
int* Neuroduino::getOutput(){
return _output;
}
int Neuroduino::signThreshold(double sum){
if (sum >= _net.Theta) {
return 1;
} else {
return -1;
}
}
double Neuroduino::weightedSum(int l, int node){
// calculates input activation for a particular neuron
int i;
double currentWeight, sum = 0.0;
for (i=0; i<_net.Layer[l-1]->Units; i++) {
currentWeight = _net.Layer[l]->Weight[node][i];
sum += currentWeight * _net.Layer[l-1]->Output[i];
}
return sum;
}
void Neuroduino::adjustWeights(int trainArray[]){
int l,i,j;
int in,out, error;
int activation; // for each "rightmost" node
double delta;
for (l=1; l<_numLayers; l++) {
// cycle through each pair of nodes
for (i=0; i<_net.Layer[l]->Units; i++) {
// "rightmost" layer
// calculate current activation of this output node
activation = signThreshold(weightedSum(l,i));
out = trainArray[i]; // correct activation
error = out - activation; // -2, 2, or 0
for (j=0; j<_net.Layer[l-1]->Units; j++) {
// "leftmost" layer
in = _net.Layer[l-1]->Output[j];
delta = _net.Eta * in * error;
_net.Layer[l]->Weight[i][j] += delta;
}
}
}
}
void Neuroduino::simulateNetwork(){
/*****
Calculate activations of each output node
*****/
int l,j;
for (l=_numLayers-1; l>0; l--) {
// step backwards through layers
// TODO: this will only work for _numLayers = 2!
for (j=0; j < _net.Layer[l]->Units; j++) {
_output[j] = signThreshold(weightedSum(1, j));
}
}
}
/********* PUBLIC *********/
void Neuroduino::train(int inputArray[], int trainArray[]) {
trace("Neuroduino::train\n");
setInput(inputArray);
adjustWeights(trainArray);
}
int* Neuroduino::simulate(int inputArray[]) {
// introduce an input stimulus, simulate the network,
// and return an output array
trace("Neuroduino::simulate\n");
setInput(inputArray);
simulateNetwork();
return _output;
}
void Neuroduino::trace(char *message) {
if(_debug){
Serial.print(message);
}
}