-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetwork.java
317 lines (316 loc) · 10.7 KB
/
Network.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
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
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
public class Network{
// sort of linked list like in terms of storage actually
static final int BATCH = 1; // BATCH MODE MAY NOT BE WORKING, COULD NOT GET IT TO WORK FOR XOR, POSSIBLE LOCAL MINIMUM OR IMPLEMENTATION BUG
static final int STOCHASTIC = 0;
static final int VERSION_NUMBER = 1;
int training_mode = STOCHASTIC;
Layer input = null;
Layer output = null;
double learning_rate;
LossFunction loss_function = new MSE(); // by default use MSE
public Network(){
}
public Network(int layer_count, double learning_rate, int[] layer_sizes, String[] layer_activation_functions){
this.learning_rate = learning_rate;
Layer prevlayer = null;
for(int i = 0; i < layer_count; i++){
Layer curlayer = new Layer(layer_sizes[i]);
curlayer.learning_rate = learning_rate;
switch(layer_activation_functions[i]){
case "identity":
curlayer.activation_function = new IdentityFunction();
break;
case "lrelu":
curlayer.activation_function = new LeakyReLU();
break;
case "relu":
curlayer.activation_function = new RectifiedLinearActivationFunction();
break;
case "sigmoid":
curlayer.activation_function = new SigmoidActivationFunction();
break;
case "softmax":
curlayer.activation_function = new Softmax();
break;
default:
throw new IllegalArgumentException("No valid activation function detected");
}
if(i == 0){
input = curlayer;
} else {
prevlayer.output = curlayer;
curlayer.input = prevlayer;
curlayer.weights = new double[curlayer.size][prevlayer.size + 1];
}
if(i == (layer_count - 1)){
output = curlayer;
}
prevlayer = curlayer;
}
}
// Constructs new network from custom file format
/*
N -> total layers, a -> learning rate, T -> training mode, v -> model version (in case i add more things its backwards compatible), L -> loss function type (MSE or log loss)
then N segments
for each segment: X -> size of layer
then binary flag numbers, 1 << 0: weights given (cant be if first layer), 1 << 1: values given
then string for activation function: "identity", "relu", "lrelu", "sigmoid"
if weights given, size * (input.size + 1) numbers in next line for weights, parse 2d array to 1d, a[i][j] = a[i*jlen + j]
if values given, size numbers in next line for values
Sample:
3 0.05 0 1 MSE
2 0 identity
2 1 lrelu
0 1 -1 0 -1 1
1 1 sigmoid
0 5 5
*/
public Network(String filename){
try {
File f = new File(filename);
Scanner in = new Scanner(f);
int N = in.nextInt();
double a = in.nextDouble();
this.learning_rate = a;
training_mode = in.nextInt();
int v = in.nextInt();
switch(v){
case 1:
break;
default:
throw new IllegalArgumentException("Version number not recognised");
}
String lossfunction = in.next();
switch(lossfunction){
case "MSE":
this.loss_function = new MSE();
break;
case "LogLoss":
this.loss_function = new LogLoss();
break;
default:
throw new IllegalArgumentException("Loss function not recognised");
}
Layer prevlayer = null;
for(int i = 0; i < N; i++){
int X = in.nextInt();
int flags = in.nextInt();
String activation_function = in.next();
Layer curlayer = new Layer(X);
curlayer.learning_rate = learning_rate;
switch(activation_function){
case "identity":
curlayer.activation_function = new IdentityFunction();
break;
case "lrelu":
curlayer.activation_function = new LeakyReLU();
break;
case "relu":
curlayer.activation_function = new RectifiedLinearActivationFunction();
break;
case "sigmoid":
curlayer.activation_function = new SigmoidActivationFunction();
break;
case "softmax":
curlayer.activation_function = new Softmax();
break;
default:
throw new IllegalArgumentException("No valid activation function detected");
}
if(i != 0){
curlayer.weights = new double[curlayer.size][prevlayer.size + 1];
}
if((flags & (1 << 0)) != 0){ // weights given
if(i == 0){
throw new IllegalArgumentException("First input layer cannot have weights");
}
for(int k = 0; k < curlayer.size; k++){
for(int j = 0; j < (prevlayer.size + 1); j++){
curlayer.weights[k][j] = in.nextDouble();
}
}
}
if((flags & (1 << 1)) != 0){ // values given
for(int k = 0; k < curlayer.size; k++){
curlayer.values[k] = in.nextDouble();
}
}
if(i == 0){
input = curlayer;
} else {
curlayer.input = prevlayer;
prevlayer.output = curlayer;
}
if(i == (N - 1)){
output = curlayer;
}
prevlayer = curlayer;
}
} catch (FileNotFoundException e){
throw new IllegalArgumentException("File not found"); // passthrough but in a more standard format
}
}
public String outputNetwork(boolean includeValues){
String s = "";
int layercount = 1;
Layer curlayer = input;
// special case for input
if(includeValues){
s += ("" + curlayer.size + " 2 " + curlayer.activation_function + "\n");
for(int i = 0; i < curlayer.size; i++){
if(i != 0){
s += " ";
}
s += curlayer.values[i];
}
s += "\n";
} else {
s += ("" + curlayer.size + " 0 " + curlayer.activation_function + "\n");
}
while(curlayer != output){
layercount++;
curlayer = curlayer.output;
if(includeValues){
s += ("" + curlayer.size + " 3 " + curlayer.activation_function + "\n");
} else {
s += ("" + curlayer.size + " 1 " + curlayer.activation_function + "\n");
}
for(int i = 0; i < curlayer.weights.length; i++){
for(int j = 0; j < curlayer.weights[i].length; j++){
if(i != 0 || j != 0){
s += " ";
}
s += curlayer.weights[i][j];
}
}
s += "\n";
if(includeValues){
for(int i = 0; i < curlayer.size; i++){
if(i != 0){
s += " ";
}
s += curlayer.values[i];
}
s += "\n";
}
}
s = ("" + layercount + " " + learning_rate + " " + training_mode + " " + VERSION_NUMBER + " " + loss_function + "\n" + s);
return s;
}
public double[] evaluate(double[] inputValues){
if(input.size != inputValues.length){
throw new IllegalArgumentException("inputValues length does not match input layer size");
}
for(int i = 0; i < input.size; i++){
input.values[i] = inputValues[i];
}
return evaluate();
}
public double[] evaluate(){
Layer curlayer = input;
while(curlayer != output){
curlayer = curlayer.output;
curlayer.forward_propagation();
}
return output.values;
}
public void randomize_weights(){ // starts each weight off with a random number between -1 and 1
Layer curlayer = input;
while(curlayer != output){
curlayer = curlayer.output;
for(int i = 0; i < curlayer.weights.length; i++){
for(int j = 0; j < curlayer.weights[i].length; j++){
curlayer.weights[i][j] = (Math.random() * 2) - 1;
}
}
}
}
public double[] get_error_partial_derivs(double[] target, double[] predicted){
// first thing to calculate, dE/df, where f is the final output, for mean squared error, just 2 * (predicted - actual)
/*
double[] error_partial_derivs = new double[predicted.length];
for(int i = 0; i < output.size; i++){
//System.out.println("OUTPUT: " + output.values[i] + ", TARGET: " + target[i]);
error_partial_derivs[i] = 2 * (predicted[i] - target[i]);
}
return error_partial_derivs;
*/
return loss_function.compute_derivative(target, predicted);
}
public void backpropagate(double[] error_partial_derivs){
Layer curlayer = output;
while(curlayer != input){
error_partial_derivs = curlayer.back_propagation(error_partial_derivs);
curlayer = curlayer.input;
}
}
// returns [error, error partial derivatives relative to output values]
public double[][] trainOneTest(double[] testcase, double[] correct){
evaluate(testcase);
double[] rv1 = new double[]{(loss_function.compute_loss(correct, output.values))};
double[][] rv = new double[2][];
rv[0] = rv1;
rv[1] = get_error_partial_derivs(correct, output.values);
if(training_mode == STOCHASTIC){
backpropagate(rv[1]);
}
//System.out.println(outputNetwork());
return rv;
}
// returns avg error
public double trainOneEpoch(double[][] testcases, double[][] correctvalues){
if(testcases.length != correctvalues.length){
throw new IllegalArgumentException("Testcases and correct value count are not equal");
}
double[][] rv;
double errorsum = 0;
double[] gradientavg = new double[correctvalues[0].length];
for(int i = 0; i < testcases.length; i++){
rv = trainOneTest(testcases[i], correctvalues[i]);
if(training_mode == BATCH){
sumTo(gradientavg, rv[1]);
}
errorsum += (rv[0][0] / testcases.length);
if(i % 10000 == 0 && i >= 10000){
System.out.println("Currently at test case " + i + ", current error sum: " + errorsum);
}
}
if(training_mode == BATCH){
for(int i = 0; i < gradientavg.length; i++){
gradientavg[i] /= testcases.length;
}
backpropagate(gradientavg);
}
return errorsum;
}
public static void sumTo(double[] a, double[] b){
if(a.length != b.length){
throw new IllegalArgumentException("sumTo called with two arrays of different length");
}
for(int i = 0; i < a.length; i++){
a[i] += b[i];
}
}
/*
public static double square(double x){
return x * x;
}
// Uses MSE to calculate error
public static double calculate_error(double[] target, double[] predicted){
//System.out.println("Expected: " + target[0] + ", got: " + predicted[0]);
if(target.length != predicted.length){
throw new IllegalArgumentException("Target and predicted lengths do not match");
}
double sum = 0;
for(int i = 0; i < target.length; i++){
sum += square(predicted[i] - target[i]);
//System.out.println(predicted[i]);
}
sum /= target.length;
return sum;
}
*/
}