2
The AI Workshop – By iTronics
Be able to use numpy functions and numpy matrix/vector operations
Understand the concept of "broadcasting"
Be able to vectorize code
Demo 1: Use Numpy
Objecifs???
3
The AI Workshop – By iTronics
Numpy is the main package for scientific computing in Python.
Demo 1: Use Numpy
What is numpy???
4
The AI Workshop – By iTronics
It is maintained by a large community (www.numpy.org).
Demo 1: Use Numpy
Value of numpy???
5
The AI Workshop – By iTronics
They are several key numpy functions such as:
np.exp, np.log, and np.reshape.
Demo 1: Use Numpy
How to use numpy???
6
The AI Workshop – By iTronics
Demo 1: Use Numpy
sigmoid function, np.exp()
7
The AI Workshop – By iTronics
It is a non-linear function used not only in Machine Learning (Logistic
Regression), but also in Deep Learning.
Demo 1: Use Numpy
We do use sigmoid in:
8
The AI Workshop – By iTronics
It is a non-linear function used not only in Machine Learning (Logistic
Regression), but also in Deep Learning.
Demo 1: Use Numpy
Why use np.exp() not math.exp():
9
The AI Workshop – By iTronics
you will need to compute gradients to optimize loss functions using back-
propagation.
Demo 1: Use Numpy
Sigmoid Gradient:
10
The AI Workshop – By iTronics
Two common numpy functions used in deep learning are np.shape and
np.reshape().
X.shape is used to get the shape (dimension) of a matrix/vector X.
X.reshape(...) is used to reshape X into some other dimension.
Demo 1: Use Numpy
Reshaping Array:
12
The AI Workshop – By iTronics
Demo 1: Use Numpy
Broadcasting and the softmax function:
13
The AI Workshop – By iTronics
Demo 1: Use Numpy
Reshaping Array:
### START CODE HERE ### (≈ 3 lines of code)
x_exp = np.exp(x)
x_sum = np.sum(x_exp, axis = 1, keepdims = True )
s = x_exp/x_sum
### END CODE HERE ###
14
The AI Workshop – By iTronics
Demo 1: Use Numpy
What you need to remember::
-np.exp(x) works for any np.array x and applies the
exponential function to every coordinate
-the sigmoid function and its gradient
-image2vector is commonly used in deep learning
-np.reshape 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