-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrsp8.scm
5638 lines (5003 loc) · 129 KB
/
grsp8.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; =========================================================================
;;
;; grsp8.scm
;;
;; Neural networks and network functions in general.
;;
;; =========================================================================
;;
;; Copyright (C) 2018 - 2024 Pablo Edronkin (pablo.edronkin at yahoo.com)
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Lesser General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this program. If not, see
;; <https://www.gnu.org/licenses/>.
;;
;; =========================================================================
;;;; General notes:
;;
;; - Read sources for limitations on function parameters.
;;
;; - A grsp neural network is essentially a list of matrices that
;; constitute a database in itself according to the developments of file
;; grsp3. The format and structure of the matrices used in grsp8
;; (contained in the list data structure just mentioned) is as follows:
;;
;; - Elem 0: nodes. Matrix. Each row of this matrix contains data
;; representing the properties and processes of a specific node of a
;; neural network.
;;
;; - Col 0: id.
;; - Col 1: status.
;;
;; - 0: dead.
;; - 1: inactive.
;; - 2: active.
;;
;; - Col 2: type.
;;
;; - 0: input.
;; - 1: neuron.
;; - 2: output.
;;
;; - Col 3: layer.
;; - Col 4: layer pos.
;; - Col 5: bias.
;; - Col 6: output value.
;; - Col 7: associated function.
;; - Col 8: evol.
;; - Col 9: weight.
;; - Col 10: iter.
;;
;; - Elem 1: conns. Matrix. Each row contains data representing the
;; properties and processes of a specific connection between nodes.
;;
;; - Col 0: id.
;; - Col 1: status.
;;
;; - 0: dead.
;; - 1: inactive.
;; - 2: active.
;;
;; - Col 2: type.
;;
;; - 1: normal.
;;
;; - Col 3: from.
;; - Col 4: to.
;; - Col 5: value.
;; - Col 6: evol.
;; - Col 7: weight.
;; - Col 8: iter.
;; - Col 9: to layer pos.
;;
;; - Elem 2: count. Matrix. Each element of this table is a counter
;; related to a specific ann aspect.
;;
;; - Col 0: nodes id counter.
;; - Col 1: conns id counter.
;; - Col 2: epoch counter.
;; - Col 3: layer counter.
;;
;; - Elem 3: idata. Matrix. Contains an instance of input data. This is
;; how the data should be passed to the network. Using this format it
;; is possible to modify the number of nodes interactively if and when
;; an evolving neural network is used. It is also possible to pass
;; data interactively to the network that does not go directly to the
;; input nodes but modifies the behavior of existing nodes.
;;
;; - Col 0: id of the receptive node or connection.
;; - Col 1: number that corresponds to the column in the nodes or conns
;; matrix in which for the row whose col 0 is equal to the id value
;; passed in col 0 of the idata matrix the input value will be
;; stored.
;; - Col 2: number.
;; - Col 3: type, the kind of element that will receive this data.
;; This means
;; that based on any given idata row, a row in nodes or conns
;; matrices will be updated.
;;
;; - 0: for node.
;; - 1: for connection.
;;
;; - Col 4: control.
;;
;; - 0: default.
;; - 1: epoch end.
;; - 2: delete node or conn.
;;
;; - Elem 4: odata. Matrix. Contains am instance of data originated in
;; the output nodes of a neural network. I.e. this matrix contains the
;; results of a network epoch.
;;
;; - Col 0: id of each output node.
;; - Col 1: layer.
;; - Col 2: layer pos.
;; - Col 3: number (result).
;; - Col 4: control.
;;
;; - 0: default.
;; - 1: epoch end.
;; - 2: delete.
;;
;; - Elem 5: specs. Matrix. Each row contains specifications for a neural
;; network layer. This is a recipe for ann construction.
;;
;; - Col 0: layer number.
;; - Col 1: number of nodes for present layer.
;; - Col 2: type of node.
;;
;; - 0: input.
;; - 1: neuron.
;; - 2: output.
;;
;; - Col 3: activation function.
;;
;; - Elem 6: odtid. Matrix. Establishes a correlation between the data
;; found on each epoch n on the output nodes of a neural network and
;; the input data that will be found on the input nodes during epoch
;; (+ n 1), in the case that the network works by means of a feedback
;; loop.
;;
;; - Col 0: input idata layer pos (pos input).
;; - Col 1: output odata layer pos (pos output).
;;
;; - Elem 7: datai. Contains data that should be passed to the input
;; stream (idata), coming from either the output stream (odata) or
;; from a dataset.
;;
;; - Col 0: id of the receptive node.
;; - Col 1: number that corresponds to the column in the nodes matrix in
;; which for the row whose col 0 is equal to the id value passed in
;; col 0 of the idata matrix the input value will be stored.
;; - Col 2: number; value to be passed.
;; - Col 3: type, the kind of element that will receive this data.
;;
;; - 0: for node.
;; - 1: for connection.
;;
;; - Col 4: record control.
;;
;; - 0: default.
;; - 1: epoch end.
;;
;; - Col 5: classifier.
;;
;; - 0: regular data.
;; - 1: training data.
;; - 2: control data.
;;
;; - Elem 8: datao. Obtained values from each iteration or forward feed
;; of the network.
;;
;; - Col 0: id of each output node.
;; - Col 1: layer.
;; - Col 2: layer pos.
;; - Col 3: number (obtained result).
;; - Col 4: record control.
;;
;; - 0: default.
;; - 1: epoch end.
;;
;; - Col 5: classifier.
;;
;; - 0: regular data.
;; - 1: training data.
;; - 2: control data.
;;
;; - Elem 9: datae. Expected and delta values. Expected values must be
;; provided along input training data in order to train the newtwork.
;;
;; - Col 0: number (expected result).
;; - Col 1: number (obtained result).
;; - Col 2: delta.
;;
;; Sources:
;;
;; See code of functions used and their respective source files for more
;; credits and references.
;;
;; - [1] En.wikipedia.org. 2021. Artificial Neural Network. [online]
;; Available at: https://en.wikipedia.org/wiki/Artificial_neural_network
;; [Accessed 25 January 2021].
;; - [2] En.wikipedia.org. 2021. Mathematics Of Artificial Neural Networks.
;; [online] Available at:
;; https://en.wikipedia.org/wiki/Mathematics_of_artificial_neural_networks
;; [Accessed 25 January 2021].
;; - [3] En.wikipedia.org. 2021. Artificial Neuron. [online] Available at:
;; https://en.wikipedia.org/wiki/Artificial_neuron [Accessed 25
;; January 2021].
;; - [4] En.wikipedia.org. 2021. Perceptron. [online] Available at:
;; https://en.wikipedia.org/wiki/Perceptron [Accessed 25 January 2021].
;; - [5] En.wikipedia.org. 2021. Activation function. [online] Available
;; at: https://en.wikipedia.org/wiki/Activation_function [Accessed 28
;; January 2021].
;; - [6] Machine Learning From Scratch. 2021. Activation Functions
;; Explained - GELU, SELU, ELU, ReLU and more. [online] Available at:
;; https://mlfromscratch.com/activation-functions-explained [Accessed 28
;; January 2021].
;; - [7] En.wikipedia.org. 2021. Evolutionary algorithm - Wikipedia.
;; [online] Available at:
;; https://en.wikipedia.org/wiki/Evolutionary_algorithm
;; [Accessed 29 September 2021].
;; - [8] En.wikipedia.org. 2021. Karger's algorithm - Wikipedia. [online]
;; Available at: https://en.wikipedia.org/wiki/Karger%27s_algorithm
;; [Accessed 7 December 2021].
;; - [9] En.wikipedia.org. 2021. Las Vegas algorithm - Wikipedia. [online]
;; Available at: https://en.wikipedia.org/wiki/Las_Vegas_algorithm
;; [Accessed 7 December 2021].
;; - [10] Es.wikipedia.org. 2022. Teoría de grafos - Wikipedia, la
;; enciclopedia libre. [online] Available at:
;; https://es.wikipedia.org/wiki/Teor%C3%ADa_de_grafos
;; [Accessed 21 February 2022].
;; - [11] En.wikipedia.org. 2022. Network science - Wikipedia. [online]
;; Available at: https://en.wikipedia.org/wiki/Network_science
;; [Accessed 21 February 2022].
;; - [12] En.wikipedia.org. 2022. Barabási–Albert model - Wikipedia.
;; [online] Available at:
;; https://en.wikipedia.org/wiki/Barab%C3%A1si%E2%80%93Albert_model
;; [Accessed 2 March 2022].
;; - [13] En.wikipedia.org. 2022. Link analysis - Wikipedia. [online]
;; Available at: https://en.wikipedia.org/wiki/Link_analysis
;; [Accessed 2 March 2022].
;; - [16] En.wikipedia.org. 2022. Evolving network - Wikipedia. [online]
;; Available at: https://en.wikipedia.org/wiki/Evolving_network
;; [Accessed 9 March 2022].
;; - [17] En.wikipedia.org. 2022. Integrated information theory -
;; Wikipedia. [online] Available at:
;; https://en.wikipedia.org/wiki/Integrated_information_theory
;; [Accessed 20 September 2022].
;; - [18] Evolutionary acquisition of Neural Topologies. (2022a). Retrieved
;; from
;; https://en.wikipedia.org/wiki/Evolutionary_acquisition_of_neural_topologies
;; - [19] Neuroevolution of augmenting topologies. (2023, March 20).
;; https://en.wikipedia.org/wiki/Neuroevolution_of_augmenting_topologies
;; - [20] Wikimedia Foundation. (2023b, April 17). Catastrophic
;; interference. Wikipedia.
;; https://en.wikipedia.org/wiki/Catastrophic_interference
;; - [21] Wikimedia Foundation. (2022a, September 6). Online machine
;; learning. Wikipedia.
;; https://en.wikipedia.org/wiki/Online_machine_learning
;; - [22] Wikimedia Foundation. (2022a, July 10). Propagación hacia atrás.
;; Wikipedia.
;; https://es.wikipedia.org/wiki/Propagaci%C3%B3n_hacia_atr%C3%A1s
(define-module (grsp grsp8)
#:use-module (grsp grsp0)
#:use-module (grsp grsp1)
#:use-module (grsp grsp2)
#:use-module (grsp grsp3)
#:use-module (grsp grsp4)
#:use-module (grsp grsp5)
#:use-module (grsp grsp9)
#:use-module (grsp grsp10)
#:use-module (grsp grsp11)
#:use-module (ice-9 threads)
#:export (grsp-ann-net-create-000
grsp-ann-net-create-ffv
grsp-ann-net-miter-omth
grsp-ann-net-reconf
grsp-ann-net-preb
grsp-ann-counter-upd
grsp-ann-id-create
grsp-ann-item-create
grsp-ann-nodes-create
grsp-ann2dbc
grsp-dbc2ann
grsp-ann-net-create-ffn
grsp-ann-net-specs-ffn
grsp-ann-net-mutate
grsp-ann-net-mutate-mth
grsp-ann-deletes
grsp-ann-row-updatei
grsp-ann-conns-of-node
grsp-ann-node-eval
grsp-ann-actifun
grsp-ann-nodes-eval
grsp-ann-idata-create
grsp-ann-net-nmutate-omth
grsp-ann-idata-update
grsp-ann-odata-update
grsp-odata2idata
grsp-ann-get-matrix
grsp-ann-matrix-create
grsp-ann-idata-atlorpn
grsp-ann-odata-atlorpn
grsp-ann-odtid-atlorpn
grsp-m2datai
grsp-ann-datai-update
grsp-datai2idata
grsp-ann-fdifm
grsp-ann-fdif
grsp-ann-updatem
grsp-nodes2odata
grsp-odata2datao
grsp-ann-net-size
grsp-ann-node-degree
grsp-ann-net-adegree
grsp-ann-net-density
grsp-ann-net-pdensity
grsp-ann-node-conns
grsp-ann-nodes-conns
grsp-ann-conn-nodes
grsp-ann-conns-nodes
grsp-ann-devt
grsp-ann-devn
grsp-ann-devc
grsp-ann-devcl
grsp-ann-devnc
grsp-ann-devnca
grsp-ann-stats
grsp-ann-display
grsp-ann-updater
grsp-ann-element-number
grsp-ann-get-element
grsp-ann-conns-opmm
grsp-ann-idata-bvw
grsp-ann-delta
grsp-ann-bp
grsp-m2datae
grsp-ds2ann
grsp-ann-ds-create
grsp-ann-nodes-select-linked
grsp-ann-nodes-select-layer
grsp-ann-nodes-select-st
grsp-ann-id-update
grsp-ann-node-info))
;;;; grsp-ann-net-create-000 - Creates an empty neural network as a list
;; data structure with basic, empty matrices as its elements.
;;
;; Keywords:
;;
;; - functions, ann, neural network, matrices, matrix, element, list,
;; skeletal
;;
;; Parameters:
;;
;; - p_b1:
;;
;; - #t: to return lists with one element with zeros as values.
;; - #f: for empty lists.
;;
;; Output:
;;
;; - A list with ten elements, in this order.
;;
;; - Elem 0: nodes, a matrix for the definition of nodes.
;; - Elem 1: conns, a matrix for the definition of connections between
;; those nodes.
;; - Elem 2: count, a 1x4 counter matrix that defines the id of nodes amd
;; conns elements, as well as the epoch and layer counters.
;; - Elem 3: idata, a data input matrix. This is what goes into an ann.
;; - Elem 4: odata, an output matrix. This is what comes out of the
;; output nodes of the ann.
;; - Elem 5: specs, a matrix that contains the structural specifications
;; of an ann.
;; - Elem 6: odtid, a matrix that provides feedback structure from odata
;; to idata.
;; - Elem 7; datai, results to be transfered to idata.
;; - Elem 8: datao, obtained results.
;; - Elem 9: datae, expected results, delta values.
;;
;; - In the case of active networs, these matrices will be filled with non-
;; trivial data. In this case, only trivial data will be placed inside
;; those matrices. This means that this fouction actually produces the
;; structure of a neural network, but not a network per se.
;; - See grsp-ann-net-create-ffv in order to create a non-trivial or empty
;; network.
;;
;; - For more details on thse matrices, see "Format of matrices used in
;; grsp8" above.
;;
(define (grsp-ann-net-create-000 p_b1)
(let ((res1 '())
(nodes 0)
(conns 0)
(count 0)
(idata 0)
(odata 0)
(specs 0)
(odtid 0)
(datai 0)
(datao 0)
(datae 0))
;; Create matrices with just one row.
(set! nodes (grsp-ann-matrix-create "nodes" 1))
(set! conns (grsp-ann-matrix-create "conns" 1))
(set! count (grsp-ann-matrix-create "count" 1))
(set! idata (grsp-ann-matrix-create "idata" 1))
(set! odata (grsp-ann-matrix-create "odata" 1))
(set! specs (grsp-ann-matrix-create "specs" 1))
(set! odtid (grsp-ann-matrix-create "odtid" 1))
(set! datai (grsp-ann-matrix-create "datai" 1))
(set! datao (grsp-ann-matrix-create "datao" 1))
(set! datae (grsp-ann-matrix-create "datae" 1))
;; Rebuild the list and compose results.
(cond ((equal? p_b1 #t)
(set! res1 (list nodes
conns
count
idata
odata
specs)))
(else (set! res1 (grsp-ann-net-preb nodes
conns
count
idata
odata
specs
odtid
datai
datao
datae))))
res1))
;;;; grsp-ann-net-create-ffv - A convenience function that creates a
;; forward feed network of a variable number of layers and elements
;; contained in each layer.
;;
;; Keywords:
;;
;; - functions, ann, neural, network, ffn
;;
;; Parameters:
;;
;; - p_b1:
;;
;; - #t if you want to return only the base ann list composed of matrices
;; nodes, conns, count, idata, odata and empty specs.
;; - #f if you want to return also the associated matrix created during
;; the process as the sixth of the ann list, meaning that this option
;; returns full matrices:
;;
;; - Elem 0: nodes.
;; - Elem 1: conns.
;; - Elem 2: count.
;; - Elem 3: idata.
;; - Elem 4: odata.
;; - Elem 5: specs.
;; - Elem 6: odtid.
;; - Elem 7: datai.
;; - Elem 8: datao.
;; - Elem 9: datae.
;;
;; - p_n2: number of mutation iterations desired.
;; - p_nl: number of nodes in layer 0.
;; - p_nm: number of intermediate layers.
;; - p_nn: number of nodes in intermediate layers.
;; - p_af: activation function for intermediate nodes.
;; - p_nh: number of nodes in final layer.
;;
;; Notes:
;;
;; - This function creates a network with all its associated values as
;; defined by the arguments passed to it.
;; - See also grsp-ann-net-specs-ffn, grsp-ann-net-create-ffn and
;; grsp-ann-net-mutate on how these functions operate.
;; - Mean and standard deviation for grsp-ann-net-mutate are 0.0 and 0.15
;; respectively.
;; - See "Format of matrices used in grsp8" on top of this file for details
;; on each matrix used.
;; - A standard distribution is used for grsp-ann-net-mutate also.
;; - Further configuration of the ann might have to be done after using
;; this function to change parameters such as activation functions per
;; node, weights, etc.
;; - A network created by this function might be used as si or it could be
;; modified later by adding or deleting nodes, connections, etc.
;; - See grsp-ann-net-create-000 to create a trivial network.
;;
;; Examples:
;;
;; - example3.scm.
;;
;; Output:
;;
;; - A list with elements combining the results provided by:
;;
;; - grsp-ann-net-specs-ffn.
;; - grsp-ann-net-create-ffn.
;; - grsp-ann-net-mutate.
;;
(define (grsp-ann-net-create-ffv p_b1 p_n2 p_nl p_nm p_nn p_af p_nh)
(let ((res1 '())
(specs 0)
(odtid 0)
(res3 0)
(l1 '(5 9))
(l3 '(5 7)))
;; Create the ann.
(set! specs (grsp-ann-net-specs-ffn p_nl p_nm p_nn p_af p_nh))
(set! res3 (grsp-ann-net-create-ffn specs))
(set! odtid (grsp-ann-matrix-create "odtid" 1))
;; Mutate in order to randomize values as many tumes as defined by
;; parameter p_n2. In order not t mutate the network, set p_n2 = 0 so
;; that the following cycle gets ignored entirely.
(let loop ((i1 1))
(if (<= i1 p_n2)
(begin (set! res3 (grsp-ann-net-mutate res3
1
"#normal"
0.0
0.15
"#normal"
0.0
0.15
l1
l3))
(loop (+ i1 1)))))
;; Compose results depending on p_b1.
(cond ((equal? p_b1 #f)
(set! res1 (list (grsp-ann-get-matrix "nodes" res3)
(grsp-ann-get-matrix "conns" res3)
(grsp-ann-get-matrix "count" res3)
(grsp-ann-get-matrix "idata" res3)
(grsp-ann-get-matrix "odata" res3)
specs
odtid
(grsp-ann-get-matrix "datai" res3)
(grsp-ann-get-matrix "datao" res3)
(grsp-ann-get-matrix "datae" res3))))
(else (set! res1 res3)))
;; Update idata, odata and odtid tables.
(set! res1 (grsp-ann-idata-atlorpn res1))
(set! res1 (grsp-ann-odata-atlorpn res1))
(set! res1 (grsp-ann-odtid-atlorpn res1))
res1))
;;;; grsp-ann-net-reconf - Reconfigure a neural network.
;;
;; Keywords:
;;
;; - functions, ann, neural, network, config, configuration, adjust
;;
;; Parameters:
;;
;; - p_s1: reconfiguration method.
;;
;; - "#no": none.
;; - "#bp": backpropagation.
;;
;; - p_l1: ann (list).
;;
;; Notes:
;;
;; - TODO: according to (Open Assistant), these algorithms could be used
;; for optimization: genetic algorithms, simulated annealing, particle
;; swarm optimization, evolutionary computation, stochastic gradient
;; descent, batch gradient descent, online gradient descent, and
;; Adaboost.
;;
;; Output:
;;
;; - List p_l1 updated.
;;
(define (grsp-ann-net-reconf p_s1 p_l1)
(let ((res1 '())
(nodes 0)
(conns 0)
(count 0)
(idata 0)
(odata 0)
(specs 0)
(odtid 0)
(datai 0)
(datao 0)
(datae 0))
;; Safety copy.
(set! res1 p_l1)
;; Delete dead elements.
(set! res1 (grsp-ann-deletes res1))
;; Extract matrices and lists.
(set! nodes (grsp-ann-get-matrix "nodes" res1))
(set! conns (grsp-ann-get-matrix "conns" res1))
(set! count (grsp-ann-get-matrix "count" res1))
(set! idata (grsp-ann-get-matrix "idata" res1))
(set! odata (grsp-ann-get-matrix "odata" res1))
(set! specs (grsp-ann-get-matrix "specs" res1))
(set! odtid (grsp-ann-get-matrix "odtid" res1))
(set! datai (grsp-ann-get-matrix "datai" res1))
(set! datao (grsp-ann-get-matrix "datao" res1))
(set! datae (grsp-ann-get-matrix "datae" res1))
;; Callibrate.
(cond ((equal? p_s1 "#bp")
(grsp-ann-bp res1)))
;; Compose results.
(set! res1 (list nodes
conns
count
idata
odata
specs
odtid
datai
datao
datae))
res1))
;;;; grsp-ann-net-miter-omth - Perform evaluations of the network p_n1
;; times (epochs).
;;
;; Keywords:
;;
;; - functions, ann, neural, network, eval, epochal
;;
;; Parameters:
;;
;; - p_b3: #t for verbosity (iterator level).
;; - p_b2: #t for verbosity (nodes eval level).
;; - p_b4: #t for verbosity (node eval level).
;; - p_b1:
;;
;; - #t: use mutithreading.
;; - #f: do not use multithreading.
;;
;; - p_s1: reconfiguration method. See grsp-ann-net-reconf for details.
;; - p_l1: ann (list).
;; - p_n1: desired iterations per epoch.
;; - p_n2: mutation cycles desired after each iteration.
;;
;; Examples:
;;
;; - example3.scm, example26.scm.
;;
;; Output:
;;
;; - List p_l1 updated.
;;
(define (grsp-ann-net-miter-omth p_b3 p_b2 p_b4 p_b1 p_s1 p_l1 p_n1 p_n2)
(let ((res1 '())
(b1 #f)
(idata 0)
(s1 (strings-append (list "\n" (gconsts "En")) 0))
(s2 (strings-append (list "\n" (gconsts "Mi")) 0))
(s3 (strings-append (list "\n" (gconsts "Isoe")) 0))
(i1 0)
(i2 0))
(set! res1 p_l1)
;; Eval loop, go for p_n1 epochs.
(while (< i1 p_n1)
;; If verbosity is on, present iteration data.
(cond ((equal? p_b3 #t)
(display s1)
(display i1)
(display "\n")))
;; Mutate ann if required.
(set! i2 0)
(while (< i2 p_n2)
;; If verbosity is on, present mutation data.
(cond ((equal? p_b3 #t)
(display s2)
(display i2)
(display "\n")))
(set! res1 (grsp-ann-net-nmutate-omth p_b1 res1))
(set! i2 (in i2)))
;; Pass idata info. This means that new idata rows might be
;; added to the said matrix and eventually passed to the neural
;; network on each new iteration.
(set! idata (grsp-ann-get-matrix "idata" res1))
;; If verbosity is on, present epoch data.
(cond ((equal? p_b3 #t)
(display s3)
(display "\n\n")
(grsp-matrix-display idata)
(display "\n")))
(set! b1 (grsp-matrix-is-empty idata))
(cond ((equal? b1 #f)
(set! res1 (grsp-ann-idata-update #t res1))))
;; Evaluate nodes.
(set! res1 (grsp-ann-nodes-eval p_b2 p_b4 res1))
;; Reconfigure ann.
(set! res1 (grsp-ann-net-reconf p_s1 res1))
(set! i1 (in i1)))
res1))
;;;; grsp-ann-net-preb - Purges and rebuilds the net from discarded
;; connections and nodes. While the network can survive and remain useful
;; even if its entropy increases, purging it should be done in order to
;; keep it to its minimum possible size for efficiency reasons.
;;
;; Keywords:
;;
;; - functions, ann, neural, network, clean, simplify, cut, renew
;;
;; Parameters:
;;
;; - p_a1: nodes.
;; - p_a2: conns.
;; - p_a3: countx.
;; - p_a4: idata.
;; - p_a5: odata.
;; - p_a6: specs.
;; - p_a7: odtid.
;; - p_a8: datai.
;; - p_a9: datao.
;; - p_a10: datae.
;;
;; Notes:
;;
;; - In this case, matrices must be passed as separate parameters, not as
;; a list of matrices like in most grsp8 functions.
;; - See "Format of matrices used in grsp8" on top of this file for details
;; on each matrix used.
;;
;; Output:
;;
;; - List.
;;
(define (grsp-ann-net-preb p_a1 p_a2 p_a3 p_a4 p_a5 p_a6 p_a7 p_a8 p_a9 p_a10)
(let ((res1 '())
(s1 "#="))
;; Delete rows where the value of col 1 is zero, meaning that
;; connections and nodes are dead.
(set! p_a2 (grsp-matrix-row-delete s1 p_a2 1 0))
(set! p_a1 (grsp-matrix-row-delete s1 p_a1 1 0))
;; Compose results.
(set! res1 (list p_a1 p_a2 p_a3 p_a4 p_a5 p_a6 p_a7 p_a8 p_a9 p_a10))
res1))
;;;; grsp-ann-counter-upd - Updates the ann id and epoch counters.
;;
;; Keywords:
;;
;; - functions, ann, neural, network, counting, count
;;
;; Parameters:
;;
;; - p_a3: count matrix.
;; - p_n1: matrix element to increment.
;;
;; - 0: updates nodes counter.
;; - 1: updates conns counter.
;; - 2: updates epoch counter.
;; - 3: updates layer counter.
;;
;; Notes:
;;
;; - See "Format of matrices used in grsp8" on top of this file for details
;; on each matrix used.
;;
;; Output:
;;
;; - Numeric. Returns a new id number, either for nodes, conns, epoch or
;; layer number.
;;
(define (grsp-ann-counter-upd p_a3 p_n1)
(let ((res1 0))
(set! res1 (+ (array-ref p_a3 0 p_n1) 1))
(array-set! p_a3 res1 0 p_n1)
res1))
;;;; grsp-ann-id-create - Creates a new id number for a row in nodes or
;; conns and updates the corresponding matrix element.
;;
;; Keywords:
;;
;; - functions, ann, neural, network, primary, key
;;
;; Parameters:
;;
;; - p_a1: nodes.
;; - p_a2: conns.
;; - p_a3: count.
;; - p_n1:
;;
;; - 0: new id for nodes.
;; - 1: new id for conns.
;;
;; Notes:
;;
;; - See "Format of matrices used in grsp8" on top of this file for details
;; on each matrix used.
;;
;; Output:
;;
;; - Matrix.
;;
(define (grsp-ann-id-create p_a1 p_a3 p_n1)
(let ((res1 p_a1)
(hm1 1))
;; Extract the boundaries of the matrix.
(set! hm1 (grsp-matrix-esi 2 res1))
(array-set! res1 (grsp-ann-counter-upd p_a3 p_n1) hm1 0)
res1))
;;;; grsp-ann-item-create - Creates in the ann a node or connection with
;; parameter list p_l2.
;;
;; Keywords:
;;
;; - functions, ann, neural, network, connections, edges, links
;;
;; Parameters:
;;
;; - p_a1: nodes matrix.
;; - p_a2: conns matrix.
;; - p_a3: count matrix.
;; - p_n1:
;;
;; - 0: for nodes.
;; - 1: for conns.
;;
;; - p_l2: list containing the values for the matrix row.
;;
;; Notes:
;;
;; - See "Format of matrices used in grsp8" on top of this file for details
;; on each matrix used.
;;
;; Output:
;;
;; - Matrix.
;;
(define (grsp-ann-item-create p_a1 p_a2 p_a3 p_n1 p_l2)
(let ((res1 0)
(res2 0)
(ln1 0)
(hm1 0)
(nodes p_a1)
(conns p_a2)
(count p_a3))
;; Select matrix.
(set! res1 (grsp-matrix-select nodes conns p_n1))
;; Add row in selected matrix.
(set! res1 (grsp-matrix-subexp res1 1 0))
;; Vectorize list.
(set! res2 (grsp-l2m p_l2))
;; Extract required boundaries of the ann matrix selected.
(set! hm1 (grsp-matrix-esi 2 res1))
(set! ln1 (grsp-matrix-esi 3 res1))
;; Add vector to matrix.
(set! res1 (grsp-matrix-subrep res1 res2 hm1 ln1))
;; Update matrix id counter and set new id on the new row in matrix.
(set! res1 (grsp-ann-id-create res1 count p_n1))
res1))
;;;; grsp-ann-nodes-create - Creates node p_l2 connected according to p_l3
;; in ann p_l1.
;;
;; Keywords:
;;
;; - functions, ann, neural, network, update, add
;;
;; Parameters:
;;
;; - p_l1: list, ann.
;; - p_l2: list, node definition.
;; - p_l3: list of connections for p_l1.
;;
;; Notes:
;;
;; - See "Format of matrices used in grsp8" on top of this file for details
;; on each matrix used.
;;
;; Output:
;;
;; - List. Updated ann.
;;
(define (grsp-ann-nodes-create p_l1 p_l2 p_l3)
(let ((res1 '())
(l2 '())
(l3 '())
(nodes 0)
(conns 0)
(count 0)
(idata 0)
(odata 0)
(specs 0)
(odtid 0)
(datai 0)
(datao 0)
(datae 0)
(hn 0)
(i1 0)
(cn 0)
(cc 0))
;; Extract matrices and lists.
(set! nodes (grsp-ann-get-matrix "nodes" p_l1))
(set! conns (grsp-ann-get-matrix "conns" p_l1))
(set! count (grsp-ann-get-matrix "count" p_l1))
(set! idata (grsp-ann-get-matrix "idata" p_l1))
(set! odata (grsp-ann-get-matrix "odata" p_l1))
(set! specs (grsp-ann-get-matrix "specs" p_l1))
(set! odtid (grsp-ann-get-matrix "odtid" p_l1))
(set! datai (grsp-ann-get-matrix "datai" p_l1))
(set! datao (grsp-ann-get-matrix "datao" p_l1))
(set! datae (grsp-ann-get-matrix "datae" p_l1))
(set! l2 p_l2)
;; Update node count in counter and l2.
(set! cn (array-ref count 0 0))
;; Update id value in l2.
(list-set! l2 0 cn)
;; Create one node per cycle with updated id.
(set! nodes (grsp-ann-item-create nodes conns count 0 l2))
(grsp-ann-counter-upd count 0)
;; Create connections only of node is not initial.
(cond ((equal? (equal? (list-ref l2 2) 0) #f)
;; Create connections. There might be several connections per
;; node in p_l3 each list element is in itself a list
;; representing one connection.
(set! hn (length p_l3))
(while (< i1 hn)
;; Update connection count in counter and l3.
(set! l3 (list-ref p_l3 i1))
(set! cc (array-ref count 0 1))
;; Update id and to values in the list corresponding to
;; the connection to be created.
(list-set! l3 0 cn)
(list-set! l3 4 cc)