【Day 4】机器学习基本功(二) --- Regression

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

接下来会以李宏毅老师在影片中讲的例子来做说明整理。
寻找一个输入为youtube後台资讯,输出为该频道隔天总点阅率的函式。

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

  • 初步猜测函数的数学式为 https://chart.googleapis.com/chart?cht=tx&chl=y%20%3D%20b%20%2B%20w%20x%20_1
    https://chart.googleapis.com/chart?cht=tx&chl=y : 今天频道预测的总观看人数
    https://chart.googleapis.com/chart?cht=tx&chl=x_1 : 昨天频道总观看人数(已知)
    https://chart.googleapis.com/chart?cht=tx&chl=b%2C%20w : 未知参数

  • Model: https://chart.googleapis.com/chart?cht=tx&chl=y%20%3D%20b%20%2B%20w%20x%20_1
    Feature: https://chart.googleapis.com/chart?cht=tx&chl=x_1
    Weight: https://chart.googleapis.com/chart?cht=tx&chl=w
    Bias: https://chart.googleapis.com/chart?cht=tx&chl=b

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

  • 损失函数:https://chart.googleapis.com/chart?cht=tx&chl=L(b%2C%20w),损失函数的输入为Model里面的参数,即为 https://chart.googleapis.com/chart?cht=tx&chl=b%2C%20w。输出为 https://chart.googleapis.com/chart?cht=tx&chl=b%2C%20w 数值的好坏,必须从训练资料进行计算。

假设 https://chart.googleapis.com/chart?cht=tx&chl=b%20%3D%200.5k%2C%20w%20%3D%201,并将训练资料的点阅人数带入Model中,得出隔天预测的点阅人数 https://chart.googleapis.com/chart?cht=tx&chl=y,并与隔天实际点阅人数 https://chart.googleapis.com/chart?cht=tx&chl=%5Chat%20y 相减得出https://chart.googleapis.com/chart?cht=tx&chl=ehttps://chart.googleapis.com/chart?cht=tx&chl=e 为估测值与真实值的差距,再将所有误差加总,得到损失函数 https://chart.googleapis.com/chart?cht=tx&chl=L%20%3D%20%5Cfrac%201%20N%20%5Cdisplaystyle%20%5Csum_n%20e_nhttps://chart.googleapis.com/chart?cht=tx&chl=L 越大,代表这组参数 https://chart.googleapis.com/chart?cht=tx&chl=b%2C%20w 越不好,反之则越好。


  • 损失函数选择:
    Mean absolute error(MAE): https://chart.googleapis.com/chart?cht=tx&chl=e%20%3D%20%7Cy%20-%20%5Chat%20y%7C
    Mean square error(MSE): https://chart.googleapis.com/chart?cht=tx&chl=e%20%3D%20(y%20-%20%5Chat%20y)%5E2
    如果 https://chart.googleapis.com/chart?cht=tx&chl=y%2C%20%5Chat%20y 都为机率分布,则会选择交叉熵(Cross-Entropy)。

步骤三:解最佳化问题

  • 找一组能让Loss最小的参数 https://chart.googleapis.com/chart?cht=tx&chl=w%5E*%2C%20b%5E*

梯度下降法(Gradient Descent)

假设只有 https://chart.googleapis.com/chart?cht=tx&chl=w 这个参数,代不同的 https://chart.googleapis.com/chart?cht=tx&chl=w 进去会得到不同的Loss,产生图中曲线(error surface)。

  • 随机选取初始点 https://chart.googleapis.com/chart?cht=tx&chl=w%5E0

  • 计算参数对Loss的微分,即为切线斜率

  • 斜率为负:增加 https://chart.googleapis.com/chart?cht=tx&chl=w 的值

  • 斜率为正:减少 https://chart.googleapis.com/chart?cht=tx&chl=w 的值

  • 参数调整范围大小取决於

    • 斜率大,调整范围大。斜率小,调整范围小。
    • 学习率(Learning rate) https://chart.googleapis.com/chart?cht=tx&chl=%5Ceta:越大,参数更新快。越小,参数更新慢。
  • 超参数(hyperparameters):需自行设定、调整。

  • 反覆进行前面的操作,即会找到解

需要注意的是梯度下降法有时会没有办法找到全域最佳解(global minima),可能会找到区域最佳解(local minima),根据初始点的不同有机率会找到不同的解。

重新回到考虑两个参数的情况,跟上述一样会先随机选取初始点,再个别对Loss进行微分,并更新参数。


参考资料

李宏毅老师 - 机器学习2021


<<:  Day02 - this&Object Prototypes Ch3 Objects - Contents - Array

>>:  Day5-如何超越Google

Day 19:深度优先搜寻(DFS)与拓朴排序(topological sorting)

深度优先搜寻(depth-first search, DFS)是一种搜寻整张图所有节点的演算法。它的...

Day-11 LinearLayout

何谓布局?布局就是版面配置,而就如同制作公告栏一样,有着不同的排版方式。 接下来我会对LinearL...

[Day13] 学 Reactstrap 就离 React 更近了 ~ Navbar ‧ 初识篇

前言 今天说好要来介绍 Navbar, 就是导览列,所以也是前端经常使用到的 Component。 ...

虹语岚访仲夏夜-0(悲伤的blue)

台北,又变了呢,记得很久前,才在盖的101到现在,烟火都不知道放了几遍,他一定没有在看那些,如果有机...

[Angular] Day22. Common Routing Tasks(二)

在上一篇中介绍了如何透过 router 传递参数,也介绍了怎麽建立嵌套的 route 等等,在本篇中...