/[marvin]/marvin/src/libnn/initCommonVisitor.ml
ViewVC logotype

Diff of /marvin/src/libnn/initCommonVisitor.ml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by matthieu, Thu Jul 17 21:30:20 2003 UTC revision 1.7 by srv89, Fri Jul 18 01:23:15 2003 UTC
# Line 43  Line 43 
43    @since 10/08/2003    @since 10/08/2003
44  *)  *)
45    
 (**  
   Needed for random number generation.  
 *)  
46  open Random  open Random
47    
48  open Nn  open Nn
# Line 53  open InitVisitor Line 50  open InitVisitor
50  open DefaultVisitor  open DefaultVisitor
51  open CommonNN  open CommonNN
52    
53    
54    (**
55      Initializes the output activation.
56    *)
57    let initOutputActivation network =
58      let outputActivation = Array.make network#getLayerNb [||] in
59        for i = 0 to network#getLayerNb - 1 do
60          outputActivation.(i) <- Array.make (network#getNeuronsPerLayer i) 0.0
61        done;
62        network#setOutputActivation outputActivation
63          
64    (**
65      Initializes the the input sum.
66    *)
67    let initInputSum network =
68      let inputSum = Array.make network#getLayerNb [||] in
69        for i = 0 to network#getLayerNb - 1 do
70          inputSum.(i) <- Array.create (network#getNeuronsPerLayer i) 0.0
71        done;
72        network#setInputSum inputSum
73          
74    (**
75      Initializes the error.
76    *)
77    let initError network =
78      let error = Array.make network#getLayerNb [||] in
79        for i = 0 to network#getLayerNb - 1 do
80          error.(i) <- Array.make (network#getNeuronsPerLayer i) 0.0
81        done;
82        network#setError error
83          
84    (**
85      Initializes the weights (using random numbers).
86    *)
87    let initWeights network =
88      let weights = Array.make (network#getLayerNb - 1) [|[||]|] in
89        for i = 0 to network#getLayerNb - 2 do
90          weights.(i) <- Array.make (network#getNeuronsPerLayer i) [||];
91          for j = 0 to (network#getNeuronsPerLayer i) - 1 do
92            weights.(i).(j) <- Array.make (network#getNeuronsPerLayer (i + 1)) (Random.float (Env.getEnv())#getRandLimit)
93          done;
94        done;
95        network#setWeights weights
96          
97    (**
98      Initializes the gradients.
99      *)
100    let initGradients network =
101      let gradients = Array.make (network#getLayerNb - 1) [|[||]|] in
102        for i = 0 to network#getLayerNb - 2 do
103          gradients.(i) <- Array.create (network#getNeuronsPerLayer i) [||];
104          for j = 0 to (network#getNeuronsPerLayer i) - 1 do
105            gradients.(i).(j) <- Array.create (network#getNeuronsPerLayer (i + 1)) 0.0
106          done;
107        done;
108        network#setGradients gradients
109          
110          
111          
112          
113  class initCommonVisitor =  class initCommonVisitor =
114  object  object
115    inherit [commonNN] initVisitor    inherit [commonNN] initVisitor
116    
117    (**    (**
118      The method which initializes the networks.      The method which initializes the networks.
119      It uses the random      It uses the Random module to initialize the weights.
120      @see      @see
121      <http://caml.inria.fr/oreilly-book/html/book-ora076.html>      <http://caml.inria.fr/oreilly-book/html/book-ora076.html>
122      Using the Random module (provided in the standard OCaml      Using the Random module (provided in the standard OCaml
# Line 68  object Line 125  object
125      Used to initialize the random number generator.      Used to initialize the random number generator.
126    *)    *)
127    method visitCommon (network : commonNN) =    method visitCommon (network : commonNN) =
128      let outputActivation = Array.create network#getLayerNb [||] and      
129        inputSum = Array.create network#getLayerNb [||] and      (** Initialize the random number generator. *)
130        error = Array.create network#getLayerNb [||] and      Random.self_init();
131        weights = Array.create (network#getLayerNb - 1) [|[||]|] and  
132        gradients = Array.create (network#getLayerNb - 1) [|[||]|] in      (** Initialize everything *)
133        begin      initOutputActivation network;
134          Random.self_init();      initInputSum network;
135          (**      initError network;
136            Initialization of the 2 dimensions arrays      initGradients network;
137          *)      initWeights network
         for i = 0 to network#getLayerNb - 1 do  
           outputActivation.(i) <- Array.create (network#getNeuronsPerLayer i) 0.0;  
           inputSum.(i) <- Array.create (network#getNeuronsPerLayer i) 0.0;  
           error.(i) <- Array.create (network#getNeuronsPerLayer i) 0.0;  
         done;  
         (**  
           Initialization of the 3 dimensions arrays  
         *)  
         for i = 0 to network#getLayerNb - 2 do  
           weights.(i) <- Array.create (network#getNeuronsPerLayer i) [||];  
           gradients.(i) <- Array.create (network#getNeuronsPerLayer i) [||];  
           for j = 0 to (network#getNeuronsPerLayer i) - 1 do  
             weights.(i).(j) <- Array.create (network#getNeuronsPerLayer (i + 1)) (Random.float (Env.getEnv())#getRandLimit);  
             gradients.(i).(j) <- Array.create (network#getNeuronsPerLayer (i + 1)) 0.0  
           done  
         done;  
         network#setOutputActivation outputActivation;  
         network#setInputSum inputSum;  
         network#setError error;  
         network#setWeights weights;  
         network#setGradients gradients  
       end  
138  end  end

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26