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;
Welcome to the SAS Community 🙂
The code works fine on my site and gives a seemingly usable result?
Remember however, that you do not want the Run Statement at the bottom. The Quit statement is enough.
Welcome to the SAS Community 🙂
The code works fine on my site and gives a seemingly usable result?
Remember however, that you do not want the Run Statement at the bottom. The Quit statement is enough.
Thank you so much! It seems I misspelled a variable name in SAS. LOL. Also thank you for your suggestion about the run statement.
Anytime. Glad you found your answer 🙂
If I may offer two suggestions:
1. Do not use RESET deflib=Sashelp;
If you try to write a data set you will get an error message the says you do not have write permission for that library.
Instead, keep RESET deflib=WORK (the default) and use the two-level data set name when you want to read data. For example,
use sashelp.class;
2. The PGRAF routine produces ancient line-plot output. Use the newer ODS graphics, such as
title 'mmmmm';
call series(xy[,1],xy[,2]) label={'x' 'y'} ;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.