[英]Gradient descent not updating theta values
Using the vectorized version of gradient as described at : gradient descent seems to fail 如以下所述使用梯度的矢量化版本: 梯度下降似乎失败
theta = theta - (alpha/m * (X * theta-y)' * X)';
The theta values are not being updated, so whatever initial theta value this is the values that is set after running gradient descent : theta值不会被更新,因此无论初始theta值如何,这都是在运行梯度下降后设置的值:
example1 : example1:
m = 1
X = [1]
y = [0]
theta = 2
theta = theta - (alpha/m .* (X .* theta-y)' * X)'
theta =
2.0000
example2 : example2:
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
Is theta = theta - (alpha/m * (X * theta-y)' * X)';
是
theta = theta - (alpha/m * (X * theta-y)' * X)';
a correct vectorised implementation of gradient descent ? 梯度下降的正确矢量化实现?
theta = theta - (alpha/m * (X * theta-y)' * X)';
is indeed the correct vectorized implementation of gradient-descent. 确实是梯度下降的正确向量化实现。
You totally forgot to set the learning rate, alpha
. 您完全忘记了设置学习率
alpha
。
After setting alpha = 0.01
, your code becomes: 设置
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.