forked from palprabhat/gismo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuralNetwork.js
executable file
·191 lines (156 loc) · 6.45 KB
/
neuralNetwork.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
/**
* argMax function from https://gist.github.com/engelen/fbce4476c9e68c52ff7e5c2da5c24a28
* Retrieve the array key corresponding to the largest element in the array.
*
* @param {Array.<number>} array Input array
* @return {number} Index of array element with largest value
*/
class NeuralNetwork {
constructor(no_of_inputs, hidden_neurons, no_of_layers, no_of_outputs, learning_rate) {
this.no_of_inputs = no_of_inputs;
this.hidden_neurons = hidden_neurons;
this.no_of_layers = no_of_layers;
this.no_of_outputs = no_of_outputs;
this.learning_rate = learning_rate;
this.weights = [];
this.bias = [];
this.weights[0] = new Matrix(this.hidden_neurons, this.no_of_inputs);
this.bias[0] = new Matrix(this.hidden_neurons, 1);
for (let i = 1; i < no_of_layers; i++) {
this.weights[i] = new Matrix(this.hidden_neurons, this.hidden_neurons);
this.bias[i] = new Matrix(this.hidden_neurons, 1);
}
this.weights[this.no_of_layers] = new Matrix(this.no_of_outputs, this.hidden_neurons);
this.bias[this.no_of_layers] = new Matrix(this.no_of_outputs, 1);
this.__initialWeights();
}
argMax(array) {
return array.map((x, i) => [x, i]).reduce((r, a) => (a[0] > r[0] ? a : r))[1];
}
__relu(val) {
return Math.max(0, val);
}
__sigmoid(val) {
return 1 / (Math.exp(-val) + 1);
}
__sigmoidDerivative(x) {
return x * (1 - x);
}
__mutate(x, learning_rate) {
if (Math.random() < learning_rate) {
// var offset = randomGaussian() * 0.5;
var offset = Math.floor(Math.random() * 2) - 0.5;
x += offset;
return x;
} else {
return x;
}
}
__feedForward(input) {
if (input instanceof Array) {
input = Matrix.toMatrix(input);
} else if (input instanceof Matrix) {
input = input;
} else {
console.error("input must be an Array or a column Matrix");
return undefined;
}
this.hidden = [];
this.hidden[0] = Matrix.multiply(this.weights[0], input);
this.hidden[0].add(this.bias[0]);
this.hidden[0].map(this.__sigmoid); //activation function
for (let i = 1; i < this.no_of_layers; i++) {
this.hidden[i] = Matrix.multiply(this.weights[i], this.hidden[i - 1]);
this.hidden[i].add(this.bias[i]);
this.hidden[i].map(this.__sigmoid); //activation function
}
this.hidden[this.no_of_layers] = Matrix.multiply(this.weights[this.no_of_layers], this.hidden[this.no_of_layers - 1]);
this.hidden[this.no_of_layers].add(this.bias[this.no_of_layers]);
this.hidden[this.no_of_layers].map(this.__sigmoid); //activation function
return Matrix.toArray(this.hidden[this.no_of_layers]);
}
__initialWeights() {
for (let i = 0; i < this.weights.length; i++) {
this.weights[i].randomize();
this.bias[i].randomize();
}
}
__error(predicted_output, actual_output) {
predicted_output.multiply(-1);
let error = [];
for (let i = 0; i <= this.no_of_layers; i++) {
error[i] = [];
}
error[this.no_of_layers] = Matrix.add(actual_output, predicted_output);
let weightTranspose;
for (let i = this.no_of_layers - 1; i >= 0; i--) {
weightTranspose = Matrix.transpose(this.weights[i + 1]);
error[i] = Matrix.multiply(weightTranspose, error[i + 1]);
}
return error;
}
__calculateCost(error) {
let sqSum = 0;
for (let i = 0; i < error.length; i++) {
sqSum += Math.pow(error[i], 2);
}
let meanSqError = sqSum / error.length;
return meanSqError;
}
__getGradient() {
let gradient = [];
for (let i = 0; i <= this.no_of_layers; i++) {
gradient[i] = Matrix.map(this.hidden[i], this.__sigmoidDerivative);
}
return gradient;
}
predict(input) {
let output = this.__feedForward(input);
return output;
}
train(train_input, train_output) {
if (train_input instanceof Array && train_output instanceof Array) {
this.train_input = Matrix.toMatrix(train_input);
this.train_output = Matrix.toMatrix(train_output);
} else if (train_input instanceof Matrix && train_output instanceof Matrix) {
this.train_input = train_input;
this.train_output = train_output;
} else {
console.error("train_input and train_output must be Arrays or column Matrices");
return undefined;
}
let output = this.__feedForward(this.train_input);
let error = this.__error(Matrix.toMatrix(output), this.train_output);
this.mse = this.__calculateCost(Matrix.toArray(error[this.no_of_layers]));
//deltaW = lr*E*gradient*input
let gradient = this.__getGradient();
gradient[0].multiply(error[0]);
gradient[0].multiply(this.learning_rate);
let inputTranspose = Matrix.transpose(this.train_input);
let deltaWeight = Matrix.multiply(gradient[0], inputTranspose);
this.weights[0].add(deltaWeight);
this.bias[0].add(gradient[0]);
for (let i = 1; i <= this.no_of_layers; i++) {
gradient[i].multiply(error[i]);
gradient[i].multiply(this.learning_rate);
inputTranspose = Matrix.transpose(this.hidden[i - 1]);
deltaWeight = Matrix.multiply(gradient[i], inputTranspose);
this.weights[i].add(deltaWeight);
this.bias[i].add(gradient[i]);
}
}
mutate() {
for (let i = 0; i < this.weights.length; i++) {
this.weights[i].map(this.__mutate, this.learning_rate);
this.bias[i].map(this.__mutate, this.learning_rate);
}
}
copy() {
var dupNN = new NeuralNetwork(this.no_of_inputs, this.hidden_neurons, this.no_of_layers, this.no_of_outputs, this.learning_rate);
for (let i = 0; i < this.weights.length; i++) {
dupNN.weights[i] = this.weights[i].copy();
dupNN.bias[i] = this.bias[i].copy();
}
return dupNN;
}
}