home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / ai / neuraln / 4636 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  8.4 KB

  1. Path: sparky!uunet!munnari.oz.au!bunyip.cc.uq.oz.au!topaz.ucq.edu.au!hannahp
  2. From: hannahp@topaz.ucq.edu.au
  3. Newsgroups: comp.ai.neural-nets
  4. Subject: Problems with Ada back-prop code, any pointers?
  5. Message-ID: <1992Dec22.232624.13139@topaz.ucq.edu.au>
  6. Date: 22 Dec 92 23:26:23 AET
  7. Organization: University of Central Queensland, Australia
  8. Lines: 342
  9.  
  10. I have tried to produce a back-prop package in Ada and have arrived
  11. at the following code.  Needless to say, it doesnt work!  Is there any 
  12. NN guru out there who might have any idea what I have done wrong/left out?
  13.  
  14. Any ideas much appreciated!!!
  15.  
  16.  
  17. Thanks.
  18. Paul Hannah.
  19.  
  20.  
  21. -------------------------------
  22.  
  23. generic
  24.  
  25.    input_nodes,
  26.    hidden1_nodes,
  27.    hidden2_nodes,
  28.    output_nodes  : positive;
  29.  
  30. package network is
  31.  
  32.    type input_array   is array (1..input_nodes)   of float;
  33.    type hidden1_array is array (1..hidden1_nodes) of float;
  34.    type hidden2_array is array (1..hidden2_nodes) of float;
  35.    type output_array  is array (1..output_nodes)  of float;
  36.  
  37.  
  38.    function learn (input_state : in input_array;
  39.                    ideal_output_state : in output_array)
  40.       return output_array;
  41.  
  42.    procedure learn (input_state : in input_array;
  43.                     ideal_output_state : in output_array);
  44.  
  45.    function recall (input_state : in input_array)
  46.       return output_array;
  47.  
  48.    procedure reset;
  49.  
  50. end network;
  51.  
  52.  
  53.  
  54. with math_lib; use math_lib;
  55.  
  56. package activation_functions is
  57.  
  58.    function sigmoid (x : float) return float;
  59.  
  60. end activation_functions;
  61.  
  62. package body activation_functions is
  63.  
  64.    function sigmoid (x : float) return float is
  65.    begin
  66.       return 1.0/(1.0+exp(-x/4.0));
  67.    end sigmoid;
  68.  
  69. end activation_functions;
  70.  
  71.  
  72.  
  73.  
  74.  
  75. with activation_functions;
  76. use activation_functions;
  77.  
  78. package body network is
  79.  
  80.    weight_1 : array (1..hidden1_nodes,1..input_nodes) of float
  81.       := (others => (others => 1.0));
  82.    weight_2 : array (1..hidden2_nodes,1..hidden1_nodes) of float
  83.       := (others => (others => 1.0));
  84.    weight_O : array (1..output_nodes,1..hidden2_nodes) of float
  85.       := (others => (others => 1.0));
  86.  
  87.    threshold_1 : array (1..hidden1_nodes) of float
  88.       := (others => 0.0);
  89.    threshold_2 : array (1..hidden2_nodes) of float
  90.       := (others => 0.0);
  91.    threshold_O : array (1..output_nodes) of float
  92.       := (others => 0.0);
  93.  
  94.    hidden1_state : hidden1_array;
  95.    hidden2_state : hidden2_array;
  96.  
  97.  
  98.    function learn (input_state : in input_array;
  99.                    ideal_output_state : in output_array)
  100.       return output_array is
  101.  
  102.       delta_O : output_array;
  103.       delta_2 : hidden2_array;
  104.       delta_1 : hidden1_array;
  105.  
  106.       neta : float := 0.8;
  107.       alpha : float := 0.2;
  108.  
  109.       output_state : output_array := recall (input_state);
  110.  
  111.    begin
  112.       for i in output_state'range loop
  113.          delta_O(i) := (ideal_output_state(i)-output_state(i))*
  114.                      output_state(i)*
  115.                      (1.0-output_state(i));
  116.       end loop;
  117.  
  118.       for i in weight_O'range(1) loop
  119.          for j in weight_O'range(2) loop
  120.             weight_O(i,j) := weight_O(i,j)+
  121.                              neta*
  122.                              delta_O(i)*
  123.                              hidden2_state(j);
  124.          end loop;
  125.       end loop;
  126.  
  127.  
  128.  
  129.  
  130.       for i in hidden2_state'range loop
  131.          declare
  132.             sum : float:=0.0;
  133.          begin
  134.             for j in output_state'range loop
  135.                sum := delta_O(j)*weight_O(j,i);
  136.             end loop;
  137.                delta_2(i) :=  hidden2_state(i)*
  138.                              (1.0-hidden2_state(i))*
  139.                               sum;
  140.          end;
  141.       end loop;
  142.  
  143.       for i in weight_2'range(1) loop
  144.          for j in weight_2'range(2) loop
  145.             weight_2(i,j) := weight_2(i,j)+
  146.                              neta*
  147.                              delta_2(i)*
  148.                              hidden1_state(j);
  149.          end loop;
  150.       end loop;
  151.  
  152.  
  153.  
  154.  
  155.  
  156.       for i in hidden1_state'range loop
  157.          declare
  158.             sum : float:=0.0;
  159.          begin
  160.             for j in hidden2_state'range loop
  161.                sum := delta_2(j)*weight_2(j,i);
  162.             end loop;
  163.                delta_1(i) :=  hidden1_state(i)*
  164.                              (1.0-hidden1_state(i))*
  165.                               sum;
  166.          end;
  167.       end loop;
  168.  
  169.       for i in weight_1'range(1) loop
  170.          for j in weight_1'range(2) loop
  171.             weight_1(i,j) := weight_1(i,j)+
  172.                              neta*
  173.                              delta_1(i)*
  174.                              input_state(j);
  175.          end loop;
  176.       end loop;
  177.  
  178.  
  179.       return output_state;
  180.    end learn;
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.    procedure learn (input_state : in input_array;
  197.                     ideal_output_state : in output_array) is
  198.  
  199.       delta_O : output_array;
  200.       delta_2 : hidden2_array;
  201.       delta_1 : hidden1_array;
  202.  
  203.       neta : float := 0.4;
  204.       alpha : float := 0.2;
  205.  
  206.       output_state : output_array := recall (input_state);
  207.  
  208.    begin
  209.       for i in output_state'range loop
  210.          delta_O(i) := (ideal_output_state(i)-output_state(i))*
  211.                      output_state(i)*
  212.                      (1.0-output_state(i));
  213.       end loop;
  214.  
  215.       for i in weight_O'range(1) loop
  216.          for j in weight_O'range(2) loop
  217.             weight_O(i,j) := weight_O(i,j)+
  218.                              neta*
  219.                              delta_O(i)*
  220.                              hidden2_state(j);
  221.          end loop;
  222.       end loop;
  223.  
  224.  
  225.  
  226.  
  227.       for i in hidden2_state'range loop
  228.          declare
  229.             sum : float:=0.0;
  230.          begin
  231.             for j in output_state'range loop
  232.                sum := delta_O(j)*weight_O(j,i);
  233.             end loop;
  234.                delta_2(i) :=  hidden2_state(i)*
  235.                              (1.0-hidden2_state(i))*
  236.                               sum;
  237.          end;
  238.       end loop;
  239.  
  240.       for i in weight_2'range(1) loop
  241.          for j in weight_2'range(2) loop
  242.             weight_2(i,j) := weight_2(i,j)+
  243.                              neta*
  244.                              delta_2(i)*
  245.                              hidden1_state(j);
  246.          end loop;
  247.       end loop;
  248.  
  249.  
  250.  
  251.  
  252.  
  253.       for i in hidden1_state'range loop
  254.          declare
  255.             sum : float:=0.0;
  256.          begin
  257.             for j in hidden2_state'range loop
  258.                sum := delta_2(j)*weight_2(j,i);
  259.             end loop;
  260.                delta_1(i) :=  hidden1_state(i)*
  261.                              (1.0-hidden1_state(i))*
  262.                               sum;
  263.          end;
  264.       end loop;
  265.  
  266.       for i in weight_1'range(1) loop
  267.          for j in weight_1'range(2) loop
  268.             weight_1(i,j) := weight_1(i,j)+
  269.                              neta*
  270.                              delta_1(i)*
  271.                              input_state(j);
  272.          end loop;
  273.       end loop;
  274.  
  275.    end learn;
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.    function recall (input_state : in input_array)
  287.       return output_array is
  288.  
  289.       output_state : output_array;
  290.  
  291.    begin
  292.       hidden1_state := (others => 0.0);
  293.       for i in weight_1'range(1) loop
  294.          for j in weight_1'range(2) loop
  295.             hidden1_state(i) := hidden1_state(i)+
  296.                                 weight_1(i,j)*
  297.                                 input_state(j);
  298.          end loop;
  299.       end loop;
  300.  
  301.       for i in hidden1_state'range loop
  302.          hidden1_state(i):=sigmoid(hidden1_state(i)-threshold_1(i));
  303.       end loop;
  304.  
  305.  
  306.       hidden2_state := (others => 0.0);
  307.       for i in weight_2'range(1) loop
  308.          for j in weight_2'range(2) loop
  309.             hidden2_state(i) := hidden2_state(i)+
  310.                                 weight_2(i,j)*
  311.                                 hidden1_state(j);
  312.          end loop;
  313.       end loop;
  314.  
  315.       for i in hidden2_state'range loop
  316.          hidden2_state(i):=sigmoid(hidden2_state(i)-threshold_2(i));
  317.       end loop;
  318.  
  319.       output_state := (others => 0.0);
  320.       for i in weight_O'range(1) loop
  321.          for j in weight_O'range(2) loop
  322.             output_state(i) := output_state(i)+
  323.                                weight_O(i,j)*
  324.                                hidden2_state(j);
  325.          end loop;
  326.       end loop;
  327.       for i in output_state'range loop
  328.          output_state(i):=sigmoid(output_state(i)-threshold_O(i));
  329.       end loop;
  330.  
  331.       return output_state;
  332.  
  333.    end recall;
  334.  
  335.    procedure reset is
  336.    begin
  337.       weight_1    := (others => (others => 0.0));
  338.       weight_2    := (others => (others => 0.0));
  339.       weight_O    := (others => (others => 0.0));
  340.  
  341.       threshold_1 := (others => 0.0);
  342.       threshold_2 := (others => 0.0);
  343.       threshold_O := (others => 0.0);
  344.    end reset;
  345.  
  346. begin
  347.  
  348.    reset;
  349.    null; -- create the network.
  350.  
  351. end network;
  352.