1 |
(****************************************************************** |
(****************************************************************** |
2 |
[LibNN - Neural Networks Library] |
[LibNN - Neural Networks Library] |
3 |
http://libnn.org |
http://libnn.org |
4 |
Copyright (C) 2002 - 2003 LAGACHERIE Matthieu RICORDEAU Olivier |
Copyright (C) 2002 - 2003 LAGACHERIE Matthieu RICORDEAU Olivier |
5 |
|
|
6 |
This program is free software; you can redistribute it and/or |
This program is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU General Public License |
modify it under the terms of the GNU General Public License |
8 |
as published by the Free Software Foundation; either version 2 |
as published by the Free Software Foundation; either version 2 |
9 |
of the License, or (at your option) any later version. This |
of the License, or (at your option) any later version. This |
10 |
program is distributed in the hope that it will be useful, |
program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. You should have |
GNU General Public License for more details. You should have |
14 |
received a copy of the GNU General Public License |
received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
17 |
USA. |
USA. |
18 |
|
|
19 |
SPECIAL NOTE (the beerware clause): |
SPECIAL NOTE (the beerware clause): |
20 |
This software is free software. However, it also falls under the beerware |
This software is free software. However, it also falls under the beerware |
21 |
special category. That is, if you find this software useful, or use it |
special category. That is, if you find this software useful, or use it |
22 |
every day, or want to grant us for our modest contribution to the |
every day, or want to grant us for our modest contribution to the |
23 |
free software community, feel free to send us a beer from one of |
free software community, feel free to send us a beer from one of |
24 |
your local brewery. Our preference goes to Belgium abbey beers and |
your local brewery. Our preference goes to Belgium abbey beers and |
25 |
irish stout (Guiness for strength!), but we like to try new stuffs. |
irish stout (Guiness for strength!), but we like to try new stuffs. |
26 |
|
|
27 |
Authors: |
Authors: |
28 |
LAGACHERIE Matthieu |
LAGACHERIE Matthieu |
29 |
Paper mail : 7 rue Delescluzes 94280 LE KREMLIN BICETRE, FRANCE |
Paper mail : 7 rue Delescluzes 94280 LE KREMLIN BICETRE, FRANCE |
30 |
E-mail : matthieu@libnn.org |
E-mail : matthieu@libnn.org |
31 |
|
|
32 |
RICORDEAU Olivier |
RICORDEAU Olivier |
33 |
Paper mail : 69 avenue d'Italie 75013 PARIS, FRANCE |
Paper mail : 69 avenue d'Italie 75013 PARIS, FRANCE |
34 |
E-mail : olivier@libnn.org |
E-mail : olivier@libnn.org |
35 |
|
|
36 |
*****************************************************************) |
*****************************************************************) |
37 |
|
|
38 |
(** |
(** |
39 |
The initTdnnVisitor class. |
The initTdnnVisitor class. |
40 |
|
|
41 |
@author Matthieu Lagacherie |
@author Matthieu Lagacherie |
42 |
@author Olivier Ricordeau |
@author Olivier Ricordeau |
43 |
@since 07/29/2003 |
@since 07/29/2003 |
44 |
*) |
*) |
45 |
|
|
46 |
open Random |
open Random |
47 |
|
|
49 |
open InitVisitor |
open InitVisitor |
50 |
open DefaultVisitor |
open DefaultVisitor |
51 |
open TdNN |
open TdNN |
52 |
|
|
53 |
|
|
54 |
(** |
(** |
55 |
Initializes a Time Delay Neural Network. |
Initializes a Time Delay Neural Network. |
56 |
*) |
*) |
57 |
class initTdnnVisitor = |
class initTdnnVisitor = |
58 |
object |
object |
59 |
inherit [tdNN] initVisitor |
inherit [tdNN] initVisitor |
60 |
|
|
61 |
initializer |
initializer |
62 |
_moduleName <- "TDNN initialization" |
_moduleName <- "TDNN initialization" |
63 |
|
|
64 |
(** |
(** |
65 |
The method which initializes the network. |
The method which initializes the network. |
66 |
It uses the Random module to initialize the weights. |
It uses the Random module to initialize the weights. |
67 |
@see |
@see |
68 |
<http://caml.inria.fr/oreilly-book/html/book-ora076.html> |
<http://caml.inria.fr/oreilly-book/html/book-ora076.html> |
69 |
Using the Random module (provided in the standard OCaml |
Using the Random module (provided in the standard OCaml |
70 |
distribution). |
distribution). |
71 |
@see <http://caml.inria.fr/devtools/doc_ocaml/Unix.html#VALtime> |
@see <http://caml.inria.fr/devtools/doc_ocaml/Unix.html#VALtime> |
72 |
Used to initialize the random number generator. |
Used to initialize the random number generator. |
73 |
*) |
*) |
74 |
method visit (network : tdNN) = |
method visit (network : tdNN) = |
75 |
|
|
76 |
(** |
(** |
77 |
First, define a bunch of functions which initializes the different |
First, define a bunch of functions which initializes the different |
78 |
stuffs. |
stuffs. |
79 |
*) |
*) |
80 |
|
|
81 |
|
let featuresNb = network#getFeaturesNb and |
82 |
|
timeNb = network#getTimeNb in |
83 |
|
|
84 |
let featuresNb = network#getFeaturesNb and |
(** |
85 |
timeNb = network#getTimeNb in |
Define a function designed to test the tdnn architecture |
86 |
|
*) |
87 |
|
|
88 |
|
(* |
89 |
|
let rec test_couches window_t field_t delay = match (window_t,field_t,delay) wit |
90 |
|
h |
91 |
|
|(a,b,c) when (b=[] && c=[]) -> true |
92 |
|
|(a,b,c) -> ((List.hd b + List.hd c)<=List.hd a && |
93 |
|
(List.hd a - List.hd b) mod (List.hd c)=0 && |
94 |
|
(1 + (List.hd a - List.hd b)/(List.hd c))=(List.hd (List.tl a))) |
95 |
|
&& |
96 |
|
test_couches (List.tl a) (List.tl b) (List.tl c) and |
97 |
|
|
98 |
|
let test_config nb_data nb_layers window_t nb_feat field_t delay = match (nb_dat |
99 |
|
a,nb_layers,window_t,nb_feat,field_t,delay) with |
100 |
|
|(a,b,c,d,e,f) when (a = 0 || b < 3 || c = [] || nb_elt c != b || d = [] || nb |
101 |
|
_elt d != b || e = [] || nb_elt e != b-1 || f = [] || nb_elt f != b-1) ->false |
102 |
|
|(a,b,c,d,e,f) when (a = (List.hd c)*(List.hd d) && test_couches c e f) -> tru |
103 |
|
e |
104 |
|
|_ -> false in |
105 |
|
*) |
106 |
|
|
107 |
(** |
(** |
108 |
Initializes the output activation. |
Initializes the output activation. |
109 |
*) |
*) |
110 |
let initOutputActivation network = |
let initOutputActivation network = |
111 |
let outputActivation = |
let outputActivation = |
112 |
Array.make network#getLayerNb [|[||]|] in |
Array.make network#getLayerNb [|[||]|] in |
113 |
for i = 0 to network#getLayerNb - 1 do |
for i = 0 to network#getLayerNb - 1 do |
114 |
outputActivation.(i) <- Array.make (featuresNb.(i)) [||]; |
outputActivation.(i) <- Array.make (featuresNb.(i)) [||]; |
115 |
for j = 0 to (featuresNb.(i)) - 1 do |
for j = 0 to (featuresNb.(i)) - 1 do |
117 |
done |
done |
118 |
done; |
done; |
119 |
network#setOutputActivation outputActivation |
network#setOutputActivation outputActivation |
120 |
and |
and |
121 |
|
|
122 |
(** |
(** |
123 |
Initializes the the input sum. |
Initializes the the input sum. |
124 |
*) |
*) |
125 |
initInputSum (network : tdNN) = |
initInputSum (network : tdNN) = |
126 |
let inputSum = Array.make network#getLayerNb [|[||]|] in |
let inputSum = Array.make network#getLayerNb [|[||]|] in |
127 |
for i = 0 to network#getLayerNb - 1 do |
for i = 0 to network#getLayerNb - 1 do |
128 |
inputSum.(i) <- Array.create (featuresNb.(i)) [||]; |
inputSum.(i) <- Array.create (featuresNb.(i)) [||]; |
129 |
for j = 0 to (featuresNb.(i)) - 1 do |
for j = 0 to (featuresNb.(i)) - 1 do |
131 |
done |
done |
132 |
done; |
done; |
133 |
network#setInputSum inputSum |
network#setInputSum inputSum |
134 |
and |
and |
135 |
|
|
136 |
(** |
(** |
137 |
Initializes the error. |
Initializes the error. |
138 |
*) |
*) |
139 |
initError (network : tdNN) = |
initError (network : tdNN) = |
140 |
let error = Array.make network#getLayerNb [|[||]|] in |
let error = Array.make network#getLayerNb [|[||]|] in |
141 |
for i = 0 to network#getLayerNb - 1 do |
for i = 0 to network#getLayerNb - 1 do |
142 |
error.(i) <- Array.make (featuresNb.(i)) [||]; |
error.(i) <- Array.make (featuresNb.(i)) [||]; |
143 |
for j = 0 to (featuresNb.(i)) - 1 do |
for j = 0 to (featuresNb.(i)) - 1 do |
145 |
done |
done |
146 |
done; |
done; |
147 |
network#setError error |
network#setError error |
148 |
and |
and |
149 |
|
|
150 |
(** |
(** |
151 |
Initializes the weights (using random numbers). |
Initializes the weights (using random numbers). |
152 |
*) |
weights[layer][feat layer][time layer][feat layer + 1] |
153 |
initWeights (network : tdNN) = |
*) |
154 |
let weights = Array.make (network#getLayerNb - 1) [|[|[||]|]|] in |
initWeights (network : tdNN) = |
155 |
|
let weights = Array.make (network#getLayerNb - 1) [|[|[||]|]|] in |
156 |
for i = 0 to network#getLayerNb - 2 do |
for i = 0 to network#getLayerNb - 2 do |
157 |
weights.(i) <- Array.make (featuresNb.(i)) [|[||]|]; |
weights.(i) <- Array.make (featuresNb.(i)) [|[||]|]; |
158 |
for j = 0 to (featuresNb.(i)) - 1 do |
for j = 0 to (featuresNb.(i)) - 1 do |
159 |
weights.(i).(j) |
weights.(i).(j) |
160 |
<- Array.make (featuresNb.(i + 1)) [||]; |
<- Array.make (timeNb.(i)) [||]; |
161 |
for k = 0 to (featuresNb.(i + 1)) - 1 do |
for k = 0 to (timeNb.(i)) - 1 do |
162 |
weights.(i).(j).(k) |
weights.(i).(j).(k) |
163 |
<- Array.make (timeNb.(i)) |
<- Array.make (featuresNb.(i + 1)) |
164 |
(Random.float (Env.getEnv())#getRandLimit) |
(Random.float (Env.getEnv())#getRandLimit) |
165 |
done |
done |
166 |
done |
done |
167 |
done; |
done; |
168 |
network#setWeights weights |
network#setWeights weights |
169 |
and |
and |
170 |
|
|
171 |
(** |
(** |
172 |
Initializes the gradients. |
Initializes the gradients. |
173 |
*) |
gradients[layer][feat layer][time layer][feat layer + 1] |
174 |
initGradients (network : tdNN) = |
*) |
175 |
let gradients = Array.make (network#getLayerNb - 1) [|[|[||]|]|] in |
initGradients (network : tdNN) = |
176 |
|
let gradients = Array.make (network#getLayerNb - 1) [|[|[||]|]|] in |
177 |
for i = 0 to network#getLayerNb - 2 do |
for i = 0 to network#getLayerNb - 2 do |
178 |
gradients.(i) <- Array.make (featuresNb.(i)) [|[||]|]; |
gradients.(i) <- Array.make (featuresNb.(i)) [|[||]|]; |
179 |
for j = 0 to (featuresNb.(i)) - 1 do |
for j = 0 to (featuresNb.(i)) - 1 do |
180 |
gradients.(i).(j) |
gradients.(i).(j) |
181 |
<- Array.make (featuresNb.(i + 1)) [||]; |
<- Array.make (timeNb.(i)) [||]; |
182 |
for k = 0 to (featuresNb.(i + 1)) - 1 do |
for k = 0 to (timeNb.(i)) - 1 do |
183 |
gradients.(i).(j).(k) |
gradients.(i).(j).(k) |
184 |
<- Array.make (timeNb.(i)) |
<- Array.make (featuresNb.(i + 1)) |
185 |
(Random.float (Env.getEnv())#getRandLimit) |
(Random.float (Env.getEnv())#getRandLimit) |
186 |
done |
done |
187 |
done |
done |
188 |
done; |
done; |
189 |
network#setGradients gradients |
network#setGradients gradients |
190 |
in |
in |
191 |
|
|
192 |
(** Initialize the random number generator. *) |
(** Initialize the random number generator. *) |
193 |
Random.self_init(); |
Random.self_init(); |
198 |
initError network; |
initError network; |
199 |
initGradients network; |
initGradients network; |
200 |
initWeights network |
initWeights network |
201 |
|
end |
|
end |
|