【Day 12】逻辑回归(Logistic Regression)(上)

步骤一:Function Set

昨天的最後我们提到我们要找一个事後机率(Posterior Probability),如果它大於 0.5 就输出 https://chart.googleapis.com/chart?cht=tx&chl=C_1,否则就输出 https://chart.googleapis.com/chart?cht=tx&chl=C_2。如果是使用Gaussian,则 https://chart.googleapis.com/chart?cht=tx&chl=P_%7Bw%2Cb%7D(C_1%7Cx)%20%3D%20%5Csigma%20(z)https://chart.googleapis.com/chart?cht=tx&chl=z%20%3D%20w%20%5Ccdot%20x%20%2B%20b

  • Function set:https://chart.googleapis.com/chart?cht=tx&chl=f_%7Bw%2Cb%7D(x)%20%3D%20P_%7Bw%2Cb%7D(C_1%7Cx)

如果用图像化表示则整个模型如下图所示,将所有的输入 https://chart.googleapis.com/chart?cht=tx&chl=x_i 分别乘上 https://chart.googleapis.com/chart?cht=tx&chl=w_i 再加上 https://chart.googleapis.com/chart?cht=tx&chl=b 就会得得到 https://chart.googleapis.com/chart?cht=tx&chl=z,即 https://chart.googleapis.com/chart?cht=tx&chl=z%20%3D%20%5Cdisplaystyle%20%5Csum_i%20w_ix_i%20%2B%20b,接着 https://chart.googleapis.com/chart?cht=tx&chl=z 通过Sigmoid function输出的值就是事後机率,这整件事情我们就称为逻辑回归(Logistic Regression)

逻辑回归(Logistic Regression) v.s. 线性回归(Linear Regression)

步骤二:Goodness of a Function

训练资料需要标记(Label),也就是说每个 https://chart.googleapis.com/chart?cht=tx&chl=x 都要标记它属於哪一个class。假设我们的训练资料是从事後机率所产生的,只要给我们一个 https://chart.googleapis.com/chart?cht=tx&chl=w%2C%20b 我们就决定了事後机率,就可以去计算某一组 https://chart.googleapis.com/chart?cht=tx&chl=w%2C%20b 产生 https://chart.googleapis.com/chart?cht=tx&chl=N 笔训练资料的机率,而我们要找一组可以最大化这个机率的 https://chart.googleapis.com/chart?cht=tx&chl=w%5E*%2C%20b%5E*

经过一些数学运算则可以将式子改为 https://chart.googleapis.com/chart?cht=tx&chl=w%5E*%2C%20b%5E*%20%3D%20arg%20%5C%20%5Cdisplaystyle%20%5Cmin_%7Bw%2C%20b%7D%20-lnL(w%2Cb)

整理过後可以发现 https://chart.googleapis.com/chart?cht=tx&chl=%5CSigma 後面那一长串是两个伯努利分布(Bernoulli distribution)的交叉熵(Cross-entropy),Cross-entropy代表的涵义是这两个distribution有多接近,如果两个分布是一样的,那Cross-entropy算出来就会是 0。因此我们在做的就是去算所有样本的Cross-entropy的总和。

逻辑回归(Logistic Regression) v.s. 线性回归(Linear Regression)

步骤三:Find the best function

要找到一个最好的function,就要让 https://chart.googleapis.com/chart?cht=tx&chl=-lnL(w%2C%20b) 最小化,也就是要最小化所有样本的Cross-entropy的总和。可以用梯度下降法(Gradient Descent),就是做对 https://chart.googleapis.com/chart?cht=tx&chl=w 的偏微分。

经过整理之後会得到 https://chart.googleapis.com/chart?cht=tx&chl=-lnL(w%2C%20b)%20%3D%20%5Cdisplaystyle%20%5Csum_n%20-(%5Chat%20y%20-%20f_%7Bw%2C%20b%7D(x%5En))%20x%5En_i。从括弧中的式子可以看出 https://chart.googleapis.com/chart?cht=tx&chl=w 的更新取决於Learning rate https://chart.googleapis.com/chart?cht=tx&chl=%5Cetahttps://chart.googleapis.com/chart?cht=tx&chl=x_i%5En 以及 https://chart.googleapis.com/chart?cht=tx&chl=%5Chat%20y%5En%20-%20f_%7Bw%2C%20b%7D(x%5En),这个式子代表function的输出跟理想目标的差距大小,如果离目标越远,则应该更新越多次参数。

逻辑回归(Logistic Regression) v.s. 线性回归(Linear Regression)

逻辑回归(Logistic Regression) + 方差(Square error)

将逻辑回归的式子的输出减掉 https://chart.googleapis.com/chart?cht=tx&chl=%5Chat%20y 的平方加总当作Loss function,一样用梯度下降法对 https://chart.googleapis.com/chart?cht=tx&chl=%5CSigma 後面的式子对 https://chart.googleapis.com/chart?cht=tx&chl=w 做偏微分,会得到如下图所示的结果。

会遇到一个问题就是,假设今天第 https://chart.googleapis.com/chart?cht=tx&chl=n 笔资料是 class 1 https://chart.googleapis.com/chart?cht=tx&chl=%5C%20%5Chat%20y%5En%20%3D%201,当 https://chart.googleapis.com/chart?cht=tx&chl=f_%7Bw%2C%20b%7D(x%5En)%20%3D%201 就代表你很接近你的目标,将它们的值代入式子算出来微分等於 0,而当 https://chart.googleapis.com/chart?cht=tx&chl=f_%7Bw%2C%20b%7D(x%5En)%20%3D%200 就代表你离你的目标还很远,将它们的值代入式子算出来微分也等於 0。

如果把参数的变化对Total Loss作图,选择Cross-entropy跟选择Square error的参数的变化跟loss的变化如下图所示。
从图中可以看到,以Cross-entropy来说距离目标越远,微分值就越大,参数更新越快,但是如果是Square error,当距离目标很远的时候,微分却非常小,就会导致不知道距离目标的远近。


参考资料

李宏毅老师 - ML Lecture 5


<<:  Day 12 | Dart 中的 Sound null safety

>>:  Day 10 情报收集 - Information Gathering (Maltego)

【Day 1】Startup x macOS setup x 一起来挖萝卜坑

tags: 铁人赛 macOS iTerm2 zsh powerlevel10k 概述 作者的碎念 ...

DAY 15- 《公钥密码》-ECC

高中听过有人念ㄙㄨㄟˊ 圆形,我当时真是害怕极了。 --- 椭圆曲线 (Elliptic curve...

【第十四天 - 堆叠型 SQL注入】

Q1. 什麽是 堆叠型 SQL注入? 堆叠型 SQL注入也称为 堆查询注入,英文为 stacked ...

时序图与 API 呼叫流程

时序图 (Sequence Diagram, Timeline Diagram),是 UML 底下的...