简要声明
- 课程学习相关网址
- 由于课程学习内容为英文,文本会采用英文进行内容记录,采用中文进行简要解释。
- 本学习笔记单纯是为了能对学到的内容有更深入的理解,如果有错误的地方,恳请包容和指正。
- 非常感谢Andrew Ng吴恩达教授的无私奉献!!!
文章目录
专有名词
Stochastic gradient descent | 随机梯度下降法 | Batch gradient descent | 批量梯度下降 |
---|---|---|---|
Mini‐batch gradient descent | 小批量梯度下降 | Online Learning | 在线学习 |
click-through rate(CTR) | 点击率 | Map‐reduce | 映射减少 |
Learning with large datasets
Machine learning and data
- “It’s not who has the best algorithm that wins. It’s who has the most data.” →在机器学习中,通常情况下决定因素往往不是更好地算法而是谁的训练数据最多
- 当我们把精力花在用一亿个样本训练模型之前,考虑为什么不只用一千个样本来训练算法
- 使用一个非常小的训练集的完整性检查方法结果可能也是一样的 →绘制学习曲线
Stochastic gradient descent
- 当m很大的时候,梯度下降求和的计算量会变得非常大 →需要对m个样本进行求和
- Batch gradient descent 批量梯度下降 →遍历所有数据计算和值再不断迭代→收敛需要花费很长时间
Stochastic gradient descent
c o s t ( θ , ( x ( i ) , y ( i ) ) = 1 2 ( h θ ( x ( i ) ) − y ( i ) ) 2 cost(\theta,(x^{(i)},y^{(i)})=\frac{1}{2}(h_{\theta}(x^{(i)})-y^{(i)})^2 cost(θ,(x(i),y(i))=21(hθ(x(i))−y(i))2
J t r a i n ( θ ) = 1 m ∑ i = 1 m c o s t ( θ , ( x ( i ) , y ( i ) ) J_{train}(\theta)=\frac{1}{m}\sum_{i=1}^m cost(\theta,(x^{(i)},y^{(i)}) Jtrain(θ)=m1i=1∑mcost(θ,(x(i),y(i))
- 随机打乱所有数据 →将所有m个训练样本重新随机排列
- 只对一个训练样本的代价函数进行梯度下降操作 →只关注一个训练样本,将参数稍微修改一下
- 不管数据是否已经随机排列过,随机打乱所有数据在收敛时能够更快一点
- 随机梯度下降与批量梯度下降不同在于随机梯度下降不需要对全部m个样本求和来得到梯度项,只需要对单个训练样本求出这个梯度项
- 随机梯度下降算法的关键是关于某一个单独的训练样本(x(i), y(i))来对参数进行更新
- 用随机梯度下降法能得到一个很接近全局最小值的参数
- 当m非常大时,内层循环通常做1到10次就可以收敛
Repeat {
for i = 1, … , m
{
θ j : = θ j − α ( h θ ( x ( i ) ) − y ( i ) ) ⋅ x j ( i ) ( f o r j = 0 , ⋯ , n ) \theta_j:=\theta_j-\alpha(h_{\theta}(x^{(i)})-y^{(i)})\cdot x_j^{(i)}\qquad (for\ j=0,\cdots,n) θj:=θj−α(hθ(x(i))−y(i))⋅xj(i)(for j=0,⋯,n)
θ j : = θ j − α ( ∂ ∂ θ j c o s t ( θ , ( x ( i ) , y ( i ) ) ) ⋅ x j ( i ) ( f o r j = 0 , ⋯ , n ) \theta_j:=\theta_j-\alpha(\frac{\partial}{\partial\theta_j}cost(\theta,(x^{(i)},y^{(i)}))\cdot x_j^{(i)}\qquad (for\ j=0,\cdots,n) θj:=θj−α(∂θj∂cost(θ,(x(i),y(i)))⋅xj(i)(for j=0,⋯,n)
}
}
Mini‐batch gradient descent
- Batch gradient descent: Use all m examples in each iteration
- Stochastic gradient descent: Use 1 examples in each iteration
- Mini-batch gradient descent: Use b examples in each iteration
- b = mini-batch size →小批量大小,通常选择b=10(2~100)
- 通过使用合适的向量化方式可以提高Mini-batch梯度下降的计算速度
- Mini-batch梯度下降算法的缺点之一是当有一个额外的参数b时,需要确定Mini-batch的大小
设b=10,m=1000
Repeat {
for i = 1, 11, 21, 31, … , 991 {
{
θ j : = θ j − α 1 10 ∑ k = i i + 9 ( h θ ( x ( k ) ) − y ( k ) ) ⋅ x j ( k ) ( f o r j = 0 , ⋯ , n ) \theta_j:=\theta_j-\alpha\frac{1}{10}\sum_{k=i}^{i+9}(h_{\theta}(x^{(k)})-y^{(k)})\cdot x_j^{(k)}\qquad (for\ j=0,\cdots,n) θj:=θj−α101k=i∑i+9(hθ(x(k))−y(k))⋅xj(k)(for j=0,⋯,n)
}
}
Gradient descent convergence
Checking for convergence
-
Batch gradient descent 批量梯度下降:绘制优化代价函数,确保每一次迭代都是下降的
-
Stochastic gradient descent 随机梯度下降:
c o s t ( θ , ( x ( i ) , y ( i ) ) = 1 2 ( h θ ( x ( i ) ) − y ( i ) ) 2 cost(\theta,(x^{(i)},y^{(i)})=\frac{1}{2}(h_{\theta}(x^{(i)})-y^{(i)})^2 cost(θ,(x(i),y(i))=21(hθ(x(i))−y(i))2
当随机梯度下降法进行学习时,在用(x(i), y(i))更新参数θ之前,使用这个样本计算对应的cost函数 →每1000次迭代,画出所计算出的前1000个样本的cost函数的平均值
-
随机梯度下降算法不是直接收敛到全局最小值,而是在一个范围内反复震荡,最后逐渐接近全局最小值
Learning rate
-
Learning rate α is typically held constant →学习速率α一般是保持不变的常数
-
Can slowly decrease α over time if we want θ to converge →如果想让随机梯度下降更好地收敛到全局最小值,需要学习速率α随时间变化逐渐减小
α = c o n s t 1 i n t e r a t i o n N u m b e r + c o n s t 2 \alpha=\frac{const1}{interationNumber+const2} α=interationNumber+const2const1
-
随着算法的运行,迭代次数回越来越大,学习速率α会慢慢变小
Online Learning
Repeat forever {
Get (x, y) corresponding to user →一个用户访问网站就得到与其对应的 (x, y) →特征x包括客户所指定的起始地和目的地以及提供给客户的价格,y取1或0取决于客户是否选择使用服务
Update θ using (x, y) →用 (x, y) 数据来更新参数 θ
θ j : = θ j − α ( h θ ( x ) − y ) ⋅ x j ( j = 0 , ⋯ , n ) \theta_j:=\theta_j-\alpha (h_\theta(x)-y)\cdot x_j \qquad (j=0,\cdots,n) θj:=θj−α(hθ(x)−y)⋅xj(j=0,⋯,n)
} →can adapt to changing user preferences 适应变化的用户偏好
- Product search 产品搜索
- User searches for “Android phone 1080p camera” →用户搜索产品
- Have 100 phones in store. Will return 10 results →推荐100个产品中的10个
- x = features of phone, how many words in user query match name of phone, how many words in query match description of phone, etc →x表示手机的各种特征
- y = 1 if user clicks on link. y=0 otherwise →如果用户点击链接则y=1,否则y=0
- Learn p(y=1|x;θ) →估测用户点击某个链接的概率 →predicted click-through rate (CTR) 点击率预测学习问题
- Use to show user the 10 phones they’re most likely to click on →给用户展示十个最有可能点击的手机
- Other examples:
- Choosing special offers to show user →给用户展示什么样的特别优惠
- customized selection of news articles →给不同的用户展示不同的新闻文章
- product recommendation →商品推荐系统
Map‐reduce and data parallelism
Map‐reduce
- Map-reduce 映射减少:把训练集分割成不同的子集,有多台计算机并行处理训练集数据,不同的计算机分别计算不同训练集的求和项,最后把分别计算的求和项发送给中心服务器,由中心服务器整合结果来更新参数 θ_j
- 实际上因为网络延迟,结果汇总也需要额外时间以及其它因素,实际速度会比4倍要小
summation over the training set
- Many learning algorithms can be expressed as computing sums of functions over the training set →很多学习算法都可以表示成对训练集的函数求和,而带大数据集上运行所消耗的计算量就在于需要对非常大的训练集进行求和
- 计算代价函数或者求导项包括对训练集的求和都可以采用并行计算
- 只要学习算法可以表示成一系列的求和形式或者表示成在训练集上对函数的求和形式,就可以使用Map-reduce来并行化学习算法,使其应用于非常大的数据集
J t r a i n ( θ ) = − 1 m ∑ i = 1 m [ y ( i ) l o g ( h θ ( x ( i ) ) ) + ( 1 − y ( i ) ) l o g ( 1 − h θ ( x ( i ) ) ) ] J_{train}(\theta)=-\frac{1}{m}\sum_{i=1}^{m} [y^{(i)}\ log(h_{\theta}(x^{(i)}))+(1-y^{(i)})\ log(1-h_{\theta}(x^{(i)}))] Jtrain(θ)=−m1i=1∑m[y(i) log(hθ(x(i)))+(1−y(i)) log(1−hθ(x(i)))]
∂ ∂ θ j = 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) \frac{\partial}{\partial\theta_j}=\frac{1}{m}\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})x^{(i)}_j ∂θj∂=m1i=1∑m(hθ(x(i))−y(i))xj(i)
吴恩达教授语录
- “If you look back at a recent 5 or 10 year history of machine learning. One of the reasons the learning algorithms work so much better now than even say, 5 years ago, is just the sheer amount of data that we have now and that we can train our algorithms on.”
- “In large scale machine learning, we like to come up with computationally reasonable ways or computationally efficient ways to deal with very big data sets.”