| 61 | } |
| 62 | |
| 63 | void knn_demo(bool console, int perc) { |
| 64 | array train_images, train_labels; |
| 65 | array test_images, test_labels; |
| 66 | int num_train, num_test, num_classes; |
| 67 | |
| 68 | // Load mnist data |
| 69 | float frac = (float)(perc) / 100.0; |
| 70 | setup_mnist<false>(&num_classes, &num_train, &num_test, train_images, |
| 71 | test_images, train_labels, test_labels, frac); |
| 72 | |
| 73 | int feature_length = train_images.elements() / num_train; |
| 74 | array train_feats = moddims(train_images, feature_length, num_train).T(); |
| 75 | array test_feats = moddims(test_images, feature_length, num_test).T(); |
| 76 | |
| 77 | timer::start(); |
| 78 | // Get the predicted results |
| 79 | array res_labels = knn(train_feats, test_feats, train_labels); |
| 80 | double test_time = timer::stop(); |
| 81 | |
| 82 | // Results |
| 83 | printf("Accuracy on testing data: %2.2f\n", |
| 84 | accuracy(res_labels, test_labels)); |
| 85 | |
| 86 | printf("Prediction time: %4.4f\n", test_time); |
| 87 | |
| 88 | if (!console) { |
| 89 | display_results<false>(test_images, res_labels, test_labels, 20); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | int main(int argc, char **argv) { |
| 94 | int device = argc > 1 ? atoi(argv[1]) : 0; |