Hello, I was trying to do gradient descent in SAS. Below is my Code. I got "Matrix has not been set to a value" error and it said the Matrix Jtheta_new has not been set to a value. I searched a lot but still can't fix it. Any idea? Thank you. options fullstimer;proc iml;
reset deflib=sashelp;
use class;
read all var {weight height} into me;
m = nrow(me);
*scaling feature;
s1 = max(me[,1])-min(me[,1]);
s2 = max(me[,2])-min(me[,2]);
mean_s1 = mean(me[,1]);
mean_s2 = mean(me[,2]);
me[,1] = (me[,1]-mean_s1)/s1;
me[,2] = (me[,2]-mean_s2)/s2;
*scaling feature;
theta_0 = 0;
theta_1 = 0;
x0 = 1;
ov = 10;
alpha = 0.03;
*print me;
rec = 0;
do while (ov>0.000000001);
theta_0Old = theta_0;
theta_1Old = theta_1;
*compute old residual and collect data to plot r*numOfIteration;
rec = rec + 1;
r2 = 0;
do i=1 to m;
residual_tt =(theta_0Old*x0 + theta_1Old*me[i,2]) - me[i,1];
r2 = r2+residual_tt*residual_tt;
end;
Jtheta = r2/2/m;
xy = xy//(rec||Jtheta);
*compute old residual and collect data to plot r*numOfIteration;
res = 0; res_1 = 0;
do i=1 to m;
residual_0 =(theta_0Old*x0 + theta_1Old*me[i,2]) - me[i,1];
res = res + (residual_0*x0);
res_1 = res_1 + (residual_0*me[i,2]);
end;
theta_0 = theta_0Old - alpha*res/m;
theta_1 = theta_1Old - alpha*res_1/m;
*update residual and decide whether it's convergence;
r2 = 0;
do i=1 to m;
residual_tt =(theta_0*x0 + theta_1*me[i,2]) - me[i,1];
r2 = r2+residual_tt*residual_tt;
end;
Jtheta_new = r2/2/m;
ov = abs(Jtheta_new - Jtheta);
*update residual and decide whether it's convergence;
end;
print ov;
call pgraf(xy,'*','x','y','mmmmm');
theta_0_last = theta_0*s1+mean_s1-mean_s2*s1*theta_1/s2;
theta_1_last = theta_1*s1/s2;
print theta_0_last theta_1_last;
run;
quit;
... View more