-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn.js
More file actions
179 lines (156 loc) · 6.89 KB
/
nn.js
File metadata and controls
179 lines (156 loc) · 6.89 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
function relu(x) {
return Math.max(0, x);
}
function sigmoid(x) {
return 1 / (1 + Math.exp(-x));
}
class NeuralNetwork {
constructor(inputNodes, hiddenNodes, outputNodes) {
if (inputNodes instanceof NeuralNetwork) {
let a = inputNodes;
this.inputNodes = a.inputNodes;
this.hiddenNodes = a.hiddenNodes;
this.outputNodes = a.outputNodes;
this.weights_ih = a.weights_ih.copy();
this.weights_hh = a.weights_hh.copy();
this.weights_ho = a.weights_ho.copy();
this.bias_h = a.bias_h.copy();
this.bias_h2 = a.bias_h2.copy();
this.bias_o = a.bias_o.copy();
} else {
this.inputNodes = inputNodes;
this.hiddenNodes = hiddenNodes;
this.outputNodes = outputNodes;
this.weights_ih = new Matrix(this.hiddenNodes, this.inputNodes);
this.weights_hh = new Matrix(this.hiddenNodes, this.hiddenNodes);
this.weights_ho = new Matrix(this.outputNodes, this.hiddenNodes);
this.weights_ih.randomize();
this.weights_hh.randomize();
this.weights_ho.randomize();
this.bias_h = new Matrix(this.hiddenNodes, 1);
this.bias_h2 = new Matrix(this.hiddenNodes, 1);
this.bias_o = new Matrix(this.outputNodes, 1);
this.bias_h.randomize();
this.bias_h2.randomize();
this.bias_o.randomize();
}
}
predict(inputArray) {
let inputs = Matrix.fromArray(inputArray);
this.lastInputs = inputs.toArray();
let hidden = Matrix.multiply(this.weights_ih, inputs);
hidden.add(this.bias_h);
hidden.map(relu);
this.lastHidden = hidden.toArray();
let hidden2 = Matrix.multiply(this.weights_hh, hidden);
hidden2.add(this.bias_h2);
hidden2.map(relu);
this.lastHidden2 = hidden2.toArray();
let output = Matrix.multiply(this.weights_ho, hidden2);
output.add(this.bias_o);
output.map(sigmoid);
this.lastOutputs = output.toArray();
return this.lastOutputs;
}
copy() {
return new NeuralNetwork(this);
}
mutate(rate) {
function mutateFunc(val) {
if (Math.random() < rate) {
// Add a random Gaussian value (approximate)
let r = (Math.random() * 2 - 1) + (Math.random() * 2 - 1) + (Math.random() * 2 - 1);
// Keep weights bounded somewhat
return val + r * 0.5;
} else {
return val;
}
}
this.weights_ih.map(mutateFunc);
this.weights_hh.map(mutateFunc);
this.weights_ho.map(mutateFunc);
this.bias_h.map(mutateFunc);
this.bias_h2.map(mutateFunc);
this.bias_o.map(mutateFunc);
}
crossover(partner, type = 'weight') {
let child = new NeuralNetwork(this.inputNodes, this.hiddenNodes, this.outputNodes);
if (type === 'neuron') {
// Crossover weights_ih and bias_h (hidden layer 1)
for (let i = 0; i < child.weights_ih.rows; i++) {
let useParent1 = Math.random() < 0.5;
for (let j = 0; j < child.weights_ih.cols; j++) {
child.weights_ih.data[i][j] = useParent1 ? this.weights_ih.data[i][j] : partner.weights_ih.data[i][j];
}
child.bias_h.data[i][0] = useParent1 ? this.bias_h.data[i][0] : partner.bias_h.data[i][0];
}
// Crossover weights_hh and bias_h2 (hidden layer 2)
for (let i = 0; i < child.weights_hh.rows; i++) {
let useParent1 = Math.random() < 0.5;
for (let j = 0; j < child.weights_hh.cols; j++) {
child.weights_hh.data[i][j] = useParent1 ? this.weights_hh.data[i][j] : partner.weights_hh.data[i][j];
}
child.bias_h2.data[i][0] = useParent1 ? this.bias_h2.data[i][0] : partner.bias_h2.data[i][0];
}
// Crossover weights_ho and bias_o (output layer)
for (let i = 0; i < child.weights_ho.rows; i++) {
let useParent1 = Math.random() < 0.5;
for (let j = 0; j < child.weights_ho.cols; j++) {
child.weights_ho.data[i][j] = useParent1 ? this.weights_ho.data[i][j] : partner.weights_ho.data[i][j];
}
child.bias_o.data[i][0] = useParent1 ? this.bias_o.data[i][0] : partner.bias_o.data[i][0];
}
} else {
// Crossover weights_ih
for (let i = 0; i < child.weights_ih.rows; i++) {
for (let j = 0; j < child.weights_ih.cols; j++) {
if (Math.random() < 0.5) {
child.weights_ih.data[i][j] = this.weights_ih.data[i][j];
} else {
child.weights_ih.data[i][j] = partner.weights_ih.data[i][j];
}
}
}
// Crossover weights_hh
for (let i = 0; i < child.weights_hh.rows; i++) {
for (let j = 0; j < child.weights_hh.cols; j++) {
if (Math.random() < 0.5) {
child.weights_hh.data[i][j] = this.weights_hh.data[i][j];
} else {
child.weights_hh.data[i][j] = partner.weights_hh.data[i][j];
}
}
}
// Crossover weights_ho
for (let i = 0; i < child.weights_ho.rows; i++) {
for (let j = 0; j < child.weights_ho.cols; j++) {
if (Math.random() < 0.5) {
child.weights_ho.data[i][j] = this.weights_ho.data[i][j];
} else {
child.weights_ho.data[i][j] = partner.weights_ho.data[i][j];
}
}
}
// Crossover biases
for (let i = 0; i < child.bias_h.rows; i++) {
for (let j = 0; j < child.bias_h.cols; j++) {
if (Math.random() < 0.5) child.bias_h.data[i][j] = this.bias_h.data[i][j];
else child.bias_h.data[i][j] = partner.bias_h.data[i][j];
}
}
for (let i = 0; i < child.bias_h2.rows; i++) {
for (let j = 0; j < child.bias_h2.cols; j++) {
if (Math.random() < 0.5) child.bias_h2.data[i][j] = this.bias_h2.data[i][j];
else child.bias_h2.data[i][j] = partner.bias_h2.data[i][j];
}
}
for (let i = 0; i < child.bias_o.rows; i++) {
for (let j = 0; j < child.bias_o.cols; j++) {
if (Math.random() < 0.5) child.bias_o.data[i][j] = this.bias_o.data[i][j];
else child.bias_o.data[i][j] = partner.bias_o.data[i][j];
}
}
}
return child;
}
}