Skip to content

Instantly share code, notes, and snippets.

@cedias
Created July 24, 2017 20:57
Show Gist options
  • Select an option

  • Save cedias/1ee68af3002877dadaa56870a7789d85 to your computer and use it in GitHub Desktop.

Select an option

Save cedias/1ee68af3002877dadaa56870a7789d85 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
class NNet(object):
def __init__(self, n_in, n_hidden, n_out):
self.n_in = n_in
self.n_hidden = n_hidden
self.n_out = n_out
self.W1 = np.random.randn(n_in, n_hidden)
self.W2 = np.random.randn(n_hidden, n_out)
self.b1 = np.random.randn(n_hidden,)
self.b2 = np.random.randn(n_out,)
def sigmoid(self, z):
return 1/(1 + np.exp(-z))
def sig_prime(self, z):
return (np.exp(-z))/((1+np.exp(-z))**2)
def propagate_forward(self, X):
self.z1 = np.dot(self.W1.T, X) + self.b1
self.a1 = self.sigmoid(self.z1)
self.z2 = np.dot(self.W2.T, self.a1) + self.b2
self.a2 = self.sigmoid(self.z2)
return self.a2
def cost(self, y, y_hat):
#y_hat = np.round(y_hat)
return np.mean((y - y_hat)**2)
def accuracy(self, y, y_hat):
y_hat = np.round(y_hat)
return 100 - (np.sum((y - y_hat)**2)/len(y))*100
def cost_grad(self, X, y):
y_hat = self.propagate_forward(X)
d2 = np.multiply(self.sig_prime(self.z2), -(y - y_hat))
gJ_W2 = np.matrix(np.multiply(self.a1.T, d2))
d1 = np.dot(self.W2, d2)*self.sig_prime(self.z1)
gJ_W1 = np.dot(np.matrix(X).T, np.matrix(d1))
return [gJ_W1, d1, gJ_W2, d2]
m = 1000
n = 2
X = np.zeros((m, n))
y = np.zeros((m,1))
import random
import math
i = 0
for r, theta in zip(np.linspace(0, 5, num=m), np.linspace(0, 8 * math.pi, num=m)):
r += random.random()
X[i] = [r * math.cos(theta), r * math.sin(theta)]
if i < 333:
y[i] = 0
elif i < 666:
y[i] = 1
else:
y[i] = 1
i += 1
nnet = NNet(n, 5, 1)
learning_rate = 0.08
improvement_threshold = 0.995
cost = np.inf
xs = []
ys = []
accs = []
itera = 0
while cost > 0:
cost = nnet.cost(y, [nnet.propagate_forward(x_train) for x_train in X])
acc = nnet.accuracy(y, [nnet.propagate_forward(x_train) for x_train in X])
if itera % 100 == 0:
xs.append(itera)
ys.append(cost)
accs.append(acc)
print("--- iter ",itera, " ---")
print("Cost", cost)
print("Acc:", acc ,"\n","-"*15)
if itera >= 1000:
print("Gradient descent is taking too long, giving up.")
break
cost_grads = [nnet.cost_grad(x_train, y_train) for x_train, y_train in zip(X, y)]
gW1 = [grad[0] for grad in cost_grads]
gb1 = [grad[1] for grad in cost_grads]
gW2 = [grad[2] for grad in cost_grads]
gb2 = [grad[3] for grad in cost_grads]
nnet.W1 -= np.mean(gW1, axis=0)/2 * learning_rate
nnet.b1 -= np.mean(gb1, axis=0)/2 * learning_rate
nnet.W2 -= np.mean(gW2, axis=0).T/2 * learning_rate
nnet.b2 -= np.mean(gb2, axis=0)/2 * learning_rate
itera += 1
plt.plot(ys)
plt.show()
plt.plot(accs)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment