In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [64]:
# X = (hours sleeping, hours studying), y = Score on test
X = np.array(([3,5], [5,1], [10,2]), dtype=float)
y = np.array(([75], [82], [93]), dtype=float)
# Normalize
X = X/np.amax(X, axis=0)
y = y/100 #Max test score is 100
In [ ]:
In [ ]:
In [ ]:
In [65]:
class NeuralNetwork:
def __init__(self):
self.inputLayerSize = 2
self.outputLayerSize = 1
self.hiddenLayerSize = 3
#initialize the random weights
self.W1 = np.random.randn(self.inputLayerSize, self.hiddenLayerSize)
self.W2 = np.random.randn(self.hiddenLayerSize, self.outputLayerSize)
def forward(self,X):
#propagates inputs through networks.
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 the sigmoid activation function
return (1/(1+np.exp(-Z)))
def sigmoidPrime(self,Z):
#apply the sigmoid activation derivation function
return (np.exp(-Z)/((1+np.exp(-Z))**2))
def costFunction(self,X,y):
self.yHat = self.forward(X)
J = 0.5*sum((y-self.yHat)**2)
return J
def costFunctionPrime(self, X, y):
#Compute derivative with respect to W and W2 for a given X and y:
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
def getParams(self):
params = np.concatenate((self.W1.ravel(),self.W2.ravel()))
return params
def setParams(self, params):
#Set W1 and W2 using single paramater vector.
W1_start = 0
W1_end = self.hiddenLayerSize * self.inputLayerSize
self.W1 = np.reshape(params[W1_start:W1_end], (self.inputLayerSize , self.hiddenLayerSize))
W2_end = W1_end + self.hiddenLayerSize*self.outputLayerSize
self.W2 = np.reshape(params[W1_end:W2_end], (self.hiddenLayerSize, self.outputLayerSize))
def computeGradients(self,X,Y):
djdw1,djdw2 = self.costFunctionPrime(X,Y)
return np.concatenate((djdw1.ravel(),djdw2.ravel()))
def computeNumericalGradient(N,X,Y):
params = N.getParams()
numgrad = np.zeros(params.shape)
perturb = np.zeros(params.shape)
e = 1e-4
print(params)
for p in range(len(params)):
perturb[p] = e
N.setParams(params+perturb)
loss2 = N.costFunction(X,Y)
#print(type(loss2))
N.setParams(params-perturb)
loss1 = N.costFunction(X,Y)
numgrad[p] = (loss2-loss1)/(2*e)
perturb[p] = 0
N.setParams(params)
return numgrad
In [72]:
NN = NeuralNetwork()
grad = NN.computeGradients(x,y)
grad
Out[72]:
In [71]:
NN = NeuralNetwork()
numGrad = computeNumericalGradient(NN,x,y)
numGrad
Out[71]:
In [26]:
NN.costFunction(x,y)
Out[26]:
In [ ]:
sigmoid(np.random.randn(3,3))
In [48]:
NN.costFunctionPrime(x,y)
Out[48]:
In [136]:
from keras.models import Model
from keras.layers import Input, Dense
X = np.array(([5,2], [5,1], [5,3]), dtype=float)
y = np.array(([75], [62], [93]), dtype=float)
# Normalize
X = X/np.amax(X, axis=0)
y = y/100 #Max test score is 100
model = Sequential()
model.add(Dense(3, input_dim=2, activation='relu'))
model.add(Dense(3, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam',metrics=['accuracy'])
model.fit(X,y)
model.predict(np.array([[10,2]]))
Out[136]:
In [1]:
import numpy as np
X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1] ])
y = np.array([[0,1,1,0]]).T
In [2]:
X
Out[2]:
In [3]:
y
Out[3]:
In [4]:
syn0 = np.random.randn(3,4)
syn1 = np.random.randn(4,1)
In [5]:
print(syn0)
print(syn1)
In [7]:
for i in range(1000):
l1 = 1/1+(np.exp(np.dot(X,syn0)))
l2 = 1/1+(np.exp(np.dot(l1,syn1)))
l2_delta = (y-l2)*(l2*(1-l2))
l1_delta = l2_delta.dot(syn1.T)*(l1*(1-l1))
syn1 += l1.T.dot(l2_delta)
syn0 += X.T.dot(l1_delta)
In [6]:
syn0
Out[6]:
In [8]:
import numpy as np
# sigmoid function
def nonlin(x,deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
# input dataset
X = np.array([ [0,0,1],
[0,1,1],
[1,0,1],
[1,1,1] ])
# output dataset
y = np.array([[0,0,1,1]]).T
# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)
# initialize weights randomly with mean 0
syn0 = 2*np.random.random((3,1)) - 1
for iter in xrange(10000):
# forward propagation
l0 = X
l1 = nonlin(np.dot(l0,syn0))
# how much did we miss?
l1_error = y - l1
# multiply how much we missed by the
# slope of the sigmoid at the values in l1
l1_delta = l1_error * nonlin(l1,True)
# update weights
syn0 += np.dot(l0.T,l1_delta)
print "Output After Training:"
print l1
In [9]:
import numpy as np
def nonlin(x,deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
X = np.array([[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]])
y = np.array([[0],
[1],
[1],
[0]])
np.random.seed(1)
# randomly initialize our weights with mean 0
syn0 = 2*np.random.random((3,4)) - 1
syn1 = 2*np.random.random((4,1)) - 1
for j in xrange(60000):
# Feed forward through layers 0, 1, and 2
l0 = X
l1 = nonlin(np.dot(l0,syn0))
l2 = nonlin(np.dot(l1,syn1))
# how much did we miss the target value?
l2_error = y - l2
if (j% 10000) == 0:
print "Error:" + str(np.mean(np.abs(l2_error)))
# in what direction is the target value?
# were we really sure? if so, don't change too much.
l2_delta = l2_error*nonlin(l2,deriv=True)
# how much did each l1 value contribute to the l2 error (according to the weights)?
l1_error = l2_delta.dot(syn1.T)
# in what direction is the target l1?
# were we really sure? if so, don't change too much.
l1_delta = l1_error * nonlin(l1,deriv=True)
syn1 += l1.T.dot(l2_delta)
syn0 += l0.T.dot(l1_delta)