]> piware.de Git - handwriting-recognition.git/blob - train.py
5ff76669709f1d2a9549dd25b1f16a7b4c97f632
[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=80)
17 A1 = nnet.SigmoidLayer(Z1.Z.shape)
18 ZO = nnet.LinearLayer(input_shape=A1.A.shape, n_out=10)
19 AO = nnet.SigmoidLayer(ZO.Z.shape)
20 net = (Z1, A1, ZO, AO)
21
22 res = nnet.forward(net, test_images[:, 0:10000])
23 print(f'output vector of first image: {res[:, 0]}')
24 digit, conf = nnet.classify(res[:, 0])
25 print(f'classification of first image: {digit} with confidence {conf}; real label {test_labels[0]}')
26 print(f'correctly recognized images after initialization: {nnet.accuracy(net, test_images, test_labels)}%')
27
28 train_y = nnet.label_vectors(train_labels, 10)
29 for i in range(100):
30     for batch in range(0, num_train, nnet_batch):
31         cost = nnet.train(net, train_images[:, batch:(batch + nnet_batch)], train_y[:, batch:(batch + nnet_batch)], learning_rate=1)
32     print(f'cost after training round {i}: {cost}')
33 print(f'correctly recognized images after training: {nnet.accuracy(net, test_images, test_labels)}%')
34
35 res = nnet.forward(net, test_images[:, 0:10000])
36 print(f'output vector of first image: {res[:, 0]}')
37 digit, conf = nnet.classify(res[:, 0])
38 print(f'classification of first image: {digit} with confidence {conf}; real label {test_labels[0]}')