Cours1 Annotations
Cours1 Annotations
2023-2024
1
CHAPTER 1
LOGISTIC REGRESSION
2
Introduction to Deep Learning
3
The Machine Learning Approach
• Instead of writing a program by hand for each specific task, we collect lots
of examples that specify the correct output for a given input.
• A machine learning algorithm then takes these examples and produces a
program that does the job.
– The program produced by the learning algorithm may look very
different from a typical hand-written program. It may contain millions
of numbers.
– If we do it right, the program works for new cases as well as the ones
we trained it on.
– If the data changes the program can change too by training on the new
data.
• Massive amounts of computation are now cheaper than paying someone
to write a task-specific program.
4
It is very hard to say what makes a 2
5
Some examples of tasks best solved by learning
• Recognizing patterns:
– Objects in real scenes
– Facial identities or facial expressions
– Spoken words
• Recognizing anomalies:
– Unusual sequences of credit card transactions
– Unusual patterns of sensor readings in a nuclear power plant
• Prediction:
– Future stock prices or currency exchange rates
– Which movies will a person like?
6
Types of learning task
• Supervised learning
– Learn to predict an output when given an input vector.
• Reinforcement learning
– Learn to select an action to maximize payoff.
• Unsupervised learning
– Discover a good internal representation of the input.
7
What you will learn in this course?
8
What is neural network?
It is a powerful learning algorithm inspired by how the brain works.
Example 1 – single neural network
• Given data about the size of houses on the real estate market and you
want to fit a function that will predict their price. It is a linear regression
problem because the price as a function of size is a continuous output.
• We know the prices can never be negative so we are creating a function
called Rectified Linear Unit (ReLU) which starts at zero.
9
Example 1 – single neural network
10
Example 2 – Multiple neural network
The price of a house can be affected by other features such as size, number of
bedrooms, zip code and wealth. The role of the neural network is to predict
the price and it will automatically generate the hidden units. We only need to
give the inputs x and the output y.
11
Supervised learning for Neural Network
In supervised learning, we are given a dataset and already know what our
correct output should look like, having the idea that there is a relationship
between the input and the output.
Supervised learning problems are categorized into "regression" and
"classification" problems.
In a regression problem, we are trying to predict results within a
continuous output, meaning that we are trying to map input variables to
some continuous function.
In a classification problem, we are instead trying to predict results in a
discrete output. In other words, we are trying to map input variables into
discrete categories.
12
Examples of supervised learning
Here are some examples of supervised learning
14
Why is deep learning taking off?
Deep learning is taking off due to a large amount of data available through
the digitization of the society, faster computation and innovation in the
development of neural network algorithm.
16
Binary Classification
In a binary classification problem, the result is a discrete value output.
For example
account hacked or compromised
a tumor malign or benign
64
64
17
Binary Classification
An image is store in the computer in three separate matrices
corresponding to the Red, Green, and Blue color channels of the image.
The three matrices have the same size as the image, for example, the
resolution of the cat image is 64 pixels x 64 pixels, the three matrices
(RGB) are 64 X 64 each.
The value in a cell represents the pixel intensity which will be used to
create a feature vector of n dimension.
In pattern recognition and machine learning, a feature vector represents
an object, in this case, a cat or no cat.
Red
- To create a feature vector, , the pixel intensity
values will be “unroll” or “reshape” for each
color. Green
- The dimension of the input feature vector is:
𝒙 .
Blue
18
Binary Classification
19
Notation
training examples: ( ) ( ) ( ) ( ) ( ) ( )
( ) ( ) ( ) ×
( ) ( ) ( ) ×
Y
20
Logistic Regression
Logistic regression is a learning algorithm used in a supervised learning
problem when the output are all either zero or one. The goal of logistic
regression is to minimize the error between its predictions and training
data.
Example: Cat vs No - cat
Given an image represented by a feature vector , the algorithm will
evaluate the probability of a cat being in that image.
21
Logistic Regression
Parameters:
Output:
23
Logistic regression cost function
( ) ( ) ( ) ( ) ( ) ( ) ()
24
Logistic regression cost function
The loss function computes the error for a single training example.
(cross-
entropy)
If 𝑦 = 1: ℒ 𝑦 , 𝑦 = −𝑙𝑜𝑔 𝑦 where 𝑙𝑜𝑔 𝑦 =0 and 𝑦 should be close to 1
25
26
Logistic regression cost function
Cost function
The cost function is the average of the loss function of the entire training
set. The goal is to find the parameters and that minimize the overall
cost function.
27
Gradient Descent
Recap:
, where
28
Gradient Descent
Repeat {
}
: Learning rate
cost function
29
Computation Graph
30
Computation Graph
31
Logistic Regression Gradient descent
32
Logistic Regression Gradient descent
33
Logistic regression on m examples
()
()
34
Logistic regression on m examples (iterative)
()
()
;
;
;
35
What is vectorization?
= ;
for i in range(nx):
z = np.dot(w,x) + b
+= [ ] [ ];
+= ;
36
Neural network programming guideline
Whenever possible, avoid explicit for loops
U = Av
𝐢 𝐢𝐣 𝐣
𝐢 𝐣
U = np.zeros((n,1))
u = np.dot(A,v)
for i=1...
for j=1...
u[i] += A[i][j]*v[j];
37
Vectors and matrix valued functions
Say you need to apply the exponential operation on every element of a
matrix/vector.
38
Logistic regression derivatives
db += dz ( )
db += dz ()
39
Vectorizing Logistic Regression
( ) ( )
, ( ) ( )
, ( ) ( )
,
( ) ( ) ( ) ( ) ( ) ( )
( ) ( ) ( )
( ) ( ) ( ) ( ) ( ) ( )
z = np.dot(w.T,x) + b
( ) ( ) ( )
A = sigmoid(Z)
40
Implementing Logistic Regression
41
References
Andrew Ng. Deep learning Specialization. Deeplearning.AI.
Geoffrey Hinton. Neural Networks for Machine Learning.
42