#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "mnist_common.h"
using std::vector;
floataccuracy(
const array&predicted,
const array&target) {
arrayval, plabels, tlabels;
max(val, tlabels, target, 1);
max(val, plabels, predicted, 1);
return100 * count<float>(plabels == tlabels) / tlabels.
elements();
}
arrayderiv(
const array&out) {
returnout * (1 - out); }
doubleerror(
const array&out,
const array&pred) {
arraydif = (out - pred);
return sqrt((
double)(sum<float>(dif * dif)));
}
}
class rbm {
private:
arrayvtoh(
const array&v) {
returnbinary(prop_up(v)); }
arrayhtov(
const array&h) {
returnbinary(prop_down(h)); }
public:
rbm() {}
rbm(int v_size, int h_size)
: weights(
randu(h_size, v_size) / 100 - 0.05)
}
}
vt = v;
for (int i = 0; i < k; i++) {
ht = vtoh(vt);
vt = htov(ht);
}
}
ht = h;
for (int i = 0; i < k; i++) {
vt = htov(ht);
ht = vtoh(vt);
}
}
voidtrain(
const array&in,
doublelr = 0.1,
intnum_epochs = 15,
int batch_size = 100, int k = 1, bool verbose = false) {
const intnum_samples = in.
dims(0);
const int num_batches = num_samples / batch_size;
for (int i = 0; i < num_epochs; i++) {
double err = 0;
for (int j = 0; j < num_batches - 1; j++) {
int st = j * batch_size;
int en = std::min(num_samples - 1, st + batch_size - 1);
int num = en - st + 1;
arrayh_pos = vtoh(v_pos);
gibbs_hvh(v_neg, h_neg, h_pos, k);
arraydelta_w = lr * (c_pos - c_neg) / num;
arraydelta_vb = lr *
sum(v_pos - v_neg) / num;
arraydelta_hb = lr *
sum(h_pos - h_neg) / num;
weights += delta_w;
v_bias += delta_vb;
h_bias += delta_hb;
if (verbose) { err += error(v_pos, v_neg); }
}
if (verbose) {
printf("Epoch %d: Reconstruction error: %0.4f\n", i + 1,
err / num_batches);
}
}
if (verbose) printf("\n");
}
};
int rbm_demo(bool , int perc) {
printf("** ArrayFire RBM Demo **\n\n");
arraytrain_images, test_images;
arraytrain_target, test_target;
int num_classes, num_train, num_test;
float frac = (float)(perc) / 100.0;
setup_mnist<true>(&num_classes, &num_train, &num_test, train_images,
test_images, train_target, test_target, frac);
intfeature_size = train_images.
elements() / num_train;
arraytrain_feats =
moddims(train_images, feature_size, num_train).
T();
arraytest_feats =
moddims(test_images, feature_size, num_test).
T();
train_target = train_target.
T();
test_target = test_target.
T();
rbm network(train_feats.
dims(1), 2000);
network.train(train_feats,
0.1,
15,
100,
1,
true);
for (int ii = 0; ii < 5; ii++) {
arrayin = test_feats(ii, span);
network.gibbs_vhv(res, tmp, in);
in =
moddims(in, dims[0], dims[1]);
res =
moddims(res, dims[0], dims[1]);
printf("Reconstructed Error for image %2d: %.4f\n", ii,
sum<float>(
abs(in - res)) / feature_size);
}
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 rbm_demo(console, perc);
return 0;
}
A multi dimensional data container.
dim4 dims() const
Get dimensions of the array.
const array as(dtype type) const
Casts the array into another data type.
array T() const
Get the transposed the array.
dim_t elements() const
Get the total number of elements across all dimensions of the array.
Generic object that represents size and shape.
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
@ f32
32-bit floating point values
AFAPI array abs(const array &in)
C++ Interface to calculate the absolute value.
AFAPI array round(const array &in)
C++ Interface to round numbers.
AFAPI array sigmoid(const array &in)
C++ Interface to evaluate the logistical sigmoid function.
AFAPI array sqrt(const array &in)
C++ Interface to evaluate the square root.
AFAPI array matmulTN(const array &lhs, const array &rhs)
C++ Interface to multiply two matrices.
AFAPI 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.
AFAPI array matmulNT(const array &lhs, const array &rhs)
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.
AFAPI void setDevice(const int device)
Sets the current device.
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 tile(const array &in, const unsigned x, const unsigned y=1, const unsigned z=1, const unsigned w=1)
C++ Interface to generate a tiled array.
AFAPI array randu(const dim4 &dims, const dtype ty, randomEngine &r)
C++ Interface to create an array of random numbers uniformly distributed.
AFAPI array sum(const array &in, const int dim=-1)
C++ Interface to sum array elements over 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