Linear Regression and Gradient Descent Optimization
Base(想跳可以跳
How does a model learn?
Supervised learning (监督学习):
Given examples of data, learn a model that can estimate the value of label variables from feature variables.- Unsupervised learning (非监督学习):
The given data is not labeled. And the model searches for patterns, but cannot assign meanings.
Goal for Supervised Learning:
- Classification: predict a discrete label.
- Regression: predict a ordered quantitative value.
=> So to optimize the model, we should solve 2 problems:Structure/Class?- Parameters ?
Funcion Classes
- Linear:
- Polynomial:
- Exponential:
- Logarithmic:
- Logistic:
-
How to pick
Class?- Search the function space (搜索函数空间):
逐一搜索每种函数形式以找到最优的模型【但这在计算上是不可行的.. - Domain knowledge (领域知识):
比如经济学可能时对数,物理问题可能是指数函数..【但这太limited - Visual inspection of data (数据可视化检查):
【高维数据不直观 - Occam's Razor (奥卡姆剃刀原则): 在所有可能的模型中,选择最简单的那个,最小化模型复杂度 -> 减少过拟合风险【但太简单可能不足以捕捉数据的复杂关系
- Search the function space (搜索函数空间):
-
How to pick
Parameters?最小化预测值
y_pred和真实值y的误差。常用的目标函数(Objective Function):- SAE (Sum of Absolute Errors)(绝对误差和):
- MSE (Mean Squared Error)(均方误差):
-
一、Gradient Descent
For a linear univariate regression model:
The MSE loss function:
Do partial derivatives for m / b independently:
Then update the parameters (in which is the learning rate):
Dynamic
Some options:
- ↓ over time:
- Increase while improving, decrease with error:
- Search for the best :
Stochastic Gradient Descent (SGD)
Too many samples?
Use one or a subset of the data to calculate the gradient:
Overfitting
可能原因:
- Too many parameters.(recording noise)
- Too less data.
- Lack of
regularization.
Regularization
Cross Validation
Randomly divide the data into:
- Training set: Used to train the model.
- Validation set: Used to test whether the model can correctly predict the data, during the training process.
So that we can adjust the model parameters to minimize the error on the validation set during training.
But this cannot prevent overfitting. So we need Regularization.
L1 Regularization (Lasso)
In which:
- is the new objective function.
- is the regularization parameter, to control the importance of the regularization term.
为了减少过拟合,也就是为了让 尽可能小,我们需要同时减小 和 。
而MSE的减少是通过前面的梯度下降来实现的,所以我们只需要考虑如何减小 。
这就是L1(Lasso)正则化的Sparsity特性:
- 通过调整 的大小,可以将一些不重要的特征的权重变为0,从而减少特征的数量,提高模型的泛化能力。
HW3
作业中有提到LASSO在淘汰参数时的的参考:
- F-Statisitc: 越大越显著
- P-Value: 越小越显著
- 方差:大不一定说明贡献大,但是
小的话->取值范围窄->贡献有限 【3不一定准确,还是以其他判断方式为准】
L2 Regularization (Ridge)
相比于L1, L2正则化的特性为Smoothness:
- 鼓励参数的权重尽可能小,但不会变为0。
二、Closed Form Solution(闭式解)
1. Ordinary Least Squares (OLS)
梯度下降的优化思路是:
为了优化 MSE --> 对MSE的参数求偏导 --> 重复直到收敛。
而闭式解:
为了优化 MSE =
--> 简单来说就是要优化残差平方和 RSS(Residual Sum of Squares) =
--> 对于给定的数据集:
- X 为输入特征矩阵,包含m个样本,每个样本有n个特征 ()
- y 为输出目标变量 ()
- 为特征权重向量 ()
- 为误差项
线性回归模型可以表示为:
--> 于是RSS可以表示为:
(), 而 (),所以是一个标量。标量的转置等于自身,所以:
得:
--> 为了minimize RSS,对求导:
而:

正定矩阵什么的以后再说吧,总之就是这样喵..
所以令导数为0,得:
然鹅,目前的问题是:
- 可能
不可逆 - 过拟合
2. Ridge Regression
原理差不多,只是在RSS后面加上了L2正则化项。就不过多推导了:
在OLS的基础上,加上了,in which 是单位(对角)矩阵
是正定矩阵,所以一定是可逆的。
Why we still use Gradient Descent?
计算量太大:
Closed Form Solution 光计算一个 就需要 的时间复杂度,并且,还要求矩阵可逆(虽然已经在Ridge中解决了这个问题)。
反观GD,作为一个迭代的方法:
- 适用于大数据集 - 不需要存储整个数据矩阵
- 延展性 - 机器学习哪都能用,可不止regression
Evaluation
-
Objective Function: e.g. MSE
-
Variance Explained, aka :
所以呢, 也可以被称为
VE(Variance Explained):其中,,, 是y的均值。
越接近1,说明模型越好。