Skip to content

Commit

Permalink
Minor spacing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jtlowery committed Mar 15, 2017
1 parent 0dd7a31 commit a373b3f
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 100 deletions.
8 changes: 4 additions & 4 deletions ex3/displayData.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ def displayData(X):
max_val = np.max(np.abs(X[curr_ex, :]))
rows = [pad + j * (example_height + pad) + x for x in np.arange(example_height + 1)]
cols = [pad + i * (example_width + pad) + x for x in np.arange(example_width + 1)]
display_array[min(rows):max(rows), min(cols):max(cols)] = X[curr_ex, :].reshape(example_height, example_width) / max_val
display_array[min(rows):max(rows), min(cols):max(cols)] = (X[curr_ex, :].
reshape(example_height, example_width) / max_val)
curr_ex = curr_ex + 1
if curr_ex > m:
break

# Display Image
# Display Image
display_array = display_array.astype('float32')
plt.imshow(display_array.T)
plt.set_cmap('gray')
# Do not show axis
# Do not show axis
plt.axis('off')
show()

23 changes: 11 additions & 12 deletions ex3/ex3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
## Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all
import scipy.io
import numpy as np
from matplotlib import use
Expand All @@ -8,6 +7,7 @@
from predictOneVsAll import predictOneVsAll
from displayData import displayData

# Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all
# Instructions
# ------------
#
Expand All @@ -22,17 +22,17 @@
#
# For this exercise, you will not need to change any code in this file,
# or any other files other than those mentioned above.
#

## Setup the parameters you will use for this part of the exercise
input_layer_size = 400 # 20x20 Input Images of Digits
num_labels = 10 # 10 labels, from 1 to 10
# (note that we have mapped "0" to label 10)

## =========== Part 1: Loading and Visualizing Data =============
# Setup the parameters you will use for this part of the exercise
# 20x20 Input Images of Digits
input_layer_size = 400
# 10 labels, from 1 to 10 (note that we have mapped "0" to label 10)
num_labels = 10

# =========== Part 1: Loading and Visualizing Data =============
# We start the exercise by first loading and visualizing the dataset.
# You will be working with a dataset that contains handwritten digits.
#

# Load Training Data
print('Loading and Visualizing Data ...')
Expand All @@ -50,13 +50,12 @@

input('Program paused. Press Enter to continue...')

## ============ Part 2: Vectorize Logistic Regression ============
# ============ Part 2: Vectorize Logistic Regression ============
# In this part of the exercise, you will reuse your logistic regression
# code from the last exercise. You task here is to make sure that your
# regularized logistic regression implementation is vectorized. After
# that, you will implement one-vs-all classification for the handwritten
# digit dataset.
#

print('Training One-vs-All Logistic Regression...')

Expand All @@ -65,9 +64,9 @@

input('Program paused. Press Enter to continue...')


## ================ Part 3: Predict for One-Vs-All ================
# ================ Part 3: Predict for One-Vs-All ================
# After ...

pred = predictOneVsAll(all_theta, X)

accuracy = np.mean(np.double(pred == np.squeeze(y))) * 100
Expand Down
40 changes: 21 additions & 19 deletions ex3/ex3_nn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks
from matplotlib import use
use('TkAgg')
import scipy.io
import numpy as np
import matplotlib.pyplot as plt

from displayData import displayData
from predict import predict

# Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks
# Instructions
# ------------
#
Expand All @@ -14,23 +22,17 @@
#
# For this exercise, you will not need to change any code in this file,
# or any other files other than those mentioned above.
#
from matplotlib import use
use('TkAgg')
import scipy.io
import numpy as np
import matplotlib.pyplot as plt

from displayData import displayData
from predict import predict

## Setup the parameters you will use for this exercise
input_layer_size = 400 # 20x20 Input Images of Digits
hidden_layer_size = 25 # 25 hidden units
num_labels = 10 # 10 labels, from 1 to 10
# (note that we have mapped "0" to label 10)
# Setup the parameters you will use for this exercise
# 20x20 Input Images of Digits
input_layer_size = 400
# 25 hidden units
hidden_layer_size = 25
# 10 labels, from 1 to 10 (note that we have mapped "0" to label 10)
num_labels = 10

## =========== Part 1: Loading and Visualizing Data =============
# =========== Part 1: Loading and Visualizing Data =============
# We start the exercise by first loading and visualizing the dataset.
# You will be working with a dataset that contains handwritten digits.
#
Expand All @@ -47,11 +49,11 @@
sel = np.random.permutation(range(m))
sel = sel[0:100]

displayData(X[sel,:])
displayData(X[sel, :])

input('Program paused. Press Enter to continue...')

## ================ Part 2: Loading Pameters ================
# ================ Part 2: Loading Pameters ================
# In this part of the exercise, we load some pre-initialized
# neural network parameters.

Expand All @@ -62,7 +64,7 @@
Theta1 = data['Theta1']
Theta2 = data['Theta2']

## ================= Part 3: Implement Predict =================
# ================= Part 3: Implement Predict =================
# After training the neural network, we would like to use it to predict
# the labels. You will now implement the "predict" function to use the
# neural network to predict the labels of the training set. This lets
Expand All @@ -83,7 +85,7 @@
plt.figure()
for i in range(m):
# Display
X2 = X[rp[i],:]
X2 = X[rp[i], :]
print('Displaying Example Image')
X2 = np.matrix(X[rp[i]])
displayData(X2)
Expand Down
34 changes: 17 additions & 17 deletions ex3/lrCostFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@


def lrCostFunction(theta, X, y, Lambda):
"""computes the cost of using
theta as the parameter for regularized logistic regression and the
gradient of the cost w.r.t. to the parameters.
""" computes the cost of using
theta as the parameter for regularized logistic regression and the
gradient of the cost w.r.t. to the parameters.
"""

# ====================== YOUR CODE HERE ======================
# Instructions: Compute the cost of a particular choice of theta.
# You should set J to the cost.
#
# Hint: The computation of the cost function and gradients can be
# efficiently vectorized. For example, consider the computation
#
# sigmoid(X * theta)
#
# Each row of the resulting matrix will contain the value of the
# prediction for that example. You can make use of this to vectorize
# the cost function and gradient computations.
#
# =============================================================
# ====================== YOUR CODE HERE ======================
# Instructions: Compute the cost of a particular choice of theta.
# You should set J to the cost.
#
# Hint: The computation of the cost function and gradients can be
# efficiently vectorized. For example, consider the computation
#
# sigmoid(X * theta)
#
# Each row of the resulting matrix will contain the value of the
# prediction for that example. You can make use of this to vectorize
# the cost function and gradient computations.
#
# =============================================================

return J
31 changes: 15 additions & 16 deletions ex3/oneVsAll.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def oneVsAll(X, y, num_labels, Lambda):
"""trains multiple logistic regression classifiers and returns all
""" trains multiple logistic regression classifiers and returns all
the classifiers in a matrix all_theta, where the i-th row of all_theta
corresponds to the classifier for label i
"""
Expand All @@ -20,25 +20,24 @@ def oneVsAll(X, y, num_labels, Lambda):
# Add ones to the X data matrix
X = np.column_stack((np.ones((m, 1)), X))

# ====================== YOUR CODE HERE ======================
# Instructions: You should complete the following code to train num_labels
# logistic regression classifiers with regularization
# parameter lambda.
#
# Hint: theta(:) will return a column vector.
#
# Hint: You can use y == c to obtain a vector of 1's and 0's that tell use
# whether the ground truth is true/false for this class.
#
# Note: For this assignment, we recommend using fmincg to optimize the cost
# function. It is okay to use a for-loop (for c = 1:num_labels) to
# loop over the different classes.
# ====================== YOUR CODE HERE ======================
# Instructions: You should complete the following code to train num_labels
# logistic regression classifiers with regularization
# parameter lambda.
#
# Hint: theta(:) will return a column vector.
#
# Hint: You can use y == c to obtain a vector of 1's and 0's that tell use
# whether the ground truth is true/false for this class.
#
# Note: For this assignment, we recommend using fmincg to optimize the cost
# function. It is okay to use a for-loop (for c = 1:num_labels) to
# loop over the different classes.

# Set Initial theta
initial_theta = np.zeros((n + 1, 1))

# This function will return theta and the cost
# =========================================================================
# =========================================================================

return all_theta

25 changes: 12 additions & 13 deletions ex3/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ def predict(Theta1, Theta2, X):
m, _ = X.shape
num_labels, _ = Theta2.shape

# ====================== YOUR CODE HERE ======================
# Instructions: Complete the following code to make predictions using
# your learned neural network. You should set p to a
# vector containing labels between 1 to num_labels.
#
# Hint: The max function might come in useful. In particular, the max
# function can also return the index of the max element, for more
# information see 'help max'. If your examples are in rows, then, you
# can use max(A, [], 2) to obtain the max for each row.
#
# =========================================================================

return p + 1 # add 1 to offset index of maximum in A row
# ====================== YOUR CODE HERE ======================
# Instructions: Complete the following code to make predictions using
# your learned neural network. You should set p to a
# vector containing labels between 1 to num_labels.
#
# Hint: The max function might come in useful. In particular, the max
# function can also return the index of the max element, for more
# information see 'help max'. If your examples are in rows, then, you
# can use max(A, [], 2) to obtain the max for each row.
#
# =========================================================================

return p + 1 # add 1 to offset index of maximum in A row
39 changes: 20 additions & 19 deletions ex3/predictOneVsAll.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@


def predictOneVsAll(all_theta, X):
"""will return a vector of predictions
for each example in the matrix X. Note that X contains the examples in
rows. all_theta is a matrix where the i-th row is a trained logistic
regression theta vector for the i-th class. You should set p to a vector
of values from 1..K (e.g., p = [1 3 1 2] predicts classes 1, 3, 1, 2
for 4 examples) """
""" will return a vector of predictions
for each example in the matrix X. Note that X contains the examples in
rows. all_theta is a matrix where the i-th row is a trained logistic
regression theta vector for the i-th class. You should set p to a vector
of values from 1..K (e.g., p = [1 3 1 2] predicts classes 1, 3, 1, 2
for 4 examples)
"""

m = X.shape[0]

Expand All @@ -19,18 +20,18 @@ def predictOneVsAll(all_theta, X):
# Add ones to the X data matrix
X = np.column_stack((np.ones((m, 1)), X))

# ====================== YOUR CODE HERE ======================
# Instructions: Complete the following code to make predictions using
# your learned logistic regression parameters (one-vs-all).
# You should set p to a vector of predictions (from 1 to
# num_labels).
#
# Hint: This code can be done all vectorized using the max function.
# In particular, the max function can also return the index of the
# max element, for more information see 'help max'. If your examples
# are in rows, then, you can use max(A, [], 2) to obtain the max
# for each row.
#
# =========================================================================
# ====================== YOUR CODE HERE ======================
# Instructions: Complete the following code to make predictions using
# your learned logistic regression parameters (one-vs-all).
# You should set p to a vector of predictions (from 1 to
# num_labels).
#
# Hint: This code can be done all vectorized using the max function.
# In particular, the max function can also return the index of the
# max element, for more information see 'help max'. If your examples
# are in rows, then, you can use max(A, [], 2) to obtain the max
# for each row.
#
# =========================================================================

return p + 1 # add 1 to offset index of maximum in A row

0 comments on commit a373b3f

Please sign in to comment.