36 |
*****************************************************************) |
*****************************************************************) |
37 |
|
|
38 |
(** |
(** |
39 |
The xmlVisitor virtual class |
The xmlVisitor class |
40 |
|
|
41 |
@author Matthieu Lagacherie |
@author Matthieu Lagacherie |
42 |
@author Olivier Ricordeau |
@author Olivier Ricordeau |
43 |
@since 08/26/2003 |
@since 08/26/2003 |
44 |
*) |
*) |
45 |
|
|
46 |
|
open Sys |
47 |
|
|
48 |
open DefaultVisitor |
open DefaultVisitor |
49 |
|
open Nn |
50 |
|
|
51 |
|
(** |
52 |
|
Thrown if _overwriteFile is false, and someone tries to overwrite an |
53 |
|
existing file. |
54 |
|
*) |
55 |
|
exception FileExists |
56 |
|
|
57 |
(** |
(** |
58 |
The abstract xml export visitor. |
The abstract XML export visitor. |
59 |
Instances of derived classes are supposed to export the various kinds of |
This object exports a neural network as an XML file. |
60 |
neural networks as XML files. |
@raise FileExists Thrown if _overwriteFile is false, and someone tries to |
61 |
|
overwrite an existing file. |
62 |
@see 'nn.dtd' The DTD to which the generated XML files are supposed to |
@see 'nn.dtd' The DTD to which the generated XML files are supposed to |
63 |
be conform to. |
be conform to. |
64 |
*) |
*) |
65 |
class virtual ['a] xmlVisitor = |
class virtual ['a] xmlVisitor = |
66 |
object |
object (this) |
67 |
inherit ['a] defaultVisitor |
inherit ['a] defaultVisitor |
68 |
|
|
69 |
(** |
(** |
70 |
|
The XML file's header. Contains the DTD. |
71 |
|
@see <http://www.w3schools.com/dtd/dtd_intro.asp> How to get a DTD |
72 |
|
whithin a XML file. |
73 |
|
*) |
74 |
|
val _header = |
75 |
|
"<!-- This file was generated by LibNN --> |
76 |
|
<!-- http://libnn.org --> |
77 |
|
|
78 |
|
|
79 |
|
<?xml version=\"1.0\"?> |
80 |
|
|
81 |
|
|
82 |
|
<!-- The DTD --> |
83 |
|
<!DOCTYPE nn [ |
84 |
|
|
85 |
|
<!-- Neural Network (the XML file's root. `nn' class) --> |
86 |
|
<!ELEMENT nn (step,corpus,env,(mlpnn|tdnn))> |
87 |
|
|
88 |
|
<!-- Neural Networks kinds (concrete classes which inherit from the `nn' class) --> |
89 |
|
<!-- Multi Layer Perceptron (`mlpNN' class) --> |
90 |
|
<!ELEMENT mlpnn (mlpnn_output_activation,mlpnn_input_sum,mlpnn_error,\ |
91 |
|
mlpnn_weights,mlpnn_gradients,layer_nb,mlpnn_neurons_per_layer)> |
92 |
|
<!ELEMENT mlpnn_output_activation (3d_array)> |
93 |
|
<!ELEMENT mlpnn_input_sum (3d_array)> |
94 |
|
<!ELEMENT mlpnn_error (3d_array)> |
95 |
|
<!ELEMENT mlpnn_weights (4d_array)> |
96 |
|
<!ELEMENT mlpnn_gradients (4d_array)> |
97 |
|
<!ELEMENT mlpnn_neurons_per_layer (2d_array)> |
98 |
|
<!-- Time Delay Neural Network (`tdNNN' class) --> |
99 |
|
<!ELEMENT tdnn (tdnn_output_activation,tdnn_input_sum,tdnn_error,\ |
100 |
|
tdnn_weights,tdnn_gradients,layer_nb,tdnn_delay,tdnn_features_nb,\ |
101 |
|
tdnn_time_nb,tdnn_field_size)> |
102 |
|
<!ELEMENT tdnn_output_activation (4d_array)> |
103 |
|
<!ELEMENT tdnn_input_sum (4d_array)> |
104 |
|
<!ELEMENT tdnn_error (4d_array)> |
105 |
|
<!ELEMENT tdnn_weights (5d_array)> |
106 |
|
<!ELEMENT tdnn_gradients (5d_array)> |
107 |
|
<!ELEMENT tdnn_delay (2d_array)> |
108 |
|
<!ELEMENT tdnn_features_nb (2d_array)> |
109 |
|
<!ELEMENT tdnn_time_nb (2d_array)> |
110 |
|
<!ELEMENT tdnn_field_size (2d_array)> |
111 |
|
|
112 |
|
<!-- Common stuffs --> |
113 |
|
<!-- Corpus (`corpus' class) --> |
114 |
|
<!ELEMENT corpus (pattern*)> |
115 |
|
<!-- Pattern (`pattern' class) --> |
116 |
|
<!ELEMENT pattern (input,output)> |
117 |
|
<!ELEMENT input (#CDATA)> |
118 |
|
<!ELEMENT output (#CDATA)> |
119 |
|
<!-- Environment (`env' class) --> |
120 |
|
<!ELEMENT env (verbosity,randlimit,channel)> |
121 |
|
<!ELEMENT verbosity (#CDATA)> |
122 |
|
<!ELEMENT randlimit (#CDATA)> |
123 |
|
<!ELEMENT channel (#CDATA)> |
124 |
|
<!-- Arrays --> |
125 |
|
<!ELEMENT 2d_array ((x,y,value)*)> |
126 |
|
<!ELEMENT 3d_array ((x,y,z,value)*)> |
127 |
|
<!ELEMENT 4d_array ((x,y,z,t,value)*)> |
128 |
|
<!ELEMENT 5d_array ((x,y,z,t,u,value)*)> |
129 |
|
<!ELEMENT x (#CDATA)> |
130 |
|
<!ELEMENT y (#CDATA)> |
131 |
|
<!ELEMENT z (#CDATA)> |
132 |
|
<!ELEMENT t (#CDATA)> |
133 |
|
<!ELEMENT u (#CDATA)> |
134 |
|
<!ELEMENT value (#CDATA)> |
135 |
|
<!-- Others --> |
136 |
|
<!ELEMENT step (#CDATA)> |
137 |
|
<!ELEMENT layer_nb (#CDATA)> |
138 |
|
|
139 |
|
]> |
140 |
|
|
141 |
|
|
142 |
|
<!-- Dump of the neural network --> |
143 |
|
<nn> |
144 |
|
" |
145 |
|
|
146 |
|
(** |
147 |
|
The XML file's footer. Closes all pending tags. |
148 |
|
*) |
149 |
|
val _footer = |
150 |
|
"</nn> |
151 |
|
|
152 |
|
<!-- End of the dump --> |
153 |
|
" |
154 |
|
|
155 |
|
(** |
156 |
The name of the .xml file which is to be generated. |
The name of the .xml file which is to be generated. |
157 |
*) |
*) |
158 |
val mutable _filename = "mlpnn.xml" |
val mutable _fileName = "neural_network_dump.xml" |
159 |
|
|
160 |
(** |
(** |
161 |
A get*. |
A get*. |
162 |
@return the name of the .xml file which is to be generated. |
@return the name of the .xml file which is to be generated. |
163 |
*) |
*) |
164 |
method getFilename = |
method getFileName = |
165 |
_filename |
_fileName |
166 |
|
|
167 |
(** |
(** |
168 |
Sets the name of the .xml file which is to be generated. |
Sets the name of the .xml file which is to be generated. |
169 |
*) |
*) |
170 |
method setFilename newval = |
method setFileName newval = |
171 |
_filename <- newval |
_fileName <- newval |
172 |
|
|
173 |
(** |
(** |
174 |
If true, then if _filename already exists on the disk, it is overwritten |
If true, then if _fileName already exists on the disk, it is overwritten |
175 |
during the dump. Otherwise the `FileExists' exception is thrown during |
during the dump. Otherwise the `FileExists' exception is thrown during |
176 |
the dump. |
the dump. |
177 |
*) |
*) |
188 |
Enables to choose whether LibNN is supposed to overwrite files during |
Enables to choose whether LibNN is supposed to overwrite files during |
189 |
XML dumps or not. |
XML dumps or not. |
190 |
*) |
*) |
191 |
method setOverwriteFile newval = |
method setOverwriteFile newval = |
192 |
_overwriteFile <- newval |
_overwriteFile <- newval |
193 |
|
|
|
(** |
|
|
Creates the XML file with its embedded DTD, and dump stuffs which are |
|
|
inherited from the `nn' class. |
|
|
@param filename The name of the .xml file which is to be generated. |
|
|
*) |
|
|
method xml_begin (filename : string) = |
|
|
() |
|
|
|
|
194 |
(** |
(** |
195 |
Closes tags which are opened by xml_begin, and closes the xile. |
The out_channel used to dump the neural network. stdout is actually |
196 |
@param filename The name of the .xml file which is to be generated. |
never used (since there is a default value for _fileName), but OCaml |
197 |
*) |
needs an initial value... |
198 |
method xml_end (filename : string) = |
*) |
199 |
() |
val mutable _outChannel = stdout |
200 |
|
|
201 |
|
(** |
202 |
|
Writes a string in the opened file. |
203 |
|
@param str The string that is to be written. |
204 |
|
*) |
205 |
|
method private write (str : string) = |
206 |
|
output _outChannel str 0 (String.length str) |
207 |
|
|
208 |
|
(** |
209 |
|
The method which creates the XML file with its embedded DTD, and dump |
210 |
|
stuffs which are inherited from the `nn' class. |
211 |
|
@param network The neural network which is to be dumped. |
212 |
|
*) |
213 |
|
method beginDump (network : nn) = |
214 |
|
if (Sys.file_exists _fileName & not _overwriteFile) then |
215 |
|
raise FileExists; |
216 |
|
_outChannel <- open_out _fileName; |
217 |
|
this#write _header; |
218 |
|
(** |
219 |
|
Dump stuffs inherited from the `nn' class. |
220 |
|
*) |
221 |
|
let step = "<step>" ^ (string_of_float network#getStep) ^ "</step>" in |
222 |
|
this#write step |
223 |
|
|
224 |
|
(** |
225 |
|
Close tags which have to be closed, and close the file. |
226 |
|
@param network The neural network which is to be dumped. |
227 |
|
*) |
228 |
|
method endDump (network : nn) = |
229 |
|
this#write _footer; |
230 |
|
close_out _outChannel |
231 |
|
|
232 |
end |
end |