In [89]:
import numpy as np
import matplotlib.pyplot as plt
import time
%matplotlib inline
Define the Neural Network Class
In [90]:
class n_network(object):
# Initialize the class / constructor()!!
def __init__(self, w1,w2):
self.input_layer_size = 2
self.output_layer_size = 1
self.hidden_layer_size = 3
#self.W1 = np.randn(self.input_layer_size,
# self.hidden_layer_size)
#self.W2 = np.randn(self.hidden_layer_size,
# self.output_layer_size)
self.w1=np.reshape(w1, (3,4))
self.w2=np.reshape(w2, (4,1))
print("************** The Weights **************")
print("w1:\n", self.w1)
print("w2:\n", self.w2)
# Define the forward propagation function
def forward_propagation(self, x):
self.z2 = np.dot(x, self.w1)
#print("z2:\n", self.z2)
self.a2 = self.Zigmoid(self.z2)
self.z3 = np.dot(self.a2, self.w2)
#print("z3:\n", self.z3)
yHat = self.Zigmoid(self.z3)
return yHat
# Define the sigmoid function
def Zigmoid(self, z):
return 1/(1+np.exp(-z))
In [91]:
print("****************** Machine Learning Fall 2016 ******************")
print("************** Assignment 2 Program implementation **************")
print("************************ Prasanna Kolar ************************")
w1=np.array(([-0.18,-0.16,-0.03,-0.15],
[0.166,-0.18,0.01,-0.06],
[0.14,-0.14,-0.065,-0.06]), dtype=float)
w2=np.array(([-1.01],
[-1.99],
[-0.25],
[-1.64]), dtype=float)
nn = n_network(w1,w2)
x = (1, 2.00, 0.26 )
yHat = nn.forward_propagation(x)
print("************** The Part a output is given below **************")
print("h(x): \n", yHat)
print("error e :\n", 1-yHat)
w1=np.array(([12.70,-0.20,-0.74,-0.19],
[-1.49,-8.85,7.08,-8.29],
[-19.85,-2.61,-3.59,-2.70]), dtype=float)
w2=np.array(([7.44],
[2.78],
[-4.31],
[2.70]), dtype=float)
nn = Pras_nn(w1,w2)
x = (1, 2.00, 0.26 )
yHat = nn.forward_propagation(x)
print("************** The Part b output is given below **************")
print("h(x): \n", yHat)
print("error e :\n", 1-yHat)