Conway’s Game of Life Using ArrayFire
Visit the [Wikipedia page][2] for a description of Conway’s Game of Life.
static const float h_kernel[] = { 1, 1, 1, 1, 0, 1, 1, 1, 1 };
static const array kernel(3, 3, h_kernel, afHost);
array state = (randu(128, 128, f32) > 0.5).as(f32); // Init state
Window myWindow(256, 256);
while(!myWindow.close()) {
array nHood = convolve(state, kernel); // Obtain neighbors
array C0 = (nHood == 2); // Generate conditions for life
array C1 = (nHood == 3);
state = state * C0 + C1; // Update state
myWindow.image(state); // Display
}
The complete source code can be found [here][3].
Perceptron
array predict(const array &X, const array &W) {
return sigmoid(matmul(X, W));
}
array train(const array &X, const array &Y,
double alpha = 0.1, double maxerr = 0.05,
int maxiter = 1000, bool verbose = false) {
array Weights = constant(0, X.dims(1), Y.dims(1));
for (int i = 0; i < maxiter; i++) {
array P = predict(X, Weights);
array err = Y - P;
if (mean<float>(abs(err) < maxerr) break;
Weights += alpha * matmulTN(X, err);
}
return Weights;
}
...
array Weights = train(train_feats, train_targets);
array test_outputs = predict(test_feats, Weights);
display_results<true>(test_images, test_outputs,
test_targets, 20);
The complete source code can be found [here][31].
For more code examples, visit the [examples/
][4] directory.
Documentation
You can find the complete documentation here.
Quick links: