Skip to content

Commit 751dbd1

Browse files
authored
Merge pull request manvillej#3 from manvillej/Ex3Multiclass&NeuralNets1
Ex3 multiclass&neural nets1
2 parents 524f5b1 + e9722a4 commit 751dbd1

11 files changed

+338
-6
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Python implementation of Andrew Ng's ML course projects
33
- Ex1 (Linear Regression) = Complete
44
- Ex2 (Logistic Regression & Regulation) = Complete
5-
- Ex3 = Incomplete
5+
- Ex3 (MultiClass LR and Neural Network Prediction) = Complete
66
- Ex4 = Incomplete
77
- Ex5 = Incomplete
88
- Ex6 = Incomplete

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

306 Bytes
Binary file not shown.

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

2.55 KB
Binary file not shown.

Diff for: data/ex3data1.mat

7.16 MB
Binary file not shown.

Diff for: data/ex3weights.mat

77.7 KB
Binary file not shown.

Diff for: ex2.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@
4040

4141
print('\nPlotting data with \'o\' indicating (y = 1) examples and \'x\' indicating (y = 0) examples.')
4242

43-
helper.plotData(x,y,'Exam Score 1', 'Exam Score 2')
44-
43+
helper.plotData(x,y)
44+
plt.xlabel('Exam Score 1')
45+
plt.ylabel('Exam Score 2')
46+
plt.show()
4547

4648
input('\nPart 1 completed. Program paused. Press enter to continue: ')
4749
## ============ Part 2: Compute Cost and Gradient ============
@@ -95,7 +97,10 @@
9597
print(theta)
9698
print('Expected theta (approx):')
9799
print('[ -25.161 0.206 0.201]')
98-
helper.plotDecisionBoundary(theta,x,y,'Exam Score 1', 'Exam Score 2')
100+
helper.plotDecisionBoundary(theta,x,y)
101+
plt.xlabel('Exam Score 1')
102+
plt.ylabel('Exam Score 2')
103+
plt.show()
99104

100105
input('\nPart 3 completed. Program paused. Press enter to continue: ')
101106

Diff for: ex2_reg.py

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
lambdaVal = 1
9999

100100
results = helper.optimizeReg(theta,x,y,lambdaVal)
101+
print(x.shape)
102+
print(theta.shape)
103+
print(y.shape)
101104
theta = results.x
102105
cost = results.fun
103106

Diff for: ex2helper.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ def mapFeatures(X):
7373
return mapped_X
7474

7575
def costFunctionReg(theta, x, y, lambdaVal):
76-
76+
if(y.ndim>1):
77+
y = np.squeeze(y)
7778
m = x.shape[0]
79+
if(y.shape[0]!=m):
80+
raise ValueError('Y & X are not compatible: X.shape = {} & y.shape = {}'.format(X.shape, y.shape))
7881

7982
z = sigmoid(np.matmul(x,theta))
8083

@@ -91,8 +94,12 @@ def costFunctionReg(theta, x, y, lambdaVal):
9194

9295

9396
def gradientReg(theta, x, y, lambdaVal):
97+
if(y.ndim>1):
98+
y = np.squeeze(y)
9499
m = x.shape[0]
95-
100+
if(y.shape[0]!=m):
101+
raise ValueError('Y & X are not compatible: X.shape = {} & y.shape = {}'.format(X.shape, y.shape))
102+
96103
z = sigmoid(np.matmul(x,theta))
97104

98105
grad = np.matmul(x.transpose(),np.subtract(z,y))/m

Diff for: ex3.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all
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+
# lrCostFunction (logistic regression cost function) - complete
11+
# oneVsAll - complete
12+
# predictOneVsAll - complete
13+
# predict - complated
14+
#
15+
# For this exercise, you will not need to change any code in this file,
16+
# or any other files other than those mentioned above.
17+
#
18+
19+
## Initialization
20+
import numpy as np
21+
import matplotlib.pyplot as plt
22+
import scipy.io as io
23+
import ex2helper as helper2
24+
import ex3helper as helper
25+
26+
## Setup the parameters you will use for this part of the exercise
27+
input_layer_size = 400; # 20x20 Input Images of Digits
28+
num_labels = 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/ex3data1.mat')
39+
X = mat['X']
40+
y = np.squeeze(mat['y'])
41+
42+
43+
m = X.shape[0]
44+
45+
# Randomly select 100 data points to display
46+
perm = np.random.permutation(m)
47+
sel = X[perm[0:100],:]
48+
49+
#display data as image
50+
helper.displayData(sel)
51+
52+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
53+
54+
## ============ Part 2a: Vectorize Logistic Regression ============
55+
# In this part of the exercise, you will reuse your logistic regression
56+
# code from the last exercise. You task here is to make sure that your
57+
# regularized logistic regression implementation is vectorized. After
58+
# that, you will implement one-vs-all classification for the handwritten
59+
# digit dataset.
60+
61+
62+
# Test case for lrCostFunction
63+
print('\nTesting lrCostFunction() with regularization')
64+
65+
theta_t = np.array([-2,-1,1,2])
66+
X_t = np.concatenate((np.array([np.ones(5)]),np.divide(np.arange(1,16,1),10).reshape(3,5)),axis=0).transpose()
67+
Y_t = np.array([1,0,1,0,1])
68+
lambda_t = 3
69+
70+
J = helper2.costFunctionReg(theta_t,X_t,Y_t,lambda_t)
71+
grad = helper2.gradientReg(theta_t,X_t,Y_t,lambda_t)
72+
73+
print('Cost: {:.6f}'.format(J))
74+
print('Expected cost: 2.534819')
75+
print('Gradients:')
76+
print(grad)
77+
print('Expected gradients:')
78+
print('[0.146561 -0.548558 0.724722 1.398003]')
79+
80+
81+
input('\nPart 2a completed. Program paused. Press enter to continue: ')
82+
83+
## ============ Part 2b: One-vs-All Training ============
84+
print('\nTraining One-vs-All Logistic Regression...')
85+
86+
lambdaVal = .1
87+
allTheta = helper.OneVsAll(X, y, np.unique(y), lambdaVal)
88+
89+
90+
input('\nPart 2b completed. Program paused. Press enter to continue: ')
91+
## ================ Part 3: Predict for One-Vs-All ================
92+
93+
p = helper.predictOneVsAll(allTheta,X)
94+
predictions = np.zeros(p.shape)
95+
predictions[np.where(p==y)] = 1
96+
97+
print('Train Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
98+
print('Expected Accuracy: 96.5%')

Diff for: ex3_nn.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks
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+
# lrCostFunction (logistic regression cost function) - completed
11+
# oneVsAll - completed
12+
# predictOneVsAll - completed
13+
# predict - completed
14+
#
15+
# For this exercise, you will not need to change any code in this file,
16+
# or any other files other than those mention d above.
17+
#
18+
19+
## Initialization
20+
import numpy as np
21+
import matplotlib.pyplot as plt
22+
import scipy.io as io
23+
import ex2helper as helper2
24+
import ex3helper as helper
25+
26+
## Setup the parameters you will use for this exercise
27+
input_layer_size = 400; # 20x20 Input Images of Digits
28+
hidden_layer_size = 25; # 25 hidden units
29+
num_labels = 10; # 10 labels, from 1 to 10
30+
# (note that we have mapped "0" to label 10)
31+
32+
## =========== Part 1: Loading and Visualizing Data =============
33+
# We start the exercise by first loading and visualizing the dataset.
34+
# You will be working with a dataset that contains handwritten digits.
35+
#
36+
37+
# Load Training Data
38+
print('Loading and Visualizing Data ...')
39+
40+
mat = io.loadmat('./data/ex3data1.mat')
41+
X = mat['X']
42+
y = np.squeeze(mat['y'])
43+
44+
45+
m = y.shape[0]
46+
47+
# Randomly select 100 data points to display
48+
perm = np.random.permutation(m)
49+
sel = X[perm[0:100],:]
50+
51+
#display data as image
52+
helper.displayData(sel)
53+
54+
55+
input('\nPart 1 completed. Program paused. Press enter to continue: ')
56+
57+
58+
## ================ Part 2: Loading Pameters ================
59+
# In this part of the exercise, we load some pre-initialized
60+
# neural network parameters.
61+
62+
print('\nLoading Saved Neural Network Parameters ...')
63+
64+
# Load the weights into variables Theta1 and Theta2
65+
mat = io.loadmat('./data/ex3weights.mat')
66+
theta1 = mat['Theta1']
67+
theta2 = mat['Theta2']
68+
69+
## ================= Part 3: Implement Predict =================
70+
# After training the neural network, we would like to use it to predict
71+
# the labels. You will now implement the "predict" function to use the
72+
# neural network to predict the labels of the training set. This lets
73+
# you compute the training set accuracy.
74+
75+
p = helper.predict(theta1, theta2, X)
76+
predictions = np.zeros(p.shape)
77+
predictions[np.where(p==y)] = 1
78+
79+
print('Train Set Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
80+
81+
input('\nPart 3 completed. Program paused. Press enter to continue: ')
82+
83+
# Randomly select 100 data points to display
84+
perm = np.random.permutation(m)
85+
for i in range(0,m):
86+
print('\n Displaying Example Image...\n')
87+
example = X[perm[i],:]
88+
example = example[np.newaxis,:]
89+
90+
helper.displayData(example)
91+
p = helper.predict(theta1, theta2, example)
92+
print(' Neural Network Prediction: {}'.format(p[0]%10))
93+
print(' Correct Answer: {}\n'.format(y[perm[i]]%10))
94+
95+
96+
97+
answer = input('Paused - press enter to continue, q to exit:')
98+
if(answer=='q'):
99+
break

Diff for: ex3helper.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 OneVsAll(X, y, numlabels, lambdaVal):
9+
m = X.shape[0] #number of examples
10+
n = X.shape[1] #number of data points
11+
12+
X = np.insert(X,0,np.ones(X.shape[0]),axis=1) # adding bias unit
13+
theta = np.array([])#initialize theta
14+
15+
16+
for i in numlabels:
17+
yTemp = np.zeros(y.shape[0])
18+
yTemp[np.where(y==i)] = 1
19+
thetaTemp = np.zeros(n + 1)
20+
21+
#run regularized optimization
22+
results = helper.optimizeReg(thetaTemp, X, yTemp, lambdaVal)
23+
thetaTemp = results.x
24+
25+
#get prediction accuracy
26+
p = helper.predict(thetaTemp, X)
27+
predictions = np.zeros(p.shape)
28+
predictions[np.where(p==yTemp)] = 1
29+
p = helper.sigmoid(np.matmul(X,thetaTemp))
30+
31+
#calculating cost and accuracy to validate that the function is working correctly
32+
print('Train Accuracy: {:.1f}%'.format(np.mean(predictions) * 100))
33+
print('cost for {} = {:.3f}, max = {:.3f}'.format(i%10,results.fun,np.max(p)))
34+
35+
theta = np.append(theta, thetaTemp)#appending discovered theta to theta
36+
37+
#struggled on this for awhile. Reshape works from left to right, top to bottom.
38+
#so if your data needs to be in columns instead of rows. It messes it all up, but it still works
39+
theta = np.reshape(theta, (numlabels.shape[0],n + 1))
40+
return theta.transpose()
41+
42+
def predictOneVsAll(allTheta, X):
43+
X = np.insert(X,0,np.ones(X.shape[0]),axis=1) # adding bias unit
44+
45+
pred = helper.sigmoid(np.matmul(X,allTheta))#calculate predictions for all thetas
46+
47+
#return vector of position of maximum for each row +1 to adjust for arrays initializing at 0
48+
return(np.argmax(pred,axis=1)+1)
49+
50+
def displayData(X, **keywordParameters):
51+
#set example width automatically if not given
52+
if('exampleWidth' in keywordParameters):
53+
exampleWidth = keywordParameters['exampleWidth']
54+
else:
55+
exampleWidth = round(math.sqrt(X.shape[1]))
56+
57+
#calculate size of rows and columns
58+
[m, n] = X.shape
59+
exampleHeight = n//exampleWidth #eliminating float with // divide
60+
61+
#calculate number of items to display
62+
displayRows = math.floor(math.sqrt(m))
63+
displayColumns = math.ceil(m/displayRows)
64+
65+
#set padding between images
66+
padding = 1
67+
68+
#set up blank display
69+
displayHeight = padding + displayRows * (exampleHeight + padding)
70+
displayWidth = padding + displayColumns * (exampleWidth + padding)
71+
72+
displayArray = - np.ones([displayHeight, displayWidth])
73+
74+
#Copy each example into a path on the display array
75+
currentExample = 0
76+
for j in range(0,displayRows):
77+
for i in range(0, displayColumns):
78+
if(currentExample > m):
79+
break
80+
81+
#Copy the Patch
82+
83+
#1. get the max value of the patch
84+
maxValue = np.amax(np.absolute(X[currentExample,:]))
85+
86+
#2. get current example in the correct shape
87+
example = np.reshape(X[currentExample,:], [exampleHeight, exampleWidth])/maxValue
88+
example = example.transpose()
89+
90+
#3. calculate current position height and width
91+
currentPositionHeight = padding + j * (exampleHeight + padding)
92+
currentPositionWidth = padding + i * (exampleWidth + padding)
93+
94+
#4. assign current example to correct position in the display array
95+
displayArray[currentPositionHeight:currentPositionHeight + exampleHeight, currentPositionWidth:currentPositionWidth + exampleWidth] = example
96+
97+
#5. iterate current example
98+
currentExample = currentExample + 1
99+
100+
if(currentExample>m):
101+
break
102+
103+
#show image
104+
imgplot = plt.imshow(displayArray, cmap='gray')
105+
plt.axis('off')
106+
plt.show()
107+
108+
def predict(theta1, theta2, X):
109+
m = X.shape[0]
110+
num_labels = theta2.shape[0]
111+
112+
X = np.insert(X,0,np.ones(X.shape[0]),axis=1) # adding bias unit
113+
a1 = np.matmul(X,theta1.transpose())
114+
a1 = helper.sigmoid(a1)
115+
a1 = np.insert(a1,0,np.ones(a1.shape[0]),axis=1) # adding bias unit
116+
a2 = np.matmul(a1,theta2.transpose())
117+
a2 = helper.sigmoid(a2)
118+
119+
return(np.argmax(a2,axis=1)+1)
120+

0 commit comments

Comments
 (0)