-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMultiLayerNeuralNet.java
More file actions
302 lines (271 loc) · 9.51 KB
/
MultiLayerNeuralNet.java
File metadata and controls
302 lines (271 loc) · 9.51 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Arrays;
public class MultiLayerNeuralNet implements Classifier {
/* algorithm's learning rate. */
private final double learningRate = 0.5;
/* algorithm's momentum parameter. */
private final double momentumFactor = 0.1;
/* weights[i][j] is the weight on edge from node i -> j.
* as a special case, weights[i][i] is the threshold value
* of the perceptron i. */
private double[][] weights;
/* outgoingEdges[i] is a list of edges from i. */
private final LinkedList<Integer>[] outgoingEdges;
/* incomingEdges[i] is a list of edges to i. */
private final LinkedList<Integer>[] incomingEdges;
/* layer[i] is a list of nodes in layer i. */
private final LinkedList<Integer>[] layer;
/* data set on which to make predictions. */
private final DataSet d;
/* number of attributes in data set. */
private final int N;
/* number of nodes in the network. */
private final int numNodes;
/** Calculates the error on the training examples of
* data set d. */
private double error(DataSet d) {
if (d.numTrainExs == 0) return 0.0;
double sum = 0.0;
for (int i = 0; i < d.numTrainExs; i++)
sum += Math.abs(d.trainLabel[i] - predict(d.trainEx[i]));
return sum/d.numTrainExs;
}
/** Runs an activation threshold function g on some
* input value d.
*/
private double g(double d) {
return 1.0/(1.0 + Math.exp(-d));
}
/** Runs an activation threshold function g's derivative
* on some input value d.
*/
private double gPrime(double d) {
double g = g(d);
return g * (1.0 - g);
}
/**
* Trains the neural network on every example in data set d
* using previous deltas prevDelta.
*/
private void backProp(DataSet d, double[] prevDelta) {
for (int i = 0; i < d.numTrainExs; i++)
backProp(d.trainEx[i], d.trainLabel[i], prevDelta);
}
/** Trains the neural network on an example ex by using
* the back propagation technique to adjust the net's weights.
* Example ex is known to be of classification label. Uses
* 0 for all previous deltas.
*/
private void backProp(int[] ex, int label) {
backProp(ex, label, new double[this.numNodes]);
}
/** Trains the neural network on an example ex by using
* the back propagation technique to adjust the net's weights.
* Example ex is known to be of classification label. It is
* assumed that prevDelta are the deltas for time t-1, used
* to add momentum to the gradient descent calculation. Stores
* the calculated deltas in prevDelta for future use.
*/
private void backProp(int[] ex, int label, double[] prevDelta) {
// output of each node
double[] a = new double[this.numNodes];
// input to each node
double[] in = new double[this.numNodes];
// delta for adjusting each edge weight
double[] delta = new double[this.numNodes];
// first N nodes are input nodes
for (int i = 0; i < this.N; i++)
a[i] = ex[i];
// compute outputs by propagating inputs forward
for (int l = 1; l < this.layer.length; l++) {
for (int dest : this.layer[l]) {
for (int src : this.incomingEdges[dest]) {
in[dest] += this.weights[src][dest]*a[src];
}
// subtract threshold value
in[dest] -= this.weights[dest][dest];
a[dest] = g(in[dest]);
}
}
// return if accurate prediction
if (predict(a[this.numNodes - 1]) == label) return;
// compute deltas by propagating backward
// degin with delta of output layer as base case
delta[this.numNodes - 1] =
gPrime(a[this.numNodes - 1])*(label - (int)Math.round(a[this.numNodes - 1]));
for (int l = this.layer.length - 2; l >= 0; l--) {
for (int src : this.layer[l]) {
double sum = 0;
for (int dest : this.outgoingEdges[src]) {
sum += this.weights[src][dest]*delta[dest];
}
// compute delta and add momentum factor
delta[src] = gPrime(a[src])*sum;
delta[src] += this.momentumFactor*prevDelta[src];
// store momentum for future use
prevDelta[src] = delta[src];
}
}
// adjust weights
for (int i = 0; i < this.weights.length; i++) {
for (int j = i+1; j < this.weights.length; j++) {
this.weights[i][j] += this.learningRate*a[i]*delta[j];
this.weights[j][i] = this.weights[i][j];
}
}
}
/** Returns a random weight for an edge. */
private double randomWeight() {
return 0.5 - Math.random();
}
/** Resets the weights of a neural network to avoid getting caught
* in a local minimum.
*/
private void randomizeWeights() {
for (int i = 0; i < this.weights.length; i++) {
for (int j = i+1; j < this.weights.length; j++) {
this.weights[i][j] = randomWeight();
this.weights[j][i] = this.weights[i][j];
}
}
}
/** Constructor for the MultiLayerNeuralNet class that
* creates a multi-layer, feed-forward neural network
* from a data set.
*/
@SuppressWarnings("unchecked")
public MultiLayerNeuralNet(DataSet d) {
this.d = d;
this.N = this.d.numAttrs;
// number of nodes in hidden layer
int numHidden = this.N;
this.numNodes = this.N + numHidden + 1;
this.weights = new double[this.numNodes][this.numNodes];
randomizeWeights();
// create and initialize list of edges
this.incomingEdges = (LinkedList<Integer>[]) new LinkedList[this.numNodes];
this.outgoingEdges = (LinkedList<Integer>[]) new LinkedList[this.numNodes];
for (int i = 0; i < this.numNodes; i++) {
this.incomingEdges[i] = new LinkedList<Integer>();
this.outgoingEdges[i] = new LinkedList<Integer>();
}
// number of layers to be included
int numLayers = 3;
this.layer = (LinkedList<Integer>[]) new LinkedList[numLayers];
for (int i = 0; i < numLayers; i++)
this.layer[i] = new LinkedList<Integer>();
/* Create first layer and links to hidden layer. */
for (int i = 0; i < this.N; i++) {
this.layer[0].add(i);
// add incoming and outgoing edges
for (int j = this.N; j < this.N + numHidden; j++) {
this.outgoingEdges[i].add(j);
this.incomingEdges[j].add(i);
}
}
/* Create second layer and links to third layer. */
for (int i = this.N; i < this.N + numHidden; i++) {
this.layer[1].add(i);
this.incomingEdges[this.numNodes - 1].add(i);
this.outgoingEdges[i].add(this.numNodes - 1);
}
/* Create list of third layer (output node). */
this.layer[2].add(this.numNodes - 1);
// train neural net on each training example
// run until epsilon threshold error is breached
double epsilon = 0.05;
double minError = Double.MAX_VALUE;
double lastError = Double.MAX_VALUE;
double[][] bestWeights = new double[this.weights.length][this.weights.length];
double[] prevDelta = new double[this.numNodes];
int maxRuns = 100;
for (int runs = 0; runs < maxRuns; runs++) {
// run back prop
backProp(this.d, prevDelta);
double error = error(this.d);
// if error is sufficiently low, cut-off
if (error < epsilon) {
bestWeights = this.weights;
break;
}
// if error has not improved, reset
else if (error >= lastError) {
randomizeWeights();
lastError = Double.MAX_VALUE;
}
else {
lastError = error;
// if error is best seen, remember weights
if (error < minError) {
minError = error;
for (int i = 0; i < this.weights.length; i++)
System.arraycopy(this.weights[i], 0, bestWeights[i], 0, this.weights.length);
}
}
}
// assign permanent weights to the best weights observed
this.weights = bestWeights;
}
/** A method for predicting the label of a given example <tt>ex</tt>
* represented, as in the rest of the code, as an array of values
* for each of the attributes. The method should return a
* prediction, i.e., 0 or 1.
*/
public int predict(int[] ex) {
double[] a = new double[this.numNodes];
double[] in = new double[this.numNodes];
// First N nodes are input nodes
for (int i = 0; i < this.N; i++)
a[i] = ex[i];
// Compute outputs by propagating inputs forward
for (int l = 1; l < this.layer.length; l++) {
for (int dest : this.layer[l]) {
for (int src : this.incomingEdges[dest]) {
in[dest] += this.weights[src][dest]*a[src];
}
a[dest] = g(in[dest]);
}
}
// Return based on output of sigmoid function
return predict(a[this.numNodes - 1]);
}
/** Makes a prediction based on some input value a, which
* should--in practice--be the output value of the final
* perceptron.
*/
private int predict(double a) {
return (int)Math.round(a);
}
/** This method should return a very brief but understandable
* description of the learning algorithm that is being used,
* appropriate for posting on the class website.
*/
public String algorithmDescription() {
return "A multi layer neural network.";
}
/** This method should return the "author" of this program as you
* would like it to appear on the class website. You can use your
* real name, or a pseudonym, or a name that identifies your
* group.
*/
public String author() {
return "crm";
}
/** A simple main for testing this algorithm. This main reads a
* filestem from the command line, runs the learning algorithm on
* this dataset, and prints the test predictions to filestem.testout.
*/
public static void main(String argv[])
throws FileNotFoundException, IOException {
if (argv.length < 1) {
System.err.println("argument: filestem");
return;
}
String filestem = argv[0];
DataSet d = new BinaryDataSet(filestem);
Classifier c = new MultiLayerNeuralNet(d);
d.printTestPredictions(c, filestem);
}
}