Skip to content

Commit da51bc1

Browse files
committed
Added NN functions
added the nnCostFunction, nnGradFunction, and sigmoid gradient. I used numpy's 'np.random.rand' function to initialize thetas. Still need to complete the nnCheckGradients, debugInitializeWeights, and fmincg
1 parent 751dbd1 commit da51bc1

File tree

7 files changed

+10527
-0
lines changed

7 files changed

+10527
-0
lines changed

Diff for: __pycache__/ex3helper.cpython-36.pyc

0 Bytes
Binary file not shown.

Diff for: __pycache__/ex4helper.cpython-36.pyc

2.19 KB
Binary file not shown.

Diff for: data/ex4data1.mat

7.16 MB
Binary file not shown.

Diff for: data/ex4weights.mat

77.7 KB
Binary file not shown.

Diff for: ex4.py

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
## Machine Learning Online Class - Exercise 4 Neural Network Learning
2+
3+
# Instructions
4+
# ------------
5+
#
6+
# This file contains code that helps you get started on the
7+
# linear exercise. You will need to complete the following functions
8+
# in this exericse:
9+
#
10+
# sigmoidGradient.m
11+
# randInitializeWeights.m
12+
# nnCostFunction.m
13+
#
14+
# For this exercise, you will not need to change any code in this file,
15+
# or any other files other than those mentioned above.
16+
#
17+
18+
## Initialization
19+
import numpy as np
20+
import matplotlib.pyplot as plt
21+
import scipy.io as io
22+
import ex3helper as helper3
23+
import ex4helper as helper
24+
25+
## Setup the parameters you will use for this exercise
26+
inputLayerSize = 400; # 20x20 Input Images of Digits
27+
hiddenLayerSize = 25; # 25 hidden units
28+
numLabels = 10; # 10 labels, from 1 to 10
29+
# (note that we have mapped "0" to label 10)
30+
31+
## =========== Part 1: Loading and Visualizing Data =============
32+
# We start the exercise by first loading and visualizing the dataset.
33+
# You will be working with a dataset that contains handwritten digits.
34+
#
35+
36+
# Load Training Data
37+
print('Loading and Visualizing Data ...')
38+
mat = io.loadmat('./data/ex4data1.mat')
39+
X = mat['X']
40+
y = np.squeeze(mat['y'])
41+
42+
m = X.shape[0]
43+
44+
# Randomly select 100 data points to display
45+
perm = np.random.permutation(m)
46+
sel = X[perm[0:100],:]
47+
48+
helper3.displayData(sel)
49+
50+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
51+
52+
53+
## ================ Part 2: Loading Parameters ================
54+
# In this part of the exercise, we load some pre-initialized
55+
# neural network parameters.
56+
57+
print('\nLoading Saved Neural Network Parameters ...')
58+
59+
mat = io.loadmat('./data/ex4weights.mat')
60+
61+
theta1 = mat['Theta1']
62+
theta2 = mat['Theta2']
63+
64+
nnParams = np.array([theta1.flatten(), theta2.flatten()])
65+
66+
67+
## ================ Part 3: Compute Cost (Feedforward) ================
68+
# To the neural network, you should first start by implementing the
69+
# feedforward part of the neural network that returns the cost only. You
70+
# should complete the code in nnCostFunction.m to return cost. After
71+
# implementing the feedforward to compute the cost, you can verify that
72+
# your implementation is correct by verifying that you get the same cost
73+
# as us for the fixed debugging parameters.
74+
#
75+
# We suggest implementing the feedforward cost *without* regularization
76+
# first so that it will be easier for you to debug. Later, in part 4, you
77+
# will get to implement the regularized cost.
78+
#
79+
print('Feedforward Using Neural Network ...')
80+
81+
# Weight regularization parameter (we set this to 0 here).
82+
lambdaVal = 0
83+
84+
J = helper.nnCostFunction(nnParams, X, y, lambdaVal)
85+
86+
print('Cost at parameters (loaded from ex4weights): {:.6f}'.format(J))
87+
print('this value should be approx: 0.287629')
88+
89+
input('\nPart 2 & 3 completed. Program paused. Press enter to continue: ')
90+
91+
## =============== Part 4: Implement Regularization ===============
92+
# Once your cost function implementation is correct, you should now
93+
# continue to implement the regularization with the cost.
94+
#
95+
96+
print('\nChecking Cost Function (w/ Regularization) ... ')
97+
98+
# Weight regularization parameter (we set this to 1 here).
99+
lambdaVal = 1
100+
101+
J = helper.nnCostFunction(nnParams, X, y, lambdaVal)
102+
103+
print('Cost at parameters (loaded from ex4weights): {:.6f}'.format(J))
104+
print('this value should be approx: 0.383770')
105+
106+
input('\nPart 4 completed. Program paused. Press enter to continue: ')
107+
108+
109+
## ================ Part 5: Sigmoid Gradient ================
110+
# Before you start implementing the neural network, you will first
111+
# implement the gradient for the sigmoid function. You should complete the
112+
# code in the sigmoidGradient.m file.
113+
#
114+
115+
print('\nEvaluating sigmoid gradient...')
116+
117+
g = helper.sigmoidGradient(np.array([-1, -0.5, 0, 0.5, 1]))
118+
print('Sigmoid gradient evaluated at [-1 -0.5 0 0.5 1]:');
119+
print(g);
120+
121+
input('\nPart 5 completed. Program paused. Press enter to continue: ')
122+
123+
## ================ Part 6: Initializing Pameters ================
124+
# In this part of the exercise, you will be starting to implment a two
125+
# layer neural network that classifies digits. You will start by
126+
# implementing a function to initialize the weights of the neural network
127+
# (randInitializeWeights.m)
128+
129+
print('\nInitializing Neural Network Parameters ...')
130+
131+
initialTheta1 = np.random.rand(inputLayerSize + 1, hiddenLayerSize)
132+
initialTheta2 = np.random.rand(hiddenLayerSize + 1, num_labels)
133+
134+
# Unroll parameters
135+
initialNNParams = np.array([initialTheta1.flatten(), initialTheta2.flatten()])
136+
137+
## =============== Part 7: Implement Backpropagation ===============
138+
# Once your cost matches up with ours, you should proceed to implement the
139+
# backpropagation algorithm for the neural network. You should add to the
140+
# code you've written in nnCostFunction.m to return the partial
141+
# derivatives of the parameters.
142+
#
143+
print('\nChecking Backpropagation... ')
144+
145+
#Check gradients by running checkNNGradients
146+
#helper.checkNNGradients()
147+
148+
#input('\nPart 6 & 7 completed. Program paused. Press enter to continue: ')

Diff for: ex4helper.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import scipy.optimize as op
4+
import ex2helper as helper
5+
import math
6+
import matplotlib.image as mpimg
7+
8+
def nnCostFunction(nnParams, X, y, lambdaVal):
9+
m = X.shape[0]
10+
print(m)
11+
theta1 = nnParams[0]
12+
n1 = X.shape[1] + 1
13+
n2 = theta1.shape[0]
14+
theta1 = theta1.reshape(int(n2/n1),n1)
15+
theta2 = nnParams[1]
16+
n1 = theta1.shape[0] + 1
17+
n2 = theta2.shape[0]
18+
theta2 = theta2.reshape(int(n2/n1),n1)
19+
20+
21+
#prepare Y matrix for cost function
22+
numLabels = np.unique(y).shape[0]+1
23+
#create boolean array of value or not out of 1s and 0s
24+
Y = (y==1).astype(int)
25+
for i in range(2,numLabels):
26+
Y = np.append(Y,(y==i).astype(int))
27+
#reshape so first dimension corresponds with label
28+
Y = Y.reshape(10,5000)
29+
30+
X = np.insert(X,0,np.ones(X.shape[0]),axis=1) # adding bias unit
31+
h1 = helper.sigmoid(np.matmul(X,theta1.transpose()))
32+
h1 = np.insert(h1,0,np.ones(h1.shape[0]),axis=1) # adding bias unit
33+
h2 = helper.sigmoid(np.matmul(h1,theta2.transpose())).transpose()
34+
35+
#getting regulation parameters
36+
R1 = theta1[:,1:]
37+
R2 = theta2[:,1:]
38+
# calculating the cost of regulation
39+
costRegulation = lambdaVal*(np.sum(np.square(R1.flatten())) + np.sum(np.square(R2.flatten())))/(2*m)
40+
41+
#calculating true cost without regulation
42+
cost = np.sum(np.multiply(np.log(h2),Y)) + np.sum(np.multiply(np.log(1-h2),1-Y))
43+
cost = -cost/m
44+
45+
#calculate total cost
46+
totalCost = cost + costRegulation
47+
48+
return totalCost
49+
50+
51+
def nnGradFunction(nnParams, X, y, lambdaVal):
52+
m = X.shape[0]
53+
theta1 = nnParams[0]
54+
n1 = X.shape[1] + 1
55+
n2 = theta1.shape[0]
56+
theta1 = theta1.reshape(int(n2/n1),n1)
57+
theta2 = nnParams[1]
58+
n1 = theta1.shape[0] + 1
59+
n2 = theta2.shape[0]
60+
theta2 = theta2.reshape(int(n2/n1),n1)
61+
62+
63+
#prepare Y matrix for cost function
64+
numLabels = np.unique(y).shape[0]+1
65+
#create boolean array of value or not out of 1s and 0s
66+
Y = (y==1).astype(int)
67+
for i in range(2,numLabels):
68+
Y = np.append(Y,(y==i).astype(int))
69+
#reshape so first dimension corresponds with label
70+
Y = Y.reshape(10,5000)
71+
72+
X = np.insert(X,0,np.ones(X.shape[0]),axis=1) # adding bias unit
73+
h1 = helper.sigmoid(np.matmul(X,theta1.transpose()))
74+
h1 = np.insert(h1,0,np.ones(h1.shape[0]),axis=1) # adding bias unit
75+
h2 = helper.sigmoid(np.matmul(h1,theta2.transpose())).transpose()
76+
77+
78+
#calculate gradients
79+
theta2Error = h2-Y
80+
theta1Error = np.multiply(np.matmul(theta2Error.transpose(),theta2),np.multiply(h1,1-h1))
81+
theta1Grad = np.matmul(theta1Error.transpose(),X)
82+
theta1Grad = theta1Grad[1:,:]#drop bias unit error from hiddent layer
83+
theta2Grad = np.matmul(theta2Error,h1)
84+
85+
86+
return np.array([theta1Grad.flatten(), theta2Grad.flatten()])
87+
88+
89+
def sigmoidGradient(Z):
90+
R = helper.sigmoid(Z)
91+
return np.multiply(R,1-R)
92+
93+
94+

0 commit comments

Comments
 (0)