-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralnetwork.java
More file actions
186 lines (111 loc) · 4.41 KB
/
Copy pathNeuralnetwork.java
File metadata and controls
186 lines (111 loc) · 4.41 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
import java.io.IOException;
import java.util.Collections;
public class Neuralnetwork {
static Layers[] l = new Layers[4];
public static double bce(double[][] yHat, double[][] y) {
double loss = 0.0;
double eps = 1e-15;
for (int i = 0; i < y.length; i++) {
double a = yHat[i][0];
// avoid log(0)
if (a < eps) a = eps;
if (a > 1.0 - eps) a = 1.0 - eps;
loss += -(y[i][0] * Math.log(a) + (1 - y[i][0]) * Math.log(1 - a));
}
return loss;
}
public static int argmax(double[][] a) {
int maxIndex = 0;
for (int i = 1; i < a.length; i++) {
if (a[i][0] > a[maxIndex][0]) {
maxIndex = i;
}
}
return maxIndex;
}
public static void main(String[] args) throws IOException{
Dataloader.loaddata();
l[0] = new Layers(128, 784);
l[1] = new Layers(64,128);
l[2] = new Layers(32,64);
l[3] = new Layers(10, 32);
for(int j = 0 ; j < 5 ; j++){
double totalloss = 0.0 ;
int correct = 0;
for(int i = 0 ; i < Dataloader.samples.size() ; i++){
l[0].forward(Dataloader.samples.get(i).image);
l[1].forward(l[0].A);
l[2].forward(l[1].A);
l[3].forward(l[2].A);
double loss= bce(l[3].A , Dataloader.samples.get(i).label);
totalloss+=loss;
int predicted = argmax(l[3].A);
int actual = argmax(Dataloader.samples.get(i).label);
if (predicted == actual) {
correct++;
}
l[3].backpropagate_initial(Dataloader.samples.get(i).label);
l[2].backpropagate(l[3].dz, l[3].w);
l[1].backpropagate(l[2].dz, l[2].w);
l[0].backpropagate(l[1].dz, l[1].w);
l[0].update();
l[1].update();
l[2].update();
l[3].update();
}
double avgCost = totalloss / Dataloader.samples.size();
double accuracy = (double) correct / Dataloader.samples.size();
System.out.println("Epoch : " + j + " Cost : " + avgCost + " accuracy : "+ accuracy);
Collections.shuffle(Dataloader.samples);
}
Dataloader.loadtestdata();
int correct = 0;
int[][] confusionMatrix = new int[10][10];
for(int i = 0 ; i < Dataloader.samples.size() ; i++){
l[0].forward(Dataloader.samples.get(i).image);
l[1].forward(l[0].A);
l[2].forward(l[1].A);
l[3].forward(l[2].A);
int predicted = argmax(l[3].A);
int actual = argmax(Dataloader.samples.get(i).label);
confusionMatrix[actual][predicted]++;
if (predicted == actual) {
correct++;
}
}
double accuracy = (double) correct / Dataloader.samples.size();
System.out.println();
System.out.println("Test accuracy : " + accuracy);
System.out.println("Correct : " + correct + " / " + Dataloader.samples.size());
System.out.println();
System.out.println("Confusion Matrix");
System.out.println("Rows = actual labels, Columns = predicted labels");
System.out.print(" ");
for (int i = 0; i < 10; i++) {
System.out.printf("%5d", i);
}
System.out.println();
for (int actual = 0; actual < 10; actual++) {
System.out.printf("%5d ", actual);
for (int predicted = 0; predicted < 10; predicted++) {
System.out.printf("%5d", confusionMatrix[actual][predicted]);
}
System.out.println();
}
System.out.println();
System.out.println("Digit TP FP TN FN");
for (int digit = 0; digit < 10; digit++) {
int tp = confusionMatrix[digit][digit];
int fp = 0;
int fn = 0;
for (int i = 0; i < 10; i++) {
if (i != digit) {
fp += confusionMatrix[i][digit];
fn += confusionMatrix[digit][i];
}
}
int tn = Dataloader.samples.size() - tp - fp - fn;
System.out.printf("%5d %6d %7d %7d %7d%n", digit, tp, fp, tn, fn);
}
}
}