Logistic Regression With A Neural Network Mindset: 1 - Packages
Logistic Regression With A Neural Network Mindset: 1 - Packages
Instructions:
Do not use loops (for/while) in your code, unless the instructions explicitly ask you to do
so.
1 - Packages
First, let's run the cell below to import all the packages that you will need during this assignment.
numpy (www.numpy.org) is the fundamental package for scientific computing with Python.
h5py (http://www.h5py.org) is a common package to interact with a dataset that is stored
on an H5 file.
matplotlib (http://matplotlib.org) is a famous library to plot graphs in Python.
PIL (http://www.pythonware.com/products/pil/) and scipy (https://www.scipy.org/) are used
here to test your model with your own picture at the end.
%matplotlib inline
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 1/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
You will build a simple image-recognition algorithm that can correctly classify pictures as cat or non-
cat.
Let's get more familiar with the dataset. Load the data by running the following code.
We added "_orig" at the end of image datasets (train and test) because we are going to preprocess
them. After preprocessing, we will end up with train_set_x and test_set_x (the labels train_set_y
and test_set_y don't need any preprocessing).
Each line of your train_set_x_orig and test_set_x_orig is an array representing an image. You can
visualize an example by running the following code. Feel free also to change the index value and
re-run to see other images.
Many software bugs in deep learning come from having matrix/vector dimensions that don't fit. If
you can keep your matrix/vector dimensions straight you will go a long way toward eliminating many
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 2/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
bugs.
Remember that train_set_x_orig is a numpy-array of shape (m_train, num_px, num_px, 3). For
instance, you can access m_train by writing train_set_x_orig.shape[0].
m_train 209
m_test 50
num_px 64
For convenience, you should now reshape images of shape (num_px, num_px, 3) in a numpy-array
∗ ∗
of shape (num_px num_px 3, 1). After this, our training (and test) dataset is a numpy-array
where each column represents a flattened image. There should be m_train (respectively m_test)
columns.
Exercise: Reshape the training and test data sets so that images of size (num_px, num_px, 3) are
∗
flattened into single vectors of shape (num_px num_px 3, 1). ∗
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 3/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b c d, ∗∗
a) is to use:
Expected Output:
train_set_x_flatten (12288,
shape 209)
test_set_x_flatten (12288,
shape 50)
To represent color images, the red, green and blue channels (RGB) must be specified for each
pixel, and so the pixel value is actually a vector of three numbers ranging from 0 to 255.
One common preprocessing step in machine learning is to center and standardize your dataset,
meaning that you substract the mean of the whole numpy array from each example, and then divide
each example by the standard deviation of the whole numpy array. But for picture datasets, it is
simpler and more convenient and works almost as well to just divide every row of the dataset by
255 (the maximum value of a pixel channel).
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 4/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
You will build a Logistic Regression, using a Neural Network mindset. The following Figure explains
why Logistic Regression is actually a very simple Neural Network!
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 5/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
1 m
J = m ∑ (a(i) , y(i) ) (6)
i=1
Key steps: In this exercise, you will carry out the following steps:
You often build 1-3 separately and integrate them into one function we call model().
def sigmoid(z):
"""
Compute the sigmoid of z
Arguments:
z -- A scalar or numpy array of any size.
Return:
s -- sigmoid(z)
"""
s = 1/(1+ np.exp(-z))
### END CODE HERE ###
return s
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 6/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Expected Output:
def initialize_with_zeros(dim):
"""
This function creates a vector of zeros of shape (dim, 1) for w and initialize
Argument:
dim -- size of the w vector we want (or number of parameters in this case)
Returns:
w -- initialized vector of shape (dim, 1)
b -- initialized scalar (corresponds to the bias)
"""
w, b = np.zeros((dim,1)), 0
### END CODE HERE ###
assert(w.shape == (dim, 1))
assert(isinstance(b, float) or isinstance(b, int))
return w, b
In [25]: dim = 2
w, b = initialize_with_zeros(dim)
print ("w = " + str(w))
print ("b = " + str(b))
w = [[ 0.]
[ 0.]]
b = 0
Expected Output:
w [[ 0.] [ 0.]]
b 0
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 7/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Exercise: Implement a function propagate() that computes the cost function and its gradient.
Hints:
Forward Propagation:
You get X
You compute A = σ(wT X + b) = (a(0)1, a(1)m,...,(i) a(m−1)(i), a(m) ) (i)
J = − m ∑i=1 y log(a ) + (1 − y )log(1 − a(i) )
You calculate the cost function:
∂J = 1 X(A − Y )T (7)
∂w m
∂J = 1 m (a(i) − y(i) ) (8)
∂b m ∑ i=1
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 8/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Arguments:
w -- weights, a numpy array of size (num_px * num_px * 3, 1)
b -- bias, a scalar
X -- data of size (num_px * num_px * 3, number of examples)
Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, numbe
Return:
cost -- negative log-likelihood cost for logistic regression
dw -- gradient of the loss with respect to w, thus same shape as w
db -- gradient of the loss with respect to b, thus same shape as b
Tips:
- Write your code step by step for the propagation. np.log(), np.dot()
"""
m = X.shape[1]
A = sigmoid(np.dot(w.T,X)+b)
cost = -(1/m)*np.sum(Y*np.log(A) + (1-Y)*np.log(1-A))
# compute cost
### END CODE HERE ###
dw = 1/m*np.dot(X,(A-Y).T)
db = 1/m*np.sum(A-Y)
### END CODE HERE ###
assert(dw.shape == w.shape)
assert(db.dtype == float)
cost =np.squeeze(cost)
assert(cost.shape == ())
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Network… 9/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
dw = [[ 0.99845601]
[ 2.39507239]]
db = 0.00145557813678
cost = 5.80154531939
Expected Output:
dw [[ 0.99845601] [ 2.39507239]]
db 0.00145557813678
cost 5.801545319394553
d) Optimization
You have initialized your parameters.
You are also able to compute a cost function and its gradient.
Now, you want to update the parameters using gradient descent.
w b
Exercise: Write down the optimization function. The goal is to learn and by minimizing the cost
J θ
function . For a parameter , the update rule is θ = θ − α dθ α
, where is the learning rate.
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 10/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Arguments:
w -- weights, a numpy array of size (num_px * num_px * 3, 1)
b -- bias, a scalar
X -- data of shape (num_px * num_px * 3, number of examples)
Y -- true "label" vector (containing 0 if non-cat, 1 if cat), of shape (1, num
num_iterations -- number of iterations of the optimization loop
learning_rate -- learning rate of the gradient descent update rule
print_cost -- True to print the loss every 100 steps
Returns:
params -- dictionary containing the weights w and bias b
grads -- dictionary containing the gradients of the weights and bias with res
costs -- list of all the costs computed during the optimization, this will be
Tips:
You basically need to write down two steps and iterate through them:
1) Calculate the cost and the gradient for the current parameters. Use pro
2) Update the parameters using gradient descent rule for w and b.
"""
costs = []
for i in range(num_iterations):
params = {"w": w,
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 11/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
"b": b}
w = [[ 0.19033591]
[ 0.12259159]]
b = 1.92535983008
dw = [[ 0.67752042]
[ 1.41625495]]
db = 0.219194504541
Expected Output:
w [[ 0.19033591] [ 0.12259159]]
b 1.92535983008
dw [[ 0.67752042] [ 1.41625495]]
db 0.219194504541
Exercise: The previous function will output the learned w and b. We are able to use w and b to
predict the labels for a dataset X. Implement the predict() function. There is two steps to
computing predictions:
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 12/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Arguments:
w -- weights, a numpy array of size (num_px * num_px * 3, 1)
b -- bias, a scalar
X -- data of size (num_px * num_px * 3, number of examples)
Returns:
Y_prediction -- a numpy array (vector) containing all predictions (0/1) for t
'''
m = X.shape[1]
Y_prediction = np.zeros((1, m))
w = w.reshape(X.shape[0], 1)
A = sigmoid(np.dot(w.T, X) + b)
### END CODE HERE ###
for i in range(A.shape[1]):
return Y_prediction
In [31]: w = np.array([[0.1124579],[0.23106775]])
b = -0.3
X = np.array([[1.,-1.1,-3.2],[1.2,2.,0.1]])
print ("predictions = " + str(predict(w, b, X)))
predictions = [[ 1. 1. 0.]]
Expected Output:
predictions [[ 1. 1. 0.]]
Initialize (w,b)
Optimize the loss iteratively to learn parameters (w,b):
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 13/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 14/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Arguments:
X_train -- training set represented by a numpy array of shape (num_px * num_px
Y_train -- training labels represented by a numpy array (vector) of shape (1,
X_test -- test set represented by a numpy array of shape (num_px * num_px * 3
Y_test -- test labels represented by a numpy array (vector) of shape (1, m_te
num_iterations -- hyperparameter representing the number of iterations to opt
learning_rate -- hyperparameter representing the learning rate used in the upd
print_cost -- Set to true to print the cost every 100 iterations
Returns:
d -- dictionary containing information about the model.
"""
w, b = initialize_with_zeros(X_train.shape[0])
# Gradient descent (≈ 1 line of code)
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations": num_iterations}
return d
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 15/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Expected Output:
Cost after
0.693147
iteration 0
⋮ ⋮
Train 99.04306220095694
Accuracy %
Comment: Training accuracy is close to 100%. This is a good sanity check: your model is working
and has high enough capacity to fit the training data. Test error is 68%. It is actually not bad for this
simple model, given the small dataset we used and that logistic regression is a linear classifier. But
no worries, you'll build an even better classifier next week!
Also, you see that the model is clearly overfitting the training data. Later in this specialization you
will learn how to reduce overfitting, for example by using regularization. Using the code below (and
changing the index variable) you can look at predictions on pictures of the test set.
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 16/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 17/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Interpretation: You can see the cost decreasing. It shows that the parameters are being learned.
However, you see that you could train the model even more on the training set. Try to increase the
number of iterations in the cell above and rerun the cells. You might see that the training set
accuracy goes up, but the test set accuracy goes down. This is called overfitting.
Reminder: In order for Gradient Descent to work you must choose the learning rate wisely. The
α
learning rate determines how rapidly we update the parameters. If the learning rate is too large
we may "overshoot" the optimal value. Similarly, if it is too small we will need too many iterations to
converge to the best values. That's why it is crucial to use a well-tuned learning rate.
Let's compare the learning curve of our model with several choices of learning rates. Run the cell
below. This should take about 1 minute. Feel free also to try different values than the three we have
initialized the learning_rates variable to contain, and see what happens.
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 18/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 19/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
for i in learning_rates:
plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learn
plt.ylabel('cost')
plt.xlabel('iterations')
-------------------------------------------------------
-------------------------------------------------------
-------------------------------------------------------
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 20/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Interpretation:
Different learning rates give different costs and thus different predictions results.
If the learning rate is too large (0.01), the cost may oscillate up and down. It may even
diverge (though in this example, using 0.01 still eventually ends up at a good value for the
cost).
A lower cost doesn't mean a better model. You have to check if there is possibly overfitting.
It happens when the training accuracy is a lot higher than the test accuracy.
In deep learning, we usually recommend that you:
Choose the learning rate that better minimizes the cost function.
If your model overfits, use other techniques to reduce overfitting. (We'll talk about
this in later videos.)
1. Click on "File" in the upper bar of this notebook, then click "Open" t
o go on your Coursera Hub.
2. Add your image to this Jupyter Notebook's directory, in the "images" f
older
3. Change your image's name in the following code
4. Run the code and check if the algorithm is right (1 = cat, 0 = non-ca
t)!
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 21/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a
Finally, if you'd like, we invite you to try different things on this Notebook. Make sure you submit
before trying anything. Once you submit, things you can play with include:
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 22/23
11/8/2017 Logistic Regression with a Neural Network mindset v4
Bibliography:
http://www.wildml.com/2015/09/implementing-a-neural-network-from-scratch/
(http://www.wildml.com/2015/09/implementing-a-neural-network-from-scratch/)
https://stats.stackexchange.com/questions/211436/why-do-we-normalize-images-by-
subtracting-the-datasets-image-mean-and-not-the-c
(https://stats.stackexchange.com/questions/211436/why-do-we-normalize-images-by-
subtracting-the-datasets-image-mean-and-not-the-c)
In [ ]:
https://hub.coursera-notebooks.org/user/ceugcspkoirhiylurudnwn/notebooks/Week%202/Logistic%20Regression%20as%20a%20Neural%20Networ… 23/23