-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.js
233 lines (223 loc) · 6.44 KB
/
Network.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
import { Connection, RELUprime } from "./connection.js";
/**
*
* Basiacally a collection of connections and some helpers
*
*
*/
export class Network {
constructor() {
this.connections = [];
}
addConnection(conx) {
this.connections.push(conx);
}
/**
*
* @param {Array} arr Input
* @param {*} bias
*/
forwardPropogate(arr, bias = 0) {
let activations = [arr];
let zs = [arr];
for (var i = 0; i < this.connections.length; i++) {
let conx = this.connections[i];
let a = conx.forwardPropogate(activations[i], bias);
activations.push(a.activations);
zs.push(a.z);
for (var n of a.activations) {
if (isNaN(n)) {
throw "Nan encountered";
}
}
for (var n of a.z) {
if (isNaN(n)) {
throw "Nan encountered";
}
}
}
return { activations, zs };
}
duplicate() {
let res = new Network();
for (var c of this.connections) {
let size = c.size();
let con = new Connection(size[0], size[1]);
res.addConnection(con);
}
return res;
}
backPropogation(batch, learningRate = 0.0000001, biasNeurons = true) {
let acc = this.duplicate();
for (var b of batch) {
let fwd = this.forwardPropogate(b.input);
let gradient = this.calculateGradient(
b.output,
fwd.activations,
fwd.zs,
this.connections //this.getWeights()
);
for (var i = 0; i < acc.connections.length; i++) {
let r = gradient.dCdWs[i + 1];
// scale value
let r2 = r.map(a => {
return numeric.mulVS(a, (1 / batch.length) * learningRate);
});
var s = [];
let q = acc.connections[i].weights;
for (var j = 0; j < r2.length; j++) {
let tmp = numeric.addVV(r2[j], q[j]);
s.push(tmp);
}
acc.connections[i].weights = s;
}
}
////
// finally add weights plus accumulation of gradients
// It turns out numeric cant add matrices 😕
//
let result = this.duplicate();
for (var j = 0; j < result.connections.length; j++) {
let newWeights = [];
for (var i = 0; i < result.connections[j].weights.length; i++) {
let tmp = numeric.addVV(
acc.connections[j].weights[i],
this.connections[j].weights[i]
);
newWeights.push(tmp);
}
result.connections[j].weights = newWeights;
}
if (biasNeurons) result.maintainBiasNeurons();
//console.log(result, this);
this.connections = result.connections;
}
getWeights() {
return this.connections.map(a => {
return a.weights;
});
}
/**
*
* Calculate the gradient for every weight
*
* @param {Array} expectedOutput
* @param {Array} activations
* @param {Array} zs
* @param {Array} weights
* @param {Array} bias
*/
calculateGradient(expectedOutput, activations, zs, weights) {
let dcdb = [];
let dCdWs = [];
const opErrors = [];
let weights2 = [[]].concat(weights); //Change indices. Weights have a length of one less than the activations becuase i included the input values
//
// calculate final cost
const actLast = activations[activations.length - 1];
const C = new Array(actLast.length);
const Cprime = new Array(actLast.length);
for (let i = 0; i < actLast.length; i++) {
C[i] = (1 / 2) * (expectedOutput[i] - actLast[i]) ** 2;
Cprime[i] = expectedOutput[i] - actLast[i];
}
let reluP = RELUprime(zs[zs.length - 1]);
const opErrL = hadamardProduct(Cprime, reluP);
// record final cost
opErrors[zs.length - 1] = opErrL;
//
// Compute intermediate change in cost in terms of weights
//
for (let i = activations.length - 1; i >= 1; i--) {
if (i < activations.length - 1) {
let wiPlus1 = weights2[i + 1]; // this is i+1
let opErriPlus1 = opErrors[i + 1];
let rz = RELUprime(zs[i]);
let tmp = numeric.dot(wiPlus1.weights, opErriPlus1);
let opErri = hadamardProduct(tmp, rz);
opErrors[i] = opErri;
}
dcdb[i] = opErrors[i];
let dcdw_jk = new Connection(
activations[i - 1].length,
activations[i].length
);
for (var k = 0; k < activations[i].length; k++) {
for (var j = 0; j < activations[i - 1].length; j++) {
let delta = activations[i - 1][j] * opErrors[i][k] + Math.random();
dcdw_jk.connect(j, k, delta);
}
}
dCdWs[i] = dcdw_jk.weights;
}
return { dcdb, dCdWs, error: opErrL };
}
maintainBiasNeurons(bias = 1) {
for (var n = 0; n < this.connections.length - 1; n++) {
let c = this.connections[n];
for (var i = 1; i < c.weights.length; i++) {
c.connect(i, 0, 0);
}
//for (var j = 0; j < c.weights[0].length; j++) {
//}
}
for (var c2 of this.connections) {
c2.connect(0, 0, bias);
}
this.connections[this.connections.length - 1].connect(0, 0, 0);
}
/**
* Draw. returns a Canvas
* @param {*} w
* @param {*} h
*/
draw(w = 150, h = 50) {
var maxNeurons = 0;
this.connections.forEach(conx => {
maxNeurons = Math.max(maxNeurons, conx.weights.length);
});
var can = document.createElement("canvas");
can.width = w * this.connections.length;
can.height = h * maxNeurons;
let ctx = can.getContext("2d");
ctx.strokeStyle = "#000";
for (var x = 0; x < this.connections.length; x++) {
let conx = this.connections[x];
for (var i = 0; i < conx.weights.length; i++) {
let row = conx.weights[i];
for (var j = 0; j < row.length; j++) {
// if (Math.abs(row[j]) > 0.001) {
ctx.beginPath();
if (Math.abs(row[j] < 0.001)) ctx.strokeStyle = "#aaa";
else if (row[j] == 1) ctx.strokeStyle = "#00f";
else if (row[j] < 0) ctx.strokeStyle = "#092";
else if (row[j] > 0) ctx.strokeStyle = "#900";
let xl = x * w;
let xr = xl + w;
let yl = i * h;
let yr = j * h;
ctx.moveTo(xl, yl);
ctx.lineTo(xr, yr);
ctx.fillRect(xl, yl, 4, 4);
ctx.fillRect(xr, yr, 4, 4);
ctx.stroke();
ctx.closePath();
}
// }
}
}
return can;
}
}
/**
* Helper function
* @param {Array} a
* @param {Array} b
*/
export function hadamardProduct(a, b) {
const y = new Array(a.length);
for (var i = 0; i < a.length; i++) {
y[i] = a[i] * b[i];
}
return y;
}