-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron.js
49 lines (40 loc) · 1.13 KB
/
perceptron.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
var perceptron = function (inputs, weights, bias) {
var sum = 0;
var len = inputs.length;
for (var i = 0; i < len; i++) {
sum += inputs[i] * weights[i];
}
if ((sum + bias) >= 1) {
return 1;
}
return 0;
};
// adderPerceptron wraps perceptron with
// weights for a NAND gate.
var adderPerceptron = function (i, j) {
var bias = 3;
var weights = [-2, -2];
return perceptron([i, j], weights, bias);
};
var adder = function (inputOne, inputTwo) {
var layerOneOut = adderPerceptron(inputOne, inputTwo);
// Two hidden layers.
var layerTwoFirst = adderPerceptron(layerOneOut, inputOne);
var layerTwoSecond = adderPerceptron(layerOneOut, inputTwo);
var carryBit = adderPerceptron(layerOneOut, layerOneOut);
return {
'value': adderPerceptron(layerTwoFirst, layerTwoSecond),
'carry': carryBit
};
};
var sigmoid = function (val) {
return 1 / (1 + Math.pow(Math.E, -val));
};
var signoidPerceptron = function (inputs, weights, bias) {
var sum = 0;
var len = inputs.length;
for (var i = 0; i < len; i++) {
sum += inputs[i] * weights[i];
}
return sigmoid(sum + 1);
};