/**********     Neural Network Model  -- BPN      ****************
Author: Yoon Jae Ho
        mailto:yoon@bh.kyungpook.ac.kr
        http://ie.korea.ac.kr/~supercom/
Date:   31 July 2000
Ref : JAMES A. FREEMAN
  --- Simulating Neural Networks  with Mathematica,
      Addison-Wesley publishing co. 1992 ISBN 0-201-56629-X
This code is supplied gratis, without performance warrantee.
******************************************************************/
                                                                                                                                                                                         /*  9 - 3 - 1 BPN  */

new;
library pgraph;
graphset;

format 8,7;

/* Initial parameter */
i = 1;
n = 1000;                                                                                                                                                            /* Iteration number           */
tol = 0.000000001;                                                                                                                                               /* tolerance number         */
eta = 0.5 ;                                                                                                                                                           /* learning rate parameter */
heta = 0.5 ;

/* Input the data */
io =  (0.9~0.9~0.9~0.9~0.1~0.1~0.9~0.9~0.9) |                                                                                                          /* 8 X 9 matrics */
      (0.9~0.9~0.9~0.1~0.9~0.1~0.1~0.9~0.1) |
      (0.9~0.9~0.9~0.9~0.1~0.9~0.9~0.1~0.9) |
      (0.1~0.1~0.9~0.9~0.9~0.9~0.1~0.1~0.9) |
      (0.9~0.9~0.9~0.1~0.1~0.9~0.9~0.9~0.9) |
      (0.1~0.9~0.1~0.1~0.9~0.1~0.9~0.9~0.9) |
      (0.9~0.1~0.9~0.9~0.1~0.9~0.9~0.9~0.9) |
      (0.9~0.1~0.1~0.9~0.9~0.9~0.9~0.1~0.1) ;

outdesired = (0.1~0.9~0.1~0.9~0.1~0.9~0.1~0.9) ;                                                                                                      /*  1 x 8 matrics */

/* load io[.,.] = c:\gauss\yoon\nn1.da1 ;  */
/* load outdesired[.,.]=c:\gauss\yoon\nn1.da2 ;  */

t = rows(io) ;
innum = cols(io) ;
hidnum = 3;
outnum = rows(outdesired) ;


output file=c:\gauss\yoon\nn1.out reset;
"Start Time" ;
time ;

"Neural Network Model  -- BPN  Input-Hidden-Output " ;
innum~hidnum~outnum ;

hidwt = rndn(hidnum,innum);                                                                                                                                                     /* 3 X 9 matrics */
outwt = rndn(outnum,hidnum);                                                                                                                                                    /* 1 X 3 matrics */
mset = zeros(n,1);


outt=zeros(t,1) ;
outerror = outdesired - outt ;
mse = (1/t) * sumc(outerror' ^2) ;


do until i >n  or  abs(mse)  < tol ;


hidout = f(hidwt * io') ;                                                                                                                                              /* 3X9  9X8 matrics */
outt = f(outwt * hidout) ;                                                                                                                                          /*  1X3  3X8 matrics */

outerror = outdesired - outt ;
mse = (1/t) * sumc(outerror' ^2) ;
mset[i,1] =mse[1,1] ;

outdelta = outerror .* (outt .* ( 1- outt)) ;                                                                                                                   /* 1X8 1X8 1X8 matrics */
hiddelta = (hidout .*(1-hidout)) .* (outwt' *outdelta ) ;                                                                                                  /* 3X8 3X8 3X1 1X8 matrics */

outwt =  outwt + eta * (outdelta * hidout' ) ;                                                                                                              /* 1X3    1X8 8X3 matrics */
hidwt = hidwt + heta * (hiddelta * io) ;                                                                                                                      /* 3X9  3X8 8X9 matircs */

                                                                                                                                                                           /* 3X9 matrics */
i = i + 1;
endo;

"Stop Time" ;
time ;
"Hidwts " ;
hidwt;
"Outwts";
outwt;

format 16,8;

"MSE" ;
mse;

output off;

xy(seqa(1,1,n),mset);

end;

proc f(x) ;
    local z;
    z =1/( 1+exp(-x) );
        retp(z);
endp;

