-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTrainMNISTConv.java
More file actions
55 lines (44 loc) · 2.27 KB
/
TrainMNISTConv.java
File metadata and controls
55 lines (44 loc) · 2.27 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
package tests;
import javamachinelearning.layers.feedforward.ActivationLayer;
import javamachinelearning.layers.feedforward.ConvLayer;
import javamachinelearning.layers.feedforward.DropoutLayer;
import javamachinelearning.layers.feedforward.FCLayer;
import javamachinelearning.layers.feedforward.FlattenLayer;
import javamachinelearning.layers.feedforward.MaxPoolingLayer;
import javamachinelearning.layers.feedforward.ConvLayer.PaddingType;
import javamachinelearning.networks.SequentialNN;
import javamachinelearning.optimizers.AdamOptimizer;
import javamachinelearning.utils.Activation;
import javamachinelearning.utils.Loss;
import javamachinelearning.utils.MNISTUtils;
import javamachinelearning.utils.Tensor;
import javamachinelearning.utils.Utils;
public class TrainMNISTConv{
public static void main(String[] args) throws Exception{
// very slow!
SequentialNN nn = new SequentialNN(28, 28, 1);
nn.add(new ConvLayer(5, 32, PaddingType.SAME));
nn.add(new ActivationLayer(Activation.relu));
nn.add(new MaxPoolingLayer(2, 2));
nn.add(new ConvLayer(5, 64, PaddingType.SAME));
nn.add(new ActivationLayer(Activation.relu));
nn.add(new MaxPoolingLayer(2, 2));
nn.add(new FlattenLayer());
nn.add(new FCLayer(1024));
nn.add(new ActivationLayer(Activation.relu));
nn.add(new DropoutLayer(0.3));
nn.add(new FCLayer(10));
nn.add(new ActivationLayer(Activation.softmax));
System.out.println(nn);
Tensor[] x = MNISTUtils.loadDataSetImages("train-images-idx3-ubyte", Integer.MAX_VALUE);
Tensor[] y = MNISTUtils.loadDataSetLabels("train-labels-idx1-ubyte", Integer.MAX_VALUE);
long start = System.currentTimeMillis();
nn.train(Utils.reshapeAll(x, 28, 28, 1), y, 100, 100, Loss.softmaxCrossEntropy, new AdamOptimizer(0.01), null, true, false);
System.out.println("Training time: " + Utils.formatElapsedTime(System.currentTimeMillis() - start));
nn.saveToFile("mnist_weights_conv.nn");
Tensor[] testX = MNISTUtils.loadDataSetImages("t10k-images-idx3-ubyte", Integer.MAX_VALUE);
Tensor[] testY = MNISTUtils.loadDataSetLabels("t10k-labels-idx1-ubyte", Integer.MAX_VALUE);
Tensor[] testResult = nn.predict(Utils.reshapeAll(testX, 28, 28, 1));
System.out.println("Classification accuracy: " + Utils.format(Utils.classificationAccuracy(testResult, testY)));
}
}