逻辑回归的Model为:$ h_\theta(x)=\frac 1 {1+e^{-(\theta^Tx+b)}} $
针对一个样本的代价函数为:
if y = 1 : $ cost(x)= -log(h_\theta(x))$
if y = 0 : $ cost(x)= -log(1-h_\theta(x)) $
上述代价函数可以写成一个式子:即$ cost(x)=-ylog(h_\theta(x))-(1-y)log(1-h_\theta(x)) $
损失函数可以看成是针对所有样本的代价函数的平均值,即:
$ J_\theta(x)=\frac 1 m \displaystyle\sum_{i=1}^m(cost(x^{(i)})) = -\frac 1 m \displaystyle\sum_{i=1}^m(y^{(i)}log(h_\theta(x^{(i)}))+(1-y^{(i)})log(1-h_\theta(x^{(i)})))$
对参数\(\theta\)求导:
$ \frac \partial {\partial\theta_j} J_\theta(x)= \frac 1 m \displaystyle\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})x_j^{(i)} $
对参数\(b\)求导:
$ \frac \partial {\partial b} J_\theta(x)= \frac 1 m \displaystyle\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)}) $
$\theta_j := \theta_j - \alpha \frac \partial {\partial\theta_j} J_\theta(x) = \theta_j - \alpha \frac 1 m \displaystyle\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})x_j^{(i)} $
$b := b - \alpha \frac \partial {\partial b} J_\theta(x) = b - \alpha \frac 1 m \displaystyle\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)}) $
原文:https://www.cnblogs.com/xiaoma927/p/9716018.html