【学习笔记】吴恩达机器学习 | 第十五章 | 大规模机器学习

在这里插入图片描述

简要声明


  1. 课程学习相关网址
    1. Bilibili
    2. 网易云课堂
    3. 学习讲义
  2. 由于课程学习内容为英文,文本会采用英文进行内容记录,采用中文进行简要解释。
  3. 本学习笔记单纯是为了能对学到的内容有更深入的理解,如果有错误的地方,恳请包容和指正。
  4. 非常感谢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

  1. “It’s not who has the best algorithm that wins. It’s who has the most data.” →在机器学习中,通常情况下决定因素往往不是更好地算法而是谁的训练数据最多
  2. 当我们把精力花在用一亿个样本训练模型之前,考虑为什么不只用一千个样本来训练算法
  3. 使用一个非常小的训练集的完整性检查方法结果可能也是一样的 →绘制学习曲线

在这里插入图片描述

Stochastic gradient descent


在这里插入图片描述

  1. 当m很大的时候,梯度下降求和的计算量会变得非常大 →需要对m个样本进行求和
  2. 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=1mcost(θ,(x(i),y(i))

  1. 随机打乱所有数据 →将所有m个训练样本重新随机排列
  2. 只对一个训练样本的代价函数进行梯度下降操作 →只关注一个训练样本,将参数稍微修改一下
  3. 不管数据是否已经随机排列过,随机打乱所有数据在收敛时能够更快一点
  4. 随机梯度下降与批量梯度下降不同在于随机梯度下降不需要对全部m个样本求和来得到梯度项,只需要对单个训练样本求出这个梯度项
  5. 随机梯度下降算法的关键是关于某一个单独的训练样本(x(i), y(i))来对参数进行更新
  6. 用随机梯度下降法能得到一个很接近全局最小值的参数
  7. 当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α(θjcost(θ,(x(i),y(i)))xj(i)(for j=0,,n)

}

}

在这里插入图片描述

Mini­‐batch gradient descent


  1. Batch gradient descent: Use all m examples in each iteration
  2. Stochastic gradient descent: Use 1 examples in each iteration
  3. Mini-batch gradient descent: Use b examples in each iteration
  4. b = mini-batch size →小批量大小,通常选择b=10(2~100)
  5. 通过使用合适的向量化方式可以提高Mini-batch梯度下降的计算速度
  6. 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=ii+9(hθ(x(k))y(k))xj(k)(for j=0,,n)

}

}

Gradient descent convergence


Checking for convergence

在这里插入图片描述

  1. Batch gradient descent 批量梯度下降:绘制优化代价函数,确保每一次迭代都是下降的

  2. 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函数的平均值

  3. 随机梯度下降算法不是直接收敛到全局最小值,而是在一个范围内反复震荡,最后逐渐接近全局最小值

在这里插入图片描述

Learning rate

  1. Learning rate α is typically held constant →学习速率α一般是保持不变的常数

  2. 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

  3. 随着算法的运行,迭代次数回越来越大,学习速率α会慢慢变小

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 适应变化的用户偏好

在这里插入图片描述

  1. Product search 产品搜索
    1. User searches for “Android phone 1080p camera” →用户搜索产品
    2. Have 100 phones in store. Will return 10 results →推荐100个产品中的10个
    3. 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表示手机的各种特征
    4. y = 1 if user clicks on link. y=0 otherwise →如果用户点击链接则y=1,否则y=0
    5. Learn p(y=1|x;θ) →估测用户点击某个链接的概率 →predicted click-through rate (CTR) 点击率预测学习问题
    6. Use to show user the 10 phones they’re most likely to click on →给用户展示十个最有可能点击的手机
  2. Other examples:
    1. Choosing special offers to show user →给用户展示什么样的特别优惠
    2. customized selection of news articles →给不同的用户展示不同的新闻文章
    3. product recommendation →商品推荐系统

Map‐reduce and data parallelism


Map‐reduce

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  1. Map-reduce 映射减少:把训练集分割成不同的子集,有多台计算机并行处理训练集数据,不同的计算机分别计算不同训练集的求和项,最后把分别计算的求和项发送给中心服务器,由中心服务器整合结果来更新参数 θ_j
  2. 实际上因为网络延迟,结果汇总也需要额外时间以及其它因素,实际速度会比4倍要小

summation over the training set

  1. Many learning algorithms can be expressed as computing sums of functions over the training set →很多学习算法都可以表示成对训练集的函数求和,而带大数据集上运行所消耗的计算量就在于需要对非常大的训练集进行求和
  2. 计算代价函数或者求导项包括对训练集的求和都可以采用并行计算
  3. 只要学习算法可以表示成一系列的求和形式或者表示成在训练集上对函数的求和形式,就可以使用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=1m[y(i) log(hθ(x(i)))+(1y(i)) log(1hθ(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=1m(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.”
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Benjamin Chen.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值