-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayer.java
56 lines (45 loc) · 1.66 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
import java.util.ArrayList;
import java.util.Collections;
public class Layer {
private ArrayList<Neuron> neurons;
private final int numNeurons;
public Layer() {
throw new IllegalArgumentException("Layer must be initialized with a list of neurons!");
}
public Layer(ArrayList<Neuron> neurons) {
this.neurons = neurons;
numNeurons = neurons.size();
}
public Layer(Neuron... neurons) {
this.neurons = new ArrayList<>();
Collections.addAll(this.neurons, neurons);
numNeurons = this.neurons.size();
}
public Layer(Layer prevLayer, int numNeurons) {
neurons = new ArrayList<>();
for (int i = 0; i < numNeurons; i++) {
neurons.add(new Neuron(prevLayer, numNeurons));
}
this.numNeurons = numNeurons;
}
public double[] evaluate() {
double[] results = new double[numNeurons];
for (int i = 0; i < numNeurons; i++)
results[i] = neurons.get(i).evaluate();
return results;
}
public double evaluateNeuron(int i) {
if (i < numNeurons) return neurons.get(i).evaluate();
else throw new IndexOutOfBoundsException("Attempted to evaluate neuron " + i + " in layer containing " + numNeurons + " neurons!");
}
public void updateLayer(double[] values) {
if (values.length != numNeurons) throw new IllegalArgumentException("Invalid number of values. Expected: " + numNeurons + ", actual: " + values.length);
else {
for (int i = 0; i < numNeurons; i++)
neurons.get(i).updateNode(values[i]);
}
}
public int size() {
return numNeurons;
}
}