-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_perceptron.c
207 lines (178 loc) · 5.99 KB
/
sequential_perceptron.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
#include <stdio.h>
#include <math.h>
//For benchmarking
#include <time.h>
int main(int argc, const char * argv[]) {
//Start benchmarking
float startTime = (float)clock()/CLOCKS_PER_SEC;
//Initialize our training set (This comes from the first 100 rows of the famous Iris dataset)
//The first four columns are the input attributes and the last column in the label
// 1 & -1 denote two different species of flowers that can be linearly separable
static const float trainingSet[100][5] = {
{5.1,3.5,1.4,0.2,1},
{4.9,3.0,1.4,0.2,1},
{4.7,3.2,1.3,0.2,1},
{4.6,3.1,1.5,0.2,1},
{5.0,3.6,1.4,0.2,1},
{5.4,3.9,1.7,0.4,1},
{4.6,3.4,1.4,0.3,1},
{5.0,3.4,1.5,0.2,1},
{4.4,2.9,1.4,0.2,1},
{4.9,3.1,1.5,0.1,1},
{5.4,3.7,1.5,0.2,1},
{4.8,3.4,1.6,0.2,1},
{4.8,3.0,1.4,0.1,1},
{4.3,3.0,1.1,0.1,1},
{5.8,4.0,1.2,0.2,1},
{5.7,4.4,1.5,0.4,1},
{5.4,3.9,1.3,0.4,1},
{5.1,3.5,1.4,0.3,1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,2.2,4.5,1.5,-1},
{5.6,2.5,3.9,1.1,-1},
{5.9,3.2,4.8,1.8,-1},
{6.1,2.8,4.0,1.3,-1},
{6.3,2.5,4.9,1.5,-1},
{6.1,2.8,4.7,1.2,-1},
{6.4,2.9,4.3,1.3,-1},
{6.6,3.0,4.4,1.4,-1},
{6.8,2.8,4.8,1.4,-1},
{6.7,3.0,5.0,1.7,-1},
{6.0,2.9,4.5,1.5,-1},
{5.7,2.6,3.5,1.0,-1},
{5.5,2.4,3.8,1.1,-1},
{5.5,2.4,3.7,1.0,-1},
{5.8,2.7,3.9,1.2,-1},
{6.0,2.7,5.1,1.6,-1},
{5.4,3.0,4.5,1.5,-1},
{6.0,3.4,4.5,1.6,-1},
{6.7,3.1,4.7,1.5,-1},
{6.3,2.3,4.4,1.3,-1},
{5.6,3.0,4.1,1.3,-1},
{5.5,2.5,4.0,1.3,-1},
{5.5,2.6,4.4,1.2,-1},
{6.1,3.0,4.6,1.4,-1},
{5.8,2.6,4.0,1.2,-1},
{5.0,2.3,3.3,1.0,-1},
{5.6,2.7,4.2,1.3,-1},
{5.7,3.0,4.2,1.2,-1},
{5.7,2.9,4.2,1.3,-1},
{6.2,2.9,4.3,1.3,-1},
{5.1,2.5,3.0,1.1,-1},
{5.7,2.8,4.1,1.3,-1}
};
//Row and column sizes of trainingset
static const int ROW_SIZE = 100;
static const int COLUMN_SIZE = 4; //excludes y
//Initialize weights to 5 and of same length of inputs
float weights[4] = {5.0,5.0,5.0,5.0};
//Initialize offset term to 0
float b = 0;
//Learning rate
float n = 0.3;
float radius = 0;
//Calculate the radius by obtaining the max length of all input data rows
for (int i=0; i<ROW_SIZE; i++) {
//Loop through rows
float sum = 0;
//Loop through columns
for (int j=0; j<COLUMN_SIZE; j++) {
sum += (trainingSet[i][j] * trainingSet[i][j]);
}
float length = sqrt(sum);
if (length > radius) {
radius = length;
}
}
//Until the search converges and stops making mistakes
while (1) {
int mistake = 0;
//Loop through all rows
for (int i = 0; i<ROW_SIZE; i++) {
int xi[4] = {trainingSet[i][0], trainingSet[i][1], trainingSet[i][2], trainingSet[i][3]};
int yi = trainingSet[i][4];
//compute dot product of weights and input data (W*Xi)
int sum = 0;
for (int j=0; j<COLUMN_SIZE; j++) {
sum += weights[j] * xi[j];
}
//Result of aggregation function plus offset term
int aggregation = sum - b;
//sign of aggregate
int sign = (aggregation > 0) - (aggregation < 0);
//Check if result of aggregate function does not match label point
if (sign != yi) {
//mistake was made
mistake = 1;
//Update weights
for (int j=0; j<COLUMN_SIZE; j++) {
weights[j] = weights[j] + n*yi*xi[j];
}
//Update offset
b = b - n*yi*radius*radius;
}
}
if (!mistake) {
break;
}
}
//Calculate and log elapsed time
float endTime = (float)clock()/CLOCKS_PER_SEC;
float timeElapsed = endTime - startTime;
printf("timeElapsed is %f \n", timeElapsed);
//Print the resulting weights and offset free parameters that caused the convergence
printf("b is %f\n", b);
printf("w[0] is %f\n", weights[0]);
printf("w[1] is %f\n", weights[1]);
printf("w[2] is %f\n", weights[2]);
printf("w[3] is %f\n", weights[3]);
}