-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlvq_rout.c
More file actions
1006 lines (852 loc) · 24.4 KB
/
lvq_rout.c
File metadata and controls
1006 lines (852 loc) · 24.4 KB
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
/************************************************************************
* *
* Program package 'lvq_pak': *
* *
* lvq_rout.c *
* -routines needed in some programs *
* -training routines *
* -classification routines *
* -etc. *
* *
* Version 3.0 *
* Date: 1 Mar 1995 *
* *
* NOTE: This program package is copyrighted in the sense that it *
* may be used for scientific purposes. The package as a whole, or *
* parts thereof, cannot be included or used in any commercial *
* application without written permission granted by its producents. *
* No programs contained in this package may be copied for commercial *
* distribution. *
* *
* All comments concerning this program package may be sent to the *
* e-mail address 'lvq@cochlea.hut.fi'. *
* *
************************************************************************/
#include <stdio.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include "lvq_pak.h"
#include "lvq_rout.h"
#include "datafile.h"
/* Check whether the vector 'code' (codebook vector) is correctly
classified by knn-classification with respect to the codebook
'data'. Return 1 if correct, 0 if incorrect, -1 on error */
int correct_by_knn(struct entries *data, struct data_entry *code, int knn,
WINNER_FUNCTION *find_knn)
{
int corr = 0;
int i;
int codelabel;
struct winner_info *winners;
struct hitlist *hits;
hits = new_hitlist();
if (hits == NULL)
return -1;
if (knn < 1) knn = 1;
winners = malloc(sizeof(struct winner_info) * knn);
if (winners == NULL)
{
perror("correct_by_knn");
free_hitlist(hits);
return -1;
}
/* Find nearest neighbours. Note: data is codebook, code is sample */
if (find_knn(data, code, winners, knn) != knn)
{
fprintf(stderr, "correct_by_knn: can't find winners\n");
corr = -1;
goto end;
}
for (i = 0; i < knn; i++)
add_hit(hits, get_entry_label(winners[i].winner));
codelabel = get_entry_label(code);
if (hits->head)
if (hits->head->label == codelabel)
corr = 1;
end:
free_hitlist(hits);
free(winners);
return(corr);
}
/* Pick a given number of entries from the beginning of the entry list */
struct entries *pick_codes(int num, struct entries *data)
{
struct entries *datac;
struct data_entry *prev, *loca, tmp;
eptr p;
datac = copy_entries(data);
if (datac == NULL)
{
fprintf(stderr, "extract_codes: can't copy entries structure\n");
return NULL;
}
/* Pick (at most) 'num' entries from beginning */
if ((loca = rewind_entries(data, &p)) == NULL)
{
fprintf(stderr, "pick_codes: can't get data\n");
close_entries(datac);
return NULL;
}
prev = &tmp;
tmp.next = NULL;
while ((loca != NULL) && (num--)) {
prev->next = copy_entry(data, loca);
prev = prev->next;
datac->num_entries++;
loca = next_entry(&p);
}
datac->entries = tmp.next;
datac->num_loaded = datac->num_entries;
datac->flags.totlen_known = 1;
return(datac);
}
/* Pick a given number of entries of given class from entry list */
struct data_entry *pick_known_codes(int num, struct entries *data, int label)
{
struct data_entry *prev, *loca, tmp;
eptr p;
if ((loca = rewind_entries(data, &p)) == NULL)
{
fprintf(stderr, "pick_known_codes: can't get data\n");
return NULL;
}
prev = &tmp;
tmp.next = NULL;
while ((loca != NULL) && (num)) {
if (get_entry_label(loca) == label) {
prev->next = copy_entry(data, loca);
prev = prev->next;
num--;
}
loca = next_entry(&p);
}
return(tmp.next);
}
/* Pick a given number of entries of each class from entry list. The
numbers of entries are given in an array. The selected entries
should fall inside class borders */
struct data_entry *pick_inside_codes(struct hitlist *classes,
struct entries *data, int knn,
WINNER_FUNCTION *find_knn)
{
int still, datalabel;
long total = 0;
struct data_entry *prev, *loca, tmp;
struct hit_entry *class;
eptr p;
/* Pick (at most) 'topick' entries from the beginning of each class
so that they are also correctly classified by KNN */
for (class = classes->head, total = 0; class != NULL; class = class->next)
total += class->freq;
if ((loca = rewind_entries(data, &p)) == NULL)
{
fprintf(stderr, "pick_inside_codes: can't get data\n");
return NULL;
}
prev = &tmp;
tmp.next = NULL;
still = 1;
ifverbose(1)
mprint((long) total);
while ((total) && (loca != NULL)) {
datalabel = get_entry_label(loca);
class = find_hit(classes, datalabel);
if (class)
if (class->freq > 0) {
/* test if it is correctly classified */
if (correct_by_knn(data, loca, knn, find_knn))
{
ifverbose(1)
mprint((long) total);
total--;
prev->next = copy_entry(data, loca);
if (prev->next == NULL)
{
fprintf(stderr, "pick_inside_codes: can't copy entry\n");
return tmp.next;
}
prev = prev->next;
class->freq--;
}
}
loca = next_entry(&p);
}
ifverbose(1)
{
mprint((long) 0);
fprintf(stderr, "\n");
}
return(tmp.next);
}
/* Pick one entry from a given class. */
struct data_entry *force_pick_code(struct entries *data, int ind)
{
struct data_entry *prev, *loca, tmp;
eptr p;
/* pick one entry from a given class */
if ((loca = rewind_entries(data, &p)) == NULL)
{
fprintf(stderr, "force_pick_codes: can't get data\n");
return NULL;
}
prev = &tmp;
tmp.next = NULL;
while (loca != NULL) {
if (get_entry_label(loca) == ind) {
prev->next = copy_entry(data, loca);
prev = prev->next;
break;
}
loca = next_entry(&p);
}
return(tmp.next);
}
struct mindists *alloc_mindists(void)
{
struct mindists *md;
if ((md = malloc(sizeof(struct mindists))) == NULL)
return NULL;
md->num_classes = 0;
md->class = NULL;
md->dists = NULL;
md->devs = NULL;
md->noe = NULL;
if ((md->classes = new_hitlist()) == NULL)
{
free(md);
return NULL;
}
return md;
}
void free_mindists(struct mindists *md)
{
if (md)
{
if (md->classes)
free_hitlist(md->classes);
if (md->class)
free(md->class);
if (md->dists)
free(md->dists);
if (md->devs)
free(md->devs);
if (md->noe)
free(md->noe);
free(md);
}
}
/* Compute the average shortest distances */
struct mindists *min_distances(struct entries *codes, DIST_FUNCTION *distance)
{
long nol, i;
int note, fou, dim;
float *dists;
int *class, *noe;
float dissf, dist;
struct data_entry *entr, *ensu, *d;
eptr p, p2;
struct hitlist *classes;
struct hit_entry *h;
struct mindists *md;
if (distance == NULL)
distance = vector_dist_euc;
if ((md = alloc_mindists()) == NULL)
return NULL;
classes = md->classes;
for (d = rewind_entries(codes, &p); d != NULL; d = next_entry(&p))
add_hit(classes, get_entry_label(d));
nol = classes->entries; /* number of classes */
md->num_classes = nol;
class = calloc(nol, sizeof(int));
if ((md->class = class) == NULL)
{
free_mindists(md);
return NULL;
}
noe = calloc(nol, sizeof(int));
if ((md->noe = noe) == NULL)
{
free_mindists(md);
return NULL;
}
dists = calloc(nol, sizeof(float));
if ((md->dists = dists) == NULL)
{
free_mindists(md);
return NULL;
}
dim = codes->dimension;
for (i = 0, h = classes->head; i < nol; i++, h = h->next)
{
dists[i] = 0.0;
class[i] = h->label;
noe[i] = h->freq;
entr = rewind_entries(codes, &p);
p2.parent = p.parent;
note = 0;
while (entr != NULL)
{
if (get_entry_label(entr) == class[i]) {
p2.current = p.current;
p2.index = p.index;
ensu = next_entry(&p2);
dissf = FLT_MAX;
fou = 0;
while (ensu != NULL)
{
if (get_entry_label(ensu) == class[i])
{
fou = 1;
dist = distance(ensu, entr, dim);
if (dist < dissf)
dissf = dist;
}
ensu = next_entry(&p2);
}
if (fou)
{
dists[i] += dissf;
note++;
}
}
entr = next_entry(&p);
}
if (note > 0)
dists[i] /= note;
}
return(md);
}
/* Comparison routine for the distances (used in qsort) */
int compar(const void *a, const void *b)
{
if (*(float *)a < *(float *)b)
return(-1);
if (*(float *)a > *(float *)b)
return(1);
return(0);
}
/* Compute the median of shortest distances */
struct mindists *med_distances(struct entries *codes, DIST_FUNCTION *distance)
{
long i, nol;
int not, mnoe, fou, dim;
int *class, *noe;
float *dists;
float dissf, dist;
float *meds;
struct data_entry *entr, *ensu, *d;
struct hitlist *classes;
struct hit_entry *h;
struct mindists *md;
eptr p, p2;
dim = codes->dimension;
if (distance == NULL)
distance = vector_dist_euc;
if ((md = alloc_mindists()) == NULL)
return NULL;
classes = md->classes;
/* find out number of labels in each class */
for (d = rewind_entries(codes, &p); d != NULL; d = next_entry(&p))
add_hit(classes, get_entry_label(d));
nol = classes->entries; /* number of classes */
md->num_classes = nol;
class = calloc(nol, sizeof(int));
if ((md->class = class) == NULL)
{
free_mindists(md);
return NULL;
}
noe = calloc(nol, sizeof(int));
if ((md->noe = noe) == NULL)
{
free_mindists(md);
return NULL;
}
dists = calloc(nol, sizeof(float));
if ((md->dists = dists) == NULL)
{
free_mindists(md);
return NULL;
}
#if 0
devs = calloc(nol, sizeof(float));
if ((md->devs = devs) == NULL)
{
free_mindists(md);
return NULL;
}
#endif
for (i = 0; i < nol; i++)
dists[i] = 0.0;
/* Find the max number of entries in one class */
mnoe = classes->head->freq;
/* Allocate space for the distances (to find the median) */
meds = (float *) oalloc(sizeof(float) * mnoe);
for (i = 0, h = classes->head; i < nol; i++, h = h->next)
{
dists[i] = 0.0;
class[i] = h->label;
noe[i] = h->freq;
not = 0;
entr = rewind_entries(codes, &p);
p2.parent = p.parent;
while (entr != NULL) {
if (get_entry_label(entr) == class[i]) {
p2.current = p.current;
p2.index = p.index;
ensu = next_entry(&p2);
dissf = FLT_MAX;
fou = 0;
while (ensu != NULL) {
if (get_entry_label(ensu) == class[i])
{
dist = 0.0;
fou = 1;
dist = distance(ensu, entr, dim);
if (dist < dissf) {
dissf = dist;
}
}
ensu = next_entry(&p2);
}
if (fou)
meds[not++] = dissf;
}
entr = next_entry(&p);
}
if (not > 0) {
/* find the median */
qsort((void *) meds, not, sizeof(float), compar);
dists[i] = meds[not/2];
}
}
ofree(meds);
return(md);
}
/* Train by lvq1; the nearest codebook vector is modified. If
classification is correct, move it towards the input entry; if
classification is incorrect, move it away from the input entry */
struct entries *lvq1_training(struct teach_params *teach)
{
long le, total_length, length = teach->length;
int label, dim;
int numofe;
float shortest;
float talpha;
struct data_entry *datatmp, *best;
struct winner_info win;
VECTOR_ADAPT *adapt_vector = teach->vector_adapt;
WINNER_FUNCTION *find_winner = teach->winner;
ALPHA_FUNC *get_alpha = teach->alpha_func;
struct entries *data = teach->data;
struct entries *codes = teach->codes;
struct snapshot_info *snap = teach->snapshot;
float alpha = teach->alpha;
eptr p;
total_length = length;
dim = codes->dimension;
if ((datatmp = rewind_entries(data, &p)) == NULL)
{
fprintf(stderr, "lvq1_training: can't get data\n");
return NULL;
}
numofe = data->flags.totlen_known ? data->num_entries : 0;
for (le = 0; le < length; le++, datatmp = next_entry(&p))
{
if (datatmp == NULL)
{
datatmp = rewind_entries(data, &p);
if (datatmp == NULL)
{
fprintf(stderr, "lvq1_training: can't rewind data (%ld/%ld iterations)\n",
le, length);
return NULL;
}
}
find_winner(codes, datatmp, &win, 1);
shortest = win.diff;
best = win.winner;
label = get_entry_label(best);
talpha = get_alpha(le, length, alpha);
/* Was the classification correct? If classification was
correct; move towards, else move away */
if (label == get_entry_label(datatmp))
adapt_vector(best, datatmp, dim, talpha);
else
adapt_vector(best, datatmp, dim, -talpha);
/* save snapshot when needed */
if ((snap) && ((le % snap->interval) == 0) && (le > 0))
{
ifverbose(3)
fprintf(stderr, "Saving snapshot, %ld iterations\n", le);
if (save_snapshot(teach, le))
{
fprintf(stderr, "snapshot failed\n");
}
}
ifverbose(1)
mprint(length - le);
}
ifverbose(1)
fprintf(stderr, "\n");
return(codes);
}
/* Train by olvq1, whereby optimized alpha values are used. The
nearest code vector is modified; If classification is correct, move
it towards the input entry, if classification is incorrect, move it
away from the input entry */
struct entries *olvq1_training(struct teach_params *teach,
char *infile, char *outfile)
{
long i, le, noc, length = teach->length;
int label;
int numofe;
int potobe, dim;
float shortest;
float *talpha;
struct data_entry *datatmp, *best;
VECTOR_ADAPT *adapt = teach->vector_adapt;
WINNER_FUNCTION *find_winner = teach->winner;
/* ALPHA_FUNC *get_alpha = teach->alpha_func; */
struct entries *data = teach->data;
struct entries *codes = teach->codes;
struct snapshot_info *snap = teach->snapshot;
float alpha = teach->alpha;
struct winner_info win;
eptr p;
dim = codes->dimension;
rewind_entries(codes, &p); /* make sure codes are loaded */
noc = codes->num_entries;
/* Get the alpha values: */
/* There are several possibilities: the user may define them; */
/* if not, then they may be read from file; */
/* if no file exists, then default values are used. */
talpha = (float *) oalloc(sizeof(float) * noc);
if (alpha == 0.0) {
if (!alpha_read(talpha, noc, infile)) {
alpha = 0.3;
for (i = 0; i < noc; i++) {
talpha[i] = alpha;
}
}
}
else {
for (i = 0; i < noc; i++) {
talpha[i] = alpha;
}
}
if ((datatmp = rewind_entries(data, &p)) == NULL)
{
fprintf(stderr, "olvq1_training: can't get data\n");
return NULL;
}
numofe = data->flags.totlen_known ? data->num_entries : 0;
for (le = 0; le < length; le++, datatmp = next_entry(&p))
{
if (datatmp == NULL)
{
datatmp = rewind_entries(data, &p);
if (datatmp == NULL)
{
fprintf(stderr, "olvq1_training: can't rewind data (%ld/%ld iterations)\n",
le, length);
return NULL;
}
}
find_winner(codes, datatmp, &win, 1);
shortest = win.diff;
best = win.winner;
label = get_entry_label(best);
potobe = win.index;
/* Individual alphas for every codebook vector; */
/* Was the classification correct? */
if (label == get_entry_label(datatmp)) {
/* If classification was correct, move towards */
adapt(best, datatmp, dim, talpha[potobe]);
talpha[potobe] = talpha[potobe] / (1 + talpha[potobe]);
}
else {
/* If classification was incorrect, move away */
adapt(best, datatmp, dim, -talpha[potobe]);
talpha[potobe] = talpha[potobe] / (1 - talpha[potobe]);
if (talpha[potobe] > alpha)
talpha[potobe] = alpha;
}
/* save snapshot when needed */
if ((snap) && ((le % snap->interval) == 0) && (le > 0))
{
ifverbose(3)
fprintf(stderr, "Saving snapshot, %ld iterations\n", le);
if (save_snapshot(teach, le))
{
fprintf(stderr, "snapshot failed\n");
}
}
ifverbose(1)
mprint(length - le);
}
ifverbose(1)
fprintf(stderr, "\n");
/* Store the alphas */
alpha_write(talpha, noc, outfile);
return(codes);
}
/* Train by lvq2. Two nearest codebook vectors are modified under
specified conditions */
struct entries *lvq2_training(struct teach_params *teach, float winlen)
{
long le, total_length, numofe;
int label, nlabel, datalabel, dim;
float shortest, nshortest;
float talpha;
struct data_entry *datatmp;
struct data_entry *best, *nbest, *ntmp;
VECTOR_ADAPT *adapt = teach->vector_adapt;
WINNER_FUNCTION *find_winners = teach->winner;
ALPHA_FUNC *get_alpha = teach->alpha_func;
struct entries *data = teach->data;
struct entries *codes = teach->codes;
long length = teach->length;
float alpha = teach->alpha;
struct snapshot_info *snap = teach->snapshot;
struct winner_info win[2];
eptr p;
dim = codes->dimension;
total_length = length;
if ((datatmp = rewind_entries(data, &p)) == NULL)
{
fprintf(stderr, "lvq2_training: can't get data\n");
return NULL;
}
numofe = data->flags.totlen_known ? data->num_entries : 0;
for (le = 0; le < length; le++, datatmp = next_entry(&p))
{
if (datatmp == NULL)
{
datatmp = rewind_entries(data, &p);
if (datatmp == NULL)
{
fprintf(stderr, "lvq2_training: can't rewind data (%ld/%ld iterations)\n",
le, length);
return NULL;
}
}
/* True alpha is decreasing linearly during the training */
talpha = get_alpha(le, length, alpha);
/* find two best mathing units */
find_winners(codes, datatmp, win, 2);
shortest = win[0].diff;
best = win[0].winner;
label = get_entry_label(best);
nshortest = win[1].diff;
nbest = win[1].winner;
nlabel = get_entry_label(nbest);
datalabel = get_entry_label(datatmp);
/* Corrections are made only if the two nearest codebook vectors
belong to different classes, one of them correct, and if the
input entry is located inside a window between the nearest codebook
vectors */
if (label != nlabel) {
if ((label == datalabel) || (nlabel == datalabel)) {
/* If the ration of distances to the two nearest codebook vectors
satisfies a condition */
if ((shortest/nshortest) > ((1-winlen)/(1+winlen))) {
/* If the second best is correct swap the entries */
if (nlabel == datalabel) {
ntmp = best;
best = nbest;
nbest = ntmp;
}
/* Move the entries */
adapt(best, datatmp, dim, talpha);
adapt(nbest, datatmp, dim, -talpha);
}
}
}
/* save snapshot when needed */
if ((snap) && ((le % snap->interval) == 0) && (le > 0))
{
ifverbose(3)
fprintf(stderr, "Saving snapshot, %ld iterations\n", le);
if (save_snapshot(teach, le))
{
fprintf(stderr, "snapshot failed\n");
}
}
ifverbose(1)
mprint(length - le);
}
ifverbose(1)
fprintf(stderr, "\n");
return(codes);
}
/* Train by lvq3. Two nearest codebook vectors are modified under
specified conditions */
struct entries *lvq3_training(struct teach_params *teach,
float epsilon, float winlen)
{
long le, numofe, total_length;
int label, nlabel, datalabel, dim;
float shortest, nshortest;
float talpha;
struct data_entry *datatmp, *best, *nbest, *ntmp;
VECTOR_ADAPT *adapt = teach->vector_adapt;
WINNER_FUNCTION *find_winners = teach->winner;
ALPHA_FUNC *get_alpha = teach->alpha_func;
struct entries *data = teach->data;
struct entries *codes = teach->codes;
struct snapshot_info *snap = teach->snapshot;
long length = teach->length;
float alpha = teach->alpha;
struct winner_info win[2];
eptr p;
dim = codes->dimension;
total_length = length;
if ((datatmp = rewind_entries(data, &p)) == NULL)
{
fprintf(stderr, "lvq3_training: can't get data\n");
return NULL;
}
numofe = data->flags.totlen_known ? data->num_entries : 0;
for (le = 0; le < length; le++, datatmp = next_entry(&p))
{
if (datatmp == NULL)
{
datatmp = rewind_entries(data, &p);
if (datatmp == NULL)
{
fprintf(stderr, "lvq3_training: can't rewind data (%ld/%ld iterations)\n",
le, length);
return NULL;
}
}
/* True alpha is decreasing linearly during the training */
talpha = get_alpha(le, length, alpha);
/* find two best mathing units */
find_winners(codes, datatmp, win, 2);
shortest = win[0].diff;
best = win[0].winner;
label = get_entry_label(best);
nshortest = win[1].diff;
nbest = win[1].winner;
nlabel = get_entry_label(nbest);
datalabel = get_entry_label(datatmp);
/* Corrections are made if the two nearest codebook vectors
belong to different classes, one of them correct, and if the
input entry is located inside a window between the nearest codebook
vectors OR the two nearest codebook vectors both belong to the
correct class */
if (label != nlabel) {
if ((label == datalabel) || (nlabel == datalabel)) {
/* If the ration of distances to the two nearest codebook vectors
satisfies a condition */
if ((shortest/nshortest) > ((1-winlen)/(1+winlen))) {
/* If the second best is correct swap the entries */
if (nlabel == datalabel) {
ntmp = best;
best = nbest;
nbest = ntmp;
}
/* Move the entries */
adapt(best, datatmp, dim, talpha);
adapt(nbest, datatmp, dim, -talpha);
}
}
}
else {
if (label == datalabel) {
/* Move the entries, both toward */
adapt(best, datatmp, dim, talpha * epsilon);
adapt(nbest, datatmp, dim, talpha * epsilon);
}
}
/* save snapshot when needed */
if ((snap) && ((le % snap->interval) == 0) && (le > 0))
{
ifverbose(3)
fprintf(stderr, "Saving snapshot, %ld iterations\n", le);
if (save_snapshot(teach, le))
{
fprintf(stderr, "snapshot failed\n");
}
}
ifverbose(1)
mprint(length - le);
}
ifverbose(1)
fprintf(stderr, "\n");
return(codes);
}
float devdist( float *v1, float *v2, int dim)
{
float diff, d;
d = 0.0;
while (dim-- > 0) {
diff = *v1++ - *v2++;
d += diff*diff;
}
return(d);
}
struct mindists *deviations(struct entries *codes, struct mindists *md)
{
int i, j;
int nol, dim, *noe, label;
int *count;
float *devs;
float *avers;
struct data_entry *entr;
eptr p;
dim = codes->dimension;
nol = md->num_classes;
noe = md->noe;
count = (int *) oalloc(sizeof(int) * nol);
devs = calloc(nol, sizeof(float));
if ((md->devs = devs) == NULL)
{
return NULL;
}
avers = (float *) oalloc(sizeof(float) * nol * dim);
for (i = 0; i < nol; i++) {
devs[i] = 0.0;
for (j = 0; j < dim; j++) {
avers[i*dim+j] = 0.0;
}
count[i] = 0;
}
/* Compute averages */
entr = rewind_entries(codes, &p);
while (entr != NULL) {
label = get_entry_label(entr);
for (i = 0; i < nol; i++)
if (label == md->class[i])
break;
for (j = 0; j < dim; j++)
if (!((entr->mask != NULL) && (entr->mask[j] != 0)))
{
avers[i*dim+j] += entr->points[j];
}
/* count[i]++; */
entr = next_entry(&p);
}
for (i = 0; i < nol; i++) {
for (j = 0; j < dim; j++) {
avers[i*dim+j] /= noe[i];
}
}
/* Compute deviations */
entr = rewind_entries(codes, &p);
while (entr != NULL) {
label = get_entry_label(entr);
for (i = 0; i < nol; i++)
if (label == md->class[i])
break;
devs[i] += devdist(entr->points,
&(avers[i*dim]), dim);
entr = next_entry(&p);
}
for (i = 0; i < nol; i++) {
devs[i] = sqrt(devs[i]/noe[i]);