机器学习学习笔记(五)—— 利用正则化(Regularization)解决过度拟合(Overfitting)问题

过度拟合(overfitting)和欠拟合(underfitting):

第一张图片对训练数据没有很好的匹配,所以说是欠拟合的;中间的图片能大致的描述训练数据,所以说是正常的;最后一张图片虽然对所有的训练数据都拟合得很好,但是没有了通用性,所以说是过度拟合的。

有两种方法可以很好的解决过度拟合问题,There are two main options to address the issue of overfitting:

1) 减少特征数量,Reduce the number of features:

    Manually select which features to keep.

    Use a model selection algorithm.

2) 正则化,Regularization

    Keep all the features, but reduce the magnitude of parameters \theta _{j}.

    Regularization works well when we have a lot of slightly useful features.

正则化的理解: 

如果我们有一个多项式的假设函数

如果我们舍弃后面两个特征

如果后面两个特征对我们很重要,我们需要保留,利用之前的梯度下降法将会得到下面的蓝色函数图像。而我们希望得到的正确函数图像可能是品红色的

引入正则化(regularization)

我们在原有的代价函数上惩罚(penalize)所有的\theta _{j}, j \in [1,2,3...n], We could also regularize all of our theta parameters in a single summation as:

The λ, or lambda, is the regularization parameter. It determines how much the costs of our theta parameters are inflated. Using the above cost function with the extra summation, we can smooth the output of our hypothesis function to reduce overfitting. If lambda is chosen to be too large, it may smooth out the function too much and cause underfitting. Hence, what would happen if \lambda = 0λ=0 or is too small ?

λ被称为正则化参数,λ的作用就是控制以下两个目标中的一个平衡关系:

1.我们要使得训练出的假设函数更好的拟合训练数据,使假设更好的适应训练集。

2.我们要保持theta值较小。

λ取值大小决定了是否很好的平衡拟合训练。λ过大,会导致underfitting,λ过小将不能很好的解决overfitting。

正则化线性回归

梯度下降法中,Gradient Descent:

把 \frac{\lambda }{m}\theta _{j} 提取出来就变成了下面的式子:

这里可以发现,减号后面的部分跟之前是一样的,\left ( 1-\alpha \frac{\lambda }{m} \right ) 是恒小于1的.

正规方程法中,Normal Equation:

To add in regularization, the equation is the same as our original, except that we add another term inside the parentheses:

Recall that if m < n, then  is non-invertible. However, when we add the term λ⋅L, then  becomes invertible.

正则化逻辑回归

加入正则化后的代价函数:

计算代价函数和梯度的示例代码:

h = sigmoid(X * theta);
J = (-log(h)' * y - log(1 - h)' * (1 - y)) / m + sum(theta(2: size(theta)) .^ 2) * lambda / 2 / m;
grad = (X' * (h - y)) ./ m + (lambda / m) .* [0; theta(2 : size(theta))];
% Initialize fitting parameters
initial_theta = zeros(size(X, 2), 1);

% Set regularization parameter lambda to 1 (you should vary this)
lambda = 1;

% Set Options
options = optimset('GradObj', 'on', 'MaxIter', 400);

% Optimize
[theta, J, exit_flag] = ...
	fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值