-
Notifications
You must be signed in to change notification settings - Fork 9
/
MNISTDemoBits.c
185 lines (150 loc) · 4.35 KB
/
MNISTDemoBits.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
#include "MultiClassTsetlinMachineBits.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define EPOCHS 400
#define NUMBER_OF_TRAINING_EXAMPLES 60000
#define NUMBER_OF_TEST_EXAMPLES 10000
unsigned int X_train[NUMBER_OF_TRAINING_EXAMPLES][LA_CHUNKS];
int y_train[NUMBER_OF_TRAINING_EXAMPLES];
unsigned int X_test[NUMBER_OF_TEST_EXAMPLES][LA_CHUNKS];
int y_test[NUMBER_OF_TEST_EXAMPLES];
unsigned int X_training_2[NUMBER_OF_TEST_EXAMPLES][LA_CHUNKS];
int y_training_2[NUMBER_OF_TEST_EXAMPLES];
void read_file(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
const char *s = " ";
char *token = NULL;
// Training Dataset
for (int i = 0; i < NUMBER_OF_TRAINING_EXAMPLES; i++) {
for (int j = 0; j < LA_CHUNKS; j++) {
X_train[i][j] = 0;
}
}
fp = fopen("MNISTTraining.txt", "r");
if (fp == NULL) {
printf("Error opening\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < NUMBER_OF_TRAINING_EXAMPLES; i++) {
getline(&line, &len, fp);
token = strtok(line, s);
for (int j = 0; j < FEATURES; j++) {
if (atoi(token) == 1) {
int chunk_nr = j / INT_SIZE;
int chunk_pos = j % INT_SIZE;
X_train[i][chunk_nr] |= (1 << chunk_pos);
} else {
int chunk_nr = (j + FEATURES) / INT_SIZE;
int chunk_pos = (j + FEATURES) % INT_SIZE;
X_train[i][chunk_nr] |= (1 << chunk_pos);
}
token=strtok(NULL,s);
}
y_train[i] = atoi(token);
}
fclose(fp);
// Test Dataset I
for (int i = 0; i < NUMBER_OF_TEST_EXAMPLES; i++) {
for (int j = 0; j < LA_CHUNKS; j++) {
X_test[i][j] = 0;
}
}
fp = fopen("MNISTTest.txt", "r");
if (fp == NULL) {
printf("Error opening\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < NUMBER_OF_TEST_EXAMPLES; i++) {
getline(&line, &len, fp);
token = strtok(line, s);
for (int j = 0; j < FEATURES; j++) {
if (atoi(token) == 1) {
int chunk_nr = j / INT_SIZE;
int chunk_pos = j % INT_SIZE;
X_test[i][chunk_nr] |= (1 << chunk_pos);
} else {
int chunk_nr = (j + FEATURES) / INT_SIZE;
int chunk_pos = (j + FEATURES) % INT_SIZE;
X_test[i][chunk_nr] |= (1 << chunk_pos);
}
token=strtok(NULL,s);
}
y_test[i] = atoi(token);
}
fclose(fp);
// Sample of training dataset for speed
for (int i = 0; i < NUMBER_OF_TEST_EXAMPLES; i++) {
for (int j = 0; j < LA_CHUNKS; j++) {
X_training_2[i][j] = 0;
}
}
fp = fopen("MNISTTrainingSampled.txt", "r");
if (fp == NULL) {
printf("Error opening\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < NUMBER_OF_TEST_EXAMPLES; i++) {
getline(&line, &len, fp);
token = strtok(line, s);
for (int j = 0; j < FEATURES; j++) {
if (atoi(token) == 1) {
int chunk_nr = j / INT_SIZE;
int chunk_pos = j % INT_SIZE;
X_training_2[i][chunk_nr] |= (1 << chunk_pos);
} else {
int chunk_nr = (j + FEATURES) / INT_SIZE;
int chunk_pos = (j + FEATURES) % INT_SIZE;
X_training_2[i][chunk_nr] |= (1 << chunk_pos);
}
token=strtok(NULL,s);
}
y_training_2[i] = atoi(token);
}
fclose(fp);
}
void output_digit(unsigned int Xi[])
{
for (int y = 0; y < 28; y++) {
for (int x = 0; x < 28; x++) {
int chunk_nr = (x + y*28) / INT_SIZE;
int chunk_pos = (x + y*28) % INT_SIZE;
if ((Xi[chunk_nr] & (1 << chunk_pos)) > 0) {
printf("@");
} else {
printf(".");
}
}
printf("\n");
}
}
int main(void)
{
srand(time(NULL));
read_file();
int example = (int)(NUMBER_OF_TEST_EXAMPLES-1) * 1.0*rand()/RAND_MAX;
printf("\nExample of Digit %d\n\n", y_test[example]);
output_digit(X_test[example]);
struct MultiClassTsetlinMachine *mc_tm = CreateMultiClassTsetlinMachine();
for (int i = 0; i < EPOCHS; i++) {
printf("\nEPOCH %d\n", i+1);
clock_t start_total = clock();
mc_tm_fit(mc_tm, X_train, y_train, NUMBER_OF_TRAINING_EXAMPLES, 1);
clock_t end_total = clock();
double time_used = ((double) (end_total - start_total)) / CLOCKS_PER_SEC;
printf("Training Time: %.1f s\n", time_used);
start_total = clock();
float test_accuracy = mc_tm_evaluate(mc_tm, X_test, y_test, NUMBER_OF_TEST_EXAMPLES);
end_total = clock();
time_used = ((double) (end_total - start_total)) / CLOCKS_PER_SEC;
printf("Evaluation Time: %.1f s\n", time_used);
printf("Test Accuracy: %.2f\n", 100*test_accuracy);
float training_2_accuracy = mc_tm_evaluate(mc_tm, X_training_2, y_training_2, NUMBER_OF_TEST_EXAMPLES);
printf("Training Accuracy: %.2f\n", 100*training_2_accuracy);
}
return 0;
}