-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnnModel.js
More file actions
executable file
·148 lines (116 loc) · 4.2 KB
/
nnModel.js
File metadata and controls
executable file
·148 lines (116 loc) · 4.2 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
class ActivationFunction {
constructor(func, dfunc) {
this.func = func;
this.dfunc = dfunc;
}
}
let sigmoid = new ActivationFunction(
x => 1 / (1 + Math.exp(-x)),
y => y * (1 - y)
);
let tanh = new ActivationFunction(
x => Math.tanh(x),
y => 1 - (y * y)
);
class NeuralNetworkModel {
constructor(inpNum, hidNum, outNum) {
if (inpNum instanceof NeuralNetworkModel) {
let a = inpNum;
this.inpNum = a.inpNum;
this.hidNum = a.hidNum;
this.outNum = a.outNum;
this.ihWeights = a.ihWeights.copy();
this.hoWeights = a.hoWeights.copy();
this.hBias = a.hBias.copy();
this.oBias = a.oBias.copy();
} else {
this.inpNum = inpNum;
this.hidNum = hidNum;
this.outNum = outNum;
//presta atenção nessa linha e coluna aqui
this.ihWeights = new Matrix(this.hidNum, this.inpNum);
this.hoWeights = new Matrix(this.outNum, this.hidNum);
//
this.ihWeights.randomMatrix();
this.hoWeights.randomMatrix();
this.hBias = new Matrix(this.hidNum, 1);
this.oBias = new Matrix(this.outNum, 1);
this.hBias.randomMatrix();
this.oBias.randomMatrix();
}
this.setLearningRate();
this.setActivationFunction();
}
feedForward(inputArray) {
let input = Matrix.transfArrayM(inputArray);
let hidden = Matrix.mult(this.ihWeights, input);
hidden.add(this.hBias);
hidden.addFunc(this.activation_function.func);
let output = Matrix.mult(this.hoWeights, hidden);
output.add(this.oBias);
output.addFunc(this.activation_function.func);
return output.transfMatrixA();
}
setLearningRate(learning_rate = 0.1) {
this.lr = learning_rate;
}
setActivationFunction(func = sigmoid) {
this.activation_function = func;
}
train(inputArray, expectedOutArray) {
///feed forward
let input = Matrix.transfArrayM(inputArray);
let hidden = Matrix.mult(this.ihWeights, input);
hidden.add(this.hBias);
hidden.addFunc(this.activation_function.func);
let output = Matrix.mult(this.hoWeights, hidden);
output.add(this.oBias);
output.addFunc(this.activation_function.func);
//
///train
let expectedOut = Matrix.transfArrayM(expectedOutArray);
///output error;
let outError = Matrix.sub(expectedOut, output);
////gradient
let gradient = Matrix.addFunc(output, this.activation_function.dfunc);
gradient.mult(outError);
gradient.mult(this.lr);
////delta
let hidT = Matrix.transp(hidden);
let hoWeightsDeltas = Matrix.mult(gradient, hidT)
//Ajustando pesos e bias
this.hoWeights.add(hoWeightsDeltas);
this.oBias.add(gradient);
///hidden error;
///usar um looping para adicionar mais layers;
///cuidado a matriz dos pesos para o backpropagation é a transposta da usada no feedForward
let hoWeightsT = Matrix.transp(this.hoWeights);
let hidError = Matrix.mult(hoWeightsT, outError);
////gradient
let hGradient = Matrix.addFunc(hidden, this.activation_function.dfunc);
hGradient.mult(hidError);
hGradient.mult(this.lr);
////delta
let inpT = Matrix.transp(input);
let ihWeightsDeltas = Matrix.mult(hGradient, inpT);
//ajustando pesos e bias
this.ihWeights.add(ihWeightsDeltas);
this.hBias.add(hGradient);
}
copy() {
return new NeuralNetworkModel(this);
}
mutate(rate) {
function mutate(val) {
if (Math.random() < rate) {
return val + randomGaussian(0, 0.1);
} else {
return val;
}
}
this.ihWeights.addFunc(mutate);
this.hoWeights.addFunc(mutate);
this.hBias.addFunc(mutate);
this.oBias.addFunc(mutate);
}
}