| 1 |
% Don't load this package twice: |
| 2 |
if known neuralnetwork_version: expandafter endinput; fi; |
| 3 |
|
| 4 |
numeric neuralnetwork_version;string neuralnetwork_date; |
| 5 |
neuralnetwork_version=0.1; |
| 6 |
neuralnetwork_date="2025/01/27"; |
| 7 |
% The banner: |
| 8 |
message "******* mp-neuralnetwork " & decimal (neuralnetwork_version) & |
| 9 |
" (c) Maxime Chupin (" & neuralnetwork_date & ") *******";message ""; |
| 10 |
|
| 11 |
input metaobj; |
| 12 |
input mp-neuralnetwork-metaobj-patch; |
| 13 |
if not known mplib: input latexmp fi; |
| 14 |
input colorbrewer-rgb; |
| 15 |
|
| 16 |
interim ahangle := 40; |
| 17 |
interim ahlength := 2; |
| 18 |
|
| 19 |
numeric _ANN_Vspace,_ANN_Hspace; |
| 20 |
_ANN_Vspace:=1.5cm; |
| 21 |
_ANN_Hspace:=2.2cm; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
boolean |
| 26 |
_ANN_Hidden_b,_ANN_Input_b,_ANN_Output_b,_ANN_Legends_b,_ANN_show_node_labels_b,_ANN_weights_b,_ANN_abstract_b,_ANN_Legends_b,_ANN_colored_b,_ANN_user_color; |
| 27 |
|
| 28 |
string _ANN_input_legend,_ANN_output_legend, _ANN_hidden_legend; |
| 29 |
string _ANN_weights; |
| 30 |
|
| 31 |
numeric _ANN_instance_nbr; |
| 32 |
numeric _ANN_label_nbr; |
| 33 |
color _ANN_colors[],_ANN_connect_color; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
% init of colors |
| 38 |
for ii:=0 upto 30: |
| 39 |
_ANN_colors[ii]:=white; |
| 40 |
endfor; |
| 41 |
|
| 42 |
_ANN_connect_color := 0.4*white; |
| 43 |
|
| 44 |
_ANN_input_legend := "Input layer"; |
| 45 |
_ANN_output_legend := "Output layer"; |
| 46 |
_ANN_hidden_legend := "Hidden layer(s)"; |
| 47 |
|
| 48 |
_ANN_colored_b := true; |
| 49 |
_ANN_Legends_b:=false; |
| 50 |
_ANN_abstract_b:=false; |
| 51 |
_ANN_Hidden_b := false; |
| 52 |
_ANN_Input_b := false; |
| 53 |
_ANN_Output_b := false; |
| 54 |
_ANN_show_node_labels_b := true; |
| 55 |
_ANN_weights_b := false; |
| 56 |
_ANN_user_color:=false; |
| 57 |
_ANN_weights := "w"; |
| 58 |
|
| 59 |
def mpneural_init_values= |
| 60 |
_ANN_Vspace:=1.5cm; |
| 61 |
_ANN_Hspace:=2.2cm; |
| 62 |
_ANN_input_legend := "Input layer"; |
| 63 |
_ANN_output_legend := "Output layer"; |
| 64 |
_ANN_hidden_legend := "Hidden layer(s)"; |
| 65 |
|
| 66 |
_ANN_colored_b := true; |
| 67 |
_ANN_Legends_b:=false; |
| 68 |
_ANN_abstract_b:=false; |
| 69 |
_ANN_Hidden_b := false; |
| 70 |
_ANN_Input_b := false; |
| 71 |
_ANN_Output_b := false; |
| 72 |
_ANN_show_node_labels_b := true; |
| 73 |
_ANN_weights_b := false; |
| 74 |
_ANN_user_color:=false; |
| 75 |
_ANN_weights := "w"; |
| 76 |
_ANN_connect_color := 0.4*white; |
| 77 |
enddef; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
def set_vspace(expr d)= |
| 83 |
_ANN_Vspace:=d; |
| 84 |
enddef; |
| 85 |
|
| 86 |
def set_hspace(expr d)= |
| 87 |
_ANN_Hspace:=d; |
| 88 |
enddef; |
| 89 |
def set_connection_color(expr c)= |
| 90 |
_ANN_connect_color:=c; |
| 91 |
enddef; |
| 92 |
|
| 93 |
|
| 94 |
def show_node_labels(expr b)= |
| 95 |
_ANN_show_node_labels_b:=b; |
| 96 |
enddef; |
| 97 |
|
| 98 |
def framed_input(expr b)= |
| 99 |
_ANN_Input_b:=b; |
| 100 |
enddef; |
| 101 |
|
| 102 |
def framed_output(expr b)= |
| 103 |
_ANN_Output_b:=b; |
| 104 |
enddef; |
| 105 |
|
| 106 |
def framed_hidden(expr b)= |
| 107 |
_ANN_Hidden_b:=b; |
| 108 |
enddef; |
| 109 |
|
| 110 |
def print_legends(expr b)= |
| 111 |
_ANN_Legends_b:=b; |
| 112 |
enddef; |
| 113 |
|
| 114 |
def colored_layers(expr b)= |
| 115 |
_ANN_colored_b:=b; |
| 116 |
enddef; |
| 117 |
|
| 118 |
vardef set_layer_colors(text t)= |
| 119 |
save ii,_i_layer; |
| 120 |
% initialisation |
| 121 |
% init of colors |
| 122 |
for ii:=1 upto 30: |
| 123 |
_ANN_colors[ii]:=(1,1,1); |
| 124 |
endfor; |
| 125 |
for c=t: |
| 126 |
show(c); |
| 127 |
if(numeric c): |
| 128 |
_i_layer:=c; |
| 129 |
elseif(color c): |
| 130 |
_ANN_colors[_i_layer]:=c; |
| 131 |
fi |
| 132 |
endfor; |
| 133 |
_ANN_user_color:=true; |
| 134 |
enddef; |
| 135 |
|
| 136 |
def print_weights(expr b)= |
| 137 |
_ANN_weights_b:=b; |
| 138 |
enddef; |
| 139 |
|
| 140 |
def set_weights_notation(expr s)= |
| 141 |
_ANN_weights:=s; |
| 142 |
enddef; |
| 143 |
|
| 144 |
|
| 145 |
def set_input_legend(expr s)= |
| 146 |
_ANN_input_legend := s; |
| 147 |
enddef; |
| 148 |
|
| 149 |
def set_output_legend(expr s)= |
| 150 |
_ANN_output_legend := s; |
| 151 |
enddef; |
| 152 |
|
| 153 |
def set_hidden_legend(expr s)= |
| 154 |
_ANN_hidden_legend := s; |
| 155 |
enddef; |
| 156 |
|
| 157 |
|
| 158 |
_ANN_instance_nbr :=0; |
| 159 |
|
| 160 |
def drawANN(text t) text options= |
| 161 |
% t : list of ("str1",int1,"str2",int2, etc.) |
| 162 |
|
| 163 |
save prefixANN,_ANN_i,_ANN_i_couple,_ANN_n,_ANN_lb,_ANN_lb_int; |
| 164 |
string prefixANN; |
| 165 |
|
| 166 |
numeric _ANN_n[],_ANN_i,_ANN_i_couple; |
| 167 |
string _ANN_lb[],_ANN_lb_int[]; |
| 168 |
|
| 169 |
_ANN_instance_nbr := _ANN_instance_nbr+1; |
| 170 |
|
| 171 |
prefixANN:="ANN"&decimal(_ANN_instance_nbr); |
| 172 |
_ANN_abstract_b:=false; |
| 173 |
_ANN_i:=1; |
| 174 |
_ANN_i_couple:=1; |
| 175 |
% parsing the list |
| 176 |
for x=t: |
| 177 |
if(odd _ANN_i): |
| 178 |
_ANN_lb[_ANN_i_couple]:=prefixANN&"_"&x; |
| 179 |
else: |
| 180 |
if(numeric x): |
| 181 |
_ANN_n[_ANN_i_couple]:=x; |
| 182 |
_ANN_i_couple:=_ANN_i_couple+1; |
| 183 |
else: |
| 184 |
_ANN_abstract_b:=true; |
| 185 |
_ANN_n[_ANN_i_couple]:=5; |
| 186 |
_ANN_lb_int[_ANN_i_couple]:=x; |
| 187 |
_ANN_i_couple:=_ANN_i_couple+1; |
| 188 |
fi |
| 189 |
fi |
| 190 |
_ANN_i:=_ANN_i+1; |
| 191 |
endfor |
| 192 |
|
| 193 |
% colors |
| 194 |
if(_ANN_user_color=false): |
| 195 |
for i:=1 upto _ANN_i_couple-1: |
| 196 |
_ANN_colors[i]:=Spectral[min(11,_ANN_i_couple-1)][i mod 11]; |
| 197 |
endfor |
| 198 |
fi |
| 199 |
|
| 200 |
if(_ANN_abstract_b=true): % small size for input and output |
| 201 |
_ANN_n[1]:=4; |
| 202 |
_ANN_n[_ANN_i_couple-1]:=4; |
| 203 |
fi |
| 204 |
for i:=1 upto _ANN_i_couple -1: |
| 205 |
for j:=1 upto _ANN_n[i]: |
| 206 |
if( not _ANN_show_node_labels_b): |
| 207 |
newCircle.scantokens(_ANN_lb[i])[j](textext("$\phantom{a}$")) |
| 208 |
if(_ANN_colored_b): "filled(true)","fillcolor("&str _ANN_colors[i] &")", fi options; |
| 209 |
elseif(_ANN_abstract_b): % abstract |
| 210 |
|
| 211 |
if(j<3): % for the two first ones |
| 212 |
newCircle.scantokens(_ANN_lb[i])[j](textext("$"&substring |
| 213 |
(4+length(decimal(_ANN_instance_nbr)),length _ANN_lb[i]) of |
| 214 |
_ANN_lb[i]&"_{"&decimal(j)&"}$")) if(_ANN_colored_b): "filled(true)","fillcolor("&str _ANN_colors[i] &")", fi options; |
| 215 |
elseif(j=3): % dots |
| 216 |
newBox.scantokens(_ANN_lb[i])[j](image(drawoptions(withpen |
| 217 |
pencircle scaled 1bp);drawdot (0,0);drawdot |
| 218 |
(0,0.1cm);drawdot |
| 219 |
(0,0.2cm);drawoptions();)) if(_ANN_colored_b): |
| 220 |
"filled(true)","fillcolor("&str _ANN_colors[i] &")", fi ; |
| 221 |
else: % lasts |
| 222 |
|
| 223 |
if((i=1) or (i=_ANN_i_couple-1)): %if input or output only last |
| 224 |
newCircle.scantokens(_ANN_lb[i])[j](textext("$"&substring |
| 225 |
(4+length(decimal(_ANN_instance_nbr)),length _ANN_lb[i]) of |
| 226 |
_ANN_lb[i]&"_{"&_ANN_lb_int[i]&"}$")) if(_ANN_colored_b): "filled(true)","fillcolor("&str _ANN_colors[i] &")", fi options; |
| 227 |
else: |
| 228 |
if(j=4): |
| 229 |
newBox.scantokens(_ANN_lb[i])[j](image(drawoptions(withpen |
| 230 |
pencircle scaled 1bp);drawdot (0,0);drawdot |
| 231 |
(0,0.1cm);drawdot (0,0.2cm);drawoptions();)) if(_ANN_colored_b): "filled(true)","fillcolor("&str _ANN_colors[i] &")", fi ; |
| 232 |
else: |
| 233 |
newCircle.scantokens(_ANN_lb[i])[j](textext("$"&substring |
| 234 |
(4+length(decimal(_ANN_instance_nbr)),length _ANN_lb[i]) of |
| 235 |
_ANN_lb[i]&"_{"&_ANN_lb_int[i]&"}$")) if(_ANN_colored_b): "filled(true)","fillcolor("&str _ANN_colors[i] &")", fi options; |
| 236 |
fi |
| 237 |
fi |
| 238 |
fi |
| 239 |
else: |
| 240 |
newCircle.scantokens(_ANN_lb[i])[j](textext("$"&substring |
| 241 |
(4+length(decimal(_ANN_instance_nbr)),length _ANN_lb[i]) of |
| 242 |
_ANN_lb[i]&"_{"&decimal(j)&"}$")) if(_ANN_colored_b): |
| 243 |
"filled(true)","fillcolor("&str _ANN_colors[i] &")", fi options; |
| 244 |
fi |
| 245 |
if(j=1): |
| 246 |
if(i=1): |
| 247 |
scantokens(_ANN_lb[i])[j].c = origin; |
| 248 |
else: |
| 249 |
scantokens(_ANN_lb[i])[j].c = scantokens(_ANN_lb[i-1])[j].c+(_ANN_Hspace,0)+(0,-(_ANN_n[i]-_ANN_n[i-1])/3.5*_ANN_Vspace); |
| 250 |
fi |
| 251 |
else: |
| 252 |
scantokens(_ANN_lb[i])[j].n-scantokens(_ANN_lb[i])[j-1].s=(0,_ANN_Vspace); |
| 253 |
fi |
| 254 |
endfor |
| 255 |
for j:=1 upto _ANN_n[i]: |
| 256 |
drawObj(scantokens(_ANN_lb[i])[j]); |
| 257 |
endfor |
| 258 |
endfor |
| 259 |
% group for hiddenlayer |
| 260 |
|
| 261 |
if(_ANN_i_couple-1>3): |
| 262 |
newGroup.scantokens(prefixANN)_hidden( for i:=2 upto (_ANN_i_couple-2): for j:=1 upto _ANN_n[i]: |
| 263 |
scantokens(_ANN_lb[i])[j] if((i<_ANN_i_couple-2)or(j<_ANN_n[i])): , fi |
| 264 |
endfor endfor) "framed(true)","dx(3pt)","dy(3pt)"; |
| 265 |
fi |
| 266 |
if(_ANN_Hidden_b): |
| 267 |
drawObj(scantokens(prefixANN)_hidden) ; |
| 268 |
fi |
| 269 |
% rectangle for inputlayer |
| 270 |
|
| 271 |
if(_ANN_n[1]>1): |
| 272 |
newGroup.scantokens(prefixANN)_input(for j:=1 upto _ANN_n[1]: |
| 273 |
scantokens(_ANN_lb[1])[j] if(j<_ANN_n[1]): , fi |
| 274 |
endfor) |
| 275 |
"framed(true)","dx(3pt)","dy(3pt)"; |
| 276 |
else: |
| 277 |
newBox.scantokens(prefixANN)_input(scantokens(_ANN_lb[1])[1]) "filled(false)","framed(true)","dx(3pt)","dy(3pt)"; |
| 278 |
fi |
| 279 |
if(_ANN_Input_b): |
| 280 |
drawObj(scantokens(prefixANN)_input); |
| 281 |
fi |
| 282 |
|
| 283 |
% rectangle for outputlayer |
| 284 |
|
| 285 |
if(_ANN_n[_ANN_i_couple-1]>1): |
| 286 |
newGroup.scantokens(prefixANN)_output(for j:=1 upto _ANN_n[_ANN_i_couple-1]: |
| 287 |
scantokens(_ANN_lb[_ANN_i_couple-1])[j] if(j<_ANN_n[_ANN_i_couple-1]): , fi |
| 288 |
endfor) |
| 289 |
"framed(true)","dx(3pt)","dy(3pt)"; |
| 290 |
else: |
| 291 |
newBox.scantokens(prefixANN)_output(scantokens(_ANN_lb[_ANN_i_couple-1])[1]) "filled(false)","framed(true)","dx(3pt)","dy(3pt)"; |
| 292 |
fi |
| 293 |
if(_ANN_Output_b): |
| 294 |
drawObj(scantokens(prefixANN)_output); |
| 295 |
fi |
| 296 |
|
| 297 |
% links |
| 298 |
for i:=1 upto _ANN_i_couple-1: |
| 299 |
if(i>1): |
| 300 |
for j:=1 upto _ANN_n[i-1]: |
| 301 |
for k:=1 upto _ANN_n[i]: |
| 302 |
ncline(scantokens(_ANN_lb[i-1])[j])(scantokens(_ANN_lb[i])[k]) |
| 303 |
"linecolor(_ANN_connect_color)" |
| 304 |
; |
| 305 |
endfor |
| 306 |
endfor |
| 307 |
|
| 308 |
fi |
| 309 |
endfor |
| 310 |
% weights |
| 311 |
if(_ANN_weights_b): |
| 312 |
for j:=1 upto _ANN_i_couple-2: |
| 313 |
newBox.scantokens(prefixANN)_weights[j](textext("$"&_ANN_weights&"_{ij}^{("&decimal(j)&")}$")); |
| 314 |
scantokens(prefixANN)_weights.[j].c=1/4*(scantokens(_ANN_lb[j])[1].c+scantokens(_ANN_lb[j])[_ANN_n[j]].c+scantokens(_ANN_lb[j+1])[1].c+scantokens(_ANN_lb[j+1])[_ANN_n[j+1]].c); |
| 315 |
drawObj(scantokens(prefixANN)_weights[j]); |
| 316 |
endfor; |
| 317 |
fi |
| 318 |
% labels |
| 319 |
if(_ANN_Legends_b): |
| 320 |
newBox.scantokens(prefixANN)_legend_input(textext(_ANN_input_legend)) "framed(false)"; |
| 321 |
scantokens(prefixANN)_legend_input.n=scantokens(prefixANN)_input.s; |
| 322 |
drawObj(scantokens(prefixANN)_legend_input); |
| 323 |
newBox.scantokens(prefixANN)_legend_output(textext(_ANN_output_legend)) "framed(false)"; |
| 324 |
scantokens(prefixANN)_legend_output.n=scantokens(prefixANN)_output.s; |
| 325 |
drawObj(scantokens(prefixANN)_legend_output); |
| 326 |
|
| 327 |
if(_ANN_i_couple-1>3): |
| 328 |
newBox.scantokens(prefixANN)_legend_hidden(textext(_ANN_hidden_legend)) "framed(false)"; |
| 329 |
scantokens(prefixANN)_legend_hidden.n=scantokens(prefixANN)_hidden.s; |
| 330 |
drawObj(scantokens(prefixANN)_legend_hidden); |
| 331 |
fi |
| 332 |
fi |
| 333 |
% build global group |
| 334 |
newGroup.scantokens(prefixANN)_global(for i:=1 upto (_ANN_i_couple-1): for j:=1 upto _ANN_n[i]: |
| 335 |
scantokens(_ANN_lb[i])[j] if((i<_ANN_i_couple-1)or(j<_ANN_n[i])): , fi |
| 336 |
endfor endfor); |
| 337 |
enddef; |
| 338 |
|
| 339 |
|
| 340 |
_ANN_label_nbr:=0; |
| 341 |
vardef labelANN(expr s)(expr a,b) text options= |
| 342 |
% s: string for the label |
| 343 |
% (a,b): point at which put the label in relative coordinate, that is |
| 344 |
% $(0,0)$ at bottom left of the bounding box of the complete graph and |
| 345 |
% $(1,1)$ at top right. |
| 346 |
|
| 347 |
save _xmin, _xmax, _ymin,_ymax; |
| 348 |
save prefixANN; |
| 349 |
string prefixANN; |
| 350 |
_ANN_label_nbr:=_ANN_label_nbr+1; |
| 351 |
|
| 352 |
prefixANN:="ANN"&decimal(_ANN_instance_nbr); |
| 353 |
(_xmin,_ymin)=scantokens(prefixANN)_global.sw; |
| 354 |
(_xmax,_ymax)=scantokens(prefixANN)_global.ne; |
| 355 |
newBox.scantokens(prefixANN)_label scantokens(decimal(_ANN_label_nbr))(textext(s)) options; |
| 356 |
scantokens(prefixANN)_label |
| 357 |
scantokens(decimal(_ANN_label_nbr)).c=(_xmin+a*(_xmax-_xmin),_ymin+b*(_ymax-_ymin)); |
| 358 |
drawObj(scantokens(prefixANN)_label scantokens(decimal(_ANN_label_nbr))); |
| 359 |
enddef; |
| 360 |
|
| 361 |
endinput |