Progress report

Neural Networks
Sara Jones

Abstract

This project will take hand written digits 0 to 9 and recognize them through a computer-learning program. The neural network will require a training sets to 'teach' the network how to recognize the indiviualites between the diffrent digits and return the proper identification. The Network will be required to know the diffrences between the diffrent styles of handwriting (such as bars or no bars in sevens) and account for other factors such as messy handwriting. These factors will be determined by giving weights to the characteristics of each digits (accounting for various stylization diffrences) to detrmine what factors are important for identification of a digit and what can be given less weight or even ignored in identifcation.

Base question

The base question for this project is taking hand written numbers and recognizing them through a neural network. This will require a computerized learning system that must be trained to recognize the digits 0 to 9. This network should have over 90% accuracy when recognizing hand written digits.

Additional questions

This project will also attempt to take hand written numbers that are more then one digit (10 or grater) and recognize them. This will have to take in to account stylization factors such as comas in writing larger numbers and spacing between digits. This network will also attempt to integrate more hidden layers in to the network to train and work with more accuracy and efficiency.

Packages and Libraries Needed


In [1]:
import numpy as np
import math
import random
import string
import seaborn as sns
from scipy import optimize
from scipy.interpolate import griddata

In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.html.widgets import interact


:0: FutureWarning: IPython widgets are experimental and may change in the future.

In [3]:
from sklearn.datasets import load_digits
digits = load_digits()
print(digits.data.shape)


(1797, 64)

Core Algorithms

Neuron


In [4]:
"""Neron for for detrminig what is put in and the weights of certin aspects of the input"""
def __init__(self, n_inputs ):
    self.n_inputs = n_inputs
    self.set_weights( [random.uniform(0,1) for x in range(0,n_inputs+1)] )
            
def sum(self, inputs ):
        return sum(val*self.weights[i] for i,val in enumerate(inputs))

def set_weights(self, weights ):
    self.weights = weights
        
def str_(self):
    return ( str(self.weights[:-1]),str(self.weights[-1]) )

Neuron Layer


In [5]:
def init(self, n_neurons, n_inputs):
    self.n_neurons = n_neurons
    self.neurons = [Neuron( n_inputs ) for _ in range(0,self.n_neurons)]

def str_(self):
    return 'Layer:\n\t'+'\n\t'.join([str(neuron) for neuron in self.neurons])+''

Training Set


In [6]:
def learn(self, X, y, learning_rate=0.2, epochs=10000):
    X = np.atleast_2d(X)
    temp = np.ones([X.shape[0], X.shape[1]+1])
    temp[:, 0:-1] = X  
    X = temp
    y = np.array(y)

    for i in range(epochs):
        k = np.random.randint(X.shape[0])
        a = [X[i]]

        for j in range(len(self.weights)):
            a.append(self.activation(np.dot(a[j], self.weights[j])))
        error = y[i] - a[-1]
        deltas = [error * self.activation_deriv(a[-1])]

        for i in range(len(a) - 2, 0, -1):
            deltas.append(deltas[-1].dot(self.weights[i].T)*self.activation_deriv(a[i]))
        deltas.reverse()
        
        for i in range(len(self.weights)):
            layer = np.atleast_2d(a[i])
            delta = np.atleast_2d(deltas[i])
            self.weights[i] += learning_rate * layer.T.dot(delta)
        
        return self.weights[i]

Visualizations

Can be used for training set and learning to recognize the characteristics of each digit. This will be used as part of the training set.


In [7]:
def show_digit(i):
    k = plt.matshow(digits.images[i], cmap='gray_r');
    return k

In [8]:
print (show_digit(0))


AxesImage(48,28.8;248x248)

In [9]:
print (show_digit(1))


AxesImage(48,28.8;248x248)

In [10]:
print (show_digit(2))


AxesImage(48,28.8;248x248)

In [11]:
print (show_digit(3))


AxesImage(48,28.8;248x248)

In [12]:
print (show_digit(4))


AxesImage(48,28.8;248x248)

In [13]:
print (show_digit(5))


AxesImage(48,28.8;248x248)

In [14]:
print (show_digit(6))


AxesImage(48,28.8;248x248)

In [15]:
print (show_digit(7))


AxesImage(48,28.8;248x248)

In [16]:
print (show_digit(8))


AxesImage(48,28.8;248x248)

In [17]:
print (show_digit(9))


AxesImage(48,28.8;248x248)

In [18]:
interact(show_digit, i=(0,100));