#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "mnist_common.h"
float accuracy(
const array &predicted,
const array &target) {
array val, plabels, tlabels;
max(val, tlabels, target, 1);
max(val, plabels, predicted, 1);
}
float abserr(
const array &predicted,
const array &target) {
}
}
const array &Y,
double lambda = 1.0) {
array H = predict(X, Weights);
array Jreg = 0.5 *
sum(lambdat * Weights * Weights);
J = (Jerr + Jreg) / m;
dJ = (
matmulTN(X, D) + lambdat * Weights) / m;
}
double lambda = 1.0, double maxerr = 0.01, int maxiter = 1000,
bool verbose = false) {
float err = 0;
for (int i = 0; i < maxiter; i++) {
cost(J, dJ, Weights, X, Y, lambda);
if (err < maxerr) {
printf("Iteration %4d Err: %.4f\n", i + 1, err);
printf("Training converged\n");
return Weights;
}
if (verbose && ((i + 1) % 10 == 0)) {
printf("Iteration %4d Err: %.4f\n", i + 1, err);
}
Weights = Weights - alpha * dJ;
}
printf("Training stopped after %d iterations\n", maxiter);
return Weights;
}
void benchmark_softmax_regression(
const array &train_feats,
const array &train_targets,
const array test_feats) {
array Weights = train(train_feats, train_targets, 0.1, 1.0, 0.01, 1000);
const int iter = 100;
for (int i = 0; i < iter; i++) {
array test_outputs = predict(test_feats, Weights);
}
printf(
"Prediction time: %4.4lf s\n",
timer::stop() / iter);
}
int logit_demo(bool console, int perc) {
array train_images, train_targets;
array test_images, test_targets;
int num_train, num_test, num_classes;
float frac = (float)(perc) / 100.0;
setup_mnist<true>(&num_classes, &num_train, &num_test, train_images,
test_images, train_targets, test_targets, 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();
train_targets = train_targets.
T();
test_targets = test_targets.
T();
train_feats =
join(1,
constant(1, num_train, 1), train_feats);
train(train_feats, train_targets,
0.1,
1.0,
0.01,
1000,
true);
array train_outputs = predict(train_feats, Weights);
array test_outputs = predict(test_feats, Weights);
printf("Accuracy on training data: %2.2f\n",
accuracy(train_outputs, train_targets));
printf("Accuracy on testing data: %2.2f\n",
accuracy(test_outputs, test_targets));
printf("Maximum error on testing data: %2.2f\n",
abserr(test_outputs, test_targets));
benchmark_softmax_regression(train_feats, train_targets, test_feats);
if (!console) {
test_outputs = test_outputs.
T();
display_results<true>(test_images, test_outputs, test_targets.
T(), 20);
}
return 0;
}
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 {
return logit_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.
array abs(const array &in)
C++ Interface to calculate the absolute value.
array exp(const array &in)
C++ Interface to evaluate the exponential.
array log(const array &in)
C++ Interface to evaluate the natural logarithm.
array matmulTN(const array &lhs, const array &rhs)
C++ Interface to multiply two matrices.
array matmul(const array &lhs, const array &rhs, const matProp optLhs=AF_MAT_NONE, const matProp optRhs=AF_MAT_NONE)
C++ Interface to multiply two matrices.
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.
void setDevice(const int device)
Sets the current device.
void sync(const int device=-1)
Blocks until the device is finished processing.
array join(const int dim, const array &first, const array &second)
C++ Interface to join 2 arrays along a dimension.
array moddims(const array &in, const dim4 &dims)
C++ Interface to modify the dimensions of an input array to a specified shape.
array count(const array &in, const int dim=-1)
C++ Interface to count non-zero values in an array along a given dimension.
array max(const array &in, const int dim=-1)
C++ Interface to return the maximum along a given dimension.
array sum(const array &in, const int dim=-1)
C++ Interface to sum array elements over a given dimension.
array batchFunc(const array &lhs, const array &rhs, batchFunc_t func)
seq span
A special value representing the entire axis of an af::array.