CogSci131 Assignment 1 NeuralNetsFall23
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
class Neural_Network(object):
def __init__(self):
#Define Parameters
self.inputLayerSize = 2
self.outputLayerSize=1
self.hiddenLayerSize=3
#Define Weights
self.W1=np.random.rand(self.inputLayerSize,self.hiddenLayerSize)
self.W2=np.random.rand(self.hiddenLayerSize,self.outputLayerSize)
def forward(self,X):
#Propagate inputs through network
self.z2 = np.dot(X,self.W1)
self.a2 = self.sigmoid(self.z2)
self.z3 = np.dot(self.a2,self.W2)
yHat = self.sigmoid(self.z3)
return yHat
def sigmoid(self, z):
#Apply Sigmoid Activation Function
return 1/(1+np.exp(-z))
def sigmoidPrime(self,z):
#Derivative of Sigmoid Function
return np.exp(-z)/((1+np.exp(-z))**2)
def relu(self, z):
return np.maximum(0, z)
def reluPrime(self,z):
return 1 * (z > 0)
def costFunction(self, X, y):
#Compute Cost Function with weights already stored in class
self.yHat=self.forward(X)
J=0.5*sum((y-self.yHat)**2)
def costFunctionPrime(self, X, y):
#Compute derivatives with respect to W1 and W2
self.yHat=self.forward(X)
delta3 = np.multiply(-(y-self.yHat),self.sigmoidPrime(self.z3))
dJdW2=np.dot(self.a2.T,delta3)
delta2=np.dot(delta3,self.W2.T)*self.sigmoidPrime(self.z2)
dJdW1=np.dot(X.T,delta2)
return dJdW1,dJdW2
X=np.array(([3,5],[5,1],[10,1]),dtype=float)
y=np.array(([75],[80],[93]),dtype=float)
array([[ 3., 5.],
[ 5., 1.],
[10., 1.]])
array([[75.],
X=X/np.amax(X,axis=0)
(array([[0.3, 1. ],
[0.5, 0.2],
[1. , 0.2]]),
array([[0.75],
NN=Neural_Network()
yH=NN.forward(X)
array([[0.84235222],
[0.81141213],
[0.83173149]])
array([[0.75],
testValues=np.arange(-5,5,0.01)
plt.plot(testValues,NN.sigmoid(testValues),linewidth=2)
plt.plot(testValues, NN.sigmoidPrime(testValues),linewidth=2)
plt.grid(1)
plt.legend([‘Sigmoid’,’SigmoidPrime’])
NN=Neural_Network()
cost1=NN.costFunction(X,y)
array([0.01530567])
dJdW1,dJdW2=NN.costFunctionPrime(X,y)
array([[-0.00598957, -0.00831583, -0.0018686 ],
[-0.00105227, -0.0014616 , -0.00034617]])
array([[-0.02324737],
[-0.01920621],
[-0.02749615]])
NN.W1 = NN.W1+scalar*dJdW1
NN.W2 = NN.W2+scalar*dJdW2
cost2 = NN.costFunction(X,y)
print (cost1,cost2)
[0.01530567] [0.01721018]
scalar=0.1
NN.W1 = NN.W1-scalar*dJdW1
NN.W2 = NN.W2-scalar*dJdW2
cost3 = NN.costFunction(X,y)
print (cost2,cost3)
[0.01721018] [0.01700811]