机器学习笔记_02单变量线性回归

二、单变量线性回归(Linear Regression with One Variable)

2.1 模型表示

notation(符号):

  • m = Number of trainging examples
  • x's = "input" variable / features (输入变量或者特征)
  • y's = "output" variable / "target" variable (输出变量或者目标变量)
  • (x,y) = one training example (一个训练样本)
  • (\(x^i\), \(y^i\)) = \(i^{th}\) training example (第i个训练样本)
  • h = hypothesis (假设)

单变量性线回归:

\[h_\theta(x) = \theta_0 + \theta_1 x \]

st=>start: Training Set
op=>operation: Learning Algorithm
op1=>end: hypothesis(假设)
x=>inputoutput: sizeof house
y=>inputoutput: estimated price

st->op->op1
input->hypothesis:sizeof house
hypothesis-->output:estimated price

2.2 cost function

我们选择的参数决定了我们得到的直线相对于我们的训练集的准确度,模型所预测的值与训练集中实际值之间的差距就是建模误差(modeling error)。

2.3 cost function - intuition 1

Hypothesis(假设函数):

\[h_{\theta}(x^i)=\theta_0 + \theta_1x \]

Parameters :

\[\theta_0, \theta_1 \]

Cost Function(代价函数) :

\[J(\theta_0, \theta_1)=\frac1{2m}\sum_{i=1}^m(h_{\theta}(x^i) - y ^i)^2 \]

Goal :

\[minimize_{\theta_0, \theta_1}J(\theta_0, \theta_1) \]

2.4 cost function - intuition 2

2.5 Gradient Descent

梯度下降是一个用来求函数最小值的算法。

梯度下降背后的思想是:开始时我们随机选择一个参数的组合(θ0,θ1,...,θn),计算代价 函数,然后我们寻找下一个能让代价函数值下降最多的参数组合。我们持续这么做直到到到 一个局部最小值(local minimum),因为我们并没有尝试完所有的参数组合,所以不能确定 我们得到的局部最小值是否便是全局最小值(global minimum),选择不同的初始参数组合, 可能会找到不同的局部最小值。


批量梯度下降(batch gradient descent)算法的公式:

repeat until convergence {

\[\theta_j := \theta_j - \alpha \frac\partial{\partial\theta_j}J(\theta_0, \theta_1) \]

for j = 0 and j = 1
}
\(\alpha\) 是学习率 learning rate,它决定了我们沿着能让代价函数下降程度最大的方向向下迈出的步子有多大,在批量梯度下降中,我们每一次都同时让所有的参数减去学习速率乘以代价函数的导数。


Gradient descent algorithm梯度下降算法 :

\[\theta_j := \theta_j - \alpha \frac{\partial J(\theta_0,\theta_1)}{\partial{\theta_{j}}} \]

说明:
\(\alpha\) : learning rate
\(:=\) : assignment
Correct : Simultaneous update (同步更新是正确的)

\[temp0 := \theta_0 - \alpha \frac{\partial }{\partial \theta_0}J(\theta_0, \theta_1) \]

\[temp1 := \theta_1 - \alpha \frac{\partial }{\partial \theta_1}J(\theta_0, \theta_1) \]

\[\theta_0 := temp0 \]

\[\theta_1 := temp1 \]

2.6 gradient Descent intuition

2.7 gradient Descent for linear regression

Gradient descent algorithm :

repeat until convergence {

\[\theta_j := \theta_j - \alpha \frac\partial{\partial\theta_j}J(\theta_0, \theta_1) \]

for j = 1 and j = 0
}


Linear Regression Model :

\[h_{\theta}(x) = \theta_0 + \theta_1 x \]

\[J(\theta_0, \theta_1)=\frac{1}{2m}\sum^m_{i=1}(h_{\theta}(x^i) - y^i)^2 \]

对我们之前的线性回归问题运用梯度下降法,关键在于求出代价函数的导数,即:

\[J(\theta_0, \theta_1)=\frac{1}{2m}\sum^m_{i=1}(\theta_0 + \theta_1x^i - y^i)^2 \]

\(j = 0 :\frac{\partial}{\partial {\theta_0}}J(\theta_0, \theta_1) = \frac{1}{m}\sum^m_{i=1}(h_{\theta}(x^i) - y^i)\)
\(j = 1 : \frac{\partial}{\partial {\theta_1}}J(\theta_0, \theta_1) = \frac{1}{m}\sum^m_{i=1}(h_{\theta}(x^i) - y^i)x^i\)

则算法改写成:
Repeat {

\[\theta_0 := \theta_0 - \alpha \frac 1 m \sum^m_{i=1}(h_\theta(x^i) - y^i) \]

\[\theta_1 := \theta_1 - \alpha \frac 1 m \sum^m_{i=1}(h_\theta(x^i) - y^i)x^i \]

}

Batch Graddient Descent 批量梯度下降 :
"Batch" : each step of gradient descent uses all the training examples.

2.8 接下来的内容

线性代数回顾

posted on 2016-04-14 10:25  cv_ml_张欣男  阅读(508)  评论(0编辑  收藏  举报

导航