Overview

This tutorial covers defining and training a neural net to recognize hand-written digits. We will use the popular, classic data-set MNIST.
Also, it is loosely based on an already existing Kaggle tutorial by Kirill Kliavin.
What I am aiming for is to create a tutorial with lower entry level, even if you do not know any of the AI lingo.

You can find the full implementation on my github:
Digit Recognizer with Tensorflow

Obtain data.

You can download the data set from here: Kaggle Digit Recognizer competition

Imports.

Make sure you have all those libraries installed:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import tensorflow as tf

Helper tools.

You can just copy paste it right after imports.

class Tools:
    @staticmethod
    def display(image, width, height):
        image = image.reshape(width, height)
        plt.imshow(image, cmap=cm.get_cmap("binary"))

    @staticmethod
    def dense_to_hot(labels_dense, num_classes):
        num_labels = labels_dense.shape[0]
        index_offset = np.arange(num_labels) * num_classes
        labels_one_hot = np.zeros((num_labels, num_classes))
        labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
        return labels_one_hot

    @staticmethod
    def weight_variable(shape):
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)

    @staticmethod
    def bias_variable(shape):
        initial = tf.constant(0.1, shape=shape)
        return tf.Variable(initial)

    @staticmethod
    def convolution2d(x, w):
        return tf.nn.conv2d(x, w, strides=[1,1,1,1], padding='SAME')

    @staticmethod
    def max_pool_2x2(x):
        return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')

Define hyperparameters.

We can tweak them later.

LEARNING_RATE = 1e-4
TRAINING_ITERATIONS = 1000
DROPOUT = 0.5
BATCH_SIZE = 50
VALIDATION_SIZE = 2000
IMAGE_TO_DISPLAY = 10

Pre-process data.

Store training data in memory.

data = pd.read_csv('../input/train.csv')

Allocate values, convert to float and normalize.

images = data.iloc[:,1:].values
images = images.astype(np.float)
images = np.multiply(images, 1.0 / 255.0)

Construct layers of Artificial Neural Net.

Train.

Save model.

Print confusion matrix.

But what does it mean?

Parameters of our classifier: - - - Layers - - -



blog comments powered by Disqus

Published

31 January 2017

Category

tutorial

Tags