-
Notifications
You must be signed in to change notification settings - Fork 0
/
NN.java
451 lines (403 loc) · 12.2 KB
/
NN.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//This isn't my code; copy pasted just for learning and visualisation purposes. Credits to Deus Jeraldy :)
import java.util.Arrays;
import java.util.Random;
/**
*
* @author Deus Jeraldy
* @Email: [email protected]
*/
/*public*/ class np {
private static Random random;
private static long seed;
static {
seed = System.currentTimeMillis();
random = new Random(seed);
}
/**
* Sets the seed of the pseudo-random number generator. This method enables
* you to produce the same sequence of "random" number for each execution of
* the program. Ordinarily, you should call this method at most once per
* program.
*
* @param s the seed
*/
public static void setSeed(long s) {
seed = s;
random = new Random(seed);
}
/**
* Returns the seed of the pseudo-random number generator.
*
* @return the seed
*/
public static long getSeed() {
return seed;
}
/**
* Returns a random real number uniformly in [0, 1).
*
* @return a random real number uniformly in [0, 1)
*/
public static double uniform() {
return random.nextDouble();
}
/**
* Returns a random integer uniformly in [0, n).
*
* @param n number of possible integers
* @return a random integer uniformly between 0 (inclusive) and {@code n}
* (exclusive)
* @throws IllegalArgumentException if {@code n <= 0}
*/
public static int uniform(int n) {
if (n <= 0) {
throw new IllegalArgumentException("argument must be positive: " + n);
}
return random.nextInt(n);
}
/**
* Returns a random long integer uniformly in [0, n).
*
* @param n number of possible {@code long} integers
* @return a random long integer uniformly between 0 (inclusive) and
* {@code n} (exclusive)
* @throws IllegalArgumentException if {@code n <= 0}
*/
public static long uniform(long n) {
if (n <= 0L) {
throw new IllegalArgumentException("argument must be positive: " + n);
}
long r = random.nextLong();
long m = n - 1;
// power of two
if ((n & m) == 0L) {
return r & m;
}
// reject over-represented candidates
long u = r >>> 1;
while (u + m - (r = u % n) < 0L) {
u = random.nextLong() >>> 1;
}
return r;
}
/**
* Returns a random integer uniformly in [a, b).
*
* @param a the left endpoint
* @param b the right endpoint
* @return a random integer uniformly in [a, b)
* @throws IllegalArgumentException if {@code b <= a}
* @throws IllegalArgumentException if {@code b - a >= Integer.MAX_VALUE}
*/
public static int uniform(int a, int b) {
if ((b <= a) || ((long) b - a >= Integer.MAX_VALUE)) {
throw new IllegalArgumentException("invalid range: [" + a + ", " + b + ")");
}
return a + uniform(b - a);
}
/**
* Returns a random real number uniformly in [a, b).
*
* @param a the left endpoint
* @param b the right endpoint
* @return a random real number uniformly in [a, b)
* @throws IllegalArgumentException unless {@code a < b}
*/
public static double uniform(double a, double b) {
if (!(a < b)) {
throw new IllegalArgumentException("invalid range: [" + a + ", " + b + ")");
}
return a + uniform() * (b - a);
}
/**
* @param m
* @param n
* @return random m-by-n matrix with values between 0 and 1
*/
public static double[][] random(int m, int n) {
double[][] a = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = uniform(0.0, 1.0);
}
}
return a;
}
/**
* Transpose of a matrix
*
* @param a matrix
* @return b = A^T
*/
public static double[][] T(double[][] a) {
int m = a.length;
int n = a[0].length;
double[][] b = new double[n][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
b[j][i] = a[i][j];
}
}
return b;
}
/**
* @param a matrix
* @param b matrix
* @return c = a + b
*/
public static double[][] add(double[][] a, double[][] b) {
int m = a.length;
int n = a[0].length;
double[][] c = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}
/**
* @param a matrix
* @param b matrix
* @return c = a - b
*/
public static double[][] subtract(double[][] a, double[][] b) {
int m = a.length;
int n = a[0].length;
double[][] c = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
return c;
}
/**
* Element wise subtraction
*
* @param a scaler
* @param b matrix
* @return c = a - b
*/
public static double[][] subtract(double a, double[][] b) {
int m = b.length;
int n = b[0].length;
double[][] c = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = a - b[i][j];
}
}
return c;
}
/**
* @param a matrix
* @param b matrix
* @return c = a * b
*/
public static double[][] dot(double[][] a, double[][] b) {
int m1 = a.length;
int n1 = a[0].length;
int m2 = b.length;
int n2 = b[0].length;
if (n1 != m2) {
throw new RuntimeException("Illegal matrix dimensions.");
}
double[][] c = new double[m1][n2];
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < n1; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
/**
* Element wise multiplication
*
* @param a matrix
* @param x matrix
* @return y = a * x
*/
public static double[][] multiply(double[][] x, double[][] a) {
int m = a.length;
int n = a[0].length;
if (x.length != m || x[0].length != n) {
throw new RuntimeException("Illegal matrix dimensions.");
}
double[][] y = new double[m][n];
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
y[j][i] = a[j][i] * x[j][i];
}
}
return y;
}
/**
* Element wise multiplication
*
* @param a matrix
* @param x scaler
* @return y = a * x
*/
public static double[][] multiply(double x, double[][] a) {
int m = a.length;
int n = a[0].length;
double[][] y = new double[m][n];
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
y[j][i] = a[j][i] * x;
}
}
return y;
}
/**
* Element wise power
*
* @param x matrix
* @param a scaler
* @return y
*/
public static double[][] power(double[][] x, int a) {
int m = x.length;
int n = x[0].length;
double[][] y = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
y[i][j] = Math.pow(x[i][j], a);
}
}
return y;
}
/**
* @param a matrix
* @return shape of matrix a
*/
public static String shape(double[][] a) {
int m = a.length;
int n = a[0].length;
String Vshape = "(" + m + "," + n + ")";
return Vshape;
}
/**
* @param a matrix
* @return sigmoid of matrix a
*/
public static double[][] sigmoid(double[][] a) {
int m = a.length;
int n = a[0].length;
double[][] z = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
z[i][j] = (1.0 / (1 + Math.exp(-a[i][j])));
}
}
return z;
}
/**
* Element wise division
*
* @param a scaler
* @param x matrix
* @return x / a
*/
public static double[][] divide(double[][] x, int a) {
int m = x.length;
int n = x[0].length;
double[][] z = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
z[i][j] = (x[i][j] / a);
}
}
return z;
}
/**
* Element wise division
*
* @param A matrix
* @param Y matrix
* @param batch_size scaler
* @return loss
*/
public static double cross_entropy(int batch_size, double[][] Y, double[][] A) {
int m = A.length;
int n = A[0].length;
double[][] z = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
z[i][j] = (Y[i][j] * Math.log(A[i][j])) + ((1 - Y[i][j]) * Math.log(1 - A[i][j]));
}
}
double sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sum += z[i][j];
}
}
return -sum / batch_size;
}
public static double[][] softmax(double[][] z) {
double[][] zout = new double[z.length][z[0].length];
double sum = 0.;
for (int i = 0; i < z.length; i++) {
for (int j = 0; j < z[0].length; j++) {
sum += Math.exp(z[i][j]);
}
}
for (int i = 0; i < z.length; i++) {
for (int j = 0; j < z[0].length; j++) {
zout[i][j] = Math.exp(z[i][j]) / sum;
}
}
return zout;
}
public static void print(String val) {
System.out.println(val);
}
}
public class NN {
public static void main(String args[]) {
double[][] X = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
double[][] Y = {{0}, {1}, {1}, {0}};
int m = 4;
int nodes = 400;
X = np.T(X);
Y = np.T(Y);
double[][] W1 = np.random(nodes, 2);
double[][] b1 = new double[nodes][m];
double[][] W2 = np.random(1, nodes);
double[][] b2 = new double[1][m];
for (int i = 0; i < 4000; i++) {
// Foward Prop
// LAYER 1
double[][] Z1 = np.add(np.dot(W1, X), b1);
double[][] A1 = np.sigmoid(Z1);
//LAYER 2
double[][] Z2 = np.add(np.dot(W2, A1), b2);
double[][] A2 = np.sigmoid(Z2);
double cost = np.cross_entropy(m, Y, A2);
//costs.getData().add(new XYChart.Data(i, cost));
// Back Prop
//LAYER 2
double[][] dZ2 = np.subtract(A2, Y);
double[][] dW2 = np.divide(np.dot(dZ2, np.T(A1)), m);
double[][] db2 = np.divide(dZ2, m);
//LAYER 1
double[][] dZ1 = np.multiply(np.dot(np.T(W2), dZ2), np.subtract(1.0, np.power(A1, 2)));
double[][] dW1 = np.divide(np.dot(dZ1, np.T(X)), m);
double[][] db1 = np.divide(dZ1, m);
// G.D
W1 = np.subtract(W1, np.multiply(0.01, dW1));
b1 = np.subtract(b1, np.multiply(0.01, db1));
W2 = np.subtract(W2, np.multiply(0.01, dW2));
b2 = np.subtract(b2, np.multiply(0.01, db2));
if (i % 400 == 0) {
System.out.println("==============");
System.out.println("Cost = " + cost);
System.out.println("Predictions = " + Arrays.deepToString(A2));
}
}
}
}