-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayer.java
109 lines (92 loc) · 3.07 KB
/
Layer.java
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
105
106
107
108
109
import java.util.function.DoubleFunction;
import java.util.stream.DoubleStream;
import java.util.Arrays;
import java.util.Random;
import java.util.InputMismatchException;
public class Layer {
protected Neuron [] neurons;
protected double [] guess;
protected double learningRate = 0.9;
protected DoubleFunction<Double> activationFunc = (x) -> (1 / (1 + Math.exp(-x))); // Sigmoid function
protected DoubleFunction<Double> derivActivation = (x) -> (x * (1 - x));
protected Layer next;
protected Layer previous;
public Layer() {
this.neurons = new Neuron[0];
}
public Layer(int nbNeurons, int inputLength) {
this.neurons = new Neuron [nbNeurons];
for (int i = 0; i < nbNeurons; i++) {
this.neurons[i] = new Neuron(inputLength);
}
}
public Layer getNext() {
return this.next;
}
public Layer getPrevious() {
return this.previous;
}
public double [] getGuess() {
return this.guess;
}
public Neuron [] getNeurons() {
return this.neurons;
}
public void setGuess(double [] input) {
this.guess = input;
}
public void setPrevious(Layer another) {
this.previous = another;
}
public void reset() {
for (Neuron n : this.neurons) {
n.reset();
}
}
public void addNext(Layer another) {
this.next = another;
another.setPrevious(this);
}
public void update() {
for (Neuron n : this.neurons) {
n.update(this.learningRate, this.getPrevious().getGuess());
}
}
public void updateSignalErrors(double [] error) {
if (this.neurons.length != error.length) {
throw new InputMismatchException("Wrong length in updateSignalErrors Layer: " + this.neurons.length + " != " + error.length);
} else {
int i = 0;
for (Neuron n : this.neurons) {
n.setSignalError( error[i] * derivActivation.apply(this.getGuess()[i++]) );
}
}
}
public double [] computeError(double [] realisation) { // realisation is useless
double [] error = new double [this.guess.length];
for (int j = 0; j < error.length; j++) {
error[j] = 0;
for (Neuron nNext : this.getNext().getNeurons()) {
error[j] += (nNext.getWeights()[j] * nNext.getSignalError());
}
}
return error;
}
public double [] execute(double [] input) {
int i = 0;
this.guess = new double [this.neurons.length];
for (Neuron n : this.neurons) {
this.guess[i++] = this.activationFunc.apply(n.execute(input));
}
return this.guess;
}
@Override
public String toString() {
StringBuilder strb = new StringBuilder();
for (Neuron n : this.neurons) {
strb.append(n);
strb.append("\n");
}
return strb.toString();
}
}