-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron.js
84 lines (73 loc) · 2.45 KB
/
neuron.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
class Neuron {
constructor(numFeatures, activationFunctionName) {
this.activationFunction = this.activationFunctionFromName(activationFunctionName);
this.numTrainSteps = 1;
this.initializeWeights(numFeatures + 1);
}
activationFunctionFromName(name) {
switch (name) {
case "threshold": return this.thresholdActivationFunction;
case "sigmoid": return this.sigmoidActivationFunction;
case "rectifier": return this.rectifierActivationFunction;
case "hyperbolic": return this.hyperbolicTangentActivationFunction;
case "sign": return this.signActivationFunction;
default: throw Error("unknow activation function " + name);
}
}
reset(){
this.initializeWeights(this.weights.length + 1);
}
guess(data) {
let weightedSum = 0;
const features = Object.keys(data.features);
for (let i = 0; i < features.length; i++) {
weightedSum += data.features[features[i]] * this.weights[i]
}
weightedSum += this.weights[features.length];
let guess = this.activationFunction(weightedSum);
data.guess = guess;
return guess;
}
train(data, learningRate) {
let error = 1000;
for (let i = 0; i < this.numTrainSteps && error != 0; i++) {
this.guess(data);
error = data.actual - data.guess;
const features = Object.keys(data.features);
for (let i = 0; i < this.weights.length; i++) {
this.weights[i] += error * (data.features[features[i]] || 1) * learningRate
}
if (error != 0 && data.features[features[i]] != 0 && learningRate != 0) {
}
}
}
initializeWeights(num) {
this.weights = [];
for (let i = 0; i < num; i++) {
this.weights.push(Math.random() * 2 - 1)
}
}
signActivationFunction(x) {
if (x < 0) {
return -1;
} else {
return 1;
}
}
thresholdActivationFunction(x) {
if (x < 0) {
return 0;
} else {
return 1;
}
}
sigmoidActivationFunction(x) {
return 1 / (1 + Math.pow(Math.E, x));
}
rectifierActivationFunction(x) {
return Math.max(x, 0);
}
hyperbolicTangentActivationFunction(x) {
return ((1 - Math.pow(Math.E, -2 * x)) / (1 + Math.pow(Math.E, -2 * x)));
}
}