// system include files // #include #include "FuzzyClassifier.h" int main() { // local varables // int value, i; int num_class = 5, dim = 39; int num_seq = 5; FuzzyClassifier fc; fc.num_class_d = num_class; fc.dimension_d = dim; fc.class_d.setLength(num_class); fc.num_vec_d.setLength(num_class); for (i = 0; i < num_class; i++) { fc.class_d(i) = i + 1; } // input the transcription file of training data // //char *input_file = "../data/train/train_trans_2.text"; char *input_file = "../data/training/training_2.class"; FILE* fin = fopen((char*)input_file, "r"); if (fin == (FILE*)NULL) { fprintf(stdout, "Error : cannot open input file ../data/train/train_trans_1.text%s\n", input_file); exit(1); } VectorLong trans; i = 0; while (!feof(fin)) { fscanf(fin, "%d\n", &value); trans.setLength(i + 1); trans(i) = value; fc.num_vec_d(value - 1) += 1; i++; // printf("%d %d\n", i, value); } fclose(fin); fc.vec_d = new VectorFloat**[num_class]; for (i = 0; i < num_class; i++) { fc.vec_d[i] = new VectorFloat*[fc.num_vec_d(i)]; int num_vec = fc.num_vec_d(i); for (int j = 0; j < num_vec; j++) { fc.vec_d[i][j] = new VectorFloat(dim); } } //input_file = "../data/train/train_2.text"; input_file = "../data/training/training_2.text"; fin = fopen((char*)input_file, "r"); if (fin == (FILE*)NULL) { fprintf(stdout, "Error : cannot open input file %s\n", input_file); exit(1); } float v[dim]; int m = 0; fc.num_vec_d.clear(Integral::RETAIN); while (!feof(fin)) { for (int j = 0; j < dim; j++) { fscanf(fin, "%f\n", &v[j]); } i = trans(m) - 1; (*fc.vec_d[i][fc.num_vec_d(i)]).assign(fc.dimension_d, v); fc.num_vec_d(i) += 1; m++; } fc.num_vec_d.debug(L"num_vec.d = "); fclose(fin); // ===TRAINING======================================= fc.normalize(); fc.vec_max_d.debug(L"vec_max_d"); fc.train(); for (int i = 0; i <1; i++) { if (fc.remove_junk()) { fc.train(); } } fc.debug(); // ===TEST======================================== // long num_test = 925 + 925/5; //long num_test = 350 + 350/5; long num_test = 225 + 225/5; VectorFloat vec_test[num_test]; //input_file = "../data/train/train_2.text"; //input_file = "../data/test/test_2.text"; input_file = "../data/eval/eval_2.data"; fin = fopen((char*)input_file, "r"); if (fin == (FILE*)NULL) { fprintf(stdout, "Error : cannot open input test file %s\n", input_file); exit(1); } i = 0; int cc = 0; while (!feof(fin)) { for (int j = 0; j < dim; j++) { fscanf(fin, "%f\n", &v[j]); } vec_test[i].setLength(dim); vec_test[i].assign(dim, v); // normaliztion // vec_test[i].div(fc.vec_max_d); i++; cc++; if (cc == num_seq) { vec_test[i].setLength(dim); vec_test[i].assign((float)0); for (int j = 1; j <= num_seq; j++) { vec_test[i].add(vec_test[i-j]); } vec_test[i].div(num_seq); cc = 0; i++; } } num_seq++; printf("num_test = %d\n", i); fclose(fin); VectorLong hypothesis(num_test), tmp(num_class); long max, pos; int count = num_seq; for (i = 0; i < num_test; i++) { count--; hypothesis(i) = fc.decode(vec_test[i]); if (!count) { tmp.assign((long)0); for (int j = 0; j < num_seq; j++) { tmp((long)hypothesis(i - j) - 1)++; } tmp.debug(L"hypo statistics"); max = tmp.max(pos); // if any ties // int n; n = 0; Long ran; for (int ii = 0; ii < num_class; ii++) { if (tmp(ii) == max) { n++; } else { tmp(ii) = 100000; } } if (n == 1) { pos = pos + 1; } else { ran.rand(0, n); ran.debug(L"random"); for (int ii = 0; ii < num_class; ii++) { if (tmp(ii) == max) { if (ran == 0) { pos = ii + 1; break; } ran--; } } } for (int j = 0; j < num_seq; j++) { hypothesis(i - j) = pos; } count = num_seq; } } char *output_file = "test_hypo.text"; FILE* fout = fopen((char*)output_file, "w"); if (fout == (FILE*)NULL) { fprintf(stdout, "Error : cannot open input test file %s\n", input_file); exit(1); } cc = 0; for (i = 0; i < num_test; i++) { cc++; if (cc == num_seq) { cc = 0; continue; } fprintf(fout, "%d\n", (int)hypothesis(i)); } fclose(fout); // fc.debug(); fc.record_class_d.debug(L"record_class_d"); fc.record_num_cluster_d.debug(L"record_num_cluster_d"); // ------------------------------------------ // delete vectors // for (i = 0; i < num_class; i++) { int num_vec = fc.num_vec_d(i); for (int j = 0; j < num_vec; j++) { delete fc.vec_d[i][j]; } } for (i = 0; i < num_class; i++) { delete [] fc.vec_d[i]; } delete [] fc.vec_d; return 1; }