逻辑回归
logistic回归又称logistic回归分析,是一种广义的线性回归分析模型
目标函数
$$
\begin{align*}
& f(x)=wx+b \
& p(y|x,w,b)=\frac{1}{1+e^{-f(x)}} \qquad #加入sigmoid \
& p(y|x,w,b)=\frac{1}{1+e^{-(wx+b)}}\
& p(y|x,w,b)=p(y=1|x,w,b)^y[1-p(y=1|x,w,b)]^{1-y}\qquad # 目标函数\
\end{align*}
$$
优化目标
$$
\begin{align*}
& argmax_w\prod_{i=1}^{n}p(y_i|x_i,w,b) \qquad#最优化目标函数的w和b值 \
& argmax_w log(\prod_{i=1}^{n}p(y_i|x_i,w,b))=argmax_w\sum_{i=1}^nlog(p(y_i|x_i,w,b))\qquad#因为对数函数是单调递增的所以可以对函数进行log一下\&并且通过对数函数的性质把连乘变成了加,免得连乘出来的书过于小\
& L(x)=argmin_{w,b}-\sum_{i=1}^nlog(p(y_i|x_i, w,b))=argmin_{w,b}-\sum_{i=1}^n(log(p(y_i|x_i,w,b))^{y_i}+log((1-p(y_i|x_i,w,b))^{(1-y_i)})\qquad \
\end{align*}
$$
求导过程
$$
\begin{align*}
& #基础公式 \
& (log(x))^”=\frac{1}{x} \
& (x^y)^”=yx \
& (\frac{x}{y})^”=(\frac{x^”y-xy^”}{y^2}) \
& #对sigmoid求导 \
& \frac{\delta p(y|x,w,b)}{\delta f(x)}=\frac{-(e^{-f(x)})^”}{(1+e^{-f(x)})^2}=\frac{e^{-f(x)}}{(1+e^{-f(x)})^2}=\frac{1+e^{-f(x)}-1}{(1+e^{-f(x)})^2}=(\frac{1}{1+e^{-f(x)}}-\frac{1}{(1+e^{-f(x)})^2}) \
& =\frac{1}{1+e^{-f(x)}}(1-\frac{1}{1+e^{-f(x)}})=p(y|x,w,b)(1-p(y|x,w,b)) \
& #对目标函数求导 \
& \frac{\delta L(x)}{\delta f(x)}=argmin_{w,b}-(\sum_{i=1}^{n}(logp(y_i|x_i,w,b)^{y_i}+log(1-p(y_i|x,w,b))^{(1-y_i)})^” \
& =argmin_{w,b}-(\sum_{i=1}^n(y_ilogp(y_i|x_i,w,b)+((1-y_i))log((1-p(y_i|x,w,b))))^” \
& =argmin_{w,b}-(\sum_{i=1}^ny_i\frac{p(y_i|x_i,w,b)^”}{p(y_i|x_i,w,b)}+(1-y_i)\frac{-p(y_i|x_i,w,b)^”}{1-p(y_i|x_i,w,b)})\
& =argmin_{w,b}-(\sum_{i=1}^ny_i\frac{p(y_i|x_i,w,b)^”}{p(y_i|x_i,w,b)}+(y_i-1)\frac{p(y_i|x_i,w,b)^”}{1-p(y_i|x_i,w,b)})\
& =argmin_{w,b}-(\sum_{i=1}^ny_i\frac{p(y|x,w,b)(1-p(y|x,w,b))}{p(y_i|x_i,w,b)}+(y_i-1)\frac{p(y|x,w,b)(1-p(y|x,w,b))}{1-p(y_i|x_i,w,b)}) \
& =argmin_{w,b}-(\sum_{i=1}^ny_i(1-p(y|x,w,b)+(y_i-1)p(y|x,w,b)) \
& =argmin_{w,b}-(\sum_{i=1}^ny_i-y_ip(y|x,w,b)+y_ip(y|x,w,b)-p(y|x,w,b)) \
& =argmin_{w,b}-(\sum_{i=1}^ny_i-p(y|x,w,b))=argmin_{w,b}(\sum_{i=1}^np(y|x,w,b)-y_i) \
\end{align*}
$$
通过SGD求解w和b
$$
\begin{align*}
& #使用梯度下降来优化w和b \
& w_i=w_i-\lambda\frac{\delta L(x)}{\delta f(x)}\frac{\delta f(x)}{\delta w_i} \
& =w_i-\lambda(\sum_{i=1}^np(y|x,w,b)-y_i)x_i \
& b=b-\lambda\frac{\delta L(x)}{\delta f(x)}\frac{\delta f(x)}{\delta b} \
& =b-\lambda\sum_{i=1}^n(p(y|x,w,b)-y_i) \
& \
\end{align*}
$$
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 zoubinbf@163.com