{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"collapsed": true,
"id": "obZEfolG4xHj"
},
"source": [
"# Digit Classification with Neural Networks"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "4jtHr7EC4xHo"
},
"source": [
"## About this project\n",
"Our goal with this notebook is to build a neural network classifier in Keras to recognize hand-written digits. We will use the MNIST data set that consists of thousands of hand writtent numbers that span 0-9\n",
"\n",
"Similar to the process shown below:\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "9oLA4Heh4xHq"
},
"source": [
"## Import Libraries\n",
"\n",
"Here we import the libraries that we will be using throughout the notebook.\n",
"\n",
"The libraries that we will be using include:
\n",
"* [Numpy](https://www.numpy.org/)\n",
"* [Keras](https://keras.io/)\n",
"* [Matplotlib](https://matplotlib.org/) <-for plotting and visualizations"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "HB6rnLAz4xHs",
"outputId": "23a111f9-04d8-4279-e3b0-3767f6e6d73b",
"trusted": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"from keras.datasets import mnist\n",
"from keras.preprocessing.image import load_img, array_to_img\n",
"from keras.utils.np_utils import to_categorical\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "MrFQgWRQ4xH7"
},
"source": [
"## Load the data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 50
},
"colab_type": "code",
"id": "dH1okxfx4xH-",
"outputId": "e9e9361c-464e-409f-840e-960741361605",
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz\n",
"11493376/11490434 [==============================] - 1s 0us/step\n"
]
}
],
"source": [
"(X_train, y_train), (X_test, y_test) = mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 101
},
"colab_type": "code",
"id": "ACVJd8884xIG",
"outputId": "3b27fd04-95cb-41a0-c0af-696cdcb90ed8",
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"(60000, 28, 28)\n",
"(60000,)\n",
"(10000, 28, 28)\n",
"(10000,)\n"
]
}
],
"source": [
"print(type(X_train))\n",
"print(X_train.shape)\n",
"print(y_train.shape) #60k is the answers\n",
"print(X_test.shape) #10K entries\n",
"print(y_test.shape)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"collapsed": true,
"id": "JS8ISwOt4xIR"
},
"source": [
"## Understanding the image data format"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 302
},
"colab_type": "code",
"id": "YEEJkppA4xIT",
"outputId": "4e5b1aa8-12f7-4d9b-cc5b-e78dcb7c62e0",
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(28, 28)\n",
"The answer is 5\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAADoBJREFUeJzt3X2MXOV1x/HfyXq9jo1JvHHYboiLHeMEiGlMOjIgLKCiuA5CMiiKiRVFDiFxmu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