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 :
example1 :
m = 1
X = [1]
y = [0]
theta = 2
theta = theta - (alpha/m .* (X .* theta-y)' * X)'
theta =
2.0000
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)';
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
.
After setting alpha = 0.01
, your code becomes:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.