Day-03 Regression & Gradient Descent 介绍

  • 我们昨天聊过了到底如何做机器学习,且也知道机器学习的核心概念是取得所谓的最佳 function,回顾一下 Machine Learning 的期望,我们是希望利用答案 + 资料回推判断资料的规则
  • 那规则不会凭空产生,所以我们还是需要提供规则的底,也就是 function set 去让电脑判断哪个 function 才是所谓的最佳状况,因此昨天提过的运作的流程

  • 那 Regression(预测)是做什麽的,怎麽做?

Regression 可以做什麽

  • Regression(预测)可以做甚麽,
    • 你可以预测天气,f(今天天气资讯) = 明天天气状况
    • 你可以预测股票,f(今天股市资讯) = 明天股市走向
    • 等等等等
  • 那我们就直接拿一个例子来解释 Regression 的运作

天气预报

  • 我们假设我们已经可以用今天的天气资讯去推断明天的天气资讯了,然後我们给个目标,目标就是预测明天的降雨机率
  • 我们用 https://chart.googleapis.com/chart?cht=tx&chl=%24x%24 表示今天天气资讯,https://chart.googleapis.com/chart?cht=tx&chl=%24x_%5C%25%24 代表今天的降雨机率,https://chart.googleapis.com/chart?cht=tx&chl=%24y%24 则代表预测的降雨机率
  • 那我们就用这个例子来解释机器学习的三个步骤

Model

  • 我们假设明天的降雨机率 https://chart.googleapis.com/chart?cht=tx&chl=%24y%24 会等於某个常数向 https://chart.googleapis.com/chart?cht=tx&chl=%24b%24 加上某一个数值 https://chart.googleapis.com/chart?cht=tx&chl=%24w%24 乘上现在的降雨机率 https://chart.googleapis.com/chart?cht=tx&chl=%24x_%5C%25%24 ,会得到一个式子
    • https://chart.googleapis.com/chart?cht=tx&chl=%24y%20%3D%20b%20%2B%20w%20%5Ctimes%20x_%5C%25%24
  • 那在这里我们的 https://chart.googleapis.com/chart?cht=tx&chl=%24w%24https://chart.googleapis.com/chart?cht=tx&chl=%24b%24 是两个可变参数,且可以是任何数值,那我们其实就得到了一堆可能的 function
    • Ex:
      • https://chart.googleapis.com/chart?cht=tx&chl=%24f1%3A%20y%20%3D%2010.0%20%2B%209.0%20%5Ctimes%20x_%5C%25%24
      • https://chart.googleapis.com/chart?cht=tx&chl=%24f2%3A%20y%20%3D%203.0%20%2B%205.0%20%5Ctimes%20x_%5C%25%24
      • https://chart.googleapis.com/chart?cht=tx&chl=%24f3%3A%20y%20%3D%20-10.0%20-%209.0%20%5Ctimes%20x_%5C%25%24
      • ... infinite
  • 所以这样我们就得到我们的 function set,而现在我们就希望之後的步骤能够告诉我们这个 function set 中哪个 function 是最正确的
  • 那在这边说的 https://chart.googleapis.com/chart?cht=tx&chl=%24y%20%3D%20b%20%2B%20w%20%5Ctimes%20x_%5C%25%24 他是一个 Linear model,我们可以把 function set 写成 https://chart.googleapis.com/chart?cht=tx&chl=%24y%20%3D%20b%20%2B%20%5Csum%20w_ix_i%24 ,且其中的 https://chart.googleapis.com/chart?cht=tx&chl=%24x_i%24 是有关於 https://chart.googleapis.com/chart?cht=tx&chl=%24x%24 的各种属性(例如气象的湿度、风速等等),那我们也会称这些属性为 feature(特徵),那这里的 https://chart.googleapis.com/chart?cht=tx&chl=%24w_i%24 我们会称为 weight,https://chart.googleapis.com/chart?cht=tx&chl=%24b%24 称为 bias
    • 关於 weight and bias 会在後面文章解释,这边先当作两个参数就可以了

Goodness of Function

  • 我们现在希望找到这麽多的 function 的那个是最好的 function,因此概念上,我们必须穷举所有的可能性,也就是穷举 https://chart.googleapis.com/chart?cht=tx&chl=%24y%20%3D%20b%20%2B%20w%20%5Ctimes%20x_%5C%25%24 中的所有 https://chart.googleapis.com/chart?cht=tx&chl=%24b%24https://chart.googleapis.com/chart?cht=tx&chl=%24w%24,但这里有两个问题,
    • 第一,怎样的 function 叫做 "好" 的 function
    • 第二,你不知道到底要穷举到怎样的可能性,才会拿到真正好的 function
  • 那针对第一个问题,怎麽知道现在这麽 function 好不好,我们要检查,怎麽检查?利用一个叫做 loss function 的 function 来检查结果
    • Loss function L:
      • input: a function
      • output: how bad it is
    • https://chart.googleapis.com/chart?cht=tx&chl=%24L(f)%20%3D%20L(w%2C%20b)%24
    • Loss function 可以随自己喜好挑选,那我们这边就介绍一个常见的 Loss function(损失函数) Estimation error(估计误差),https://chart.googleapis.com/chart?cht=tx&chl=%24L(f)%20%3D%20L(w%2C%20b)%20%3D%20%5Csum%20(%5Chat%7By%7D%5En-(b%20%2B%20w%20%5Ctimes%20x%5En_%5C%25))%5E2%24
      • https://chart.googleapis.com/chart?cht=tx&chl=%24%5Chat%7By%7D%24 是原本的答案
      • https://chart.googleapis.com/chart?cht=tx&chl=%24(b%20%2B%20w%20%5Ctimes%20x%5En_%5C%25)%24 其实就是我们计算出来的答案,也就是 https://chart.googleapis.com/chart?cht=tx&chl=%24y%24

Best Function

  • 那我们已经有 Loss function 可以来判断 Function set 中每个 function 的误差状况了,我们要找最佳的 function 就是要找到误差最小的那个
  • 所以换句话说我们要找到 https://chart.googleapis.com/chart?cht=tx&chl=%24f%5E*%20%3D%20arg%5C%20%5Cmin%5Climits_%7Bf%7D%20L(f)%24,也就是要找到参数 https://chart.googleapis.com/chart?cht=tx&chl=%24w%5E*%2C%20b%5E*%20%3D%20arg%5C%20%5Cmin%5Climits_%7Bw%2C%20b%7D%20L(w%2C%20b)%20%3D%20arg%5C%20%5Cmin%5Climits_%7Bw%2C%20b%7D%20%5Csum%20(%5Chat%7By%7D%5En-(b%20%2B%20w%20%5Ctimes%20x%5En_%5C%25))%5E2%24
  • 那这边我们已经知道怎麽找到最佳参数了,收工吧~~

    等等等等,你是不是少讲了一个东西,我们知道怎麽找最佳解了,但是我们不可能穷举阿

  • ㄚㄚㄚ,这边我们来说说到底该如何找到最佳解吧~

如何找最佳解?

  • 我们刚刚提到了,如果我们的目标现在的目标就是利用 https://chart.googleapis.com/chart?cht=tx&chl=%24b%2C%20w%24 这两个参数来找到所谓的最佳函数,但是我们又不可能穷举所有的 https://chart.googleapis.com/chart?cht=tx&chl=%24b%2C%20w%24 参数,那怎麽办
  • 概念上我们会先给一个初始的 https://chart.googleapis.com/chart?cht=tx&chl=%24b%2C%20w%24 ,然後利用其中一种算法来帮忙推断更好的 https://chart.googleapis.com/chart?cht=tx&chl=%24b%2C%20w%24 数字,来达到更新参数去找寻最佳参数的过程,这就是找最佳解的过程
  • 而在机器学习中常见找数字的方式就是梯度下降法

Gradienet Descent(梯度下降法)

  • Gradienet Descent 是一个最佳化理论里面的一个一阶找最佳解的一种方法。
  • 首先 Gradiennet Descent 的概念是希望利用梯度下降法找到函数的局部最小值,因为梯度的方向是走向局部最大的方向,所以在梯度下降法中是往梯度的反方向走。
  • 那这里要注意一个前提,就是要算一个 函数 https://chart.googleapis.com/chart?cht=tx&chl=%24f(x)%24 的梯度,这个函数要是任意可微分函数
  • 操作步骤,我们用 https://chart.googleapis.com/chart?cht=tx&chl=%24w%5E*%20%3D%20arg%20%5Cmin%5Climits_%7Bw%7D%20L(w)%24 来介绍:
    • 先随机挑选一个初始参数 https://chart.googleapis.com/chart?cht=tx&chl=%24w%5E0%24
    • 计算其微分 https://chart.googleapis.com/chart?cht=tx&chl=%24%7BdL%20%5Cover%20dw%7D%20%7C_%7Bw%20%3D%20w%5E0%7D%24
    • 取得下一个参数 https://chart.googleapis.com/chart?cht=tx&chl=%24w%5E1%20%5Cleftarrow%20w%5E0%20-%20%5Ceta%20%7BdL%20%5Cover%20dw%7D%7C_%7Bw%20%3D%20w%5E0%7D%24
      • https://chart.googleapis.com/chart?cht=tx&chl=%24%5Ceta%24 is called "learning rate"
    • 重复此步骤直到找到相对最佳解
  • 所以如果是两个参数? https://chart.googleapis.com/chart?cht=tx&chl=%24w%5E*%2C%20b%5E*%20%3D%20arg%20%5Cmin%5Climits_%7Bw%2C%20b%7D%20L(w%2C%20b)%24
    • 先随机挑选一个初始参数 https://chart.googleapis.com/chart?cht=tx&chl=%24w%5E0%2C%20b%5E0%24
    • 计算其微分 https://chart.googleapis.com/chart?cht=tx&chl=%24%7B%5Cpartial%20L%20%5Cover%20%5Cpartial%20w%7D%20%7C_%7Bw%20%3D%20w%5E0%2C%20b%3Db%5E0%7D%2C%5C%20%7B%5Cpartial%20L%20%5Cover%20%5Cpartial%20b%7D%20%7C_%7Bw%20%3D%20w%5E0%2C%20b%3Db%5E0%7D%24
    • 取得下一个参数 https://chart.googleapis.com/chart?cht=tx&chl=%24w%5E1%20%5Cleftarrow%20w%5E0%20-%20%5Ceta%20%7B%5Cpartial%20L%20%5Cover%20%5Cpartial%20w%7D%20%7C_%7Bw%20%3D%20w%5E0%2C%20b%3Db%5E0%7D%2C%5C%20b%5E1%20%5Cleftarrow%20b%5E0%20-%20%5Ceta%20%7B%5Cpartial%20L%20%5Cover%20%5Cpartial%20b%7D%20%7C_%7Bw%20%3D%20w%5E0%2C%20b%3Db%5E0%7D%24
    • ...
  • 所以 gradient 是甚麽? 就是偏微分那边的值串在一起变成一个 vector 之後的这个 vector 就叫做 gradient

  • 所以我们到这里可以知道,我们可以利用 Gradient Descent 达到取得下一个新的 function 的作法,也补齐了机器学习的三个步骤了

每日小节

  • 机器学习的概念就是利用 Model 和 Data 来选出最佳的 Function,因此 Regression 也不例外
  • 那如何选取最好的 Model ,就需要一个判断现在 function 好不好的东西,而这个工具就是 loss function,利用 loss function 来判断这个现在选择的这个 function 有多差
  • 那我们知道一个 function 有多差之後,我们当然希望找到更好的 function,找更好的 function 参数这边,就要利用找最佳解的方法,而我们常见的用法就是利用 gradient descent 来更新参数
  • 有了 Model(First function) + Loss function(Check Error) + Gradient Descent(Update Parameter) 就完成了整个机器学习的架构了,也达到了 Regression 的 Training 了

  • 那我们已经知道 Regression 的概念了,明天让我们来看看如果实作的话,我们要如何去实作

<<:  [Tableau Public] day 18:试着分析appstore资料集-3

>>:  初学者跪着学JavaScript Day3 : 变数Variable、宣吿var

Day 26 测试 React 元件:使用 React Testing Library 体验 Test Driven Development (TDD) - 6

前面几天我们已经用 TDD 的方式完成了 <Editor /> 元件,但不要忘了 TDD...

New Real and Authentic Cisco 350-701 Exam Dumps

If you are expecting to clear the Implementing and...

[番外篇]Go Generate & Makefile(Day25)

文章同步更新於https://kevinyay945.com/smart-home-tutorial...

iOS工程师面试深入浅出(OC)- NSInterger 与 Int 有什麽不一样?

iOS工程师面试深入浅出(Objective-C)- NSInterger 与 Int 有什麽不一样...

【Day 03】第一个小程序

今天,就让我们写一个小程序,向 C 语言这个世界打招呼吧! #include<stdio.h&...