forked from mark-watson/Java-AI-Book-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeural_2H_momentum.java
More file actions
executable file
·321 lines (292 loc) · 10.8 KB
/
Neural_2H_momentum.java
File metadata and controls
executable file
·321 lines (292 loc) · 10.8 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package neuralnetworks;
import java.util.*;
import java.io.*;
/**
* Back-propagation neural network with 2 hidden layers and momentum
*
* <p/>
* Copyright 1998-2012 by Mark Watson. All rights reserved.
* <p/>
* This software is can be used under either of the following licenses:
* <p/>
* 1. LGPL v3<br/>
* 2. Apache 2
* <p/>
*/public class Neural_2H_momentum implements Serializable {
private static final long serialVersionUID = 5869773395153615662L;
protected int numInputs;
protected int numHidden1;
protected int numHidden2;
protected int numOutputs;
protected int numTraining;
public float inputs[];
protected float hidden1[];
protected float hidden2[];
public float outputs[];
protected float W1[][];
protected float W2[][];
protected float W3[][];
// last delta weights for momentum term:
protected float W1_last_delta[][];
protected float W2_last_delta[][];
protected float W3_last_delta[][];
protected float output_errors[];
protected float hidden1_errors[];
protected float hidden2_errors[];
transient protected ArrayList inputTraining = new ArrayList();
transient protected ArrayList outputTraining = new ArrayList();
public float TRAINING_RATE = 0.5f;
private float alpha = 0f; // momentum scaling term that is applied to last delta weight
// use a reasonable default momentum term (alpha):
public Neural_2H_momentum(int num_in, int num_hidden1, int num_hidden2, int num_output) {
this(num_in, num_hidden1, num_hidden1, num_output, 0.6f);
}
public Neural_2H_momentum(int num_in, int num_hidden1, int num_hidden2, int num_output,
float alpha) {
this.alpha = alpha;
numInputs = num_in;
numHidden1 = num_hidden1;
numHidden2 = num_hidden2;
numOutputs = num_output;
inputs = new float[numInputs];
hidden1 = new float[numHidden1];
hidden2 = new float[numHidden2];
outputs = new float[numOutputs];
W1 = new float[numInputs][numHidden1];
W2 = new float[numHidden1][numHidden2];
W3 = new float[numHidden2][numOutputs];
W1_last_delta = new float[numInputs][numHidden1];
W2_last_delta = new float[numHidden1][numHidden2];
W3_last_delta = new float[numHidden2][numOutputs];
randomizeWeights();
output_errors = new float[numOutputs];
hidden1_errors = new float[numHidden1];
hidden2_errors = new float[numHidden2];
}
public void addTrainingExample(float[] inputs, float[] outputs) {
if (inputs.length != numInputs || outputs.length != numOutputs) {
System.out.println("addTrainingExample(): array size is wrong");
return;
}
//System.out.println("addTrainingExample(): inputs: " + Arrays.toString(inputs));
//System.out.println("addTrainingExample(): outputs: " + Arrays.toString(outputs));
inputTraining.add(inputs);
outputTraining.add(outputs);
}
/**
* Load a trained network from a serialized file
*
* @param serialized_file_name
* @return
*/
public static Neural_2H_momentum Factory(String serialized_file_name) {
Neural_2H_momentum nn = null;
try {
InputStream ins = ClassLoader.getSystemResourceAsStream(serialized_file_name);
if (ins == null) {
System.out.println("CachedExamples(): failed to open 'cache.dat' in JAR file");
System.exit(1);
} else {
ObjectInputStream p = new ObjectInputStream(ins);
nn = (Neural_2H_momentum) p.readObject();
nn.inputTraining = new ArrayList();
nn.outputTraining = new ArrayList();
ins.close();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return nn;
}
/**
* Save a trained network to a serialized file for re-use without re-training
*
* @param file_name
*/
public void save(String file_name) {
try {
FileOutputStream ostream = new FileOutputStream(file_name);
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeObject(this);
p.flush();
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void randomizeWeights() {
// Randomize weights here:
for (int ii = 0; ii < numInputs; ii++)
for (int hh = 0; hh < numHidden1; hh++)
W1[ii][hh] =
2f * (float) Math.random() - 1f;
for (int ii = 0; ii < numHidden1; ii++)
for (int hh = 0; hh < numHidden2; hh++)
W2[ii][hh] =
2f * (float) Math.random() - 1f;
for (int hh = 0; hh < numHidden2; hh++)
for (int oo = 0; oo < numOutputs; oo++)
W3[hh][oo] =
2f * (float) Math.random() - 1f;
}
public void slightlyRandomizeWeights() {
// Randomize weights here:
for (int ii = 0; ii < numInputs; ii++)
for (int hh = 0; hh < numHidden1; hh++)
W1[ii][hh] +=
0.2f * (float) Math.random() - 0.1f;
for (int ii = 0; ii < numHidden1; ii++)
for (int hh = 0; hh < numHidden2; hh++)
W2[ii][hh] +=
0.2f * (float) Math.random() - 0.1f;
for (int hh = 0; hh < numHidden2; hh++)
for (int oo = 0; oo < numOutputs; oo++)
W3[hh][oo] +=
0.2f * (float) Math.random() - 0.1f;
}
public float[] recall(float[] in) {
//System.out.println("recall(" + Arrays.toString(inputs) + ")");
for (int i = 0; i < numInputs; i++) inputs[i] = in[i];
forwardPass();
float[] ret = new float[numOutputs];
for (int i = 0; i < numOutputs; i++) ret[i] = outputs[i];
return ret;
}
public void forwardPass() {
int i, h, o;
for (h = 0; h < numHidden1; h++) {
hidden1[h] = 0.0f;
}
for (h = 0; h < numHidden2; h++) {
hidden2[h] = 0.0f;
}
for (i = 0; i < numInputs; i++) {
for (h = 0; h < numHidden1; h++) {
hidden1[h] +=
inputs[i] * W1[i][h];
}
}
for (i = 0; i < numHidden1; i++) {
for (h = 0; h < numHidden2; h++) {
hidden2[h] +=
hidden1[i] * W2[i][h];
}
}
for (o = 0; o < numOutputs; o++)
outputs[o] = 0.0f;
for (h = 0; h < numHidden2; h++) {
for (o = 0; o < numOutputs; o++) {
outputs[o] +=
sigmoid(hidden2[h]) * W3[h][o];
}
}
}
public float train() {
return train(inputTraining, outputTraining);
}
private int current_example = 0;
public float train(ArrayList ins, ArrayList v_outs) {
int i, h, o;
float error = 0.0f;
int num_cases = ins.size();
//for (int example=0; example<num_cases; example++) {
// zero out error arrays:
for (h = 0; h < numHidden1; h++)
hidden1_errors[h] = 0.0f;
for (h = 0; h < numHidden2; h++)
hidden2_errors[h] = 0.0f;
for (o = 0; o < numOutputs; o++)
output_errors[o] = 0.0f;
// copy the input values:
for (i = 0; i < numInputs; i++) {
inputs[i] = ((float[]) ins.get(current_example))[i];
}
// copy the output values:
float[] outs = (float[]) v_outs.get(current_example);
// perform a forward pass through the network:
forwardPass();
for (o = 0; o < numOutputs; o++) {
output_errors[o] =
(outs[o] -
outputs[o])
* sigmoidP(outputs[o]);
}
for (h = 0; h < numHidden2; h++) {
hidden2_errors[h] = 0.0f;
for (o = 0; o < numOutputs; o++) {
hidden2_errors[h] +=
output_errors[o] * W3[h][o];
}
}
for (h = 0; h < numHidden1; h++) {
hidden1_errors[h] = 0.0f;
for (o = 0; o < numHidden2; o++) {
hidden1_errors[h] +=
hidden2_errors[o] * W2[h][o];
}
}
for (h = 0; h < numHidden2; h++) {
hidden2_errors[h] =
hidden2_errors[h] * sigmoidP(hidden2[h]);
}
for (h = 0; h < numHidden1; h++) {
hidden1_errors[h] =
hidden1_errors[h] * sigmoidP(hidden1[h]);
}
// update the hidden2 to output weights:
for (o = 0; o < numOutputs; o++) {
for (h = 0; h < numHidden2; h++) {
W3[h][o] +=
TRAINING_RATE * output_errors[o] * hidden2[h] +
// apply the momentum term:
alpha * W3_last_delta[h][o];
W3[h][o] = clampWeight(W3[h][o]);
W3_last_delta[h][o] = TRAINING_RATE * output_errors[o] * hidden2[h];
}
}
// update the hidden1 to hidden2 weights:
for (o = 0; o < numHidden2; o++) {
for (h = 0; h < numHidden1; h++) {
W2[h][o] +=
TRAINING_RATE * hidden2_errors[o] * hidden1[h] +
// apply the momentum term:
alpha * W2_last_delta[h][o];
W2[h][o] = clampWeight(W2[h][o]);
W2_last_delta[h][o] = TRAINING_RATE * hidden2_errors[o] * hidden1[h];
}
}
// update the input to hidden1 weights:
for (h = 0; h < numHidden1; h++) {
for (i = 0; i < numInputs; i++) {
W1[i][h] +=
TRAINING_RATE * hidden1_errors[h] * inputs[i] +
// apply the momentum term:
alpha * W1_last_delta[i][h];
W1[i][h] = clampWeight(W1[i][h]);
W1_last_delta[i][h] = TRAINING_RATE * hidden1_errors[h] * inputs[i];
}
}
for (o = 0; o < numOutputs; o++) {
error += Math.abs(outs[o] - outputs[o]);
//error += Math.abs(output_errors[o]);
}
current_example++;
if (current_example >= num_cases) current_example = 0;
return error;
}
protected float clampWeight(float weigth) {
float ret = weigth;
if (ret < -10) ret = -10;
if (ret > 10) ret = 10;
return ret;
}
protected float sigmoid(float x) {
return
(float) (1.0f / (1.0f + Math.exp((double) (-x))));
}
protected float sigmoidP(float x) {
double z = sigmoid(x); // + TRAINING_RATE;
return (float) (z * (1.0f - z));
}
}