-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathneural-network.js
More file actions
288 lines (240 loc) · 7.94 KB
/
neural-network.js
File metadata and controls
288 lines (240 loc) · 7.94 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"use strict";
const LOG_ON = true; // whether or not to show error logging
const LOG_FREQ = 20000; // how often to show error logs (in iterations)
class NeuralNetwork {
constructor(numInputs, numHidden, numOutputs) {
this._inputs = [];
this._hidden = [];
this._numInputs = numInputs;
this._numHidden = numHidden;
this._numOutputs = numOutputs;
this._bias0 = new Matrix(1, this._numHidden);
this._bias1 = new Matrix(1, this._numOutputs);
this._weights0 = new Matrix(this._numInputs, this._numHidden);
this._weights1 = new Matrix(this._numHidden, this._numOutputs);
// error logging
this._logCount = LOG_FREQ;
// randomise the initial weights
this._bias0.randomWeights();
this._bias1.randomWeights();
this._weights0.randomWeights();
this._weights1.randomWeights();
}
get inputs() {
return this._inputs;
}
set inputs(inputs) {
this._inputs = inputs;
}
get hidden() {
return this._hidden;
}
set hidden(hidden) {
this._hidden = hidden;
}
get bias0() {
return this._bias0;
}
set bias0(bias) {
this._bias0 = bias;
}
get bias1() {
return this._bias1;
}
set bias1(bias) {
this._bias1 = bias;
}
get weights0() {
return this._weights0;
}
set weights0(weights) {
this._weights0 = weights;
}
get weights1() {
return this._weights1;
}
set weights1(weights) {
this._weights1 = weights;
}
get logCount() {
return this._logCount;
}
set logCount(count) {
this._logCount = count;
}
feedForward(inputArray) {
// convert input array to a matrix
this.inputs = Matrix.convertFromArray(inputArray);
// find the hidden values and apply the activation function
this.hidden = Matrix.dot(this.inputs, this.weights0);
this.hidden = Matrix.add(this.hidden, this.bias0); // apply bias
this.hidden = Matrix.map(this.hidden, x => sigmoid(x));
// find the output values and apply the activation function
let outputs = Matrix.dot(this.hidden, this.weights1);
outputs = Matrix.add(outputs, this.bias1); // apply bias
outputs = Matrix.map(outputs, x => sigmoid(x));
return outputs;
}
train(inputArray, targetArray) {
// feed the input data through the network
let outputs = this.feedForward(inputArray);
// calculate the output errors (target - output)
let targets = Matrix.convertFromArray(targetArray);
let outputErrors = Matrix.subtract(targets, outputs);
// error logging
if (LOG_ON) {
if (this.logCount == LOG_FREQ) {
console.log("error = " + outputErrors.data[0][0]);
}
this.logCount--;
if (this.logCount == 0) {
this.logCount = LOG_FREQ;
}
}
// calculate the deltas (errors * derivitive of the output)
let outputDerivs = Matrix.map(outputs, x => sigmoid(x, true));
let outputDeltas = Matrix.multiply(outputErrors, outputDerivs);
// calculate hidden layer errors (deltas "dot" transpose of weights1)
let weights1T = Matrix.transpose(this.weights1);
let hiddenErrors = Matrix.dot(outputDeltas, weights1T);
// calculate the hidden deltas (errors * derivitive of hidden)
let hiddenDerivs = Matrix.map(this.hidden, x => sigmoid(x, true));
let hiddenDeltas = Matrix.multiply(hiddenErrors, hiddenDerivs);
// update the weights (add transpose of layers "dot" deltas)
let hiddenT = Matrix.transpose(this.hidden);
this.weights1 = Matrix.add(this.weights1, Matrix.dot(hiddenT, outputDeltas));
let inputsT = Matrix.transpose(this.inputs);
this.weights0 = Matrix.add(this.weights0, Matrix.dot(inputsT, hiddenDeltas));
// update bias
this.bias1 = Matrix.add(this.bias1, outputDeltas);
this.bias0 = Matrix.add(this.bias0, hiddenDeltas);
}
}
function sigmoid(x, deriv = false) {
if (deriv) {
return x * (1 - x); // where x = sigmoid(x)
}
return 1 / (1 + Math.exp(-x));
}
/***********************
MATRIX FUNCTIONS
***********************/
class Matrix {
constructor(rows, cols, data = []) {
this._rows = rows;
this._cols = cols;
this._data = data;
// initialise with zeroes if no data provided
if (data == null || data.length == 0) {
this._data = [];
for (let i = 0; i < this._rows; i++) {
this._data[i] = [];
for (let j = 0; j < this._cols; j++) {
this._data[i][j] = 0;
}
}
} else {
// check data integrity
if (data.length != rows || data[0].length != cols) {
throw new Error("Incorrect data dimensions!");
}
}
}
get rows() {
return this._rows;
}
get cols() {
return this._cols;
}
get data() {
return this._data;
}
// add two matrices
static add(m0, m1) {
Matrix.checkDimensions(m0, m1);
let m = new Matrix(m0.rows, m0.cols);
for (let i = 0; i < m.rows; i++) {
for (let j = 0; j < m.cols; j++) {
m.data[i][j] = m0.data[i][j] + m1.data[i][j];
}
}
return m;
}
// check matrices have the same dimensions
static checkDimensions(m0, m1) {
if (m0.rows != m1.rows || m0.cols != m1.cols) {
throw new Error("Matrices are of different dimensions!");
}
}
// convert array to a one-rowed matrix
static convertFromArray(arr) {
return new Matrix(1, arr.length, [arr]);
}
// dot product of two matrices
static dot(m0, m1) {
if (m0.cols != m1.rows) {
throw new Error("Matrices are not \"dot\" compatible!");
}
let m = new Matrix(m0.rows, m1.cols);
for (let i = 0; i < m.rows; i++) {
for (let j = 0; j < m.cols; j++) {
let sum = 0;
for (let k = 0; k < m0.cols; k++) {
sum += m0.data[i][k] * m1.data[k][j];
}
m.data[i][j] = sum;
}
}
return m;
}
// apply a function to each cell of the given matrix
static map(m0, mFunction) {
let m = new Matrix(m0.rows, m0.cols);
for (let i = 0; i < m.rows; i++) {
for (let j = 0; j < m.cols; j++) {
m.data[i][j] = mFunction(m0.data[i][j]);
}
}
return m;
}
// multiply two matrices (not the dot product)
static multiply(m0, m1) {
Matrix.checkDimensions(m0, m1);
let m = new Matrix(m0.rows, m0.cols);
for (let i = 0; i < m.rows; i++) {
for (let j = 0; j < m.cols; j++) {
m.data[i][j] = m0.data[i][j] * m1.data[i][j];
}
}
return m;
}
// subtract two matrices
static subtract(m0, m1) {
Matrix.checkDimensions(m0, m1);
let m = new Matrix(m0.rows, m0.cols);
for (let i = 0; i < m.rows; i++) {
for (let j = 0; j < m.cols; j++) {
m.data[i][j] = m0.data[i][j] - m1.data[i][j];
}
}
return m;
}
// find the transpose of the given matrix
static transpose(m0) {
let m = new Matrix(m0.cols, m0.rows);
for (let i = 0; i < m0.rows; i++) {
for (let j = 0; j < m0.cols; j++) {
m.data[j][i] = m0.data[i][j];
}
}
return m;
}
// apply random weights between -1 and 1
randomWeights() {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
this.data[i][j] = Math.random() * 2 - 1;
}
}
}
}