forked from zhe-ch/ACTEV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_classify.c
143 lines (128 loc) · 3.76 KB
/
linear_classify.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CELL_NUM 89
#define LEARNER_NUM 23
#define CLASS_NUM 24
int main() {
int c, t;
int i;
float v;
FILE *ifp, *ofp;
char* subString;
static int spike_train[CELL_NUM][1000];
static float beta[CELL_NUM][LEARNER_NUM];
static float bias[LEARNER_NUM];
static float score[LEARNER_NUM];
int coding_matrix[CLASS_NUM][LEARNER_NUM];
static float loss[CLASS_NUM];
float min_loss = 0;
static int predict_output[1000];
static int predict_label[1000];
char file_name[100];
unsigned char buffer[512];
int diff = 0;
// Load traces.
sprintf(file_name, "test_input.txt");
if (!(ifp = fopen(file_name, "r"))) {
printf("File test_input.txt cannot be opened for read.\n");
return -1;
}
for (t = 0; t < 1000; ++t) {
fgets(buffer, 512, ifp);
subString = strtok(buffer, ",");
spike_train[0][t] = atoi(subString);
for (c = 1; c < CELL_NUM; ++c) {
subString = strtok(NULL, ",");
spike_train[c][t] = atoi(subString);
}
}
fclose(ifp);
// Load predict labels.
sprintf(file_name, "test_labels.txt");
if (!(ifp = fopen(file_name, "r"))) {
printf("File test_labels.txt cannot be opened for read.\n");
return -1;
}
for (t = 0; t < 1000; ++t) {
fscanf(ifp, "%d", &predict_label[t]);
}
fclose(ifp);
// Load parameters of the linear classifier.
sprintf(file_name, "./lc_parameters.txt");
if (!(ifp = fopen(file_name, "r"))) {
printf("File lc_parameters.txt cannot be opened for read.\n");
return -1;
}
for (c = 0; c < CELL_NUM; ++c) {
for (i = 0; i < LEARNER_NUM; ++i) {
fscanf(ifp, "%f", &beta[c][i]);
}
}
for (i = 0; i < CLASS_NUM; ++i) {
fscanf(ifp, "%f", &bias[i]);
}
fclose(ifp);
// Build coding matrix.
for (i = 0; i < CLASS_NUM; ++i) {
for (c = 0; c < LEARNER_NUM; ++c) {
if (c >= i) {
coding_matrix[i][c] = -1;
}
else {
coding_matrix[i][c] = 1;
}
}
}
// Linear classification based on computed score and loss.
for (t = 0; t < 1000; ++t) {
// Compute score.
for (i = 0; i < LEARNER_NUM; ++i) {
score[i] = bias[i];
for (c = 0; c < CELL_NUM; ++c) {
score[i] += (spike_train[c][t] * beta[c][i]);
}
}
// Compute loss.
for (i = 0; i < CLASS_NUM; ++i) {
loss[i] = 0;
for (c = 0; c < LEARNER_NUM; ++c) {
v = 1 - score[c] * coding_matrix[i][c];
if (v > 0) {
loss[i] += v;
}
}
}
// Compute prediction result
predict_output[t] = 0;
min_loss = loss[0];
for (i = 1; i < CLASS_NUM; ++i) {
if (loss[i] < min_loss) {
predict_output[t] = i;
min_loss = loss[i];
}
}
}
// Save prediction results to TXT file.
sprintf(file_name, "./prediction_result.txt");
if (!(ofp = fopen(file_name, "w"))) {
printf("File prediction_result.txt cannot be opened for write\n");
return -1;
}
for (t = 0; t < 1000; ++t) {
fprintf(ofp, "%d\n", predict_output[t] + 1);
}
fclose(ofp);
// Check if prediction results are correct.
for (t = 0; t < 1000; ++t) {
diff += abs((predict_output[t] + 1) - predict_label[t]);
}
if (diff == 0) {
printf("[Info] Test results are correct.\n");
}
else {
printf("[Info] Result mismatch: Diff = %d\n", diff);
}
printf("Processing complete.\n");
return 0;
}