-
Notifications
You must be signed in to change notification settings - Fork 1
/
nn.js
56 lines (39 loc) · 1.34 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
function sigmoid(a)
{
return 1/(1+Math.pow(Math.E, -a));
}
class NeuralNetwork{
constructor(input, hidden, output){
this.input_nodes=input;
this.hidden_nodes=hidden;
this.output_nodes=output;
this.weights_ih=new Matrix(this.hidden_nodes, this.input_nodes);
this.weights_ho=new Matrix(this.output_nodes, this.hidden_nodes);
this.weights_ih.randomize(2, 1);
this.weights_ho.randomize(2, 1);
this.bias_h=new Matrix(this.hidden_nodes, 1);
this.bias_o=new Matrix(this.output_nodes, 1);
this.bias_h.randomize(2, 1);
this.bias_o.randomize(2, 1);
}
feedforward(input_array)
{
let inputs = Matrix.fromArray(input_array);
let hidden=Matrix.multiply(this.weights_ih, inputs);
// this.weights_ho.print();
hidden.add(this.bias_h);
hidden.map(sigmoid);
let output=Matrix.multiply(this.weights_ho, hidden);
output.add(this.bias_o);
output.map(sigmoid);
return output.toArray();
}
train(inputs, targets)
{
let outputs=this.feedforward(inputs);
let error=Matix.subtract(targets, outputs);
outputs=Matrix.fromArray(outputs);
targets=Matrix.fromArray(targets);
let result=Matrix.subtract(targets, outputs);
}
}