Skip to content

Instantly share code, notes, and snippets.

@JoshZastrow
Created October 1, 2018 21:42
Show Gist options
  • Save JoshZastrow/1a562b2f1f7aebdbc9f135abf4e936fe to your computer and use it in GitHub Desktop.
Save JoshZastrow/1a562b2f1f7aebdbc9f135abf4e936fe to your computer and use it in GitHub Desktop.
Log Regression Classifier Model and Training Algorithm
import numpy as np
class classifier():
def __init__(self, lr=0.001, num_dims=6):
self.lr = lr
self.params = {}
self.W = np.random.rand(num_dims, 1)
self.b = np.ones((1,1))
self.dW = np.zeros_like(self.W.shape)
self.db = np.zeros_like(self.b.shape)
self.metrics = {'error': []}
def fit(self, x, Y, predict=False):
m = x.shape[0]
W = self.W
b = self.b
z = np.dot(x, W) + b
a = 1 / (1 + np.exp(-z))
if predict: return a
C = (-np.dot(np.log(a).T, Y) - np.dot(np.log(1 - a).T, (1 - Y))) / m
self.metrics['error'] += [C]
dz = a - Y
self.dW = np.dot(x.T, dz)
self.db = np.sum(dz) / m
self.W -= self.lr * self.dW
self.b -= self.lr * self.db
model = classifier()
m = x.shape[0]
epoch = range(20)
hist = {'epoch': [], 'error': []}
for e in epoch:
for i in range(10, m, 10):
x_batch = x_train[i-10:i]
y_batch = y_train[i-10:i]
model.fit(x_batch, y_batch)
hist['epoch'] += [e]
hist['error'] += [np.mean(model.metrics['error'])]
model.metrics['error'] = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment