In [66]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
from mpl_toolkits.mplot3d import Axes3D
import IPython.html.widgets as widg
%matplotlib inline
In [23]:
class Network:
def __init__(self, shape):
self.shape = np.array(shape) #shape is array-like, i.e. (2,3,4) is a 2 input, 3 hidden node, 4 output network
self.weights = [np.random.ranf((self.shape[i],self.shape[i-1]))*.1 for i in range(1,len(self.shape))]
self.biases = [np.random.ranf((self.shape[i],))*.1 for i in range(1,len(self.shape))]
self.errors = [np.random.ranf((self.shape[i],)) for i in range(1,len(self.shape))]
self.eta = .2
def sigmoid(self, inputs):
return 1/(1+np.exp(-inputs))
def feedforward(self, inputs):
assert inputs.shape==self.shape[0] #inputs must feed directly into the first layer.
self.activation = [np.zeros((self.shape[i],)) for i in range(len(self.shape))]
self.activation[0] = inputs
for i in range(1,len(self.shape)):
self.activation[i]=self.sigmoid(np.dot(self.weights[i-1],self.activation[i-1])+self.biases[i-1])
return self.activation[-1]
def comp_error(self, answer):
assert answer.shape==self.activation[-1].shape
self.errors[-1] = (self.activation[-1]-answer)*np.exp(np.dot(self.weights[-1],self.activation[-2])+self.biases[-1])/(np.exp(np.dot(self.weights[-1],self.activation[-2])+self.biases[-1])+1)**2
for i in range(len(self.shape)-2, 0, -1):
self.errors[i-1] = self.weights[i].transpose().dot(self.errors[i])*np.exp(np.dot(self.weights[i-1],self.activation[i-1])+self.biases[i-1])/(np.exp(np.dot(self.weights[i-1],self.activation[i-1])+self.biases[i-1])+1)**2
def grad_descent(self):
for i in range(len(self.biases)):
self.biases[i]=self.biases[i]-self.eta*self.errors[i]
for i in range(len(self.weights)):
for j in range(self.weights[i].shape[0]):
for k in range(self.weights[i].shape[1]):
self.weights[i][j,k] = self.weights[i][j,k] - self.eta*self.activation[i][k]*self.errors[i][j]
def train(self, inputs, answer):
self.feedforward(inputs)
self.comp_error(answer)
self.grad_descent()
In [3]:
n1 = Network([2,15,1])
print n1.feedforward(np.array([1,2]))
for i in range(1000):
n1.train(np.array([1,200]), np.array([.5]))
print n1.feedforward(np.array([1,2]))
In [4]:
from sklearn.datasets import load_digits
digits = load_digits()
print(digits.data[0]*.01)
In [14]:
iden = np.eye(10)
acc = np.zeros((400,))
num = Network([64, 6, 10])
print num.feedforward(digits.data[89]*.01)
for i in range(400):
for dig, ans in zip(digits.data[1:1000],digits.target[1:1000]):
num.train(dig*.01,iden[ans])
cor = 0
tot = 0
for dig, ans in zip(digits.data, digits.target):
if num.feedforward(dig*.1).argmax()==ans:
cor += 1
tot += 1
acc[i] = cor/float(tot)
print num.feedforward(digits.data[90]*.01), digits.target[90]
In [25]:
plt.figure(figsize=(15,10))
plt.plot(np.linspace(0,400,400),acc)
Out[25]:
In [37]:
iden = np.eye(10)
acc = np.zeros((14,500))
f = plt.figure(figsize = (15,50))
for h in range(1,15):
num = Network([64, h, 10])
for i in range(500):
for dig, ans in zip(digits.data[1:1000],digits.target[1:1000]):
num.train(dig*.01,iden[ans])
cor = 0
tot = 0
for dig, ans in zip(digits.data, digits.target):
if num.feedforward(dig*.1).argmax()==ans:
cor += 1
tot += 1
acc[h-1,i] = cor/float(tot)
plt.subplot(14,1,h)
plt.plot(np.linspace(0,1,500),acc[h-1])
In [41]:
np.savetxt("Accuracy_Data_run_1.dat", acc)
In [68]:
def plot_epochs(az_angle, eleva):
fig = plt.figure(figsize=(15, 10))
ax = fig.add_subplot(111, projection='3d')
X, Y = np.meshgrid(np.linspace(0,500,500), np.linspace(0,14, 14))
ax.plot_surface(X, Y, acc)
ax.view_init(elev=eleva, azim=az_angle)
In [69]:
widg.interact(plot_epochs, az_angle=(0, 360, 1), eleva=(0,20,1))
In [ ]: