In [ ]:
from __future__ import division
import numpy as np
import gzip
try:
import cPickle as pickle
except:
import pickle
import math
import random
In [ ]:
with gzip.open('mnist.pkl.gz', 'rb') as f:
try:
file = pickle.load(f)
except:
f.seek(0)
file = pickle.load(f, encoding="latin1")
training, validation, test = [list(zip(*data)) for data in file]
for data in (training, validation, test):
for i in range(len(data)):
data[i] = (np.reshape(data[i][0], (-1, 1)), data[i][1])
CLASSES = 10
FEATURES = 784
In [ ]:
def show_image(data):
from matplotlib import pyplot as plt
data = np.reshape(data, (28, 28))
plt.imshow(data, cmap='gray_r')
plt.show()
In [ ]:
class FeedForwardNN():
def __init__(self, training, hidden_size, epochs=10, batch_size=1, learning_rate=0.01):
# set up some weights and biases, and train that shit!
pass
# write the sigmoid function for numpy arrays
def sigmoid(self, x):
pass
# write the derivative of the sigmoid function, s(x)*(1-s(x))
def dsigmoid(self, x):
pass
# write the derivative of the cost function for a single training example (this is simple)
# remember the cost function is the L2 norm squared of y - y_hat
def cost_derivative(self, output_activations, y):
pass
# the fun part! write code to compute the gradient of the cost function for a single example
# with regard to all of the weights and biases
def backprop(self, x, y):
# make db and dw, the same size/shape as biases and weights
# pass your example forward through the network, keeping track of the pre-sigmoid and post-sigmoid activations
# compute delta, the derivative of the cost function at the last step, times dsigmoid of the last set of activatins
# put it in the gradients
# for layers L-1 to 0, set the gradients!
# return that tuple homie
pass
# write code that, given an example (and weights already trained), returns a class (0-9) for the image
def classify(self, example):
pass
In [ ]:
def one_hot(nfeatures, val):
vec = np.zeros((nfeatures, 1))
vec[val] = 1
return vec
#one_hot_training = [(x, one_hot(10, y)) for x, y in training]
#m = FeedForwardNN(one_hot_training, 15, epochs=1000)