matlab - 梯度下降和闭合形式解 - MATLAB 中的不同假设线

标签 matlab machine-learning linear-regression gradient-descent

我正在编写我从 coursera 机器学习类(class) (MATLAB) 中学到的有关线性回归的知识。我发现了一个类似的帖子 here ,但我似乎无法理解所有内容。也许是因为我的机器学习基础有点薄弱。

我面临的问题是,对于某些数据......梯度下降(GD)和封闭形式解决方案(CFS)给出相同的假设线。然而,在一个特定的数据集上,结果是不同的。我读过一些内容,如果数据是单一的,那么结果应该是相同的。但是,我不知道如何检查我的数据是否单一。

我会尽力说明:

1) 首先,这是改编自 here 的 MATLAB 代码。对于给定的数据集,一切都很好,GD 和 CFS 都给出了相似的结果。

数据集

X                   Y
2.06587460000000    0.779189260000000
2.36840870000000    0.915967570000000
2.53999290000000    0.905383540000000
2.54208040000000    0.905661380000000
2.54907900000000    0.938988900000000
2.78668820000000    0.966847400000000
2.91168250000000    0.964368240000000
3.03562700000000    0.914459390000000
3.11466960000000    0.939339440000000
3.15823890000000    0.960749710000000
3.32759440000000    0.898370940000000
3.37931650000000    0.912097390000000
3.41220060000000    0.942384990000000
3.42158230000000    0.966245780000000
3.53157320000000    1.05265000000000
3.63930020000000    1.01437910000000
3.67325370000000    0.959694260000000
3.92564620000000    0.968537160000000
4.04986460000000    1.07660650000000
4.24833480000000    1.14549780000000
4.34400520000000    1.03406250000000
4.38265310000000    1.00700090000000
4.42306020000000    0.966836480000000
4.61024430000000    1.08959190000000
4.68811830000000    1.06344620000000
4.97773330000000    1.12372390000000
5.03599670000000    1.03233740000000
5.06845360000000    1.08744520000000
5.41614910000000    1.07029880000000
5.43956230000000    1.16064930000000
5.45632070000000    1.07780370000000
5.56984580000000    1.10697580000000
5.60157290000000    1.09718750000000
5.68776170000000    1.16486030000000
5.72156020000000    1.14117960000000
5.85389140000000    1.08441560000000
6.19780260000000    1.12524930000000
6.35109410000000    1.11683410000000
6.47970330000000    1.19707890000000
6.73837910000000    1.20694620000000
6.86376860000000    1.12510460000000
7.02233870000000    1.12356720000000
7.07823730000000    1.21328290000000
7.15142320000000    1.25226520000000
7.46640230000000    1.24970650000000
7.59738740000000    1.17997060000000
7.74407170000000    1.18972990000000
7.77296620000000    1.30299340000000
7.82645140000000    1.26011340000000
7.93063560000000    1.25622670000000

我的 MATLAB 代码:

clear all; close all; clc; 
x = load('ex2x.dat');
y = load('ex2y.dat');

m = length(y); % number of training examples

% Plot the training data
figure; % open a new figure window
plot(x, y, '*r');
ylabel('Height in meters')
xlabel('Age in years')

% Gradient descent
x = [ones(m, 1) x]; % Add a column of ones to x
theta = zeros(size(x(1,:)))'; % initialize fitting parameters
MAX_ITR = 1500;
alpha = 0.07;

for num_iterations = 1:MAX_ITR

    thetax = x * theta;
    % for theta_0 and x_0
    grad0 = (1/m) .* sum( x(:,1)' * (thetax - y));
    % for theta_0 and x_0
    grad1 = (1/m) .* sum( x(:,2)' * (thetax - y));

    % Here is the actual update
    theta(1) = theta(1) - alpha .* grad0;
    theta(2) = theta(2) - alpha .* grad1;
end
% print theta to screen
theta

% Plot the hypothesis (a.k.a. linear fit)
hold on
plot(x(:,2), x*theta, 'ob')
% Plot using the Closed Form Solution
plot(x(:,2), x*((x' * x)\x' * y), '--r')
legend('Training data', 'Linear regression', 'Closed Form')
hold off % don't overlay any more plots on this figure''

[编辑:抱歉标签错误...这不是正规方程,而是闭式解。我的错] 这段代码的结果如下所示(这是桃色的:D GD 和 CFS 的结果相同)- Same results for Gradient Descent and Closed Form Solution

  • 现在,我正在使用另一个数据集测试我的代码。数据集的 URL 是 here - GREY KANGAROOS。我将其转换为 CSV 并将其读入 MATLAB。请注意,我进行了缩放(除以最大值,因为如果我不这样做,则根本不会出现假设线,并且 MATLAB 中的 theta 会显示为非数字 (NaN))。
  • 灰袋鼠数据集:

    X    Y
    609 241
    629 222
    620 233
    564 207
    645 247
    493 189
    606 226
    660 240
    630 215
    672 231
    778 263
    616 220
    727 271
    810 284
    778 279
    823 272
    755 268
    710 278
    701 238
    803 255
    855 308
    838 281
    830 288
    864 306
    635 236
    565 204
    562 216
    580 225
    596 220
    597 219
    636 201
    559 213
    615 228
    740 234
    677 237
    675 217
    629 211
    692 238
    710 221
    730 281
    763 292
    686 251
    717 231
    737 275
    816 275
    

    我对此数据集中读取的代码所做的更改

        dataset = load('kangaroo.csv');
        % scale?
        x = dataset(:,1)/max(dataset(:,1));
        y = dataset(:,2)/max(dataset(:,2));
    

    结果是这样的:[编辑:抱歉标签错误...这不是正规方程,而是闭式解。我的错误]

    Different results for GD and CFS

    我想知道对于这种差异是否有任何解释?任何帮助将不胜感激。预先感谢您!

    最佳答案

    我还没有运行你的代码,但让我给你一些理论:

    如果您的代码是正确的(看起来像这样):增加 MAX_ITER ,它看起来会更好。

    梯度下降不能保证在MAX_ITER处收敛,实际上梯度下降是一种相当慢的方法(收敛方面)。

    “标准”凸函数(就像您尝试解决的函数)的梯度下降的收敛看起来像这样(来自互联网):

    https://spin.atomicobject.com/wp-content/uploads/gradient_descent_error_by_iteration.png

    忘记迭代次数,因为它取决于问题,而专注于形状。可能发生的情况是,您的 maxiter 落在该图像中类似“20”的位置。因此你的结果很好,但不是最好的!

    但是,直接求解正规方程将给出最小平方误差解。 (我假设正规方程你的意思是x=(A'*A)^(-1)*A'*b)。问题是,在很多情况下,您无法将A存储在内存中,或者在不适定问题中,正规方程将导致不适-条件矩阵在数值上不稳定,因此使用梯度下降。

    more info

    关于matlab - 梯度下降和闭合形式解 - MATLAB 中的不同假设线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40546064/

    相关文章:

    linux - 从 Matlab 启动 Linux 终端

    machine-learning - 在 Weka 中对单实例进行分类

    java - Swiftkey 就像 Java 中的文本预测(下一个单词预测)

    r - 直线和水平线在断点处连接的分段回归

    python - python中用于计算最小范数解或从伪逆获得的解的最准确方法是什么?

    Matlab - 根据条件选择特定行

    matlab - 如何在 MATLAB 中对 2 个数字进行 AND 运算

    python - 通过Python接口(interface)在Matlab中导入Tensorflow

    python - SciKit Gradient Boosting - 如何将预测与初始表结合起来?

    python - 对 Python 上的每个系数具有特定约束的多元线性回归