-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_nn.wls
122 lines (73 loc) · 2.57 KB
/
mnist_nn.wls
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
#!/usr/bin/env wolframscript
(* ::Package:: *)
(* ::Section:: *)
(*LeNet and Digit Recognition*)
(* ::Text:: *)
(*A brief overview of the Wolfram Language neural net framework by showing how to train a net that takes an input image of a handwritten single-digit number and then predicts the number. The dataset we are training on is the classic MNIST dataset, and we will train a variant of LeNet. *)
(* ::Text:: *)
(*Obtain the MNIST dataset, which contains 60,000 training and 10,000 test images:*)
(* ::Input:: *)
(*trainingData=ResourceData["MNIST","TrainingData"];*)
(*testData=ResourceData["MNIST","TestData"];*)
(* ::Text:: *)
(*Display a few random examples from the training set:*)
(* ::Input:: *)
(*RandomSample[trainingData,5]*)
(* ::Text:: *)
(*Construct an untrained version of LeNet:*)
(* ::Input:: *)
(*lenet=*)
(*NetChain[*)
(*{ConvolutionLayer[64,{3,3}],*)
(*ElementwiseLayer[Ramp],*)
(*ConvolutionLayer[32,{3,3}],*)
(*ElementwiseLayer[Ramp],*)
(*FlattenLayer[],*)
(*LinearLayer[10],*)
(*SoftmaxLayer[]},*)
(*"Input"->NetEncoder[{"Image",{28,28},ColorSpace->"Grayscale"}],*)
(*"Output"->NetDecoder[{"Class",Range[0,9]}]*)
(*];*)
(* ::Text:: *)
(*It is extremely easy to train a network like LeNet from scratch. RefLink[NetTrain,paclet:ref/NetTrain] takes care of many details of the training process automatically, such as selecting an appropriate loss function, attaching encoders and decoders and choosing a batch size. Here is what it looks like.*)
(* ::Text:: *)
(*Number of parameters to train:*)
(* ::Input:: *)
(*Information[lenet,"ArraysTotalElementCount"]*)
(* ::Text:: *)
(*Train LeNet from scratch:*)
(* ::Input:: *)
(*digitRecognition=*)
(*NetTrain[*)
(*lenet,*)
(*trainingData,*)
(*ValidationSet->testData,*)
(*TargetDevice->"GPU",*)
(*MaxTrainingRounds->3*)
(*]*)
(* ::Input:: *)
(*digitRecognition = NetModel["LeNet Trained on MNIST Data"];*)
(* ::Text:: *)
(*Note: \[Bullet] Mutual entropy was chosen as loss function automatically.*)
(* \[Bullet] SGD method and parameters were chosen automatically.*)
(* \[Bullet] Weight initialization was done automatically.*)
(* ::Text:: *)
(*Testing the trained network. *)
(* ::Input:: *)
(*test=RandomSample[testData,10][[All,1]]*)
(* ::Input:: *)
(*digitRecognition[test]*)
(* ::Text:: *)
(*A comprehensive classifier test.*)
(* ::Input:: *)
(*cm=ClassifierMeasurements[digitRecognition, testData]*)
(* ::Text:: *)
(*Accuracy and error rate.*)
(* ::Input:: *)
(*cm["Accuracy"]*)
(* ::Input:: *)
(*cm["Error"]*)
(* ::Text:: *)
(*Confusion matrix.*)
(* ::Input:: *)
(*cm["ConfusionMatrixPlot"]*)