"Make Your Own Neural Network" in Python

This section provides a tutorial example on how to download and run the Python code associated with Tariq's book 'Make Your Own Neural Network'. Tariq's code creates a simple neural network model, trains and tests it with the MNIST database.

If you want to build your first neural network model in Python using the the MNIST database, you should start with the code example associated with the tutorial book "Make Your Own Neural Network" by Tariq Rashi.

Tariq's Python code was written for the Jupyter Notebook platform and available from GitHub. But you can download and convert it to standard Python by following this tutorial.

1. Go to Tariq's GitHub Website at https://github.com/makeyourownneuralnetwork.

2. Open "makeyourownneuralnetwork" project folder and download the Jupyter Notebook file, part2_neural_network_mnist_data.ipynb, which store Python source code and other information together in JSON format. You can take a peek at the download file:

herong$ ls -l *.ipynb
...  9033 Jul 15 22:09 part2_neural_network_mnist_data.ipynb

herong$ head part2_neural_network_mnist_data.ipynb
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
  "collapsed": true
   },
   "outputs": [],
   "source": [

3. Install the *.ipynb to *.py conversion tool, and convert Tariq's code to standard Python source code:

herong$ sudo pip3 install ipynb-py-convert
...
Successfully installed ipynb-py-convert-0.4.5

herong$ ipynb-py-convert part2_mnist_data_set.ipynb tariq.py

herong$ head tariq.py
# %%
# python notebook for Make Your Own Neural Network
# code for a 3-layer neural network, and code for learning the MNIST dataset
# (c) Tariq Rashid, 2016
# license is GPLv2

# %%
import numpy
# scipy.special for the sigmoid function expit()
import scipy.special

4. Comment out 3 lines that are related to the matplotlib library, which is not used in the Python code at all with any text editor:

herong$ vi tariq.py

...
#hy import matplotlib.pyplot
#hy # ensure the plots are inside this notebook, not an external window
#hy %matplotlib inline
...

5. Make sure MNIST database files are in the "mnist_dataset" sub-directory as described in the previous tutorial:

herong$ ls -l mnist_dataset/
-rw-r--r--  1 herong  staff   18289443 Jul 16 15:07 mnist_test.csv
-rw-r--r--  1 herong  staff  109575994 Jul 16 15:08 mnist_train.csv

6. Finally, run Tariq's code

herong$ python3 tariq.py
performance =  0.9751

Wow! Tariq's code worked well. It created a simple neural network model in Python, trained the model with 60,000 samples of handwritten digits, and tested the trained model with 10,000 samples. The output shows that 97.51% of test samples were correctly recognized.

In the next section, we will walk through Tariq's code and learn the neural network model used in the code.

Table of Contents

 About This Book

 Deep Playground for Classical Neural Networks

 Building Neural Networks with Python

Simple Example of Neural Networks

 What Is The MNIST Database

"Make Your Own Neural Network" in Python

 Walk-Through on Tariq's Code

 TensorFlow - Machine Learning Platform

 PyTorch - Machine Learning Platform

 CNN (Convolutional Neural Network)

 RNN (Recurrent Neural Network)

 GNN (Graph Neural Network)

 References

 Full Version in PDF/EPUB