/
mnist.nim
65 lines (56 loc) · 1.71 KB
/
mnist.nim
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
import nn
import os, strutils, future
proc loadImages(fname: string, size: int): Matrix[float] =
var f: File = open(fname, FileMode.fmRead)
defer: close(f)
var
images: Matrix[float] = matrix[float](colMajor, size, 28 * 28,
newSeq[float](size * 28 * 28))
pos = 0
while not f.endOfFile:
images[pos] = vector[float](f.readLine().split().map(parseFloat).map(
(val) => val / 255.0)
)
pos.inc()
result = images
proc loadLabels(fname: string, size: int): Matrix[float] =
var f: File = open(fname, FileMode.fmRead)
defer: close(f)
var
labels: Matrix[float] = matrix[float](colMajor, size, 10,
newSeq[float](size * 10))
pos = 0
while not f.endOfFile:
labels[pos] = oneHot[float](10, parseInt(f.readLine()))
pos.inc()
result = labels
network:
layers:
Dense[28 * 28, 128]
ReLU
Dense[128, 128]
ReLU
Dense[128, 10]
Softmax
minimize: CrossEntropy
optimize: SGD(0.5)
echo "loading training data..."
var
trainInput = loadImages("../data/mnist/train-images.txt", 60000)
trainOutput = loadLabels("../data/mnist/train-labels.txt", 60000)
epochs = 3
batchSize = 64
options = newOptions(epochs, batchSize)
options.addMeasure(newAccuracy())
options.addMeasure(newMeanLoss())
network.train(trainInput, trainOutput, options)
echo "loading test data..."
var
testInput = loadImages("../data/mnist/test-images.txt", 10000)
testOutput = loadLabels("../data/mnist/test-labels.txt", 10000)
var
probs = network.probs(testInput)
meanloss = network.meanLossFromProbs(probs, testOutput)
accuracy = network.accuracyFromProbs(probs, testOutput)
echo "mean loss = " & $meanloss
echo "accuracy = " & $accuracy