Explanation of logistic regression cost function (逻辑回归损失函数的解释)
练习
Python Basics with numpy
目标:
Learn how to use numpy
Implement some basic core deep learning functions such as the softmax, sigmoid, dsigmoid, etc. 熟悉几个激活函数
Learn how to handle data by normalizing inputs and reshaping images. 均一化输出及重塑图片
Recognize the importance of vectorization. 向量化
Understand how python broadcasting works. 广播
Building basic functions with numpy
sigmoid :s = 1 / ( 1 + np.exp(-x))
sigmoid gradient: ds = s*(1-s)
reshaping arrays: 将图片转化为向量(image2vector)
normalizing rows: (normalizingRows)
softmax :(softmax)
# image2vector is commonly used in deep learningdefimage2vector(image):#aurgument:#image - a numpy array of shape (length, height, depth)#returns:#v - a vector of shape (length*height*depth, 1)
v = image.reshpe((image.shape[0]*image.shape[1]*image.shape[2],1))return v
# normalizingRowsdefnormalizingRows(x):#normlize x by row (axis=1)
x_norm = np.linalg.norm(x,ord=2, axis=1, keepdims=True)
x = x / x_norm
return x
# softmax functiondefsoftmax(x):# x - matrix of shape(m,n)# s -softmax of x
x_exp = np.exp(x)
x_sum = np.sum(x_exp, axis=1, keepdims=True)
s = x_exp / x_sum
return s
Logistic Regression with a Neural Network mindset
目标
建立一个图片识别算法,用于分类猫/非猫图片,准确率70%。
Work with logistic regression in a way that builds intuition relevant to neural network.
Learn how to minimize the cost function. 学习怎样最小化代价函数
Understand how derivatives of the cost are used to update parameters. 理解代价函数的梯度是怎样用于更新参数的。
Overview of the Problem set (对数据集的总览)
Common steps for pre-processing a new dataset are:
Figure out the dimensions and shapes of the problem (m_train, m_test, num_px…)
Reshape the datasets such that each example is now a vector of size (num_pxnum_px3,1)
General Architecture of the learning algorithm (算法的搭建)
内容:
Initialize the parameters of the model (初始化参数)
Learn the parameters for the model by minimizing the cost最小化代价函数以学习参数
Use the learned parameters to make predictions (预测)
Analyse the results and conclude
算法模块:
Initialize With Zeros
Loop:
Calculate current loss (forward propagation) - propagate()
Calculate current gradient (backward propagation) - propagate()
Update parameters (gradient descent) - optimize()
Integrate them into one function - model()
# find the values for number of training examples
m_train = train_set_y.shape[1]
# fllaten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape(b*c*d, a) is to use: X_flatten = X.shape([0], -1).T
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
# standardize a image
train_set_x = train_set_x_flatten/255.
def initialize with zeros(dim):
w = np.zeros(shape =(dim,1))
b =0return w, b
dim =2
w, b = initialize_with_zeros(dim)
defpropagate(w,b,X,Y):
m = X.shape[1]
A = sigmoid(np.dot(w.T,X)+ b)
cost =(-1/ m)*np.sum(Y*np.log(A)+(1-Y)*(np.log(1-A)))
dw =(1/m)* np.dot(X,(A-Y).T)
db =(1/m)* np.sum(A-Y)
grads ={"dw":dw,"db":db}return grads, cost
defoptimize(w, b, X, Y, num_iterations, learning_rate, print_cost =False):
cost =[]for i inrange(num_iterations):
dw = grads["dw"]
db = grads["db"]
w = w - learning_rate * dw
b = b - leanring_rate * db
# Record the costsif i %100==0:
costs.append(cost)# Print the cost every 100 training iterationsif print_cost and i %100==0:print("Cost after iteration %i: %f"%(i, cost))
params ={"w": w,"b": b}
grads ={"dw": dw,"db": db}return params, grads, costs