-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuralNetwork.js
104 lines (104 loc) · 4.06 KB
/
neuralNetwork.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
let input = [20, 10, 40, 5];
let hiddenWeight;
let weight;
let hiddenNeurons = [0, 0, 0];
let outputExpected = [4, 4];
const learningRate = 0.50;
let summation = [];
let output = [];
let normalizedInput;
let normalizedOutput;
let denormalizedInput;
let denormalizedOutput;
let denormalizedExpectedOutput;
let message;
let epochCount = 1;
const epoch = 100;
let sigmoid = [];
let outputsEquals;
let activationFunction;
function measureArrayOfWeights(){
hiddenWeight = input.map((inputs)=>{
return hiddenNeurons.map((arrayOfHiddenWeights) => Math.random())
})
weight = hiddenNeurons.map((hiddenNeurons) =>{
return outputExpected.map((arrayOfWeights) => Math.random())
})
}
function foreword(weight, hiddenWeight){
hiddenLayer(hiddenWeight);
summation = hiddenNeurons.reduce((hiddenNeurons, neuronIndex) =>{
return outputExpected.map((hiddenNeurons, weightIndex)=>{return +(hiddenNeurons * weight[neuronIndex][weightIndex])}
)});
}
function hiddenLayer(hiddenWeight){
hiddenNeurons = normalizedInput.reduce((normalizedInput, neuronIndex) =>{
return hiddenNeurons.map((normalizedInput, weightIndex)=>{
return +(normalizedInput * hiddenWeight[neuronIndex][weightIndex])
}
)});
}
function backpropagation(hiddenWeights, weights, input, hiddenNeurons, learningRate){
hiddenWeight = hiddenWeight.map((hiddenWeight, externalIndex) =>{return hiddenWeight.map((hiddenWeight, internalIndex) => hiddenWeights[externalIndex][internalIndex] * learningRate * input[externalIndex])});
weight = weight.map((weight, externalIndex) =>{ return weight.map((weight, internalIndex) => weights[externalIndex][internalIndex] * learningRate * hiddenNeurons[externalIndex])});
}
function normalizeData(input, outputExpected){
normalizedInput = input.map((inputNormalization, index) =>{
if(input[index]> 0){
return Math.round(input[index] / Math.max(...input));
}else{
return 0;
}
});
normalizedOutput = outputExpected.map((outputNormalization, index) => outputExpected[index] / outputExpected[index]);
}
function denormalizeData(input, normalizedInput,outputExpected, output){
denormalizedInput = input.map((inputDenormalization, index) => input[index] * normalizedInput[index]);
denormalizedOutput = outputExpected.map((outputDenormalization, index) => outputExpected[index] * output[index]);
denormalizedExpectedOutput = output.map((expectedOutputDenormalization, index) => outputExpected[index] * output[index]);
}
activationFunction = {
step: function(summations){
output = summation.map((summation, outputs, index) => outputs = summations[index] <= 0 ? outputs = -1: outputs = 1);
},
sigmoid: function(summations){
sigmoid = outputExpected.map((sigmoid, index) => 1 /( 1 + Math.exp(summation[index])));
this.derivedFromSigmoid(sigmoid, summation);
},
derivedFromSigmoid: function(sigmoid, summation){
output = sigmoid.map((derived, index) => parseInt(1 - sigmoid[index] * summation[index]));
return parseInt(output);
}
}
function messages(){
console.log(`Epoch ${epochCount}\nOutput:${denormalizedExpectedOutput}\nOutput expected:${outputExpected}`)
}
let outputComparison = (output, normalizedOutput)=>{
outputsEquals = output.every((outputs, index)=>outputs == normalizedOutput[index]);
return outputsEquals;
}
function predict(inputs){
normalizeData(inputs, outputExpected);
foreword(weight, hiddenWeight);
activationFunction.sigmoid(summation);
denormalizeData(inputs, normalizedInput, outputExpected, output);
console.log(`===================\nInput:${inputs}\nPredict:${denormalizedOutput}`);
}
function training(){
normalizeData(input, outputExpected);
measureArrayOfWeights();
while(epochCount < epoch){
foreword(weight, hiddenWeight);
backpropagation(hiddenWeight,weight, normalizedInput, hiddenNeurons, learningRate);
activationFunction.sigmoid(summation);
denormalizeData(input, normalizedInput, outputExpected, output);
outputComparison(output, normalizedOutput);
messages();
epochCount += 1
if(outputsEquals == true){
break;
}
}
}
training();
predict([20, 10, 40, 5]);