{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "The code writes a feedforward neural network from scratch in raw Python in just a few dozen lines of code! Following the online book, we will use the code to try to classify handwritten digits as taken from the MNIST data set (a very common test data set)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Start by loading the MNIST data using mnist_loader.py . This will load three subsets of the data: the training set, the validation set, and the test set. You will need to grab the file mnist.pkl.gz first." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "length of training data: 50000\n", "length of validation data: 10000\n", "length of test data: 10000\n" ] } ], "source": [ "import mnist_loader\n", "import pickle\n", "training_data, validation_data, test_data = mnist_loader.load_data_wrapper()\n", "print('length of training data: ',len(training_data))\n", "print('length of validation data: ',len(validation_data))\n", "print('length of test data: ',len(test_data))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each data set is returned as a list of tuples, with the first element of the tuple being the image data, and the second being the truth value. For the training data, the truth is encoded in a 10-element vector, with all entries 0 except for the entry for the correct digit, which is set to 1. For the validataion and test set, the truth value is just an integer with the true value>\n", "
\n", "For use with the neural network, the input image array, which is a 28x28 image, is unwrapped into a single vector of length 784; the simple neural network just treats the image as a series of unconnected intensity value (we can do better than that later!). To display the images, you can use numpy.reshape to reshape the images to 28x28 \n", "
\n",
"Looking at code below, make sure you understand how the training and test data are stored.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"contents of one training object (784, 1) (10, 1)\n",
"[[0.]\n",
" [0.]\n",
" [0.]\n",
" [0.]\n",
" [0.]\n",
" [1.]\n",
" [0.]\n",
" [0.]\n",
" [0.]\n",
" [0.]]\n",
"contents of one test object (784, 1)