deeplearning course-01
@(学习笔记)
本笔记为Andrew NG的DeepLearning课程,其链接为:Coursera链接 网易云课堂链接
个人建议用coursera的比较好,虽然付费了,但是有题目可以做
PS:如果有错误,欢迎指正!
1.学习概论
- m作为训练数据大小
2. numpy常用操作方式
- 1.
cal = A.sum(axis = 0) #矩阵纵向列(垂直)相加
- numpy具备 broadcast功能,即,矩阵向量等相加,自动扩增维度相加;
A = [1 ,2 ,3
4, 5, 6]
B = [2 , 2, 2]
C = A + B = [3, 4, 5
6, 7, 8]
- debug方式
- 不要使用shape (5,)的方式,该方式生成的不是向量而是一排数字,其转置和本体相同,可能会造成奇怪的问题;
a = np.random.randn(5) 错误,rank = 1
a = np.random.randn(5,1) 正确
- 利用断言判断维度是否正确:
assert(a.shape ==(5,1))
- 归一化
Another common technique we use in Machine Learning and Deep Learning is to normalize our data. It often leads to a better performance because gradient descent converges faster after normalization. Here, by normalization we mean changing x to x∥x∥ x ‖ x ‖ (dividing each row vector of x by its norm).
For example, if
x=[023644](3)
(3)
x
=
[
0
3
4
2
6
4
]
then
∥x∥=np.linalg.norm(x,axis=1,keepdims=True)=[556−−√](4)
(4)
‖
x
‖
=
n
p
.
l
i
n
a
l
g
.
n
o
r
m
(
x
,
a
x
i
s
=
1
,
k
e
e
p
d
i
m
s
=
T
r
u
e
)
=
[
5
56
]
and
x_normalized=x∥x∥=[0256√35656√45456√](5)
(5)
x
_
n
o
r
m
a
l
i
z
e
d
=
x
‖
x
‖
=
[
0
3
5
4
5
2
56
6
56
4
56
]
Note that you can divide matrices of different sizes and it works fine: this is called broadcasting and you’re going to learn about it in part 5.
- 展开矩阵为一维
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).
A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b ∗ ∗ cd, a) is to use:
X_flatten = X.reshape(X.shape[0], -1).T # X.T is the transpose of X
- propagate:
m = X.shape[1]
# FORWARD PROPAGATION (FROM X TO COST)
### START CODE HERE ### (≈ 2 lines of code)
A = sigmoid(np.dot(w.T,X)+b) # compute activation
print("A shape: " + str(A.shape))
print("Y shape: " + str(Y.shape))
cost = -sum(sum(Y*np.log(A) + (1-Y)*np.log(1-A))/m) # compute cost
print("cost" + str(cost))
### END CODE HERE ###
# BACKWARD PROPAGATION (TO FIND GRAD)
### START CODE HERE ### (≈ 2 lines of code)
dw = np.dot(X,(A-Y).T)/m
db = np.sum(A-Y)/m
### END CODE HERE ###