Python Basics with Numpy (Optional Assignment)
Welcome to your first assignment. This exercise gives you a brief introduction to
Python. Even if you've used Python before, this will help familiarize you with the
functions we'll need.
Instructions:
You will be using Python 3.
Avoid using for-loops and while-loops, unless you are explicitly told to do so.
After coding your function, run the cell right below it to check if your result is
correct.
After this assignment you will:
Be able to use iPython Notebooks
Be able to use numpy functions and numpy matrix/vector operations
Understand the concept of "broadcasting"
Be able to vectorize code
Let's get started!
Important Note on Submission to the AutoGrader
Before submitting your assignment to the AutoGrader, please make sure you are not
doing the following:
1. You have not added any extra print statement(s) in the assignment.
2. You have not added any extra code cell(s) in the assignment.
3. You have not changed any of the function parameters.
4. You are not using any global variables inside your graded exercises. Unless
specifically instructed to do so, please refrain from it and use the local
variables instead.
5. You are not changing the assignment code where it is not required, like
creating extra variables.
If you do any of the following, you will get something like, Grader Error: Grader
feedback not found (or similarly unexpected) error upon submitting your assignment.
Before asking for help/debugging the errors in your assignment, check for these first.
If this is the case, and you don't remember the changes you have made, you can get
a fresh copy of the assignment by following these instructions.
Table of Contents
About iPython Notebooks
Exercise 1
1 - Building basic functions with numpy
1.1 - sigmoid function, [Link]()
o Exercise 2 - basic_sigmoid
o Exercise 3 - sigmoid
1.2 - Sigmoid Gradient
o Exercise 4 - sigmoid_derivative
1.3 - Reshaping arrays
o Exercise 5 - image2vector
1.4 - Normalizing rows
o Exercise 6 - normalize_rows
o Exercise 7 - softmax
2 - Vectorization
2.1 Implement the L1 and L2 loss functions
o Exercise 8 - L1
o Exercise 9 - L2
About iPython Notebooks
iPython Notebooks are interactive coding environments embedded in a webpage.
You will be using iPython notebooks in this class. You only need to write code
between the # your code here comment. After writing your code, you can run the cell
by either pressing "SHIFT"+"ENTER" or by clicking on "Run Cell" (denoted by a play
symbol) in the upper bar of the notebook.
We will often specify "(≈ X lines of code)" in the comments to tell you about how
much code you need to write. It is just a rough estimate, so don't feel bad if your
code is longer or shorter.
### v2.0
Exercise 1
Set test to "Hello World" in the cell below to print "Hello World" and run the two cells
below.
# (≈ 1 line of code)
# test =
# YOUR CODE STARTS HERE
# YOUR CODE ENDS HERE
print ("test: " + test)
Expected output: test: Hello World
What you need to remember :
Run your cells using SHIFT+ENTER (or "Run cell")
Write code in the designated areas using Python 3 only
Do not modify the code outside of the designated areas
1 - Building basic functions with numpy
Numpy is the main package for scientific computing in Python. It is maintained by a
large community ([Link]). In this exercise you will learn several key numpy
functions such as [Link], [Link], and [Link]. You will need to know how to use
these functions for future assignments.
1.1 - sigmoid function, [Link]()
Before using [Link](), you will use [Link]() to implement the sigmoid function.
You will then see why [Link]() is preferable to [Link]().
Exercise 2 - basic_sigmoid
Build a function that returns the sigmoid of a real number x. Use [Link](x) for the
exponential function.
Actually, we rarely use the "math" library in deep learning because the inputs of the
functions are real numbers. In deep learning we mostly use matrices and vectors.
This is why numpy is more useful.
### One reason why we use "numpy" instead of "math" in Deep Learning ###
Any time you need more info on a numpy function, we encourage you to look at the
official documentation.
You can also create a new cell in the notebook and write [Link]? (for example) to
get quick access to the documentation.
Exercise 3 - sigmoid
Implement the sigmoid function using numpy.
Instructions: x could now be either a real number, a vector, or a matrix. The data
structures we use in numpy to represent these shapes (vectors, matrices...) are
called numpy arrays. You don't need to know more for now.
1.2 - Sigmoid Gradient
As you've seen in lecture, you will need to compute gradients to optimize loss
functions using backpropagation. Let's code your first gradient function.
Exercise 4 - sigmoid_derivative
Implement the function sigmoid_grad() to compute the gradient of the sigmoid
function with respect to its input x. The formula is:
1.3 - Reshaping arrays
Two common numpy functions used in deep learning
are [Link] and [Link]().
[Link] is used to get the shape (dimension) of a matrix/vector X.
[Link](...) is used to reshape X into some other dimension.
For example, in computer science, an image is represented by a 3D array of shape
Note: In normalize_rows(), you can try to print the shapes of x_norm and x, and then
rerun the assessment. You'll find out that they have different shapes. This is normal
given that x_norm takes the norm of each row of x. So x_norm has the same number
of rows but only 1 column. So how did it work when you divided x by x_norm? This is
called broadcasting and we'll talk about it now!
Exercise 7 - softmax
Implement a softmax function using numpy. You can think of softmax as a
normalizing function used when your algorithm needs to classify two or more
classes. You will learn more about softmax in the second course of this
specialization.
Instructions:
Note that later in the course, you'll see "m" used to represent the "number of training
examples", and each training example is in its own column of the matrix. Also, each
feature will be in its own row (each row has data for the same feature).
Softmax should be performed for all features of each training example, so softmax
would be performed on the columns (once we switch to that representation later in
this course).
However, in this coding practice, we're just focusing on getting familiar with Python,
so we're using the common math notation
is the number of rows and is the number of columns.
Notes
If you print the shapes of x_exp, x_sum and s above and rerun the
assessment cell, you will see that x_sum is of shape (2,1) while x_exp and s
are of shape (2,5). x_exp/x_sum works due to python broadcasting.
Congratulations! You now have a pretty good understanding of python numpy and
have implemented a few useful functions that you will be using in deep learning.
What you need to remember:
[Link](x) works for any [Link] x and applies the exponential function to
every coordinate
the sigmoid function and its gradient
image2vector is commonly used in deep learning
[Link] is widely used. In the future, you'll see that keeping your
matrix/vector dimensions straight will go toward eliminating a lot of bugs.
numpy has efficient built-in functions
broadcasting is extremely useful
2 - Vectorization
In deep learning, you deal with very large datasets. Hence, a non-computationally-
optimal function can become a huge bottleneck in your algorithm and can result in a
model that takes ages to run. To make sure that your code is computationally
efficient, you will use vectorization. For example, try to tell the difference between the
following implementations of the dot/outer/elementwise product.
As you may have noticed, the vectorized implementation is much cleaner and more
efficient. For bigger vectors/matrices, the differences in running time become even
bigger.
Note that [Link]() performs a matrix-matrix or matrix-vector multiplication. This is
different from [Link]() and the * operator (which is equivalent to .* in
Matlab/Octave), which performs an element-wise multiplication.
2.1 Implement the L1 and L2 loss functions
Exercise 8 - L1
Implement the numpy vectorized version of the L1 loss. You may find the function
abs(x) (absolute value of x) useful.
Congratulations on completing this assignment. We hope that this little warm-up
exercise helps you in the future assignments, which will be more exciting and
interesting!
What to remember:
Vectorization is very important in deep learning. It provides computational
efficiency and clarity.
You have reviewed the L1 and L2 loss.
You are familiar with many numpy functions such as [Link], [Link],
[Link], [Link], etc...