/
NeuralNetwork.js
133 lines (100 loc) · 3.74 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
class NN {
constructor(inputs, hiddens, outputs, noH) {
this.total = noH + 2;
this.weights = [];
this.biases = [];
this.lr = 0.1;
this.weights[0] = new Matrix(hiddens, inputs);
this.weights[this.total - 2] = new Matrix(outputs, hiddens);
this.biases[0] = new Matrix(hiddens, 1);
this.biases[this.total - 2] = new Matrix(outputs, 1);
for (let i = 1; i < this.total-2; i++) {
this.weights[i] = new Matrix(hiddens, hiddens);
this.biases[i] = new Matrix(hiddens, 1);
}
//randomization not good :(
for (let i = 0; i < this.total-1; i++) {
this.weights[i].randomize();
this.biases[i].randomize();
}
//glorot and bengio
//make initialization better
}
predict(ip) {
let inputs = Matrix.fromArray(ip);
let weightedSums = [];
weightedSums[0] = Matrix.copyMatrix(inputs);
for (let i = 1; i < this.total; i++) {
weightedSums[i] = Matrix.matrixMult(this.weights[i - 1], weightedSums[i - 1]);
weightedSums[i] = Matrix.matrixSum(weightedSums[i], this.biases[i - 1]);
weightedSums[i] = Matrix.matrixMap(weightedSums[i], sigmoid);
}
let op = Matrix.toArray(weightedSums[this.total - 1]);
return op;
}
train(ip, t){
let inputs = Matrix.fromArray(ip);
let weightedSums = [];
weightedSums[0] = Matrix.copyMatrix(inputs);
for (let i = 1; i < this.total; i++) {
weightedSums[i] = Matrix.matrixMult(this.weights[i - 1], weightedSums[i - 1]);
weightedSums[i] = Matrix.matrixSum(weightedSums[i], this.biases[i - 1]);
weightedSums[i] = Matrix.matrixMap(weightedSums[i], sigmoid);
//add normalization here
//batch normalization with two trainable variables
}
let output = Matrix.copyMatrix(weightedSums[this.total - 1]);
let target = Matrix.fromArray(t);
let errors = [];
//better loss function than target - output
//cross entropy or square loss function.
//use softmax for loss function
errors[this.total - 2] = Matrix.matrixSub(target, output);
for(let i = this.total - 3;i >= 0;i--){
let trans = Matrix.transpose(this.weights[i+1]);
errors[i] = Matrix.matrixMult(trans, errors[i+1]);
}
let DSigWeightedSums = [];
for(let i = 0; i < weightedSums.length;i++){
DSigWeightedSums[i] = Matrix.matrixMap(weightedSums[i], DSigmoid);
}
let gradients = [];
let weights_deltas = [];
//momentum?
//changing lr according to the change in lr?
for(let i = 0;i < this.total - 1;i++){
gradients[i] = Matrix.hadamardMult(DSigWeightedSums[i+1], errors[i]);
gradients[i].scalarMult(this.lr);
}
for(let i = 0;i < this.total - 1;i++){
let trans = Matrix.transpose(weightedSums[i]);
weights_deltas[i] = Matrix.matrixMult(gradients[i], trans);
}
for(let i = 0;i < this.total - 1;i++){
this.weights[i].matrixSum(weights_deltas[i]);
this.biases[i].matrixSum(gradients[i]);
}
//adam
//rmsprop, adagrad, making lr for every weight rather than one general for everything
//kingma dp, a method for stochastic optimization similar to momentum
//bias correction
//regularization
//l2 regularization
//dropout
}
mutateNN(mr){
for(let i = 0;i < this.weights.length;i++){
this.weights[i].mutate(mr);
this.biases[i].mutate(mr);
}
}
static copyNN(toCopy){
let tmp = new NN();
tmp.total = toCopy.total;
for(let i = 0;i < toCopy.weights.length;i++){
tmp.weights[i] = Matrix.copyMatrix(toCopy.weights[i]);
tmp.biases[i] = Matrix.copyMatrix(toCopy.biases[i]);
}
return tmp;
}
}