-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralLayer.js
84 lines (72 loc) · 2.13 KB
/
NeuralLayer.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 NeuralLayer{
constructor(count){
this.neurons = [];
for (var i = 0; i < count; i++){
var neuron = new Neuron();
this.neurons.push(neuron);
}
}
connectTo(toLayer){
for (var i = 0; i < this.neurons.length; i++){
for (var j = 0; j < toLayer.neurons.length; j++){
var connection = new Connection(this.neurons[i], toLayer.neurons[j]);
this.neurons[i].addOutputConnection(connection);
toLayer.neurons[j].addInputConnection(connection);
}
}
}
makeInput(snake){
for (var i = 0; i < this.neurons.length; i++){
this.neurons[i].setInput(i, snake);
}
}
getData(){
var max = Number.NEGATIVE_INFINITY;
var answers = Array();
for (var i = 0; i < this.neurons.length; i++){
var answer = this.neurons[i].getData();
answers[i] = answer;
}
for (var i = 0; i < answers.length; i++){
if (answers[i] > max){
max = answers[i];
}
}
var sum = 0;
for (var i = 0; i < answers.length; i++){
var output = Math.exp(answers[i] - max);
answers[i] = output;
sum += output;
}
for (var i = 0; i < answers.length; i++){
answers[i] /= sum;
}
return answers;
}
getWeights(){
var weights = [];
for (var i = 0; i < this.neurons.length; i++){
weights[i] = this.neurons[i].getWeight();
}
this.weights = weights;
return weights;
}
getBiases(){
var biases = [];
for (var i = 0; i < this.neurons.length; i++){
biases[i] = this.neurons[i].getBias();
}
this.biases = biases;
return biases;
}
setWeights(weights){
for (var i = 0; i < this.neurons.length; i++){
this.neurons[i].setWeights(weights[i]);
}
}
setBiases(biases){
for (var i = 0; i < this.neurons.length; i++){
this.neurons[i].setBias(biases[i]);
}
}
}