#include <stdio.h>
#include <vector>
#include <string>
#include <math.h>
#include "mnist_common.h"
float accuracy(
const array& predicted,
const array& target)
{
return 100 * count<float>(predicted == target) / target.
elements();
}
{
const int feat_len = train.
dims(1);
const int num_train = train.
dims(0);
const int num_test = test.
dims(0);
for (int ii = 0; ii < feat_len; ii++) {
array train_tiled =
tile(train_i, 1, num_test);
array test_tiled =
tile( test_i, num_train, 1 );
dist = dist +
abs(train_tiled - test_tiled);
}
return dist;
}
{
array dist = distance(train_feats, test_feats);
return train_labels(idx);
}
void knn_demo(bool console, int perc)
{
array train_images, train_labels;
array test_images, test_labels;
int num_train, num_test, num_classes;
float frac = (float)(perc) / 100.0;
setup_mnist<false>(&num_classes, &num_train, &num_test,
train_images, test_images,
train_labels, test_labels, frac);
int feature_length = train_images.
elements() / num_train;
array train_feats =
moddims(train_images, feature_length, num_train).
T();
array test_feats =
moddims(test_images , feature_length, num_test ).
T();
array res_labels = knn(train_feats, test_feats, train_labels);
printf("Accuracy on testing data: %2.2f\n",
accuracy(res_labels , test_labels));
printf("Prediction time: %4.4f\n", test_time);
if (!console) {
display_results<false>(test_images, res_labels, test_labels, 20);
}
}
int main(int argc, char** argv)
{
int device = argc > 1 ? atoi(argv[1]) : 0;
bool console = argc > 2 ? argv[2][0] == '-' : false;
int perc = argc > 3 ? atoi(argv[3]) : 60;
try {
knn_demo(console, perc);
std::cerr << ae.
what() << std::endl;
}
return 0;
}