It refers to algorithms that learn X to Y or input to output mappings.The key characteristic of supervised learning is that you give your learning algorithm examples to learn from.That includes the right answers which means the correct label Y for a given input X,and is by seeing correct pairs of input X and desired output label Y that the learning algorithm eventually learns to take just the input alone without the output label and gives a reasonably accurate prediction or guess of the output.
它指的是学习X到Y或输入到输出映射的算法。监督学习的关键特点是你给你的学习算法提供了学习的例子。这包括正确的答案,即给定输入X的正确标签Y,并且通过看到正确的输入X和期望的输出标签Y对,学习算法最终学会只接受没有输出标签的输入,并给出对输出的合理准确的预测或猜测。
Example:
Input(X) | Output(Y) | Application |
---|---|---|
spam?(0/1) | spam filtering | |
audio | text transcripts | speech recognition |
English | Spanish | machine translation |
ad,user info | click?(0/1) | online advertising |
image,radar info | position of other cars | self-driving car |
image of phone | defect?(0/1) | visual inspection |
Regression 回归
Linear Regression
model:
f w , b ( x ) = w x + b f_{w,b}(x)=wx+b fw,b(x)=wx+b
parameters:
w , b w,b w,b
cost function:
J ( w , b ) = 1 2 m ∑ i = 1 m ( f w , b ( x ( i ) ) − y ( i ) ) 2 J(w,b)=\frac{1}{2m}\sum^{m}{i=1}{(f{w,b}(x^{(i)})-y^{(i)})^2} J(w,b)=2m1i=1∑m(fw,b(x(i))−y(i))2
goal:
m i n i m i z e w , b J ( w , b ) \underset {w,b}{minimize} J(w,b) w,bminimizeJ(w,b)
为了衡量w和b的选择与训练数据的拟合程度,有一个成本函数J,它来衡量模型预测与y的实际真实值之间的差异。
Gradient Descent(梯度下降)
本段内容由chat-GPT生成
梯度下降(Gradient Descent)是一种用于优化机器学习模型参数的迭代算法,广泛应用于各种监督学习和无监督学习任务中,尤其是在神经网络、回归分析等模型训练过程中。其目标是最小化损失函数,通过不断调整模型参数,使得预测误差尽可能小。
梯度下降算法的基本原理
梯度下降通过计算损失函数关于参数的梯度(即偏导数),然后按照梯度的反方向更新参数。 假设我们有一个损失函数 L ( θ ) ,其中 θ 是我们需要优化的模型参数,梯度下降的更新规则为: θ = θ − η ⋅ ∇ L ( θ ) θ :模型的参数。 η :学习率( L e a r n i n g R a t e ),控制每次更新步长的大小。 ∇ L ( θ ) :损失函数对参数 θ 的梯度(偏导数),表示在当前参数值下,损失函数最陡的上升方向。 梯度下降通过计算损失函数关于参数的梯度(即偏导数),然后按照梯度的反方向更新参数。\\ 假设我们有一个损失函数 L(\theta),其中\theta 是我们需要优化的模型参数,梯度下降的更新规则为:\\ \theta = \theta - \eta \cdot \nabla L(\theta)\\ \begin{align} \theta:模型的参数。\\ \eta:学习率(Learning Rate),控制每次更新步长的大小。\\ \nabla L(\theta):损失函数对参数 \theta 的梯度(偏导数),表示在当前参数值下,损失函数最陡的上升方向。 \end{align} 梯度下降通过计算损失函数关于参数的梯度(即偏导数),然后按照梯度的反方向更新参数。假设我们有一个损失函数L(θ),其中θ是我们需要优化的模型参数,梯度下降的更新规则为:θ=θ−η⋅∇L(θ)θ:模型的参数。η:学习率(LearningRate),控制每次更新步长的大小。∇L(θ):损失函数对参数θ的梯度(偏导数),表示在当前参数值下,损失函数最陡的上升方向。
学习率的选择
学习率是梯度下降算法中至关重要的超参数。过小的学习率会导致收敛速度过慢,而过大的学习率则可能导致参数更新幅度过大,甚至发散。常见的优化策略包括:
- 动态调整学习率:如使用学习率衰减、AdaGrad、Adam等方法。
- 学习率调度器:根据训练进度逐步减少学习率,以避免过早收敛。
优势和缺点
优点:
- 梯度下降是优化问题中最常见的算法,适用于许多机器学习任务。
- 相比于解析解法(如正规方程),梯度下降能够处理大规模数据集。
- 可以在许多不同类型的损失函数上应用(如平方误差、交叉熵等)。
缺点:
- 可能需要较长时间才能收敛到全局最优解,特别是在损失函数具有多个局部最小值时。
- 学习率选择不当可能导致算法不稳定或收敛过慢。
- 对数据的噪声比较敏感,尤其是随机梯度下降。
以线性回归的成本函数J(w,b)为例

同时更新的含义:
Correct: Simultaneous update
t m p w = w − α ∂ ∂ w J ( w , b ) t m p b = b − α ∂ ∂ b J ( w , b ) w = t m p w b = t m p b tmp_w=w-\alpha\frac{\partial}{\partial w}J(w,b)\\ tmp_b=b-\alpha\frac{\partial}{\partial b}J(w,b)\\ w=tmp_w\\ b=tmp_b tmpw=w−α∂w∂J(w,b)tmpb=b−α∂b∂J(w,b)w=tmpwb=tmpb
Incorrect
t m p w = w − α ∂ ∂ w J ( w , b ) w = t m p w t m p b = b − α ∂ ∂ b J ( w , b ) b = t m p b 这样写在更新 w 后计算 t m p b 公式里 w 的值就是更新后的 tmp_w=w-\alpha\frac{\partial}{\partial w}J(w,b)\\ w=tmp_w\\ tmp_b=b-\alpha\frac{\partial}{\partial b}J(w,b)\\ b=tmp_b\\ 这样写在更新w后计算tmp_b公式里w的值就是更新后的 tmpw=w−α∂w∂J(w,b)w=tmpwtmpb=b−α∂b∂J(w,b)b=tmpb这样写在更新w后计算tmpb公式里w的值就是更新后的
我们暂时将b设置为0,参数w为一个而不是两个,您可以查看成本函数J的二维图,而不是三维图

将微分的部分计算

"Batch" gradient descent(批量梯度下降)
"Batch": Each step of gradient descent uses all the training examples.
每次迭代时,使用整个训练集来计算梯度。这种方法虽然收敛速度慢,但每次更新都是基于全局信息的,通常能找到最优解。
更新规则:
θ = θ − η ⋅ 1 m ∑ i = 1 m ∇ L ( θ , x i , y i ) 其中, m 是训练样本的总数, ( x i , y i ) 是样本对。 \theta = \theta - \eta \cdot \frac{1}{m} \sum_{i=1}^m \nabla L(\theta, x_i, y_i)\\ 其中,m 是训练样本的总数,(x_i, y_i) 是样本对。 θ=θ−η⋅m1i=1∑m∇L(θ,xi,yi)其中,m是训练样本的总数,(xi,yi)是样本对。
多元线性回归
多个特征:

向量化表示:

多元线性回归的梯度下降
w和b的更新:

对比:
使用Feature Engineering(特征工程)和Polynomial Regression(多项式回归)为数据获得一个更好的模型
Classification 分类
Logistic Regression(逻辑回归)

Decision Boundary(决策边界)

squared error cost:

it's non-convex
Logistic loss function
if y(i)=1

if y(i)=0

上面的分段函数可以简写,同时我们也得出了简化版的cost function

使用这个函数背后是最大似然估计(maximum likelihood)的原理
Gradient descent for logistic regression

The Problem of Overfitting

解决方法
- Collect more data
- Select features
- 正则化(Regularization):Reduce size of parameters
Regularized linear regression

Implementing gradient descent

这解释了为什么正则化有缩小参数的效果