-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.js
234 lines (208 loc) · 7.76 KB
/
nn.js
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
232
233
234
class NeuralNetwork {
constructor(inputNodes, hiddenNodes, outputNodes, learningRate) {
if (inputNodes instanceof NeuralNetwork) {
let preNuralNetwork = inputNodes;
this.inputNodes = preNuralNetwork.inputNodes;
this.hiddenNodes = [...preNuralNetwork.hiddenNodes];
this.outputNodes = preNuralNetwork.outputNodes;
this.learningRate = preNuralNetwork.learningRate || 0.1;
this.wHH = preNuralNetwork.wHH.map(hiddenWeights => {
return hiddenWeights.clone();
});
this.wHO = preNuralNetwork.wHO.clone();
} else if (inputNodes instanceof Object) {
let preJsonData = inputNodes;
this.inputNodes = preJsonData.inputNodes;
this.hiddenNodes = [...preJsonData.hiddenNodes];
this.outputNodes = preJsonData.outputNodes;
this.learningRate = preJsonData.learningRate || 0.1;
this.wHH = preJsonData.wHH.map(hiddenWeights => {
return nj.array(hiddenWeights);
});
this.wHO = nj.array(preJsonData.wHO);
} else {
this.inputNodes = inputNodes;
this.hiddenNodes = hiddenNodes;
this.outputNodes = outputNodes;
this.learningRate = learningRate || 0.1;
// ! calculate all hidden weight
this.wHH = [];
for (let i = 0; i < this.hiddenNodes.length; i++) {
// check if multilayer or not
if (i == 0) {
this.wHH.push(nj.random([this.inputNodes, this.hiddenNodes[0]]).subtract(0.5))
} else {
this.wHH.push(nj.random([this.hiddenNodes[i - 1], this.hiddenNodes[i]]).subtract(0.5))
}
}
// ! calculate hidden to output
this.wHO = nj.random([this.hiddenNodes[this.hiddenNodes.length - 1], this.outputNodes]).subtract(0.5)
}
}
static randomfloat(min, max) {
min = min ? min : 0;
min = min ? min : 1;
return Math.random() * (max - min) + min;
}
static randomint(min, max) {
min = min ? min : 0;
min = min ? min : 100;
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// ! sigmoid activation function
activationFunction(x) {
return nj.sigmoid(x);
}
// ! sigmoid deactivation function
deactivationFunction(x) {
return x.multiply(x.subtract(1.0)).multiply(-1);
}
train(inputList, targetList) {
let inputs = nj.array(inputList);
let targets = nj.array(targetList);
inputs = (inputs.ndim <= 1) ? nj.array([inputList]) : inputs;
targets = (targets.ndim <= 1) ? nj.array([targetList]) : targets;
// * feed forward
// ? calculate all hidden inputs and outputs
let hiddenInputs = [];
let hiddenOutputs = [];
for (let i = 0; i < this.wHH.length; i++) {
if (i == 0) {
hiddenInputs.push(nj.dot(inputs, this.wHH[i]))
hiddenOutputs.push(this.activationFunction(hiddenInputs[hiddenInputs.length - 1]))
} else {
hiddenInputs.push(nj.dot(hiddenOutputs[i - 1], this.wHH[i]))
hiddenOutputs.push(this.activationFunction(hiddenInputs[hiddenInputs.length - 1]))
}
}
// ? calculate all hidden to outputs
let finalInputs = nj.dot(hiddenOutputs[hiddenOutputs.length - 1], this.wHO);
let finalOutputs = this.activationFunction(finalInputs);
// * error calculation
// ? error to output
let outputError = targets.subtract(finalOutputs)
// ? error of hidden
let hiddenError = []
for (let i = 0; i < this.wHH.length; i++) {
if (i == 0) {
hiddenError.push(nj.dot(outputError, this.wHO.T))
} else {
hiddenError.push(nj.dot(hiddenError[hiddenError.length - i], this.wHH[this.wHH.length - i].T))
}
}
hiddenError = hiddenError.reverse()
// * error reduction
// ? hidden error reduction
for (let i = 0; i < this.wHH.length; i++) {
if (i == 0) {
this.wHH[i] = this.wHH[i].add(nj.dot(inputs.T, hiddenError[i].multiply(this.deactivationFunction(hiddenOutputs[i]).multiply(this.learningRate))));
} else {
this.wHH[i] = this.wHH[i].add(nj.dot(hiddenOutputs[i - 1].T, hiddenError[i].multiply(this.deactivationFunction(hiddenOutputs[i]).multiply(this.learningRate))));
}
}
// ? output error reduction
this.wHO = this.wHO.add(nj.dot(hiddenOutputs[hiddenOutputs.length - 1].T, outputError.multiply(this.deactivationFunction(finalOutputs).multiply(this.learningRate))));
}
query(inputList) {
let inputs = nj.array(inputList);
inputs = (inputs.ndim <= 1) ? nj.array([inputList]) : inputs;
let hiddenInputs = [];
let hiddenOutputs = [];
for (let i = 0; i < this.wHH.length; i++) {
if (i == 0) {
hiddenInputs.push(nj.dot(inputs, this.wHH[i]))
hiddenOutputs.push(this.activationFunction(hiddenInputs[hiddenInputs.length - 1]))
} else {
hiddenInputs.push(nj.dot(hiddenOutputs[i - 1], this.wHH[i]))
hiddenOutputs.push(this.activationFunction(hiddenInputs[hiddenInputs.length - 1]))
}
}
// ? calculate all hidden to outputs
let finalInputs = nj.dot(hiddenOutputs[hiddenOutputs.length - 1], this.wHO);
let finalOutputs = this.activationFunction(finalInputs);
return finalOutputs;
}
serialize() {
let json = JSON.parse(JSON.stringify(Object.assign({}, this)));
json.wHH = this.wHH.map(hiddenWeights => {
return hiddenWeights.clone().tolist();
});
json.wHO = this.wHO.clone().tolist();
return json;
}
// ? adding code for nuroevolution
copy() {
return new NeuralNetwork(this)
}
static crossOver(nn1, nn2, crossRate, mutationRate, sd) {
crossRate = crossRate ? crossRate : 0.01;
mutationRate = mutationRate ? mutationRate : 0.1;
sd = sd ? sd : 0.1;
nn1.wHH.forEach((hidenWeights, index) => {
for (let i = 0; i < hidenWeights.shape[0]; i++) {
for (let j = 0; j < hidenWeights.shape[1]; j++) {
if (Math.random() < crossRate, mutationRate) {
hidenWeights.set(i, j, nn2.wHH[index].get(i, j));
if (Math.random() < mutationRate) {
hidenWeights.set(i, j, (hidenWeights.get(i, j) + this.randomG(0, sd)));
}
}
}
}
});
for (let i = 0; i < nn1.wHO.shape[0]; i++) {
for (let j = 0; j < nn1.wHO.shape[1]; j++) {
if (Math.random() < crossRate) {
nn1.wHO.set(i, j, nn2.wHO.get(i, j));
if (Math.random() < mutationRate) {
nn1.wHO.set(i, j, (nn1.wHO.get(i, j) + this.randomG(0, sd)));
}
}
}
}
return new NeuralNetwork(nn1);
}
mutate(rate,sd) {
sd = sd ? sd : 0.1;
this.wHH.forEach(hidenWeights => {
for (let i = 0; i < hidenWeights.shape[0]; i++) {
for (let j = 0; j < hidenWeights.shape[1]; j++) {
if (Math.random() < rate) {
hidenWeights.set(i, j, (hidenWeights.get(i, j) + this.randomG(0, sd)));
}
}
}
});
for (let i = 0; i < this.wHO.shape[0]; i++) {
for (let j = 0; j < this.wHO.shape[1]; j++) {
if (Math.random() < rate) {
this.wHO.set(i, j, (this.wHO.get(i, j) + this.randomG(0, sd)));
}
}
}
}
static randomG(mean, sd = 1) {
let y1, y2, x1, x2, w;
do {
x1 = (Math.random() * 2) - 1;
x2 = (Math.random() * 2) - 1;
w = x1 * x1 + x2 * x2;
} while (w >= 1);
w = Math.sqrt(-2 * Math.log(w) / w);
y1 = x1 * w;
const m = mean || 0;
return y1 * sd + m;
}
randomG(mean, sd = 1) {
let y1, y2, x1, x2, w;
do {
x1 = (Math.random() * 2) - 1;
x2 = (Math.random() * 2) - 1;
w = x1 * x1 + x2 * x2;
} while (w >= 1);
w = Math.sqrt(-2 * Math.log(w) / w);
y1 = x1 * w;
const m = mean || 0;
return y1 * sd + m;
}
}