-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvq.c
179 lines (147 loc) · 3.95 KB
/
vq.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
// vq.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "learn.h"
#define OUTPUTS 7
#define RATE 0.01
#define SQR( x ) ( ( x ) * ( x ) )
static double outputs[ OUTPUTS ];
static double weights[ OUTPUTS ][ MAX_FEATURES ];
static int input_feature_vector[ MAX_FEATURES ];
#define NORMALIZE( x ) ( ( ( double )x + 1.0 ) / 2.0 )
double distance( double x, double y )
{
return sqrt( SQR( ( x - y ) ) );
}
void vq_initialize( void )
{
int output, feature;
// Initialize weights
for ( output = 0 ; output < OUTPUTS ; output++ )
{
outputs[ output ] = 0.0;
for ( feature = 0 ; feature < MAX_FEATURES ; feature++ )
{
weights[ output ][ feature ] = 0.0;
}
}
return;
}
int vq_feedforward( void )
{
int best;
double best_value;
// Given the current input feature vector, compute each output node.
for ( int output = 0 ; output < OUTPUTS ; output++ )
{
outputs[ output ] = 0.0;
for ( int feature = 0 ; feature < MAX_FEATURES ; feature++ )
{
outputs[ output ] +=
distance( NORMALIZE( weights[ output ][ feature ] ) , input_feature_vector[ feature ] );
}
// Keep track of the best activation
if ( output == 0 )
{
best = 0;
best_value = outputs[ output ];
}
else
{
if ( outputs[ output ] < best_value )
{
best = output;
best_value = outputs[ output ];
}
}
}
return best;
}
void vq_updateweights( int class )
{
for ( int feature = 0 ; feature < MAX_FEATURES ; feature++ )
{
weights[ class ][ feature ] +=
RATE * ( (double) NORMALIZE( input_feature_vector[ feature ] ) -
weights[ class ][ feature ] );
}
return;
}
void vq_set_input_vector( observation *obs )
{
input_feature_vector[ 0 ] = obs->hair;
input_feature_vector[ 1 ] = obs->feathers;
input_feature_vector[ 2 ] = obs->eggs;
input_feature_vector[ 3 ] = obs->milk;
input_feature_vector[ 4 ] = obs->airborne;
input_feature_vector[ 5 ] = obs->aquatic;
input_feature_vector[ 6 ] = obs->predator;
input_feature_vector[ 7 ] = obs->toothed;
input_feature_vector[ 8 ] = obs->backbone;
input_feature_vector[ 9 ] = obs->breathes;
input_feature_vector[ 10 ] = obs->venomous;
input_feature_vector[ 11 ] = obs->fins;
input_feature_vector[ 12 ] = obs->legs_0;
input_feature_vector[ 13 ] = obs->legs_2;
input_feature_vector[ 14 ] = obs->legs_4;
input_feature_vector[ 15 ] = obs->legs_5;
input_feature_vector[ 16 ] = obs->legs_6;
input_feature_vector[ 17 ] = obs->legs_8;
input_feature_vector[ 18 ] = obs->tail;
input_feature_vector[ 19 ] = obs->domestic;
input_feature_vector[ 20 ] = obs->catsize;
return;
}
void vq_train( FILE *fptr, long iterations )
{
int result;
observation obs;
long iteration = 0;
// Initialize the first N observations to the N classes.
for ( int class = 0 ; class < OUTPUTS ; class++ )
{
result = get_observation( fptr, &obs );
vq_set_input_vector( &obs );
vq_updateweights( class );
}
while ( iteration < iterations )
{
result = get_observation( fptr, &obs );
if ( !result )
{
// Reset the file position to the beginning.
fseek( fptr, 0L, SEEK_SET );
iteration++;
}
else
{
vq_set_input_vector( &obs );
int class = vq_feedforward( );
vq_updateweights( class );
}
}
return;
}
void vq_validate( FILE *fptr, FILE *fout )
{
int result;
observation obs;
// Reset the input file
fseek( fptr, 0L, SEEK_SET );
while ( 1 )
{
result = get_observation( fptr, &obs );
if ( !result )
{
break;
}
else
{
vq_set_input_vector( &obs );
int class = vq_feedforward( );
fprintf( fout, "%s,%d\n", obs.name, class );
}
}
return;
}