Created
August 16, 2016 11:30
-
-
Save ShivendraAgrawal/5b59f593e69aeef87aabe54babf00c4a to your computer and use it in GitHub Desktop.
A single layer neural network script using numpy performed on iris set from scikit-learn
This file contains hidden or 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
import numpy as np | |
from sklearn import datasets | |
# sigmoid function | |
def activation(x,derivative=False): | |
if(derivative==True): | |
return x*(1-x) | |
return 1/(1+np.exp(-x)) | |
iris = datasets.load_iris() | |
# Reshaping to convert the inputs & outputs to 2D array | |
# Numpy does matrix multiplication for dot products of 2D arrays and | |
# inner product of vectors for 1D arrays | |
# input dataset (We have 150 examples of flowers with 4 features each) | |
x = np.reshape(iris['data'], (150, 4, 1)) | |
# output dataset (Converting the outputs to 1-hot encoded outputs) | |
real_y = iris['target'] | |
y = np.zeros((real_y.size, real_y.max()+1), dtype = np.float) | |
y[np.arange(real_y.size),real_y] = 1 | |
y = np.reshape(y, (150, 3, 1)) | |
m = 150.0 | |
Lambda = 0.2 | |
alpha = 0.1 | |
# seed random numbers to make calculation | |
# deterministic (just a good practice) | |
np.random.seed(1) | |
# initialize weights randomly with mean 0 | |
theta1 = 2*np.random.random((6,4)) - 1 | |
theta2 = 2*np.random.random((3,6)) - 1 | |
print(theta1) | |
print(theta2) | |
Delta2 = np.zeros(theta2.shape, dtype=np.float) | |
Delta1 = np.zeros(theta1.shape, dtype=np.float) | |
D2 = np.zeros(theta2.shape, dtype=np.float) | |
D1 = np.zeros(theta1.shape, dtype=np.float) | |
for j in xrange(5000): | |
cumulative_del3 = np.zeros(y[0].shape, dtype=np.float) | |
for i, example in enumerate(x): | |
# Feed forward through layers 1, 2, and 3 | |
a1 = x[i] | |
a2 = activation(np.dot(theta1, a1)) | |
a3 = activation(np.dot(theta2, a2)) | |
# how much did we miss the target value? | |
del3 = a3 - y[i] | |
cumulative_del3 += del3 | |
del2 = np.dot(theta2.T, del3) * activation(a2, derivative=True) | |
Delta2 += np.dot(del3, a2.T) | |
Delta1 += np.dot(del2, a1.T) | |
cumulative_del3 = cumulative_del3/m | |
D2 = (Delta2/m) + (Lambda*theta2) | |
D1 = (Delta1/m) + (Lambda*theta1) | |
D2[:,0] -= Lambda*theta2[:,0] | |
D1[:,0] -= Lambda*theta1[:,0] | |
theta2 -= alpha*D2 | |
theta1 -= alpha*D1 | |
if (j%1000) == 0: | |
print "Error:" + str(np.mean(np.abs(cumulative_del3))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment