Help for linear regression

Hi friends, I have a problem in finding the regression by gradient descent. I coded it.
My code is as shown;
x=[x1 x2 x3 x4 x5];
y=realoutput;
alpha=0.001;
m=length(y);
n=5;
theta_vec=zeros(5,1);
error=zeros(5,1);
itr=100;
for max_iter=1:itr
for j=1:n
sum_theta_vec=0;
for i=1:m
h_theta_v=0;
for inner_j=1:n
h_theta=(x.^i)*theta_vec;
h_theta_v=(h_theta * ones(1,n)).';
y_v= (y * ones(1,n)).';
end
sum_theta_vec= (sum_theta_vec + ((h_theta_v - y_v(i)) * x(:,inner_j).^i));
end
theta_vec=theta_vec(j) - (alpha* 1/m * sum_theta_vec);
end
theta_vec
end
But theta_vec occurs as NaN. Why? Can anybody help me?
Thank you very much.

5 件のコメント

Walter Roberson
Walter Roberson 2012 年 5 月 20 日
At first look this Question appears to be a continuation of the existing (and still active) Question http://www.mathworks.com/matlabcentral/answers/38831-error-in-gradient-descent-algorithm-code
If it is then you should edit this current information into the existing Question and then delete this one.
b
b 2012 年 5 月 20 日
Ok, i will delete that one, not this one.
Walter Roberson
Walter Roberson 2012 年 5 月 20 日
What is the purpose of your "for inner_j=1:n" ? That loop appears to just do the same thing each time through.
b
b 2012 年 5 月 20 日
"inner_j=1:n" means that x values in the last equation take all rows and 1 column in for loop.
Walter Roberson
Walter Roberson 2012 年 5 月 21 日
You did not code an assignment for "inner_j": you coded a "for" loop. The "for" loop ends after the calculation of y_v, and everything in that loop will be done "n" times. After the loop is finished, the loop index variable will have the value it was last assigned, so inner_j will be "n" not 1:n .

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 20 日

0 投票

You have m=length(y) . If y is empty then m would be 0. Then when you calculate theta_vec you have 1/m * sum_theta_vec and sum_theta_vec would be 0 because the "for i=1:m" loop would execute 0 times when m is 0. 1/0 * 0 will give you NaN.

その他の回答 (1 件)

b
b 2012 年 5 月 20 日

0 投票

The only thing is that; in the equation
sum_theta_vec= (sum_theta_vec + ((h_theta_v - y_v(i)) * x(:,inner_j).^i));
i want x to take different columns in each loop. for that purpose, i used inner_j.
But i think it can not take. How can i regulate this loop?

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by