#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "mnist_common.h"
floataccuracy(
const array&predicted,
const array&target) {
return100 * count<float>(predicted == target) / target.
elements();
}
const intfeat_len = train.
dims(1);
const intnum_train = train.
dims(0);
const intnum_test = test.
dims(0);
arraydist = constant(0, num_train, num_test);
for (int ii = 0; ii < feat_len; ii++) {
arraytrain_i = train(span, ii);
arraytest_i = test(span, ii).
T();
arraytrain_tiled = tile(train_i, 1, num_test);
arraytest_tiled = tile(test_i, num_train, 1);
dist = dist + abs(train_tiled - test_tiled);
}
return dist;
}
arraydist = distance(train_feats, test_feats);
return train_labels(idx);
}
int num_classes, int num_models, int sample_size) {
intnum_train = train_feats.
dims(0);
intnum_test = test_feats.
dims(0);
for (int i = 0; i < num_models; i++) {
arraytrain_labels_ii = train_labels(ii);
arraylabels_ii = knn(train_feats_ii, test_feats, train_labels_ii);
arraylidx = labels_ii * num_test + off;
labels_all(lidx) = labels_all(lidx) + 1;
}
max(val, labels, labels_all, 1);
return labels;
}
void bagging_demo(bool console, int perc) {
arraytrain_images, train_labels;
arraytest_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);
intfeature_length = train_images.
elements() / num_train;
arraytrain_feats =
moddims(train_images, feature_length, num_train).
T();
arraytest_feats =
moddims(test_images, feature_length, num_test).
T();
int num_models = 10;
int sample_size = 1000;
timer::start();
arrayres_labels = bagging(train_feats, test_feats, train_labels,
num_classes, num_models, sample_size);
double test_time = timer::stop();
printf("Accuracy on testing data: %2.2f\n",
accuracy(res_labels, test_labels));
printf("Prediction time: %4.4f\n", test_time);
if (false && !console) {
display_results<false>(test_images, res_labels, test_labels.
T(), 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 {
bagging_demo(console, perc);
return 0;
}
A multi dimensional data container.
dim4 dims() const
Get dimensions of the array.
void eval() const
Evaluate any JIT expressions to generate data for 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.
seq is used to create sequences for indexing af::array
AFAPI array floor(const array &in)
C++ Interface to floor numbers.
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
C++ Interface to generate an array with elements set to a specified value.
AFAPI void setDevice(const int device)
Sets the current device.
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 dim4 &dims)
C++ Interface to modify the dimensions of an input array to a specified shape.
AFAPI array randu(const dim4 &dims, const dtype ty, randomEngine &r)
C++ Interface to create an array of random numbers uniformly distributed.
AFAPI array max(const array &in, const int dim=-1)
C++ Interface to return the maximum along a given dimension.
AFAPI array min(const array &in, const int dim=-1)
C++ Interface to return the minimum along a given dimension.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4