loop causes enlargement of array
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, my theta vector 2X1 two values needs to be updated each itteration. instead i get theta to be size 1000 (as the number of the itterations)
Why my theta updating line causes theta to inflate?
Thanks
n=1000;
t=1.4;
sigma_R = t*0.001;
min_value_t = t-sigma_R;
max_value_t = t+sigma_R;
y_data = min_value_t + (max_value_t - min_value_t) * rand(n,1);
x_data=[1:1000];
L=0.0001; %learning rate
%plot(x_data,y_data);
itter=1000;
theta_0=0;
theta_1=0;
theta=[theta_0;theta_1];
itter=1000;
for i=1:itter
onss=ones(1,1000);
x_mat=[onss;x_data]';
pred=x_mat*theta;
residuals = (pred-y_data)';
theta_0=theta_0-((x_data.*residuals)*(L/n));
theta_1=theta_1-((x_data.*residuals)*(L/n));
theta=[theta_0,theta_1];
end
0 件のコメント
採用された回答
Star Strider
2020 年 4 月 13 日
The ‘x_data’ and ‘residuals’ variables are both (1x1000) vectors.
Subscripting them is likely the solution, although you need to decide that:
theta_0=theta_0-((x_data(i).*residuals(i))*(L/n));
theta_1=theta_1-((x_data(i).*residuals(i))*(L/n));
It may also be necessary to do this with other variables in the loop. You need to decide that as well.
.
2 件のコメント
Star Strider
2020 年 4 月 14 日
One problem is that ‘x’ is (1x1000). Another problem is that ‘(hypothesis-y)’ is a (1000x1000) matrix, so the sum is going to be a (1x1000) row vector:
temp0=theta(1) - Learning_step_a * (1/m)* sum((hypothesis-y).* x);
The same soirt of problem occurs in the next line:
temp1=theta(2) - Learning_step_a * (1/m) *sum(hypothesis-y);
so when you try to assign them to a scalar:
theta(1)=temp0;
theta(2)=temp1;
your code throws the error.
If you want to assign them to a scalar, it would be best to calculate:
sum((hypothesis-y)
in a separate step, then refer to the individual element of it in the ‘theta’ assignments.
Either that, or make ‘theta’ row vectors:
theta(1,:)=temp0;
theta(2,:)=temp1;
It all depends on what you want your code to do.
.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!