Created
March 22, 2016 09:41
-
-
Save RationalAsh/4485e789eb338a535bce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import numpy as np | |
from numpy import dot, random | |
import matplotlib.pyplot as plt | |
import time | |
def softmax(x): | |
exps = np.nan_to_num(np.exp(x)) | |
return exps/np.nan_to_num(np.sum(exps)) | |
class classifier(object): | |
def __init__(self, input_size, classes, debug=True): | |
'''Initialize the classifier with the input vector size | |
and the number of classes required''' | |
self.input_size = input_size | |
self.classes = classes | |
self.W = random.randn(input_size, classes) | |
self.b = random.randn(classes, 1) | |
self.DEBUG = debug | |
self.cost_over_time = np.zeros(100) | |
def setDebug(lev=True): | |
self.DEBUG = lev | |
def getCostOverTime(): | |
return self.cost_over_time | |
def Y(self, train_data): | |
'''The model that predicts the class of the input vectors using | |
the current parmeters.''' | |
a = dot(train_data, self.W) + np.tile(self.b.flatten(), (len(train_data), 1)) | |
return np.array([softmax(x) for x in a]) | |
def costf(self, train_data, train_targets): | |
'''The traindata should contain the training inputs and | |
train_targets the target vectors. Evaluates the cross entropy cost | |
with the current set of data and parameters''' | |
Y = self.Y(train_data) | |
J = -sum([dot(t, ly) for t,ly in zip(train_targets, np.nan_to_num(np.log(Y)))]) | |
return J | |
def grad_costf(self, train_data, train_targets): | |
'''Computes the gradient of the cost function for a batch. This one was hell | |
to calculate by hand but I did it.''' | |
Y = self.Y(train_data) | |
gradW = dot(train_data.T, (Y - train_targets)) | |
gradb = np.reshape(np.sum(Y - train_targets, axis=0), (self.classes, 1)) | |
return gradW, gradb | |
def GD(self, train_data, train_targets, epochs=30, eta=0.01): | |
'''Trains the classifier using gradient descent. Uses the entire | |
dataset for a single epoch. Maybe I\'ll implement the stochastic | |
version soon.''' | |
#Reserve the array | |
self.cost_over_time = np.zeros(epochs) | |
#Start the training | |
for i in range(epochs): | |
print("Training Epoch %d..." %(i)) | |
gradW, gradb = self.grad_costf(train_data, train_targets) | |
self.W = self.W - eta*gradW | |
self.b = self.b - eta*gradb | |
if self.DEBUG: | |
cost = self.costf(train_data, train_targets) | |
self.cost_over_time[i] = cost | |
print("Cost: "+str(cost)) | |
print("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment