week2_Part1&2_LR with a Neural Network mindset_yhd
目前的學習心得: 1、每週的視訊課程看一到兩遍 2、做筆記
3、做每週的作業練習,這個裡面的含金量非常高。掌握後一定要自己敲一遍,這樣以後用起來才能得心應手。
有需要全套作業練習notebook及全套資料的可以留言或者加我微信yuhaidong112
二分類
-
預測輸出為正類(+1)的概率:
-
單樣本正向傳播:
-
單樣本反向傳播:
-
多樣本的cost function和反向傳播:
- 梯度下降法:
Part 1:Python Basics with Numpy (optional assignment)
Building basic functions with numpy
Numpy is the main package for scientific computing in Python. It is maintained by a large community (www.numpy.org). In this exercise you will learn several key numpy functions such as np.exp, np.log, and np.reshape. You will need to know how to use these functions for future assignments.
1.1 - sigmoid function, np.exp()
Exercise: Build a function that returns the sigmoid of a real number x. Use math.exp(x) for the exponential function.
Reminder:
is sometimes also known as the logistic function. It is a non-linear function used not only in Machine Learning (Logistic Regression), but also in Deep Learning.
To refer to a function belonging to a specific package you could call it using package_name.function(). Run the code below to see an example with math.exp().
#GRADED FUNCTION:basic_sigmoid
import math
def basic_sigmoid(x):
'''
compute sigmoid of x
Arguments:
x --- A scalar
returns:
s --- sigmoid(x)
'''
s = 1.0/(1+1/math.exp(x))
return s
basic_sigmoid(3)
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
x = [1, 2, 3] basic_sigmoid(x) # you will see this give an error when you run it, because x is a vector.
In fact, if is a row vector then will apply the exponential function to every element of x. The output will thus be:
import numpy as np
# example of np.exp()
x = np.array([1,2,3])
print (np.exp(x))
Furthermore, if x is a vector, then a Python operation such as or will output s as a vector of the same size as x.
# example of vector operation
x = np.array([1, 2, 3])
print (x + 3)
Exercise: 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.