集训结束,开始刷课。
今天刚好刷到三剑客批评当今男青年娘炮化,我个人觉着有点言过其实。个人审美有不同,你也不能觉着别人白白净净的就不行啊。不过剑客君发的那几张战士腹肌图,让我好羡慕啊啊啊。我现在最多四块,还要加油,尤其是强化上肢力量和核心力量。
每天五点半背空呼,负重三公里。这是一个小目标,用一个月时间把三公里跑到11分以内。
还是我一直认同的:
文明其精神,野蛮其体魄
1.1 Sigmoid function, np.exp()
import numpy as np
def basic_sigmoid(x):
"""
Compute sigmoid of x.
"""
s= 1.0/(1+1/np.exp(x))
return sx= [1,2,3]
basic_sigmoid(x)
1.2 Sigmoid gradient
Formula is :
sigmoid_derivative(x) = o'(x) = o(x)(1-o(x))
### sigmoid 函数的导数,具体由导数推导而得 ###
import numpy as np
def sigmoid_derivative(x):
"""
Compute sigmoid of x.
ds - - - your computed gradient.
"""
s = 1.0 / (1 + 1 / np.exp(x))
ds= s * ( 1 - s )
return dsx= [1,2,3]
print ("sigmoid_derivative(x) = " + str(sigmoid_derivative(x)))
1.3 Reshaping arrays
wo 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.
example:
w = np.zeros((5,6))
w.shape[0] = 5 row
w.shape[1] = 6 column
shape()方法用来查看和建立数组的维度。
reshape()方法是数组对象中的方法,用于改变数组的形状。
In other words, you “unroll”, or reshape, the 3D array into a 1D vector.
具体来说就是把三维数组V给 reshape成一维数组,把行的数目算出来,就这么简单。
v = v.reshape((v.shape[0]*v.shape[1], v.shape[2])) # v.shape[0] = a ; v.shape[1] = b ; v.shape[2] = c
import numpy as np
def image2vector def image2vector(image):
v = image.reshape((image.shape[0] * image.shape[1] * image.shape[2], 1))
return v
1.4 Normalizing rows
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).
就是说在Deep learning中,初始化数据能够使梯度下降算法更高效。
梯度下降,具体在高等数学下册有详细介绍,有兴趣可以去翻一翻。
Note :
that you can divide matrices of different sizes and it works fine: this is called broadcasting
写在笔记里,不想贴上来了,麻烦
def normalizeRows(x):
x_norm = np.linalg.norm(x, axis=1, keepdims = True) # axis =1 计算每一行的长度,得到一个列向量
x = x / x_norm # 利用广播(broadcasting),用矩阵与列向量相除(自动补齐)
return x
x = np.array([
[0,3,4],
[1,6,4]
])
normalizeRows(x)
1.5 Broadcasting and the softmax function
It is very useful for performing mathematical operations between arrays of different shapes.
def softmax(x):
x_exp = np.exp(x)
x_sum = np.sum(x_exp, axis = 1, keepdims = True) # (n,1)
s = x_exp / x_sum #(n,m) 广播的作用
return s
Note:
- 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.
What you need to remember :
np.exxp(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 dimemsiongs straight will go toward rliminating a lot of bugs.
numpy has efficient bulit-in funtions
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.
将大量数据向量化能够节省大量运算时间,提高运算效率。
2.1 Implement the L1 and L2 loss functions
计算L1 和 L2 的损失函数
Exercise: Implement the numpy vectorized version of the L1 loss. You may find the function abs(x) (absolute value of x) useful.
The loss is used to evaluate the performance of your model. The bigger your loss is, the more different your predictions (y hat) are from the true values(y). In deep learning, you use optimization algorithms like Gradient Descent to train your model and to minimize the cost.
L1:
loss = np.sum(np.abs(y - yhat))
L2:
loss = np.sum(np.power((y-yhat),2))
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 np.sum, np.dot, np.multiply, np.maximum, etc…
np.dot ###矩阵乘法,符合矩阵乘法规则
np.multiply ###实现对应元素相乘,比如(2,3) 矩阵 和(2,3)相乘。按矩阵运算法则是不允许的,可以用这个来对应相乘。
np.maximum:
- np.max:(a, axis=None, out=None, keepdims=False)
- 求序列的最值
- 最少接收一个参数
- axis:默认为列向(也即 axis=0),axis = 1 时为行方向的最值;
- np.maximum:(X, Y, out=None)
- X 与 Y 逐位比较取其大者;
- 最少接收两个参数
Part 2: Logistic Regression with a Neural Network mindset
import all the packages that yu will need during this assignment.
-numpy is the fundamental package for scientific computing with Python.
-h5py is a common package to interact with a dataset that is stored on an H5 file.
-matplotlib is a famous library to plot graphs in Python.
-PIL and scipy are used here to test your model with your own piture at the end.