-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn.js
161 lines (121 loc) · 4.42 KB
/
nn.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
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 NeuralNetwork {
constructor(NumInput, NumHidden, NumOutput) {
if (NumInput instanceof NeuralNetwork) {
let oldNode = NumInput;
this.input = oldNode.input;
this.hidden = oldNode.hidden;
this.output = oldNode.output;
this.weight_input_hidden = oldNode.weight_input_hidden.copy();
this.weight_hidden_output = oldNode.weight_hidden_output.copy();
this.bias_hidden = oldNode.bias_hidden.copy();
this.bias_output = oldNode.bias_output.copy();
} else {
this.input = NumInput;
this.hidden = NumHidden;
this.output = NumOutput;
this.weight_input_hidden = new Matrix(this.hidden, this.input);
this.weight_hidden_output = new Matrix(this.output, this.hidden);
this.weight_input_hidden.randomize();
this.weight_hidden_output.randomize();
this.bias_hidden = new Matrix(this.hidden, 1);
this.bias_output = new Matrix(this.output, 1);
this.bias_hidden.randomize();
this.bias_output.randomize();
}
this.setLearningRate();
this.setActivationFunction();
}
feedforward(input) {
let input_ = Matrix.array_to_matrix(input);
let hidden_layer = this.weight_input_hidden.matrixProduct(input_);
hidden_layer.elementwiseAdd(this.bias_hidden);
hidden_layer.map(this.activation_function.func);
let output_layer = this.weight_hidden_output.matrixProduct(hidden_layer);
output_layer.elementwiseAdd(this.bias_hidden);
output_layer.map(this.activation_function.func);
return output_layer;
}
setLearningRate() {
this.lr = 0.1;
}
setActivationFunction(func = sigmoid) {
this.activation_function = func;
}
debugWeights() {
this.weight_input_hidden.print();
this.weight_hidden_output.print();
}
train(input, answer) {
let input_ = Matrix.array_to_matrix(input);
let hidden_layer = this.weight_input_hidden.matrixProduct(input_);
hidden_layer.elementwiseAdd(this.bias_hidden);
hidden_layer.map(this.activation_function.func);
let output_layer = this.weight_hidden_output.matrixProduct(hidden_layer);
output_layer.elementwiseAdd(this.bias_hidden);
output_layer.map(this.activation_function.func);
answer = Matrix.array_to_matrix(answer);
let output_error = Matrix.elementwiseSubtract(answer, output_layer);
let hidden_t = this.weight_hidden_output.transpose();
let hidden_error = hidden_t.matrixProduct(output_error);
let gradient_1 = Matrix.map(output_layer, this.activation_function.dfunc);
gradient_1 = gradient_1.elementwiseMultiply(output_error);
gradient_1 = gradient_1.scalarMultiply(this.lr);
let h_t = hidden_layer.transpose();
let Weight_delta_1 = Matrix.matrixProduct(gradient_1, h_t);
this.weight_hidden_output.elementwiseAdd(Weight_delta_1);
this.bias_output.elementwiseAdd(gradient_1);
let gradient_2 = Matrix.map(hidden_layer, this.activation_function.dfunc);
gradient_2 = gradient_2.elementwiseMultiply(hidden_error);
gradient_2 = gradient_2.scalarMultiply(this.lr);
let i_t = input_.transpose();
let Weight_delta_2 = Matrix.matrixProduct(gradient_2, i_t);
this.weight_input_hidden.elementwiseAdd(Weight_delta_2);
this.bias_hidden.elementwiseAdd(gradient_2);
}
serialize() {
return JSON.stringify(this);
}
static deserialize(data) {
if (typeof data == 'string') {
data = JSON.parse(data);
}
let nn = new NeuralNetwork(data.input, data.hidden, data.output);
nn.weight_input_hidden = Matrix.deserialize(data.weight_input_hidden);
nn.weight_hidden_output = Matrix.deserialize(data.weight_hidden_output);
nn.bias_hidden = Matrix.deserialize(data.bias_hidden);
nn.bias_output = Matrix.deserialize(data.bias_output);
nn.lr = data.lr;
return nn;
}
copy() {
return new NeuralNetwork(this);
}
// Accept an arbitrary function for mutation
mutate(rate) {
function mutate(val) {
if (Math.random() < rate) {
return val + randomGaussian(0, 0.1);
} else {
return val;
}
}
this.weight_input_hidden.map(mutate);
this.weight_hidden_output.map(mutate);
this.bias_hidden.map(mutate);
this.bias_output.map(mutate);
}
}