How to fix gradient descent code?
古いコメントを表示
I am a novice trying to do a gradient descent with one variable, but cannot figure out how to fix my code (below). Not sure if my for-part is correct. This is the error message: "In an assignment A(:) = B, the number of elements in A and B must be the same." Please help?
data = load('data.txt' );
X = data(:, 1); y = data(:, 2);
m = length(y);
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters
num_iters = 1500;
alpha = 0.01;
J = computeCost(X, y, theta)
m = length(y);
J = sum(( X * theta - y ) .^2 )/( 2 * m );
[theta J_history] = gradientDescent(X, y, theta, alpha, num_iters)
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
h=(theta(1)+ theta(2)*X)';
theta(1) = theta(1) - alpha * (1/m) * h * X(:, 1);
theta(2) = theta(2) - alpha * (1/m) * h * X(:, 2);
% Save the cost J in every iteration
J_history(num_iters) = computeCost(X, y, theta);
end
2 件のコメント
Walter Roberson
2016 年 3 月 30 日
Please show the complete error message, everything in red.
Jackwhale
2016 年 3 月 30 日
採用された回答
その他の回答 (2 件)
I don't know why you use such a complicated approach.
Just execute
data = load('data.txt' );
A = [ones(length(data(:,1)),1), data(:,1)];
b = data(:,2);
theta = A \ b
to get your optimum theta values.
Best wishes
Torsten.
14 件のコメント
Torsten
2016 年 3 月 30 日
Your objective function is
f(theta)=(X*theta-y)'*(X*theta-y)/(2*m)
with
X = [ones(m, 1), data(:,1)] and y = data(:, 2).
The gradient descend method reads
theta(n+1)=theta(n)-alpha*grad(f(theta(n)))
Now determine grad(f(theta(n))) and iterate.
Note that the update formula for theta in your code from above is incorrect.
Best wishes
Torsten.
Jackwhale
2016 年 3 月 30 日
Torsten
2016 年 3 月 31 日
Please check whether this is correct:
data = load('data.txt' );
y = data(:,2);
m = length(y);
X = [ones(m,1), data(:,1)]; % Add a column of ones to x
num_iters = 1500;
alpha = 0.01;
J_history = zeros(num_iters, 1);
theta = zeros(2, 1); % initialize fitting parameters
J_history(1) = (X*theta-y)'*(X*theta-y)/(2*m);
for iter = 2:num_iters
theta = theta-alpha*X'*(X*theta-y)/m;
J_history(iter)=(X*theta-y)'*(X*theta-y)/(2*m);
end
Best wishes
Torsten.
Jackwhale
2016 年 3 月 31 日
Torsten
2016 年 3 月 31 日
And what does the above code give as J_history(1000) and theta for the first four iterations ?
Are the values different ?
Best wishes
Torsten.
Torsten
2016 年 3 月 31 日
Can't be true.
For theta=0, the expression
(X*theta-y)'*(X*theta-y)/(2*m)
evaluates to
[1 6 4 2]*[1 6 4 2]'/(2*4) = 57/8 = 7.125,
but not 4.5161.
Best wishes
Torsten.
Jackwhale
2016 年 3 月 31 日
Please run this code and output J_history(1000) and theta at the end.
y=[1; 6; 4; 2];
m = 4;
X = [1 5; 1 2; 1 4; 1 5];
num_iters = 1000;
alpha = 0.01;
J_history = zeros(num_iters, 1);
theta = zeros(2, 1); % initialize fitting parameters
J_history(1) = (X*theta-y)'*(X*theta-y)/(2*m);
for iter = 2:num_iters
theta = theta-alpha*X'*(X*theta-y)/m;
J_history(iter)=(X*theta-y)'*(X*theta-y)/(2*m);
end
Best wishes
Torsten.
Jackwhale
2016 年 3 月 31 日
Jackwhale
2016 年 3 月 31 日
Torsten
2016 年 3 月 31 日
You seem to have a strange MATLAB version.
If I set
num_iters=1001,
I get
theta =
5.2147549
- 0.5733459
J_history(1001)
ans =
0.8554026
thus the results expected.
Best wishes
Torsten.
Torsten
2016 年 3 月 31 日
I only need to supply the updates to theta within each iteration.
If you can't read from the code I supplied how theta is updated every iteration, then you should really start with MATLAB principles.
Agbakoba Chukwunoso
2020 年 12 月 6 日
0 投票
Pls help me out.. I'm trying to find gradientdescent with this code but when I run it, it returns gradientdescents to me not the value . data = load('ex1data1.txt'); % text file conatins 2 values in each row separated by commas X = [ones(m, 1), data(:,1)]; theta = zeros(2, 1); iterations = 1500; alpha = 0.01; function [theta, J_history] = gradientdescent(X, y, theta, alpha, num_iters) m = length(y); % number of training examples J_history = zeros(num_iters, 1); for iter = 1:num_iters k=1:m; j1=(1/m)*sum((theta(1)+theta(2).*X(k,2))-y(k)) j2=(1/m)*sum(((theta(1)+theta(2).*X(k,2))-y(k)).*X(k,2)) theta(1)=theta(1)-alpha*(j1); theta(2)=theta(2)-alpha*(j2); end end
2 件のコメント
Agbakoba Chukwunoso
2020 年 12 月 6 日
data = load('ex1data1.txt');
% text file conatins 2 values in each row separated by commas
X = [ones(m, 1), data(:,1)];
theta = zeros(2, 1);
iterations = 1500;
alpha = 0.01;
function [theta, J_history] = gradientdescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
k=1:m;
j1=(1/m)*sum((theta(1)+theta(2).*X(k,2))-y(k))
j2=(1/m)*sum(((theta(1)+theta(2).*X(k,2))-y(k)).*X(k,2))
theta(1)=theta(1)-alpha*(j1);
theta(2)=theta(2)-alpha*(j2);
end
end
sivarao K
2021 年 11 月 10 日
here 'y' not defined but it excuting how?
カテゴリ
ヘルプ センター および File Exchange で Mathematics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!