Repository navigation
Expand file tree
/
Copy pathclassifier.cpp
More file actions
343 lines (282 loc) · 9.27 KB
/
Copy pathclassifier.cpp
File metadata and controls
343 lines (282 loc) · 9.27 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
/****************************************************
Name : Jatin Saini
Roll No. : 19ME30068
AI_2021: Neural Network Assignment Part 2
****************************************************/
//The non-linear sine classifier
#include <stdio.h>
#include <math.h>
#include <ctime>
#include <graphics.h>
//a point in 2D with the label
typedef struct
{
int x;
int y;
int label;
}point;
// a structure of a node in a NN
typedef struct
{
double wts[150];
double b;
}node;
// initializing model design
int hid_layer1_size = 20;
int hid_layer2_size = 20;
node hid_layer1[20];
node hid_layer2[20];
node out_node;
double sigmoid(double x)
{
return 1/(1+ exp(-x));
}
// finds the number of data points in a file
int get_size(char filename[])
{
int size=0;
FILE *fptr;
fptr = fopen(filename, "r");
char temp[20];
//If the EOF is encountered while attempting to read a character, the EOF indicator is set (feof).
//If this happens before any characters could be read, the pointer returned is a NULL pointer
while(fgets(temp, 20, fptr) != NULL)
size++;
fclose(fptr);
return size;
}
// reads the data file and stores the data set in an array
void read_data(int count, point data_pts[], char filename[])
{
FILE *fptr;
fptr = fopen(filename, "r");
for(int i=0; i<count; i++)
fscanf(fptr, "%d %d %d\n", &data_pts[i].x, &data_pts[i].y, &data_pts[i].label);
return;
}
//show all the data points on the screen
void show_points(int train_count, point train_pts[], int test_count, point test_pts[])
{
int radius = 2;
int color = WHITE;
//training set
for(int i=0; i<train_count; i++)
{
if(train_pts[i].label == 1)
color = GREEN;
else
color = RED;
setcolor(color);
circle(train_pts[i].x, train_pts[i].y, radius);
setfillstyle(SOLID_FILL, color);
floodfill(train_pts[i].x, train_pts[i].y, color);
}
//test set
color = YELLOW;
setcolor(color);
setfillstyle(SOLID_FILL, color);
for(int i=0; i<test_count; i++)
{
circle(test_pts[i].x, test_pts[i].y, radius);
floodfill(test_pts[i].x, test_pts[i].y, color);
}
return;
}
// given an input point, it gives it's label as classified by the trained model
int model_classifier(point input)
{
double out = 0;
double z = 0;
double activations1[hid_layer1_size];
for(int j=0; j<hid_layer1_size; j++)
{
activations1[j] = sigmoid(input.x * hid_layer1[j].wts[0] + input.y * hid_layer1[j].wts[1] + hid_layer1[j].b);
}
for(int j=0; j<hid_layer2_size; j++)
{
double temp = 0;
for(int k=0; k<hid_layer1_size; k++)
{
temp += activations1[k] * hid_layer2[j].wts[k];
}
z += sigmoid(temp) * out_node.wts[j];
}
z += out_node.b;
out = sigmoid(z);
if(out >= 0.5)
return 1;
else
return 0;
}
// trains the model
void train_model(int train_count, point train_pts[] )
{
// initializing model with random weights and biases
srand(time(0));
// initializing is the key step here
for(int i=0; i<hid_layer1_size; i++)
{
hid_layer1[i].wts[0] = (rand()%1001)*sqrt(2.0/2.0)/1000.0;
hid_layer1[i].wts[1] = (rand()%1001)*sqrt(2.0/2.0)/1000.0;
hid_layer1[i].b = 0.0;
}
for(int i=0; i<hid_layer2_size; i++)
{
for(int j=0; j<hid_layer1_size; j++)
{
hid_layer2[i].wts[j] = (rand()%1001)*sqrt(2.0/float(hid_layer1_size))/1000.0;
}
hid_layer2[i].b = 0.0;
out_node.wts[i] = (rand()%1001)*sqrt(2.0/float(hid_layer2_size))/1000.0;
}
out_node.b = 0.0;
float learnr = 0.01;
int convergence = 0;
int epoch_count=0;
int pt_count=0; // counting wrongly classified points in each epoch
double loss;
double sq_loss;
double sq_loss_sum;
double delta_out;
double activations1[hid_layer1_size];
double activations2[hid_layer2_size];
double z = 0;
double out = 0;
node grad_hid_layer1[hid_layer1_size];
node grad_hid_layer2[hid_layer2_size];
node grad_out_node;
while(!convergence && epoch_count < 200)
{
epoch_count++;
printf("\n\t\tEpoch %d....", epoch_count);
pt_count = 0;
sq_loss_sum = 0;
// iterating through the whole training data
for(int i=0; i<train_count; i++)
{
z = 0;
for(int j=0; j<hid_layer1_size; j++)
{
activations1[j] = sigmoid(train_pts[i].x * hid_layer1[j].wts[0] + train_pts[i].y * hid_layer1[j].wts[1] + hid_layer1[j].b);
}
for(int j=0; j<hid_layer2_size; j++)
{
double temp = 0;
for(int k=0; k<hid_layer1_size; k++)
{
temp += activations1[k] * hid_layer2[j].wts[k];
}
activations2[j] = sigmoid(temp);
z += activations2[j] * out_node.wts[j];
}
z += out_node.b;
out = sigmoid(z);
loss = (out - train_pts[i].label);
sq_loss = loss * loss;
sq_loss_sum += sq_loss;
delta_out = (2 * loss) * ( out * (1-out));
// Backpropagation
for(int j=0; j<hid_layer2_size; j++)
{
grad_out_node.wts[j] = delta_out * activations2[j];
for(int k=0; k<hid_layer1_size; k++)
{
grad_hid_layer2[j].wts[k] = delta_out * out_node.wts[j] * (activations2[j] * (1 - activations2[j])) * activations1[k];
}
grad_hid_layer2[j].b = delta_out * out_node.wts[j] * (activations2[j] * (1 - activations2[j])) * 1;
}
grad_out_node.b = delta_out * 1;
for(int j=0; j<hid_layer1_size; j++)
{
double sum =0;
for(int k=0; k<hid_layer2_size; k++)
{
sum += delta_out * out_node.wts[k] * (activations2[k] * (1 - activations2[k])) * hid_layer2[k].wts[j];
}
grad_hid_layer1[j].wts[0] = sum * (activations1[j] * (1 - activations1[j])) * train_pts[i].x;
grad_hid_layer1[j].wts[1] = sum * (activations1[j] * (1 - activations1[j])) * train_pts[i].y;
grad_hid_layer1[j].b = sum * (activations1[j] * (1 - activations1[j])) * 1;
}
// updating the parameters
for(int i = 0; i<hid_layer1_size; i++)
{
hid_layer1[i].wts[0] -= learnr * grad_hid_layer1[i].wts[0];
hid_layer1[i].wts[1] -= learnr * grad_hid_layer1[i].wts[1] ;
hid_layer1[i].b -= learnr * grad_hid_layer1[i].b ;
}
for(int j=0; j<hid_layer2_size; j++)
{
for(int k=0; k<hid_layer1_size; k++)
{
hid_layer2[j].wts[k] -= learnr * grad_hid_layer2[j].wts[k];
}
hid_layer2[j].b -= learnr * grad_hid_layer2[j].b;
out_node.wts[j] -= learnr * grad_out_node.wts[j] ;
}
out_node.b -= learnr * grad_out_node.b;
if((out >= 0.5 && train_pts[i].label == 0) || (out < 0.5 && train_pts[i].label == 1))
pt_count++;
}
printf("\nWrongly Classified Points:\n %d/%d\n",pt_count, train_count);
printf("Avg Loss over whole training set:\n %f\n",sq_loss_sum / train_count);
// if MSE becomes less than 0.5, stop training
if(sq_loss_sum / train_count < 0.001)
convergence = 1;
}
printf("\n\nModel Trained Successfully!\n");
return;
}
// test the accuracy of the classifier
void test_accuracy(int test_count, point test_pts[])
{
float accuracy;
int correct_count = 0;
int radius = 2;
int color;
for(int i=0; i<test_count; i++)
{
if( model_classifier(test_pts[i]) == 1 ) //label is 1 (all upper half/on points)
{
color = BLUE;
if(test_pts[i].label == 1)
correct_count++;
}
else //label is 0 (all lower half points)
{
color = LIGHTRED;
if(test_pts[i].label == 0)
correct_count++;
}
setcolor(color);
circle(test_pts[i].x, test_pts[i].y, radius);
setfillstyle(SOLID_FILL, color);
floodfill(test_pts[i].x, test_pts[i].y, color);
delay(5);
}
accuracy = (float)(correct_count*100) / test_count;
printf("\nAccuracy of classifier:\n%f\n",accuracy);
return;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
//getting training and test set size
int train_count = get_size((char *)"training_set.txt");
int test_count = get_size((char *)"test_set.txt");
point train_pts[train_count];
point test_pts[test_count];
//reading and storing training and test data
read_data(train_count, train_pts, (char *)"training_set.txt");
read_data(test_count, test_pts, (char *)"test_set.txt");
int gd=DETECT, gm;
initgraph(&gd, &gm, (char *)"");
// showing all train and test data on screen
show_points(train_count, train_pts, test_count, test_pts);
// training the model
train_model(train_count, train_pts);
// test the accuracy of the model trained
test_accuracy(test_count, test_pts);
getch();
closegraph();
return 0;
}