#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "mnist_common.h"
float accuracy(
const array &predicted,
const array &target) {
return 100 * count<float>(predicted == target) / target.elements();
}
void naive_bayes_train(
float *priors,
array &mu,
array &sig2,
const array &train_feats,
const array &train_classes,
int num_classes) {
const int feat_len = train_feats.dims(0);
const int num_samples = train_classes.elements();
mu =
constant(0, feat_len, num_classes);
sig2 =
constant(0, feat_len, num_classes);
for (int ii = 0; ii < num_classes; ii++) {
priors[ii] = (float)idx.
elements() / (float)num_samples;
}
mu.eval();
sig2.eval();
}
array naive_bayes_predict(
float *priors,
const array &mu,
const array &sig2,
const array &test_feats,
int num_classes) {
int num_test = test_feats.
dims(1);
for (int ii = 0; ii < num_classes; ii++) {
array Df = test_feats - Mu;
log_probs(
span, ii) =
log(priors[ii]) +
sum(log_P).
T();
}
max(val, idx, log_probs, 1);
return idx;
}
void benchmark_nb(
const array &train_feats,
const array test_feats,
const array &train_labels,
int num_classes) {
int iter = 25;
float *priors = new float[num_classes];
timer::start();
for (int i = 0; i < iter; i++) {
naive_bayes_train(priors, mu, sig2, train_feats, train_labels,
num_classes);
}
printf("Training time: %4.4lf s\n", timer::stop() / iter);
timer::start();
for (int i = 0; i < iter; i++) {
naive_bayes_predict(priors, mu, sig2, test_feats, num_classes);
}
printf("Prediction time: %4.4lf s\n", timer::stop() / iter);
delete[] priors;
}
void naive_bayes_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);
array test_feats =
moddims(test_images, feature_length, num_test);
float *priors = new float[num_classes];
naive_bayes_train(priors, mu, sig2, train_feats, train_labels, num_classes);
naive_bayes_predict(priors, mu, sig2, test_feats, num_classes);
delete[] priors;
printf("Trainng samples: %4d, Testing samples: %4d\n", num_train, num_test);
printf("Accuracy on testing data: %2.2f\n",
accuracy(res_labels, test_labels));
benchmark_nb(train_feats, test_feats, train_labels, num_classes);
if (!console) {
test_images = test_images.T();
test_labels = test_labels.T();
}
}
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 {
naive_bayes_demo(console, perc);
return 0;
}
dim4 dims() const
Get dimensions of the array.
array T() const
Get the transposed the array.
dim_t elements() const
Get the total number of elements across all dimensions of the array.
An ArrayFire exception class.
virtual const char * what() const
Returns an error message for the exception in a string format.
@ AF_VARIANCE_SAMPLE
Sample variance.
AFAPI array log(const array &in)
C++ Interface for natural logarithm.
AFAPI array sqrt(const array &in)
C++ Interface for square root of input.
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
AFAPI void setDevice(const int device)
Sets the current device.
AFAPI void sync(const int device=-1)
Blocks until the device is finished processing.
AFAPI array lookup(const array &in, const array &idx, const int dim=-1)
Lookup the values of an input array by indexing with another array.
AFAPI array moddims(const array &in, const unsigned ndims, const dim_t *const dims)
AFAPI array tile(const array &in, const unsigned x, const unsigned y=1, const unsigned z=1, const unsigned w=1)
static af::array array(af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
Create an af::array object from an OpenCL cl_mem buffer.
AFAPI array max(const array &in, const int dim=-1)
C++ Interface for maximum values in an array.
AFAPI array sum(const array &in, const int dim=-1)
C++ Interface for sum of elements in an array.
AFAPI array where(const array &in)
C++ Interface for finding the locations of non-zero values in an array.
AFAPI array mean(const array &in, const dim_t dim=-1)
C++ Interface for mean.
AFAPI array var(const array &in, const bool isbiased=false, const dim_t dim=-1)
C++ Interface for variance.
AFAPI seq span
A special value representing the entire axis of an af::array.