]> piware.de Git - handwriting-recognition.git/blob - README.md
Add backpropagation and first round of learning
[handwriting-recognition.git] / README.md
1 # Resources
2
3 Basics:
4  - [Learn numpy](https://numpy.org/learn/)
5  - [MNIST database of handwritten digits](http://yann.lecun.com/exdb/mnist/)
6  - [Neuron](https://en.wikipedia.org/wiki/Artificial_neuron)
7  - [Perceptron](https://en.wikipedia.org/wiki/Perceptron)
8  - [Backpropagation](https://en.wikipedia.org/wiki/Backpropagation)
9  - [3Blue1Brown video series](https://www.youtube.com/playlist?list=PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi)
10
11 Too high-level for first-time learning, but apparently very abstract and powerful for real-life:
12  - [keras](https://keras.io/)
13  - [tutorial how to recognize handwriting with keras/tensorflow](https://data-flair.training/blogs/python-deep-learning-project-handwritten-digit-recognition/)
14
15 # Dependencies
16
17     sudo dnf install -y python3-numpy python3-matplotlib
18
19 # Steps
20
21  - Do the [NumPy quickstart tutorial](https://numpy.org/devdocs/user/quickstart.html); example:
22
23 ```py
24 import numpy as np
25 import matplotlib.pyplot as plt
26 grad = np.linspace(0,1,10000).reshape(100,100)
27 plt.imshow(grad, cmap='gray')
28 plt.show()
29
30 plt.imshow(np.sin(np.linspace(0,10000,10000)).reshape(100,100) ** 2, cmap='gray')
31 # non-blocking does not work with QT_QPA_PLATFORM=wayland
32 plt.show(block=False)
33 plt.close()
34 ```
35
36  - Get the handwritten digits training data with `./download-mnist.sh`
37
38  - Read the MNIST database into numpy arrays with `./read_display_mnist.py`. Plot the first ten images and show their labels, to make sure the data makes sense:
39
40    ![visualize training data](screenshots/mnist-visualize-training-data.png)
41
42  - Define the structure of the neural network: two hidden layers with parametrizable sizes. Initialize weights and biases randomly. This gives totally random classifications of course, but at least makes sure that the data structures and computations work:
43
44 ```
45 $ ./train.py
46 output vector of first image: [    0.         52766.88424917     0.             0.
47  14840.28619491 14164.62850135     0.          7011.882333
48      0.         46979.62976127]
49 classification of first image: 1 with confidence 52766.88424917019; real label 5
50 correctly recognized images after initialization: 10.076666666666668%
51 ```
52
53  - Add backpropagation algorithm and run a first training round. This is slow, as expected:
54  ```
55  $ time ./train.py
56 output vector of first image: [    0.         52766.88424917     0.             0.
57  14840.28619491 14164.62850135     0.          7011.882333
58      0.         46979.62976127]
59 classification of first image: 1 with confidence 52766.88424917019; real label 5
60 correctly recognized images after initialization: 10.076666666666668%
61 round #0 of learning...
62 ./train.py:18: RuntimeWarning: overflow encountered in exp
63   return 1 / (1 + np.exp(-x))
64 correctly recognized images: 14.211666666666666%
65
66 real    0m37.927s
67 user    1m19.103s
68 sys     1m10.169s
69 ```