【Coursera-Machine Learning】自用5
目录
前言
一、进度
二、基本内容
1.数据集拆分
2.Bias/Variance
3.Learning Curve
4.Spam E-mail Classifier Example
5.Accurary/Precision/Recall
6.作业
总结
前言
基本的思想:前期选择,后期修正
一、进度
第六周(75%)
二、基本内容
1.数据集拆分
基本上,把数据集拆分成三份:Training Set, Cross Validation Set, Test set。
对于多重维度,先将所有的维度的Training Set进行训练得到所有的Cost Function,然后比较Cross Validation的误差值, 选出最小的一组,最后用Test Set进行损失评估。
2.Bias/Variance
先做个个人理解的翻译,bias翻译为不足,variance翻译为过剩,分别对应bias(underfit),variance(overfit)。
有以下图分别解析:
首先只看训练集的cost,随着维数的上升,只有可能不断拟合,所以train的cost只会递减。
至于验证集的cost,是经历了欠拟合一拟合一过拟合这个过程,所以图像先递减后递增。
而在验证集图像的极小值点之前,验证集损失总是略大于训练集损失,所以图像长这样。
回顾一下
λ的用途是regularization,用于中和离谱的θ。λ越大,越容易欠拟合;λ越小,越容易过拟合。
同样的,先考虑训练集的cost,λ从小到大,函数过拟合一拟合一欠拟合(和上面反过来),所以单调递增。而同理验证集的cost就递减后递增。
这里多提一句λ的取值。Andrew的做法是取指数函数:
通过数量级的提升来找到合理的数量级。这和我之前想象的同数量级的循环取值要高效许多。
3.Learning Curve
这里的前提是,维数不变,单纯增加训练集的数量,看看训练的结果。
首先判断二者的单调性。易知随着训练深入,验证集的损失不断减小;而对于训练集,当样本值较少的时候,我可以给出一个为了拟合而拟合的曲线,来降低cost。所以随着训练集数量增加,越来越难以为了拟合而拟合,所以cost逐渐增大。例如:
而同时验证集的cost永远是大于训练集的cost,所以验证集曲线永远是训练集曲线的一个上界(致敬数分)。
然后先考虑High Bias的细节,因为永远只是简单的一个函数,所以再怎么训练,cost永远不会降到很低,因此出现图中的情况。
对于High Variance,我的理解是你给出了足够的训练集,那就基本能覆盖到测试用的验证集的某个邻域范围内。换句话说,如果你给出足够多的训练集,我大可以把整条曲线通过描点法描出来,那损失甚至不用预测,因为已经很接近某个现有的训练集的点了。
以该题为例:
总结如下:
同时在这里再放一部分最后一块的内容:
4.Spam E-mail Classifier Example
这一部分只是讲了一个例子,对于邮件分类我们选取什么作为训练的依据。总结下来就是说通过选择特定的垃圾邮件单词与邮件本身进行对比,得出是否存该垃圾邮件单词的一个特征向量。具体内容可参考该文对该部分的总结,很详细:
斯坦福大学公开课机器学习: machine learning system design | prioritizing what to work on : spam classification example(设计复杂机器学习系统的主要问题及构建复杂的机器学习系统的建议) - 橙子牛奶糖 - 博客园
同时,在后面有讲到自动填词的预测,比如很多同音词:to,too, two等。这时候会查看该空格的邻域,确定语境,然后选择合适语境的单词填入。
5.Accurary/Precision/Recall
为数不多的之前已经在学校学懂的知识。
Accuracy:准确预测的样本/所有样本
Precision:准确预测的样本/预测正确的样本
Recall:准确预测的样本/所有正例
除了学校学过的内容,还有一些额外的内容:
有时候我们想要得到不同的结果,比如有时候要高预测准确率,有时候要不能放走一个,通过调整激活函数的阈值可以做到这个效果。提高阈值,则提高预测准确率;降低阈值,则保证不会错放一个。
同时引入一个F1值,用于评价算法的好坏:
P:Presicion,R:Recall。
不知道为什么Andrew不怎么用Accuracy。
6.作业
先放两张错题(Andrew难得把答案解析放出来):
这次编程作业其实还是很有意义的。对于一些过程的实现并不是我之前想的那样:
1.对于训练集,不断增加训练的个数,但是每次对应的验证集都是那一部分。同时在这里先不用考虑regularization,所以在绘制“训练个数一cost”的图像的时候,代码如下:
for i = 1:mtheta = trainLinearReg(X(1:i, :),y(1:i),lambda);[error_train(i) temp] = linearRegCostFunction(X(1:i, :), y(1:i), theta, 0);[error_val(i) temp] = linearRegCostFunction(Xval, yval, theta, 0);
end
2.在对不同的λ做选择的时候,代码如下:
for i = 1:length(lambda_vec)lambda = lambda_vec(i);theta = trainLinearReg(X,y,lambda);[error_train(i) temp] = linearRegCostFunction(X, y, theta, 0);[error_val(i) temp] = linearRegCostFunction(Xval, yval, theta, 0);
end
这里一开始我在想为什么0的部分不是lambda,后来在这里找到了答案:
Programming Exercise 5: Regularized Linear Regression and Bias v.s. Variance_zengxinch的博客-CSDN博客
简单来说,我们已经在求theta那一行用到了lambda,后面的两个向量只是计算在该theta的情况下求出的误差,其实再用λ做regularization没有意义。
备份:
function [error_train, error_val] = ...learningCurve(X, y, Xval, yval, lambda)
%LEARNINGCURVE Generates the train and cross validation set errors needed
%to plot a learning curve
% [error_train, error_val] = ...
% LEARNINGCURVE(X, y, Xval, yval, lambda) returns the train and
% cross validation set errors for a learning curve. In particular,
% it returns two vectors of the same length - error_train and
% error_val. Then, error_train(i) contains the training error for
% i examples (and similarly for error_val(i)).
%
% In this function, you will compute the train and test errors for
% dataset sizes from 1 up to m. In practice, when working with larger
% datasets, you might want to do this in larger intervals.
%% Number of training examples
m = size(X, 1);% You need to return these values correctly
error_train = zeros(m, 1);
error_val = zeros(m, 1);% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return training errors in
% error_train and the cross validation errors in error_val.
% i.e., error_train(i) and
% error_val(i) should give you the errors
% obtained after training on i examples.
%
% Note: You should evaluate the training error on the first i training
% examples (i.e., X(1:i, :) and y(1:i)).
%
% For the cross-validation error, you should instead evaluate on
% the _entire_ cross validation set (Xval and yval).
%
% Note: If you are using your cost function (linearRegCostFunction)
% to compute the training and cross validation error, you should
% call the function with the lambda argument set to 0.
% Do note that you will still need to use lambda when running
% the training to obtain the theta parameters.
%
% Hint: You can loop over the examples with the following:
%
% for i = 1:m
% % Compute train/cross validation errors using training examples
% % X(1:i, :) and y(1:i), storing the result in
% % error_train(i) and error_val(i)
% ....
%
% end
%% ---------------------- Sample Solution ----------------------
for i = 1:mtheta = trainLinearReg(X(1:i, :),y(1:i),lambda);[error_train(i) temp] = linearRegCostFunction(X(1:i, :), y(1:i), theta, 0);[error_val(i) temp] = linearRegCostFunction(Xval, yval, theta, 0);
end
% -------------------------------------------------------------% =========================================================================end
function [J, grad] = linearRegCostFunction(X, y, theta, lambda)
%LINEARREGCOSTFUNCTION Compute cost and gradient for regularized linear
%regression with multiple variables
% [J, grad] = LINEARREGCOSTFUNCTION(X, y, theta, lambda) computes the
% cost of using theta as the parameter for linear regression to fit the
% data points in X and y. Returns the cost in J and the gradient in grad% Initialize some useful values
m = length(y); % number of training examples% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost and gradient of regularized linear
% regression for a particular choice of theta.
%
% You should set J to the cost and grad to the gradient.
%
h = X*theta;
J = (1/(2*m))*sum((h-y).^2);
J = J + (lambda/(2*m))*sum(theta(2:end).^2);grad = (1/m)*(X'*(h-y));
grad(2:end) = grad(2:end) + lambda/m*theta(2:end);
% =========================================================================grad = grad(:);end
function [J, grad] = linearRegCostFunction(X, y, theta, lambda)
%LINEARREGCOSTFUNCTION Compute cost and gradient for regularized linear
%regression with multiple variables
% [J, grad] = LINEARREGCOSTFUNCTION(X, y, theta, lambda) computes the
% cost of using theta as the parameter for linear regression to fit the
% data points in X and y. Returns the cost in J and the gradient in grad% Initialize some useful values
m = length(y); % number of training examples% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost and gradient of regularized linear
% regression for a particular choice of theta.
%
% You should set J to the cost and grad to the gradient.
%
h = X*theta;
J = (1/(2*m))*sum((h-y).^2);
J = J + (lambda/(2*m))*sum(theta(2:end).^2);grad = (1/m)*(X'*(h-y));
grad(2:end) = grad(2:end) + lambda/m*theta(2:end);
% =========================================================================grad = grad(:);end
function [lambda_vec, error_train, error_val] = ...validationCurve(X, y, Xval, yval)
%VALIDATIONCURVE Generate the train and validation errors needed to
%plot a validation curve that we can use to select lambda
% [lambda_vec, error_train, error_val] = ...
% VALIDATIONCURVE(X, y, Xval, yval) returns the train
% and validation errors (in error_train, error_val)
% for different values of lambda. You are given the training set (X,
% y) and validation set (Xval, yval).
%% Selected values of lambda (you should not change this)
lambda_vec = [0 0.001 0.003 0.01 0.03 0.1 0.3 1 3 10]';% You need to return these variables correctly.
error_train = zeros(length(lambda_vec), 1);
error_val = zeros(length(lambda_vec), 1);% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return training errors in
% error_train and the validation errors in error_val. The
% vector lambda_vec contains the different lambda parameters
% to use for each calculation of the errors, i.e,
% error_train(i), and error_val(i) should give
% you the errors obtained after training with
% lambda = lambda_vec(i)
%
% Note: You can loop over lambda_vec with the following:
%
% for i = 1:length(lambda_vec)
% lambda = lambda_vec(i);
% % Compute train / val errors when training linear
% % regression with regularization parameter lambda
% % You should store the result in error_train(i)
% % and error_val(i)
% ....
%
% end
%
%
for i = 1:length(lambda_vec)lambda = lambda_vec(i);theta = trainLinearReg(X,y,lambda);[error_train(i) temp] = linearRegCostFunction(X, y, theta, 0);[error_val(i) temp] = linearRegCostFunction(Xval, yval, theta, 0);
end
% =========================================================================end
总结
问题不大,小鸽了一会。
越来越在各个方面感受到,像Back Propagation算法一样,很多东西不是前期一次就能做到完美的,总是要后期不断修正完善。罗翔《圆圈正义》讲到万州公交车事件,突然想起当时我在学校与孙先生、PC、儿子@Soledad215:)等人讨论时给出的观点:积累足够多的经验,在遇到这类事件时通过直觉决策。大概也有那么一点意思在里面吧。
顺便,学校的深度学习也考完了。只能说重叠的部分挺多,但是距离上交一份完美的答卷还有很多路要走。
【Coursera-Machine Learning】自用5相关推荐
- [coursera machine learning] Week 1
1. machine learning 问题的分类: Supervised Learning: right answers given in samples Regression: continuou ...
- Coursera Machine Learning 作业提交问题
关于作业提交问题的解决办法 Octave 4.0.0无法正常提交 解决办法:打两个补丁 补丁1:平台通用 补丁2:Win,Linux or Mac 注:补丁文件中有安装说明
- 吴恩达ex3_[Coursera] Machine Learning ex3 多元分类和神经网络 步骤分析
第四周的主要内容是神经网络,个人觉得讲得比较跳,所以补充几篇文章加深一下理解: But what *is* a Neural Network? 先提一下,本人设计背景,没学过微积分,这篇只当是笔记,有 ...
- Machine Learning - Andrew Ng on Coursera (Week 6)
本篇文章将分享Coursera上Andrew Ng的Machine Learning第六周的课程,主要内容有如下,详细内容可以参考文末附件: 评价机器学习算法 Diagnosing bias vs. ...
- Machine Learning - Andrew Ng on Coursera (Week 5)
本篇文章将分享Coursera上Andrew Ng的Machine Learning第五周的课程,主要内容有如下,详细内容可以参考文末附件: 代价函数及后向算法 Cost function(代价函数) ...
- Machine Learning - Andrew Ng on Coursera (Week 4)
本篇文章将分享Coursera上Andrew Ng的Machine Learning第四周的课程,主要内容有如下,详细内容可以参考文末附件: 动机 神经网络 应用 动机 为什么要引入神经网络?在分类问 ...
- Machine Learning - Andrew Ng on Coursera (Week 3)
本篇文章将分享Coursera上Andrew Ng的Machine Learning第三周的课程,主要内容有如下,详细内容可以参考文末附件: 分类问题及模型表示 逻辑回归模型 多类别的分类问题 解决过 ...
- Machine Learning - Andrew Ng on Coursera (Week 2)
本篇文章将分享Coursera上Andrew Ng的Machine Learning第二周的课程,主要内容有如下,详细内容可以参考文末附件: 设置作业环境 多变量线性回归 参数的解析算法 Octave ...
- Machine Learning - Andrew Ng on Coursera (Week 1)
转载自:http://1.kaopuer.applinzi.com/?p=110 今天分享了Coursera上Andrew Ng的Machine Learning第一周的课程,主要内容有如下,详细内容 ...
- Coursera公开课笔记: 斯坦福大学机器学习第十一课“机器学习系统设计(Machine learning system design)”
Coursera公开课笔记: 斯坦福大学机器学习第十一课"机器学习系统设计(Machine learning system design)" 斯坦福大学机器学习斯坦福大学机器学习第 ...
最新文章
- 【对讲机的那点事】带你玩转灵通LT33公网集群对讲机
- 小苏打到底能不能碱化尿液
- bootstrap .navbar-header 控制button隐藏/显示
- 我为什么离开德国顶级传统大厂IT部
- 原生 Ajax 封装 和 Axios 二次 封装
- 初次使用uwsgi:no python application found, check your startup logs for errors
- Hadoop环境搭建(6) -- 克隆
- 改善脑力的70条方法[翻译]
- 解决uniapp退出全屏视频字体变大的bug
- 行驶证OCR识别应用领域有哪些?
- 智汇华云|安超云套件Archer Cloudsuite为“信创强国”筑基
- 靠营销出圈的拉面说,会是下一个黄太吉吗?
- 【概率论】理解贝叶斯(Bayes)公式:为什么疾病检测呈阳性,得这种病的概率却不高?
- 卸载cuda11.0并安装cuda11.1和cudnn
- 泛微OA ecology8的一些开发经验汇总
- ObjectARX 2020 Wizards文件内容
- 15分钟分析伦敦银今日走势
- 关于sjh的创建步骤
- LCD背光驱动节电技术-LABC/CABC
- QT练习Http通信代码下载器
热门文章
- 元宵节没用智能名片在互联网发贺卡,那就OUT了
- 美女SEO系列六:什么是友情链接?
- 程序员之路:Gmail邮箱客户端配置
- Stata | 缺失值处理标签
- 打砖块python游戏源代码_python制作一个打砖块小游戏
- 云计算与大数据——数据中心
- android的轮播图Banner之本地加载和网络加载图片
- Android图片处理(Matrix,ColorMatrix,深入讲解Android
- 无盘服务器chkdsk *: /f)修复命令,让你的电脑运行更快点 使用CHKDSK/F磁盘修复命令...
- JavaScript学习笔记01——简介(李炎恢JavaScript教程)