前言目录
前言
一、进度
二、基本内容
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。所以穗子he训练集数量增加,越来越难以为了拟合而拟合,所以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:m theta = 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:m
theta = 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:)等人讨论时给出的观点:积累足够多的经验,在遇到这类事件时通过直觉决策。大概也有那么一点意思在里面吧。
顺便,学校的深度学习也考完了。只能说重叠的部分挺多,但是距离上交一份完美的答卷还有很多路要走。



