【学习笔记】吴恩达机器学习 | 第八章 | 应用机器学习的建议

本文详述了机器学习中遇到的问题及解决策略,包括使用更多或更少的特征、调整正则化参数λ来处理过拟合或欠拟合,以及通过学习曲线分析偏差和方差。此外,强调了训练集和测试集的划分、交叉验证集的作用,以及神经网络过拟合的应对方法。
摘要由CSDN通过智能技术生成

在这里插入图片描述

简要声明


  1. 课程学习相关网址
    1. Bilibili
    2. 网易云课堂
    3. 学习讲义
  2. 由于课程学习内容为英文,文本会采用英文进行内容记录,采用中文进行简要解释。
  3. 本学习笔记单纯是为了能对学到的内容有更深入的理解,如果有错误的地方,恳请包容和指正。
  4. 非常感谢Andrew Ng吴恩达教授的无私奉献!!!

专有名词


Diagnostic诊断法Training set训练集
Test set测试集Cross validation set交叉验证集
Learning curves学习曲线

Deciding what to try next


Debugging a learning algorithm

  1. Get more training examples →使用更多训练样本 →有时候获得更多的训练数据实际上并没有作用
  2. Try smaller sets of features →尝试选用更少的特征
  3. Try getting additional features →尝试获得更多的特征
  4. Try adding polynomial features (x_12, x_12) →尝试增加多项式特征
  5. Try decreasing λ →尝试减小正则化参数λ
  6. Try increasing λ →尝试增大正则化参数λ

Machine learning diagnostic

  1. Diagnostic: A test that you can run to gain insight what is/isn’t working with a learning algorithm, and gain guidance as to how best to improve its performance →诊断法是一种测试方法,通过执行这种测试能够了解算法在哪里出了问题,通常也能说明如何更好改进算法的效果
  2. Diagnostics can take time to implement, but doing so can be a very good use of your time →诊断法的执行和实现是需要花很多时间的,但是这样做的确是把时间花在刀刃上

Evaluating a hypothesis


Dataset division

在这里插入图片描述

  1. Overfitting: Fails to generalize to new examples not in training set →过拟合:无法泛化到新的训练集
  2. Training set 训练集(m) :Test set 测试集(m_test) = 7:3
  3. 如果数据有某种规律或顺序的话,最好是随机选择70%作为训练集,剩下30%作为测试集

Training/testing procedure for linear regression

  1. Learn parameter θ from training data (minimizing training error J(θ) ) →对训练集进行学习得到参数θ(最小化训练误差 J(θ))
  2. Compute test set error →计算测试误差 J_test(θ)

J t e s t ( θ ) = 1 2 m t e s t ∑ i = 1 m t e s t ( h θ ( x t e s t ( i ) ) − y t e s t ( i ) ) 2 J_{test}(\theta)=\frac{1}{2m_{test}} \sum_{i=1}^{m_{test}}(h_\theta(x_{test}^{(i)})-y_{test}^{(i)})^2 Jtest(θ)=2mtest1i=1mtest(hθ(xtest(i))ytest(i))2

Training/testing procedure for logistic regression

  1. Learn parameter θ from training data (minimizing training error J(θ) ) →对训练集进行学习得到参数θ(最小化训练误差 J(θ))
  2. Compute test set error →计算测试误差 J_test(θ)
  3. Misclassification error (0/1 misclassification error) →错误分类(0/1 分类错误)

J t e s t ( θ ) = − 1 m t e s t ∑ i = 1 m t e s t [ y t e s t ( i )   l o g ( h θ ( x t e s t ( i ) ) ) + ( 1 − y t e s t ( i ) )   l o g ( 1 − h θ ( x t e s t ( i ) ) ) ] J_{test}(\theta)=-\frac{1}{m_{test}}\sum_{i=1}^{m_{test}} [y_{test}^{(i)}\ log(h_{\theta}(x_{test}^{(i)}))+(1-y_{test}^{(i)})\ log(1-h_{\theta}(x_{test}^{(i)}))] Jtest(θ)=mtest1i=1mtest[ytest(i) log(hθ(xtest(i)))+(1ytest(i)) log(1hθ(xtest(i)))]

e r r ( h θ ( x ) , y ) = { 1 i f   h θ ( x ) ≥ 0.5 ,   y = 0 o r i f   h θ ( x ) < 0.5 ,   y = 1 0 o t h e r w i s e err(h_\theta(x),y)=\begin{cases} 1 \qquad if \ h_\theta(x)\ge0.5,\ y=0 \quad or\quad if \ h_\theta(x)<0.5,\ y=1 \\0 \qquad otherwise \end{cases} err(hθ(x),y)={1if hθ(x)0.5, y=0orif hθ(x)<0.5, y=10otherwise

t e s t   e r r o r = 1 m t e s t ∑ i = 1 m t e s t e r r ( h θ ( x t e s t ( i ) ) − y t e s t ( i ) ) test\ error=\frac{1}{m_{test}} \sum_{i=1}^{m_{test}}err(h_\theta(x_{test}^{(i)})-y_{test}^{(i)}) test error=mtest1i=1mtesterr(hθ(xtest(i))ytest(i))

Model selection


Model selection problem

在这里插入图片描述

  1. Once parameters were fit to some set of data θ_1, θ_2, … , θ_n (training set), the error of the parameters as measured on that data (the training error J(θ)) is likely to be lower than the actual generalization error →如果参数对某个数据集(训练集)拟合的很好,那么同一数据集计算得到的误差(训练误差)并不能很好地估计出实际的泛化误差
  2. d = degree of polynomial →多项式的次数
  3. 逐个模型最小化训练误差得到参数θ →对所有模型求出测试集误差J_test(θ)
  4. Problem: J_test(θ) is likely to be an optimistic estimate of generalization error. I.e. our extra parameter d is fit to test set →由于用测试集拟合了一个额外的参数d,再在测试集上估计泛化误差就不公平了

Training/validation/test sets

在这里插入图片描述

  1. Training set 训练集(m) :Cross validation set 交叉验证集(m_cv):Test set 测试集(m_test) = 6:2:2
  2. 逐个模型最小化训练误差得到参数θ →对所有模型求出交叉验证集误差J_cv(θ) →选择交叉验证误差最小的多项式模型 →用测试集估计算法选出的模型的泛化误差

T r a i n i n g   e r r o r : J t r a i n ( θ ) = 1 2 m t r a i n ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2 Training\ error:\qquad J_{train}(\theta)=\frac{1}{2m_{train}} \sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})^2 Training error:Jtrain(θ)=2mtrain1i=1m(hθ(x(i))y(i))2

C r o s s   V a l i d a t i o n   e r r o r : J c v ( θ ) = 1 2 m c v ∑ i = 1 m c v ( h θ ( x c v ( i ) ) − y c v ( i ) ) 2 Cross\ Validation\ error:\qquad J_{cv}(\theta)=\frac{1}{2m_{cv}} \sum_{i=1}^{m_{cv}}(h_\theta(x_{cv}^{(i)})-y_{cv}^{(i)})^2 Cross Validation error:Jcv(θ)=2mcv1i=1mcv(hθ(xcv(i))ycv(i))2

T e s t   e r r o r : J t e s t ( θ ) = 1 2 m t e s t ∑ i = 1 m t e s t ( h θ ( x t e s t ( i ) ) − y t e s t ( i ) ) 2 Test\ error:\qquad J_{test}(\theta)=\frac{1}{2m_{test}} \sum_{i=1}^{m_{test}}(h_\theta(x_{test}^{(i)})-y_{test}^{(i)})^2 Test error:Jtest(θ)=2mtest1i=1mtest(hθ(xtest(i))ytest(i))2

Bias vs. Variance


Bias/variance

在这里插入图片描述

  1. 横坐标表示多项式的次数,纵坐标表示误差
  2. 随着增大多项式次数对训练集拟合的越来越好 →训练误差趋于下降
  3. 当d很小时交叉验证误差较大,中等大小d能更好拟合数据交叉验证误差较小,当d很大时交叉验证误差较大

Diagnosing bias vs. variance

在这里插入图片描述

  1. 假设学习算法没有表现地像你预期的那么好(交叉验证误差或测试误差很大),判断是高偏差问题还是高方差问题
  2. High bias 高偏差(欠拟合)→训练误差和交叉验证误差一样很大
  3. High variance 高方差(过拟合)→训练误差很小,交叉验证误差远大于训练误差

Regularization parameter λ

在这里插入图片描述

  1. 如果正则化参数λ很大 →高偏差;如果正则化参数λ很小 →高方差
  2. J(θ)表示学习算法的目标(有正则化项),J_train(θ)表示训练集误差(没有正则化项)
  3. J_train, J_cv, J_test定义为平均的误差平方和,没有正则化项

h θ = θ 0 + θ 1 x + θ 2 x 2 + θ 3 x 3 + θ 4 x 4 h_{\theta}=\theta_0+\theta_1x+\theta_2x^2+\theta_3x^3+\theta_4x^4 hθ=θ0+θ1x+θ2x2+θ3x3+θ4x4

J ( θ ) = 1 2 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2 + λ 2 m ∑ j = 1 m θ j 2 J(\theta)=\frac{1}{2m_{}} \sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})^2 + \frac{\lambda}{2m}\sum_{j=1}^m\theta_j^2 J(θ)=2m1i=1m(hθ(x(i))y(i))2+2mλj=1mθj2

J t r a i n ( θ ) = 1 2 m t r a i n ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2 J_{train}(\theta)=\frac{1}{2m_{train}} \sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})^2 Jtrain(θ)=2mtrain1i=1m(hθ(x(i))y(i))2

J c v ( θ ) = 1 2 m c v ∑ i = 1 m c v ( h θ ( x c v ( i ) ) − y c v ( i ) ) 2 J_{cv}(\theta)=\frac{1}{2m_{cv}} \sum_{i=1}^{m_{cv}}(h_\theta(x_{cv}^{(i)})-y_{cv}^{(i)})^2 Jcv(θ)=2mcv1i=1mcv(hθ(xcv(i))ycv(i))2

J t e s t ( θ ) = 1 2 m t e s t ∑ i = 1 m t e s t ( h θ ( x t e s t ( i ) ) − y t e s t ( i ) ) 2 J_{test}(\theta)=\frac{1}{2m_{test}} \sum_{i=1}^{m_{test}}(h_\theta(x_{test}^{(i)})-y_{test}^{(i)})^2 Jtest(θ)=2mtest1i=1mtest(hθ(xtest(i))ytest(i))2

Choosing the regularization parameter λ

在这里插入图片描述

  1. 选取一系列想要尝试的λ值(0, 0.01, 0.02, 0.04, 0.08, …, 10.24)一般设为2倍速度增长直到一个比较大的值
  2. 逐个模型最小化代价函数J(θ)得到参数向量θ →通过交叉验证集估计交叉验证集误差 →选择交叉验证集误差最小的模型 →通过测试集计算模型的测试集误差

Bias/variance as a function of the regularization parameter λ

在这里插入图片描述

  1. 训练误差定义为不包括正则化项
  2. 横坐标表示正则化参数λ,纵坐标表示误差
  3. 当λ很小时对训练集拟合较好训练集误差较小,当λ很大时会出现高偏差问题训练集误差较大
  4. 当λ很小时会啊出现高方差问题交叉验证误差较大,中等大小λ能更好拟合数据交叉验证误差较小,当λ很大时会出现高偏差问题交叉验证误差较大

Learning curves


Learning curves

在这里插入图片描述

  1. 横坐标表示训练集样本数量 m ,纵坐标表示误差
  2. 为了认为减小训练集,限制只用10, 20, 30, 40个训练样本,并画出训练集误差和交叉验证集误差
  3. 当训练样本容量m很小的时候训练误差也很小,随着训练集容量增大平均训练误差也在增大
  4. 当训练集很小的时候泛化程度不好交叉验证集误差较大,当训练集较大时找到能够更好你和数据的假设函数,交叉验证集误差会随着训练样本容量m的增大而减小 →使用的数据越多越能获得更好的泛化表现
  5. High bias 高偏差 →随着样本容量的增大,交叉验证集误差逐渐减少当到达一定数量的训练样本时不再改变,训练集误差逐渐增大当到达一定数量的训练样本时不再改变,最后训练集误差与交叉验证集误差接近 →high error 误差大 →如果一个学习算法有高偏差,选取更多的训练集数据对改善算法没帮助
  6. High variance 高方差 →随着样本容量的增大,交叉验证集误差逐渐减少,训练集误差逐渐增大 →gap 训练误差和交叉验证误差直接存在很大的差距 →如果一个学习算法有高方差,增加训练集的样本数对改进算法有帮助

在这里插入图片描述

在这里插入图片描述

Deciding what to try next (revisited)


Debugging a learning algorithm

  1. Get more training examples →使用更多训练样本 →解决高方差问题 high variance
  2. Try smaller sets of features →尝试选用更少的特征 →解决高方差问题 high variance
  3. Try getting additional features →尝试获得更多的特征 →解决高偏差问题 high bias
  4. Try adding polynomial features (x_12, x_12) →尝试增加多项式特征 →解决高偏差问题 high bias
  5. Try decreasing λ →尝试减小正则化参数λ →解决高偏差问题 high bias
  6. Try increasing λ →尝试增大正则化参数λ →解决高方差问题 high variance

Neural networks and overfitting

在这里插入图片描述

  1. 比较简单的神经网络模型(只有一个隐藏层,隐藏单元少)→容易出现欠拟合 →计算量少
  2. 大型神经网络结构(很多隐藏层,很多隐藏单元)→容易出现过拟合 →很大的计算量,网络越大性能越好 →通过正则化修正
  3. 一般来说,使用一个大型的神经网络并使用正则化来修正过拟合问题,通常比使用一个小型的神经网络效果更好

吴恩达教授语录


  • “By now you’ve seen a lot of different learning algorithms, and if you’ve been following along these videos you should consider yourself an expert on many state-of-the-are machine learning techniques.”
  • “And I have a lot of times sadly seen people spend, you know, literally 6 months doing one of these avenues that they have picked sort of at random only to discover six months later that really wasn’t promising avenue to pursue.”
  • “Learning curves is a tool that I actually use very often to try to diagnose if a physical learning algorithm may be suffering from bias, variance problem or a bit of both.”
  • “If you understood the contents of the last few videos and if you apply them you actually be much more effective already at getting learning algorithms to work on problems. than even a fraction, maybe the majority of practitioners of machine learning here in Silicon Valley today doing these things as their full-time jobs.”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Chency.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值