梯度下降不更新 theta 值

Gradient descent not updating theta values

使用渐变的矢量化版本,如下所述: gradient descent seems to fail

theta = theta - (alpha/m *  (X * theta-y)' * X)';

theta 值未更新,因此无论初始 theta 值是多少 这是 运行 梯度下降后设置的值:

示例 1:

m = 1
X = [1]
y = [0]
theta = 2
theta = theta - (alpha/m .* (X .* theta-y)' * X)'

theta =

    2.0000

示例 2:

m = 1
X = [1;1;1]
y = [1;0;1]
theta = [1;2;3]
theta = theta - (alpha/m .* (X .* theta-y)' * X)'

theta =

    1.0000
    2.0000
    3.0000

theta = theta - (alpha/m * (X * theta-y)' * X)'; 是梯度下降的正确向量化实现吗?

theta = theta - (alpha/m * (X * theta-y)' * X)';确实是梯度下降的正确向量化实现。

你完全忘了设置学习率,alpha

设置alpha = 0.01后,您的代码变为:

m = 1                # number of training examples
X = [1;1;1]
y = [1;0;1]
theta = [1;2;3]
alpha = 0.01
theta = theta - (alpha/m .* (X .* theta-y)' * X)'
theta =

   0.96000
   1.96000
   2.96000