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.
The base question fir this project is taking hand written numbers and recognizing the through a neural network. This will require a computerized learning system that must be trained to recognize the digits. This network should have over 90% accuracy when recognizing hand written digits.
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.
In [1]:
import numpy as np
import math
import random
import string
from scipy import optimize
In [2]:
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.html.widgets import interact
In [3]:
from sklearn.datasets import load_digits
digits = load_digits()
print(digits.data.shape)
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]) )
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])+''
In [1]:
def learn(network, 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]
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):
plt.matshow(digits.images[i]);
In [8]:
print (show_digit(0))
In [9]:
print (show_digit(1))
In [10]:
print (show_digit(2))
In [11]:
print (show_digit(3))
In [12]:
print (show_digit(4))
In [13]:
print (show_digit(5))
In [14]:
print (show_digit(6))
In [15]:
print (show_digit(7))
In [16]:
print (show_digit(8))
In [17]:
print (show_digit(9))
In [18]:
interact(show_digit, i=(0,100));