【问题标题】:Which is the correct implementation of regularization in octave?哪个是八度音阶正则化的正确实现?
【发布时间】:2021-01-08 13:15:26
【问题描述】:

我目前正在学习 Andrew Ng 的机器学习课程,并尝试在学习过程中实现这些东西,以免忘记它们,我刚刚完成了正则化(第 7 章)。我知道 theta 0 会正常更新,与其他参数分开,但是,我不确定其中哪个是正确的实现。

实现1:在我的梯度函数中,计算正则化向量后,将theta 0部分改为0,这样当它加到总数中时,就好像theta 0从未被正则化。

实现 2:将 theta 存储在临时变量:_theta 中,将其更新为 reg_step 为 0(所以就好像没有正则化),将新的 theta 0 存储在临时变量:t1 中,然后更新原始的 theta 值用我想要的 reg_step 并将 theta 0 替换为 t1(来自非正则化更新的值)。

以下是我第一次实现的代码,它并不意味着高级,我只是在练习: 我使用的是 1-index 的 octave,所以 theta(1) 是 theta(0)

function ret = gradient(X,Y,theta,reg_step),
  H = theta' * X;
  dif = H-Y;
  mul = dif .* X;
  total = sum(mul,2);
  m=(size(Y)(1,1));

  regular = (reg_step/m)*theta;
  regular(1)=0;

  ret = (total/m)+regular,
endfunction

提前致谢。

【问题讨论】:

  • 不清楚您的问题是什么。你能否请edit在帖子中包含一个实际问题?在英语中,问题是以问号 ? 结尾的句子,可以得到答案。这也有助于我们的志愿者了解问题所在以及您需要帮助的地方。参考How to Ask

标签: machine-learning linear-regression octave regularized


【解决方案1】:

对第一个实现稍作调整对我有用。

首先,计算每个 theta 的正则化。然后继续执行梯度步骤,稍后您可以手动更改包含梯度的矩阵的第一个条目以忽略 theta_0 的正则化。

% Calculate regularization
regularization = (reg_step / m) * theta;

% Gradient Step
gradients = (1 / m) * (X' * (predictions - y)) + regularization;

% Ignore regularization in theta_0 
gradients(1) = (1 / m) * (X(:, 1)' * (predictions - y));

【讨论】:

    猜你喜欢
    • 2014-01-14
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 2014-11-06
    相关资源
    最近更新 更多