-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlvqtrain.c
255 lines (227 loc) · 8.7 KB
/
lvqtrain.c
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
/************************************************************************
* *
* Program package 'lvq_pak': *
* *
* *
* lvqtrain *
* - train a codebook with one of the following algorithms: *
* lvq1 *
* - Learning Vector Quantization Type 1 *
* lvq2 *
* - Learning Vector Quantization Type 2 *
* lvq3 *
* - Learning Vector Quantization Type 3 *
* olvq1 *
* - Optimized-learning-rate LVQ1 *
* *
* Version 3.1 *
* Date: 7 Apr 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 <string.h>
#include "lvq_pak.h"
#include "lvq_rout.h"
#include "datafile.h"
static char *usage[] = {
"lvqtrain/lvq1/lvq2/lvq3/olvq1 - teach codebook with one of the lvq algorithms\n",
" Training algorithm is determined from program name and can be overridden\n",
" with the -type option.\n",
"Required parameters:\n",
" -cin filename initial codebook file\n",
" -din filename teaching data\n",
" -cout filename output codebook filename\n",
" -rlen integer running length of teaching\n",
" -alpha float initial alpha value (optional with olvq1)\n",
" -win float (lvq2, lvq3) window width\n",
" -epsilon float (lvq3) training epsilon\n",
"Optional parameters:\n",
" -type lvqtype select which lvq algoritm to use: lvq1, lvq2,\n",
" lvq3 or olvq1\n",
" -rand integer seed for random number generator. 0 is current time\n",
" -buffer integer buffered reading of data, integer lines at a time\n",
" -alpha_type type type of alpha decrease, linear (def) or inverse_t.\n",
" -snapfile filename snapshot filename\n",
" -snapinterval integer interval between snapshots\n",
" -selfuncs name select a set of functions\n",
NULL};
#define LVQ1 1
#define LVQ2 2
#define LVQ3 3
#define OLVQ1 4
struct typelist lvq_types[] = {
{LVQ1, "lvq1", lvq1_training},
{LVQ2, "lvq2", lvq2_training},
{LVQ3, "lvq3", lvq3_training},
{OLVQ1, "olvq1", olvq1_training},
{0, NULL, NULL}};
int main(int argc, char **argv)
{
char *in_data_file, *in_code_file, *out_code_file;
char *progname, *typename, *alpha_s, *rand_s;
char *snapshot_file;
struct typelist *lvqt, *type_tmp;
struct entries *data, *codes, *codes2;
int lvqtype, randomize;
long buffer, snapshot_interval;
float winlen = 0.0, epsilon = 0.0;
struct teach_params params;
struct snapshot_info *snap = NULL;
int snap_type;
char *funcname = NULL;
global_options(argc, argv);
if (extract_parameter(argc, argv, "-help", OPTION2))
{
printhelp();
exit(0);
}
/* get program name */
progname = getprogname();
typename = extract_parameter(argc, argv, "-type", OPTION);
if (typename)
progname = typename;
/* select which lvq algorithm to use */
lvqt = get_type_by_str(lvq_types, progname);
if ((lvqtype = lvqt->id) == 0)
{
fprintf(stderr, "Unknown LVQ type %s\n", progname);
exit(1);
}
in_data_file = extract_parameter(argc, argv, IN_DATA_FILE, ALWAYS);
in_code_file = extract_parameter(argc, argv, IN_CODE_FILE, ALWAYS);
out_code_file = extract_parameter(argc, argv, OUT_CODE_FILE, ALWAYS);
params.length = oatoi(extract_parameter(argc, argv, RUNNING_LENGTH, ALWAYS),
1);
rand_s = extract_parameter(argc, argv, RANDOM, OPTION);
randomize = oatoi(rand_s, 0);
buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0);
alpha_s = extract_parameter(argc, argv, "-alpha_type", OPTION);
snapshot_file = extract_parameter(argc, argv, "-snapfile", OPTION);
snapshot_interval =
oatoi(extract_parameter(argc, argv, "-snapinterval", OPTION), 0);
snap_type =
get_id_by_str(snapshot_list,
extract_parameter(argc, argv, "-snaptype", OPTION));
if (snapshot_interval)
{
if (snapshot_file == NULL)
{
snapshot_file = out_code_file;
fprintf(stderr, "snapshot file not specified, using '%s'", snapshot_file);
}
snap = get_snapshot(snapshot_file, snapshot_interval, snap_type);
if (snap == NULL)
exit(1);
}
switch(lvqtype)
{
case OLVQ1:
params.alpha = oatof(extract_parameter(argc, argv, TRAINING_ALPHA, OPTION), 0.0);
break;
case LVQ2:
params.alpha = atof(extract_parameter(argc, argv, TRAINING_ALPHA, ALWAYS));
winlen = atof(extract_parameter(argc, argv, WINDOW_WIDTH, ALWAYS));
break;
case LVQ3:
params.alpha = atof(extract_parameter(argc, argv, TRAINING_ALPHA, ALWAYS));
epsilon = atof(extract_parameter(argc, argv, TRAINING_EPSILON, ALWAYS));
winlen = atof(extract_parameter(argc, argv, WINDOW_WIDTH, ALWAYS));
break;
case LVQ1:
default:
params.alpha = atof(extract_parameter(argc, argv, TRAINING_ALPHA, ALWAYS));
break;
}
ifverbose(2)
fprintf(stderr, "Input entries are read from file %s\n", in_data_file);
if ((data = open_entries(in_data_file)) == NULL)
{
fprintf(stderr, "Can't open data file '%s'\n", in_data_file);
exit(1);
}
ifverbose(2)
fprintf(stderr, "Codebook entries are read from file %s\n", in_code_file);
if ((codes = open_entries(in_code_file)) == NULL)
{
fprintf(stderr, "Can't open code file '%s'\n", in_data_file);
close_entries(data);
exit(1);
}
if (data->dimension != codes->dimension) {
fprintf(stderr, "Data and codebook vectors have different dimensions");
close_entries(data);
close_entries(codes);
exit(1);
}
set_teach_params(¶ms, codes, data, buffer, funcname);
init_random(randomize);
if (rand_s)
data->flags.random_order = 1;
/* alpha type */
if (alpha_s)
{
type_tmp = get_type_by_str(alpha_list, alpha_s);
if (type_tmp->data == NULL)
{
fprintf(stderr, "Unknown alpha type %s\n", alpha_s);
close_entries(data);
close_entries(codes);
exit(1);
}
}
else
type_tmp = get_type_by_id(alpha_list, ALPHA_LINEAR);
params.alpha_type = type_tmp->id;
params.alpha_func = type_tmp->data;
params.snapshot = snap;
switch (lvqtype)
{
case LVQ1:
codes2 = lvq1_training(¶ms);
break;
case OLVQ1:
codes2 = olvq1_training(¶ms, in_code_file,
out_code_file);
break;
case LVQ2:
params.winner = find_winner_knn;
codes2 = lvq2_training(¶ms, winlen);
break;
case LVQ3:
params.winner = find_winner_knn;
codes2 = lvq3_training(¶ms, epsilon, winlen);
break;
default:
fprintf(stderr, "Unknown LVQ type?!?!?\n");
close_entries(data);
close_entries(codes);
exit(1);
break;
}
if (codes2 == NULL)
{
fprintf(stderr, "Teaching failed\n");
close_entries(data);
close_entries(codes);
exit(1);
}
ifverbose(2)
fprintf(stdout, "Codebook entries are saved to file %s\n", out_code_file);
save_entries(codes, out_code_file);
invalidate_alphafile(out_code_file);
close_entries(data);
close_entries(codes);
return(0);
}