评估假设函数
如果发现训练出的模型结果不好,一般会从以下方面找问题:
- 扩充训练集
- 减少特征集
- 使用额外的特征
- 使用多项式特征
- 增减λ
测试集
为了评估假设函数,一般会将数据集分为两部分:70%的训练集和30%的测试集。
用训练集获得Theta,用测试集评估效果。
测试集的误差计算方法:
-
线性回归:
-
分类:
其中:
验证集
参考:一文看懂 AI 数据集:训练集、验证集、测试集(附:分割方法+交叉验证)
验证集是用来调整超参数的,如果无需调整超参数,可以不使用验证集,只用训练+测试集。
使用验证集调整完超参数后,再用测试集检查最终结果。
分配比例如下:
数据划分的方法并没有明确的规定,不过可以参考3个原则:
- 对于小规模样本集(几万量级),常用的分配比例是 60% 训练集、20% 验证集、20% 测试集。
- 对于大规模样本集(百万级以上),只要验证集和测试集的数量足够即可,例如有 100w 条数据,那么留 1w 验证集,1w 测试集即可。1000w 的数据,同样留 1w 验证集和 1w 测试集。
- 超参数越少,或者超参数很容易调整,那么可以减少验证集的比例,更多的分配给训练集。
怎么知道过拟合还是欠拟合
如下图:
调整λ的过程:
- 创建一系列λ的值,比如λ∈{0,0.01,0.02,0.04,0.08,0.16,0.32,0.64,1.28,2.56,5.12,10.24}
- 用不同的次方数或其它参数创建模型
- 遍历所有的λ,对每个λ都经过所有模型训练,来学习出θ
- 用学得的θ计算交叉验证误差(令λ=0)
- 找出交叉验证误差最小的θ和λ组合
- 把上面找出的最佳θ和λ组合应用到测试集,检验是否有好的泛化效果
学习曲线
欠拟合的情况,增加训练数据帮助不大,都会相比预期效果相差挺远:
过拟合的情况,增加训练数据会有所帮助,会朝着预期性能收敛:
案例分析:如何搭建垃圾邮件分类器
明确需要做什么
将邮件中的常用单词10,000 - 50,000个作为特征向量(出现记为1,不出现记为0),在标记了是否为垃圾邮件的训练集上训练。
有以下可能的改善性能的方法:
- 收集很多数据
- 使用精细的特征(比如使用邮件头信息)
- 对输入做多种不同处理(比如识别邮件中的误拼单词)
分析错误
建议的机器学习算法开发流程为:
- 快速开发一个简单的算法,在交叉验证数据上测试;
- 绘制学习曲线,来确定是否需要更多的数据或特征;
- 手动检查算法在验证数据集中出的错误,争取发现错误出现的规律;不断加入新的想法,测试是否能降低错误率;
误差标准和倾斜类
倾斜类:某个类别出现的数量级远低于另一个。
预测所有y=0,准确率就高达0.5%。
令y=1表示数量稀少的类别,下面两个指标更有参考意义。
True / False 表示预测是否正确;
Positive / Nagative 表示预测值;
预测正确的为 True Positive + True Nagative: 真的正 + 真的负
准确率
成功预测为1的 / 所有预测为1的
召回率
成功预测为1的 / 所有实际为1的
准确率越高,判断为1越有自信,宁缺毋滥,告诉病人你得癌症了,大概率一定得癌症了;
召回率越高,判断为1范围越广,宁滥勿缺,告诉更多的病人你可能得癌症了,不一定真的有;
什么样的准确率、召回率组合最好,可以用F1得分:
可以改变threshold的值,来调整准确率与召回率。比如 hx >= 0.9时才预测为1,准确率就很高,召回率就低;hx >= 0.1时就预测为1,准确率就很低,召回率就高。
提高数据量
金句:更多的数据,胜过更好的算法。
什么情况下提高数据量有用呢?以下两个场景:
- 有用的数据,人类专家能准确预测的:
- 参数多/特征多/隐藏节点多的神经网络等,原本容易过拟合的(高方差,high variance),增加数据量可以让它很难过拟合:
作业
linearRegCostFunction.m
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 = sum((h-y).^2) / 2 / m + lambda / 2 / m * sum(theta(2:end) .^ 2);
grad0 = X(:,1)'*(h-y) / m;
grad1 = X(:,2:end)'*(h-y)./ m + lambda * theta(2:end) ./ m;
% =========================================================================
grad = [grad0;grad1];
end
learningCurve.m
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);
[J, ~] = linearRegCostFunction(X(1:i, :), y(1:i), theta, 0);
error_train(i) = J;
[J, ~] = linearRegCostFunction(Xval, yval, theta, 0);
error_val(i) = J;
end
% -------------------------------------------------------------
% =========================================================================
end
polyFeatures.m
function [X_poly] = polyFeatures(X, p)
%POLYFEATURES Maps X (1D vector) into the p-th power
% [X_poly] = POLYFEATURES(X, p) takes a data matrix X (size m x 1) and
% maps each example into its polynomial features where
% X_poly(i, :) = [X(i) X(i).^2 X(i).^3 ... X(i).^p];
%
% You need to return the following variables correctly.
X_poly = zeros(numel(X), p);
% ====================== YOUR CODE HERE ======================
% Instructions: Given a vector X, return a matrix X_poly where the p-th
% column of X contains the values of X to the p-th power.
%
%
for i = 1:p,
X_poly(:,i) = X .^ i;
end
% =========================================================================
end
validationCurve.m
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);
[J, ~] = linearRegCostFunction(X, y, theta, 0);
error_train(i) = J;
[J, ~] = linearRegCostFunction(Xval, yval, theta, 0);
error_val(i) = J;
end
% =========================================================================
end