MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / knn_demo

Function knn_demo

examples/machine_learning/knn.cpp:63–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

61}
62
63void 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
93int main(int argc, char **argv) {
94 int device = argc > 1 ? atoi(argv[1]) : 0;

Callers 1

mainFunction · 0.85

Calls 5

moddimsFunction · 0.85
TMethod · 0.80
knnFunction · 0.70
accuracyFunction · 0.70
elementsMethod · 0.45

Tested by

no test coverage detected