本周学习目标:
Build a logistic regression model, structured as a shallow neural network
Implement the main steps of an ML algorithm, including making predictions, derivative computation, and gradient descent.
Implement computationally efficient, highly vectorized, versions of models.
Understand how to compute derivatives for logistic regression, using a backpropagation mindset.
Become familiar with Python and Numpy
Work with iPython Notebooks
Be able to implement vectorization across multiple training examples
yhat即给定输入特征x时,预测y为1的概率,这里我们使用sigmoid函数,当大于0时概率取值为0.5越大越接近1.
在分类问题中平方误差无法让梯度下降算法良好运行,所以在逻辑回归中我们会选择-(y*log^y+(1-y)
*log^(1-y))作为我们的误差函数
关于梯度下降:梯度是我们沿着上升率最快的地方,那么负的梯度即是下降最快的,我们要到一个局部最低点那么w = w - alha*dw
sigmoid函数图像
实现sigmoid,
import numpy as np
def sigmoid(x):
s = 1./(1.+np.exp(-x))
return s
然后计算s的梯度
def sigmoid_derivative(x):
s = sigmoid(x)
ds = s*(1-s)
return ds
重要知识:我们通常需要正规化将各个元素,这样我们的梯度下降算法将会更好的运行.
x_norm = np.linalg.norm(x,axis=1,keepdims=True)按行进行相加得出平方和的开方
如果进行多分类的话就需要用的softmax函数了
如果过拟合了我们可以要对他进行L1和L2约束
我们建立算法一般步骤是:
1.定义模型结构
2.初始化模型参数
3.开始循环计算
计算当前损失函数(前向传播)
计算当前梯度(反向传播)
更新参数(梯度下降)
网友评论