吴恩达第一课logistic Regress编程练习(学习笔记)

时间:2022-06-05 20:14:29

目标:利用logisitic Regression来预测图片是否有猫咪,即简单的二分类问题。


过程:初始化(向量输入,归一化),前向和反向传播,梯度下降,优化,预测,输出与可视化


初始化:1.明确输入集
数量;测试集数量dim

    2.输入向量化n*n*nc

A trick when you want to flatten 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.reshape(X.shape[0], -1).T # X.T is the transpose of X

    3.确定batch-size,输入归一化,压缩到【0,1】之间,便于计算。

    4.初始化权值、偏差

   w = np.zeros((dim, 1))
b = 0


前向与反向传播:

       1.激活函数sigmoid

       2.z = w.T * X + b

       3.激活函数sigmoid

       4.损失函数cost = -y*log(pred_y) -(1-y)*log(1-pred_y)

    A = sigmoid(np.dot(w.T, X) + b)                                     # compute activation
cost = -1/m * np.sum((Y*np.log(A) + (1-Y)*np.log(1-A))) # compute cost


反向传播梯度下降:根据链式法则有:损失函数对w的偏导为(a-y)*X,损失函数对b的偏导为(a-y)

    dw = np.dot(X, (A - Y).T)/m
db = np.sum(A - Y)/m

优化(多次迭代):

        w = w - learning_rate*dw

预测:

将新的输入与模型参数运算得到结果Pred-y,若大于0.5则输出1,反之输出0


模型整合:

1.初始化参数;2.正反向传播;3.优化;4.预测;5.打印显示与绘图可视化


超参数调试:

尝试不同的学习速率、batch-size和迭代次数,观察训练和测试准确率问题。


What you need to remember:

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_px * num_px * 3, 1)
  • "Standardize" the data

What to remember: You've implemented several functions that:

Initialize (w,b) Optimize the loss iteratively to learn parameters (w,b):
  • computing the cost and its gradient
  • updating the parameters using gradient descent
Use the learned (w,b) to predict the labels for a given set of examples

What to remember from this assignment:

Preprocessing the dataset is important. You implemented each function separately: initialize(), propagate(), optimize(). Then you built a model(). Tuning the learning rate (which is an example of a "hyperparameter") can make a big difference to the algorithm. You will see more examples of this later in this course!