| 102 | } |
| 103 | |
| 104 | void naive_bayes_demo(bool console, int perc) { |
| 105 | array train_images, train_labels; |
| 106 | array test_images, test_labels; |
| 107 | int num_train, num_test, num_classes; |
| 108 | |
| 109 | // Load mnist data |
| 110 | float frac = (float)(perc) / 100.0; |
| 111 | setup_mnist<false>(&num_classes, &num_train, &num_test, train_images, |
| 112 | test_images, train_labels, test_labels, frac); |
| 113 | |
| 114 | int feature_length = train_images.elements() / num_train; |
| 115 | array train_feats = moddims(train_images, feature_length, num_train); |
| 116 | array test_feats = moddims(test_images, feature_length, num_test); |
| 117 | |
| 118 | // Get training parameters |
| 119 | array mu, sig2; |
| 120 | float *priors = new float[num_classes]; |
| 121 | naive_bayes_train(priors, mu, sig2, train_feats, train_labels, num_classes); |
| 122 | |
| 123 | // Predict the classes |
| 124 | array res_labels = |
| 125 | naive_bayes_predict(priors, mu, sig2, test_feats, num_classes); |
| 126 | delete[] priors; |
| 127 | |
| 128 | // Results |
| 129 | printf("Trainng samples: %4d, Testing samples: %4d\n", num_train, num_test); |
| 130 | printf("Accuracy on testing data: %2.2f\n", |
| 131 | accuracy(res_labels, test_labels)); |
| 132 | |
| 133 | benchmark_nb(train_feats, test_feats, train_labels, num_classes); |
| 134 | |
| 135 | if (!console) { |
| 136 | test_images = test_images.T(); |
| 137 | test_labels = test_labels.T(); |
| 138 | |
| 139 | display_results<false>(test_images, res_labels, test_labels, 20); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | int main(int argc, char **argv) { |
| 144 | int device = argc > 1 ? atoi(argv[1]) : 0; |
no test coverage detected