-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode.js
83 lines (68 loc) · 1.46 KB
/
node.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
class Node {
constructor(type, id) {
this.type = type;
this.id = id;
this.inputs = [];
this.outputConnections = [];
this.activation = Node.activations.Tanh;
this.sum = 0;
}
activate(x) {
switch (this.activation) {
case Node.activations.ReLU:
return max(x, 0);
break;
case Node.activations.ReLU6:
return min(max(x, 0), 1);
break;
case Node.activations.LeakyReLU:
return max(x, 0.1 * x);
break;
case Node.activations.Sigmoid:
return 1 / (1 + exp(-x));
break;
case Node.activations.Tanh:
return Math.tanh(x);
break;
case Node.activations.Gaussian:
const sigma = 0.4;
const mu = 0;
return (1 / (sigma * sqrt(TAU))) * exp(pow(-(1 / 2) * ((x - mu) / sigma), 2));
break;
default:
return x;
}
}
addInput(input) {
this.inputs.push(input);
}
setInputs(inputs) {
this.inputs = [...inputs];
}
engage() {
this.sum = 0;
for (let i = 0; i < this.inputs.length; i++) {
this.sum += this.inputs[i];
}
this.sum = this.activate(this.sum);
for (let c of this.outputConnections) {
c.feedforward(this.sum);
}
}
copy() {
return new Node(this.type, this.id);
}
}
Node.types = {
Input: "I",
Hidden: "H",
Output: "O",
};
Node.activations = {
ReLU: "R",
ReLU6: "R6",
LeakyReLU: "LR",
Sigmoid: "S",
Tanh: "T",
Gaussian: "G",
};