forked from davecom/ClassicComputerScienceProblemsInJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork.java
More file actions
123 lines (111 loc) · 4.13 KB
/
Network.java
File metadata and controls
123 lines (111 loc) · 4.13 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Network.java
// From Classic Computer Science Problems in Java Chapter 7
// Copyright 2020 David Kopec
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chapter7;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
public class Network<T> {
private List<Layer> layers = new ArrayList<>();
public Network(int[] layerStructure, double learningRate,
DoubleUnaryOperator activationFunction, DoubleUnaryOperator derivativeActivationFunction) {
if (layerStructure.length < 3) {
throw new IllegalArgumentException("Error: Should be at least 3 layers (1 input, 1 hidden, 1 output).");
}
// input layer
Layer inputLayer = new Layer(Optional.empty(), layerStructure[0], learningRate, activationFunction,
derivativeActivationFunction);
layers.add(inputLayer);
// hidden layers and output layer
for (int i = 1; i < layerStructure.length; i++) {
Layer nextLayer = new Layer(Optional.of(layers.get(i - 1)), layerStructure[i], learningRate,
activationFunction,
derivativeActivationFunction);
layers.add(nextLayer);
}
}
// Pushes input data to the first layer, then output from the first
// as input to the second, second to the third, etc.
private double[] outputs(double[] input) {
double[] result = input;
for (Layer layer : layers) {
result = layer.outputs(result);
}
return result;
}
// Figure out each neuron's changes based on the errors of the output
// versus the expected outcome
private void backpropagate(double[] expected) {
// calculate delta for output layer neurons
int lastLayer = layers.size() - 1;
layers.get(lastLayer).calculateDeltasForOutputLayer(expected);
// calculate delta for hidden layers in reverse order
for (int i = lastLayer - 1; i >= 0; i--) {
layers.get(i).calculateDeltasForHiddenLayer(layers.get(i + 1));
}
}
// backpropagate() doesn't actually change any weights
// this function uses the deltas calculated in backpropagate() to
// actually make changes to the weights
private void updateWeights() {
for (Layer layer : layers.subList(1, layers.size())) {
for (Neuron neuron : layer.neurons) {
for (int w = 0; w < neuron.weights.length; w++) {
neuron.weights[w] = neuron.weights[w] + (neuron.learningRate *
layer.previousLayer.get().outputCache[w] * neuron.delta);
}
}
}
}
// train() uses the results of outputs() run over many inputs and compared
// against expecteds to feed backpropagate() and updateWeights()
public void train(List<double[]> inputs, List<double[]> expecteds) {
for (int i = 0; i < inputs.size(); i++) {
double[] xs = inputs.get(i);
double[] ys = expecteds.get(i);
outputs(xs);
backpropagate(ys);
updateWeights();
}
}
public class Results {
public final int correct;
public final int trials;
public final double percentage;
public Results(int correct, int trials, double percentage) {
this.correct = correct;
this.trials = trials;
this.percentage = percentage;
}
}
// for generalized results that require classification
// this function will return the correct number of trials
// and the percentage correct out of the total
public Results validate(List<double[]> inputs, List<T> expecteds, Function<double[], T> interpret) {
int correct = 0;
for (int i = 0; i < inputs.size(); i++) {
double[] input = inputs.get(i);
T expected = expecteds.get(i);
T result = interpret.apply(outputs(input));
if (result.equals(expected)) {
correct++;
}
}
double percentage = (double) correct / (double) inputs.size();
return new Results(correct, inputs.size(), percentage);
}
}