home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / b / binprolog / !BinPro330 / progs / backprop < prev    next >
Encoding:
Text File  |  1994-12-06  |  4.1 KB  |  229 lines

  1. p:-compile(backprop).
  2.  
  3. % Backproagation algorithm for Art. Neural Network training
  4. % (C) Paul Tarau 1993
  5.  
  6. go:-statistics(runtime,_),train,statistics(runtime,[_,T]),write(time=T),nl.
  7.  
  8. out_check(100).
  9.  
  10. % data
  11.     input(1,1,0.03). input(1,2,0.03).
  12.     input(2,1,0.97).   input(2,2,0.03).
  13.     input(3,1,0.03). input(3,2,0.97).
  14.     input(4,1,0.97).   input(4,2,0.97).
  15.  
  16.     output(1,1,0.03). output(1,2,0.03).
  17.     output(2,1,0.03). output(2,2,0.97).
  18.     output(3,1,0.03). output(3,2,0.97).
  19.     output(4,1,0.97).   output(4,2,0.03).
  20.     
  21. sum([],S0,S0):-!.
  22. sum([X|Xs],S1,S3):-
  23.     S2 is S1+X,
  24.     sum(Xs,S2,S3).
  25.  
  26. def2(N,I,J,X):-K is 5*I+J,bb_def(N,K,X).
  27. val2(N,I,J,X):-K is 5*I+J,val(N,K,X).
  28. put2(N,I,J,X):-K is 5*I+J,bb_set(N,K,X).
  29.  
  30. def_weight(I,J,K,X):-L is 3*I+J,bb_def(L,K,X).
  31. get_weight(I,J,K,X):-L is 3*I+J,val(L,K,X).
  32. put_weight(I,J,K,X):-L is 3*I+J,bb_set(L,K,X).
  33.  
  34. initialize :-
  35.     init_err_out
  36. ;    init_weight
  37. ;    true.
  38.  
  39. init_err_out:-
  40.     for(I,0,2),
  41.       for(J,0,2),
  42.         def2(err,I,J,0.0),
  43.         def2(out,I,J,0.97),
  44.     fail.
  45.  
  46. init_weight:-    
  47.     for(I,1,2),
  48.       for(J,1,2),
  49.         for(K,0,2),
  50.           random(100,R),Rand is R/100.0,
  51.           W is 0.2*Rand-0.1,
  52.           def_weight(I,J,K,W),
  53.     fail.
  54.           
  55. forward_pass(Row,Col):-
  56.     findall(O,forward_calc(Row,Col,O),Os),
  57.     sum(Os,0.0,Sum),NSum is -Sum,exp(NSum,Exp),
  58.         Out is 1.0/(1.0+Exp),
  59.     put2(out,Row,Col,Out),
  60.     put2(err,Row,Col,0.0).
  61.  
  62. forward_calc(Row,Col,Sum):-
  63.     Row1 is Row-1,
  64.     for(J,0,2),
  65.     val2(out,Row1,J,Out1),
  66.     get_weight(Row,Col,J,W),
  67.     Sum is W*Out1.
  68.  
  69. backward_data(Row,Col,Row1,OutFactor):-
  70.     Row1  is  Row-1,
  71.     val2(out,Row,Col,Out),
  72.     val2(err,Row,Col,Err),
  73.     OutFactor is Err*Out*(1-Out).
  74.  
  75. backward_calc(Row,Col,Row1,OutFactor):-
  76.     for(J,1,2),
  77.       val2(err,Row1,J,OldE),
  78.       get_weight(Row,Col,J,W),
  79.       NewE is OldE+OutFactor*W,
  80.       put2(err,Row1,J,NewE),
  81.     fail.
  82. backward_calc(Row,Col,Row1,OutFactor):-
  83.     for(J,0,2),
  84.       get_weight(Row,Col,J,OldW),
  85.       val2(out,Row1,J,Out1),
  86.       NewW is OldW+0.95*OutFactor*Out1,
  87.       put_weight(Row,Col,J,NewW),
  88.     fail.
  89. backward_calc(_Row,_Col,_Row1,_OutFactor).
  90.     
  91. backward_pass(Row,Col):-
  92.     backward_data(Row,Col,Row1,OutFactor),
  93.     backward_calc(Row,Col,Row1,OutFactor).
  94.  
  95. err_squared(Sqr):-
  96.     for(J,0,2),
  97.     val2(err,2,J,Err),
  98.     Sqr is Err*Err.
  99.  
  100. iterate(Step,ErrSqr):-
  101.     get_input(Step)
  102. ;    forward_pass
  103. ;    calculate_errors(Step)
  104. ;    backward_pass
  105. ;    final_error(ErrSqr).
  106.  
  107. get_input(Step):-
  108.     for(J,1,2),
  109.       input(Step,J,Output0),
  110.       put2(out,0,J,Output0),
  111.     fail.
  112.  
  113. forward_pass:-
  114.     for(Row,1,2),
  115.       for(Col,1,2),
  116.         forward_pass(Row,Col),
  117.     fail.
  118.  
  119. calculate_errors(Step):-
  120.     for(J,1,2),
  121.       output(Step,J,DesiredOut),
  122.       val2(out,2,J,Out),
  123.       NewErr is DesiredOut-Out,
  124.       put2(err,2,J,NewErr),
  125.     fail.
  126.  
  127. backward_pass:-
  128.     for(Row,-2,-1),
  129.       for(Col,1,2),
  130.         DownToRow is -Row,
  131.         backward_pass(DownToRow,Col),
  132.     fail.
  133.  
  134. final_error(ErrSqr):-
  135.     findall(E,err_squared(E),Es),
  136.     sum(Es,0.0,ErrSqr).
  137.  
  138. big_error(Iter,Sqr):-
  139.     for(Step,1,4),
  140.     iterate(Step,Sqr),
  141.     show_cells(Iter,Step,Sqr),
  142.     Sqr> 5.0e-3.
  143.  
  144.     
  145. learn :- 
  146.     for(Iter,0,32000),
  147.       findall(E,big_error(Iter,E),[]).
  148.  
  149. show_in_out:-
  150.     write('Input:'),nl,
  151.     for(I,1,4),
  152.       nl,
  153.       for(J,1,2),
  154.         input(I,J,Inp),
  155.         write(Inp),write(' '),
  156.     fail
  157. ;
  158.     nl,nl,write('Desired Output:'),nl,
  159.     for(I,1,4),
  160.       nl,
  161.       for(J,1,2),
  162.         output(I,J,Out),
  163.         write(Out),write(' '),
  164.     fail    
  165. ;
  166.     nl,nl,
  167.     write('SquaredError  Output'),
  168.     nl.
  169.  
  170. show_cells(Iter,Step,ErrSqr):-
  171.         out_check(When),
  172.         Iter mod When =:= 0,
  173.     show_cells1(Iter,Step,ErrSqr)
  174. ;
  175.         true.
  176.  
  177. show_cells1(Iter,Step,ErrSqr):-
  178.     (
  179.         N is Iter*4+Step,   
  180.         nl,write(N-[ErrSqr]),
  181.         print_cells
  182.     ;
  183.         Step =:= 4, nl
  184.     ),
  185.     fail.
  186.  
  187.  
  188. print_cells:-
  189.         for(J,1,2),
  190.           val2(out,2,J,Out),
  191.           write(' '),write(Out),
  192.         fail
  193. ;    true.
  194.  
  195. show_weights:-
  196.     nl,write('Final weights'),nl,
  197.     for(I,1,2),
  198.       for(J,1,2),
  199.         nl,
  200.         for(K,0,2),
  201.           get_weight(I,J,K,W),
  202.           write(W),write(' '),
  203.     fail
  204. ;     
  205.     nl.    
  206.  
  207. rtest:-for(_,1,30),random(20,R),write(R),nl,fail.
  208.  
  209.  
  210. random_init:-bb_let(rseed,rseed,43).
  211.  
  212. random(Max,R):-
  213.         val(rseed,rseed,X),!,
  214.         Y is (X*17+131) mod 31183,
  215.         % Y is (X*7+131) mod 331,
  216.         bb_set(rseed,rseed,Y),
  217.         R is Y mod Max.
  218.  
  219. %random(Max,R):-random(X), R is X mod Max.
  220.  
  221.  
  222. train:-    
  223.     random_init,
  224.     initialize,
  225.     show_in_out,
  226.     learn,
  227.     show_weights.
  228.  
  229.