]> piware.de Git - handwriting-recognition.git/blob - train.py
Process many images in parallel
[handwriting-recognition.git] / train.py
1 #!/usr/bin/python3
2
3 import mnist
4 import nnet
5
6 train_images, train_labels, rows, cols = mnist.load('train-images-idx3-ubyte', 'train-labels-idx1-ubyte')
7 test_images, test_labels, rows2, cols2 = mnist.load('t10k-images-idx3-ubyte', 't10k-labels-idx1-ubyte')
8 assert rows == rows2
9 assert cols == cols2
10 num_train = train_images.shape[1]
11 nnet_batch = 10000
12
13 # neural network structure: two hidden layers, one output layer
14 #                   (input)--> [Linear->Sigmoid] -> [Linear->Sigmoid] -->(output)
15 # handle 10,000 vectors at a time
16 Z1 = nnet.LinearLayer(input_shape=(rows * cols, nnet_batch), n_out=20)
17 A1 = nnet.SigmoidLayer(Z1.Z.shape)
18 Z2 = nnet.LinearLayer(input_shape=A1.A.shape, n_out=16)
19 A2 = nnet.SigmoidLayer(Z2.Z.shape)
20 ZO = nnet.LinearLayer(input_shape=A2.A.shape, n_out=10)
21 AO = nnet.SigmoidLayer(ZO.Z.shape)
22 net = (Z1, A1, Z2, A2, ZO, AO)
23
24 res = nnet.forward(net, train_images[:, 0:10000])
25 print(f'output vector of first image: {res[:, 0]}')
26 digit, conf = nnet.classify(res[:, 0])
27 print(f'classification of first image: {digit} with confidence {conf}; real label {test_labels[0]}')
28 print(f'correctly recognized images after initialization: {nnet.accuracy(net, test_images, test_labels)}%')
29
30 train_y = nnet.label_vectors(train_labels, 10)
31 for i in range(100):
32     for batch in range(0, num_train, nnet_batch):
33         cost = nnet.train(net, train_images[:, batch:(batch + nnet_batch)], train_y[:, batch:(batch + nnet_batch)])
34     print(f'cost after training round {i}: {cost}')
35 print(f'correctly recognized images after training: {nnet.accuracy(net, test_images, test_labels)}%')