【Day 5】机器学习基本功(三) --- Regression

如何找到一个函式(function)?(下)

步骤一:写出一个带有未知参数的函式

昨天举的例子 https://chart.googleapis.com/chart?cht=tx&chl=y%20%3D%20b%20%2B%20wx_1 是一个线性模型(linear model),随着 https://chart.googleapis.com/chart?cht=tx&chl=x_1 越大,https://chart.googleapis.com/chart?cht=tx&chl=y 就越大,可以透过设定不同的 https://chart.googleapis.com/chart?cht=tx&chl=w 来改变斜率,或是设定不同的 https://chart.googleapis.com/chart?cht=tx&chl=b 来改变直线与 y 轴的交叉点,但它永远会是一条直线。

然而实际情况可能会是红色曲线这样子,但不管怎麽设定 https://chart.googleapis.com/chart?cht=tx&chl=w%2C%20b 也没办法变成红色那条曲线。由此可知线性模型会有很大的限制(Model Bias),因此如要产生红色曲线我们就需要更复杂的模型。

红色曲线可以看成是一个常数加上很多个蓝色的函式,从下图中可以看到红色的曲线可以由 0(常数项), 1, 2, 3 这四个函数所组成。而这个红色曲线是由很多线段所组成的,我们称之为分段线性曲线(Piecewise Linear Curves)

那刚刚提到的蓝色曲线要如何表示呢?
我们可以使用 S型函数(Sigmoid function) 去逼近它,其式子写成 https://chart.googleapis.com/chart?cht=tx&chl=y%20%3D%20c%20%5Cfrac%201%20%7B1%2Be%5E%7B-(b%2Bwx_1)%7D%7D%20%3D%20c%20%5Ctimes%20sigmoid(b%2Bwx_1),当 https://chart.googleapis.com/chart?cht=tx&chl=x_1 趋近於无穷大,https://chart.googleapis.com/chart?cht=tx&chl=e%5E%7B-%20%5Cinfty%7D%20%3D%200https://chart.googleapis.com/chart?cht=tx&chl=y 就会收敛在 https://chart.googleapis.com/chart?cht=tx&chl=c ,而当 https://chart.googleapis.com/chart?cht=tx&chl=x_1 趋近於无穷小,分母会趋近於无穷大,即 https://chart.googleapis.com/chart?cht=tx&chl=y 会趋近於 0。这些蓝色曲线我们称之为 Hard Sigmoid。

调整 https://chart.googleapis.com/chart?cht=tx&chl=w 即会改变斜率,调整 https://chart.googleapis.com/chart?cht=tx&chl=b 就能将 Sigmoid funtion 左右移动,调整 https://chart.googleapis.com/chart?cht=tx&chl=c 则会改变高度。

藉由调整不同的参数来产生各种Sigmoid function,再透过这些Sigmoid function产生各种不同的 Piecewise linear function,最後甚至能够逼近各种连续函数。

根据上述方法我们就可以将原本的 https://chart.googleapis.com/chart?cht=tx&chl=y%20%3D%20b%20%2B%20wx_1 改成 https://chart.googleapis.com/chart?cht=tx&chl=y%20%3D%20b%20%2B%20%5Cdisplaystyle%20%5Csum_i%20c_i%20%5Ctimes%20sigmoid(b_i%20%2B%20w_ix_i)

我们将上述的数学式子透过图形来表示,每个图形都代表着一矩阵或是数值,而我们将这些未知的参数拉直,拼成一个很长的向量 https://chart.googleapis.com/chart?cht=tx&chl=%5Ctheta,即为所有未知的参数。

步骤二:从训练资料定义损失函数(Loss)

  • 损失函数:https://chart.googleapis.com/chart?cht=tx&chl=L(%5Ctheta)

计算Loss的方法跟昨天教的方法一样,首先设定一组 https://chart.googleapis.com/chart?cht=tx&chl=b%2C%20%5Cbf%20c%5ET%2C%20https://chart.googleapis.com/chart?cht=tx&chl=%5Cbf%20b%2C%20W 的值,接着把feature https://chart.googleapis.com/chart?cht=tx&chl=%5Cbf%20x 代入,得出预测的 https://chart.googleapis.com/chart?cht=tx&chl=y,再去计算预测的值与真实的值的差距 https://chart.googleapis.com/chart?cht=tx&chl=e,最後加总误差。

步骤三:解最佳化问题

  • 找一组能让Loss最小的 https://chart.googleapis.com/chart?cht=tx&chl=%5Ctheta%5E*

方法也跟昨天教的一样,使用Gradient descent,首先随机选一个初始数值 https://chart.googleapis.com/chart?cht=tx&chl=%5Ctheta%5E0,接着去计算每一个参数对Loss的微分,会得到一个向量 https://chart.googleapis.com/chart?cht=tx&chl=g,即为Gradient,我们可以用 https://chart.googleapis.com/chart?cht=tx&chl=%5Cnabla 来表示,将式子写成 https://chart.googleapis.com/chart?cht=tx&chl=g%20%3D%20%5Cnabla%20L(%5Ctheta%5E0),最後去重复更新所有的参数。

而实际上在做Gradient descent的时候,我们会将我们有的 https://chart.googleapis.com/chart?cht=tx&chl=N 笔资料随意分成好几个Batch https://chart.googleapis.com/chart?cht=tx&chl=B,并对其中一个Batch计算它的Loss https://chart.googleapis.com/chart?cht=tx&chl=L%5E1,并使用 https://chart.googleapis.com/chart?cht=tx&chl=L%5E1 来计算Gradient并更新参数,接着再取下一个Batch去计算Loss,以此类推。
每次更新一次参数,我们叫做一次Update,而将所有Batch看过一次,我们称之为一次Epoch

Sigmoid -> ReLU

Hard sigmoid也可以看成是两个 Rectified Linear Unit(ReLU) 的加总,其式子写成 https://chart.googleapis.com/chart?cht=tx&chl=c%20%5Ctimes%20max(0%2C%20b%20%2B%20wx_1)

将原本Sigmoid换成ReLU,而这两种function我们称之为Activation function。


参考资料

李宏毅老师 - 机器学习2021


<<:  [Day18]基本款网格交易

>>:  #4 Array & Object in JavaScript

[ Day 5 ] - 物件

物件 物件的格式 会使用 { } 前後包住资料集 内容会有属性和值 let shop = { Nam...

Day-9 重现於新电视的音速索尼克始祖、SEGA 的 MEGA DRIVE

我想就算没玩过游戏、应该也多少听过去年上映的音速小子电影、这是部由 SEGA 游戏音速小子(现译为索...

Day_05 opkg套件管理

在往下继续讲其他网路架构之前,想先来介绍OpenWrt的套件管理系统。常见的Linux发行版几乎都会...

Day24,试着用rancher交差Dashboard

正文 今天要来一日体验rancher server上的dashboard功能 使用racher2.6...

【day29】修改ProfileFragment X (第三方套件)ImagePicker

好的,我们会发现,有时候我们传送的照片档案太大,以及有可能我们拍完美美的照片时,却发现我们竟然不在...