DaNNet

Table of Contents

General

DaNNet is a C++ Deep Artificial Neural Network processing library/framework using the Armadillo library as a base.

Features

Installation

Download DaNNet and install/extract it to your install directory of choice.
After extracting you should have a directory structure as:

└── DaNNet
├── data
├── doc
├── examples
└── include

Download and install Armadillo, version >= 8.4 is used in the examples.

Linux

sudo apt install libarmadillo-dev

Windows

See https://gitlab.com/conradsnicta/armadillo-code/blob/9.300.x/README.md#7-windows-installation

Linux build example

> cd examples
> g++ -O3 Spiral.cpp -I../include -larmadillo -o Spiral
> ./Spiral

...or

> cd examples
> make Spiral
> ./Spiral

Code examples

Spiral.cpp:

#include "dnn.h"
using namespace dnn;
int main()
{
std::cout << "Compile time : " << __TIME__ << " at " << __DATE__ << std::endl;
std::cout << "Armadillo version: " << arma::arma_version::as_string() << std::endl;
std::cout << "DaNNet version : " << version_info() << std::endl;
arma::arma_rng::set_seed_random();
arma::Mat<DNN_Dtype> X,T;
arma::uword N = 30000, Ntest = 1000, K = 3;
arma::uword Nepoch = 20;
arma::uword Nbatch = 32;
gen_spiral(X,T,N,K);
arma::Mat<DNN_Dtype> Xtrain = X;
arma::Mat<DNN_Dtype> Ttrain = T;
// Setup layers
layer_input in(2); // x,y
layer_dense d1(20);
layer_dense d2(10);
layer_dense d3(K);
// Opt
opt_rmsprop alg1((DNN_Dtype)0.01);
d1.set_opt_alg(alg1);
d2.set_opt_alg(alg2);
d3.set_opt_alg(alg3);
// Setup network model
net model;
model.add_layer(in);
model.add_layer(d1);
model.add_layer(a1);
model.add_layer(d2);
model.add_layer(a2);
model.add_layer(d3);
model.add_layer(a3);
model.add_layer(out);
if (model.compile())
{
std::cout << "\nModel successfully validated!" << std::endl;
}
model.disp();
// Train
model.data_set(Xtrain, Ttrain,Nbatch,Nepoch);
for (arma::uword n = 0; n < Nepoch; n++)
{
model.train();
model.show_stat();
}
// Test
arma::Mat<DNN_Dtype> Xtest,Ttest;
gen_spiral(Xtest,Ttest,Ntest,K);
model.data_set(Xtest, Ttest,50);
model.test();
model.show_stat();
return 0;
}

MNIST.cpp: (Download MNIST dataset to ../data first)

#include "dnn.h"
using namespace dnn;
int main()
{
std::cout << "Compile time : " << __TIME__ << " at " << __DATE__ << std::endl;
std::cout << "Armadillo version: " << arma::arma_version::as_string() << std::endl;
std::cout << "DaNNet version : " << version_info() << std::endl;
// Generate data
arma::Mat<DNN_Dtype> Xtrain,Ttrain;
read_MNIST("../data/train-images.idx3-ubyte","../data/train-labels.idx1-ubyte",Xtrain,Ttrain,1);
// Setup layers
layer_input in(28, 28,1); // Rin, Cin, Channels
layer_conv c1(6, 5, 2, 1); // Channels, filt sz, pad, stride
pool_max p1(2); // Max pool block
layer_conv c2(16,5, 0, 1); // Channels, filt sz, pad, stride
pool_max p2(2); // Max pool block
layer_dense d1(120); // Nr of nodes
layer_drop dr1((DNN_Dtype)0.8); // Dropout keep rate
layer_dense d2(84); // Nr of nodes
layer_dense d3(10); // Nr of nodes
// Setup optimizers
opt_adadelta alg1((DNN_Dtype)0.92);
c1.set_opt_alg(alg1);
opt_rmsprop alg2((DNN_Dtype)0.02);
c2.set_opt_alg(alg2);
opt_adadelta alg21((DNN_Dtype)0.92);
bn1.set_opt_alg(alg21);
opt_SGD_nesterov alg3((DNN_Dtype)0.01, (DNN_Dtype)0.9, (DNN_Dtype)0.0001);
d1.set_opt_alg(alg3);
opt_SGD_nesterov alg4((DNN_Dtype)0.02, (DNN_Dtype)0.85, (DNN_Dtype)0.0001);
d2.set_opt_alg(alg4);
opt_SGD_nesterov alg5((DNN_Dtype)0.01, (DNN_Dtype)0.9, (DNN_Dtype)0.0001);
d3.set_opt_alg(alg5);
// Setup network model
arma::uword Nbatch=64;
arma::uword Nepoch=5;
net model;
model.add_layer(in);
model.add_layer(c1);
model.add_layer(a1);
model.add_layer(p1);
model.add_layer(c2);
model.add_layer(bn1);
model.add_layer(a2);
model.add_layer(p2);
model.add_layer(d1);
model.add_layer(a3);
model.add_layer(dr1);
model.add_layer(d2);
model.add_layer(a4);
model.add_layer(d3);
model.add_layer(out);
if (model.compile())
{
std::cout << "\nModel successfully validated!" << std::endl;
}
model.disp();
// Train
model.data_set(Xtrain, Ttrain,Nbatch,Nepoch);
for (arma::uword n = 0; n < Nepoch; n++)
{
model.train();
model.show_stat();
}
// Test
arma::Mat<DNN_Dtype> Xtest,Ttest;
read_MNIST("../data/t10k-images.idx3-ubyte","../data/t10k-labels.idx1-ubyte",Xtest,Ttest,1);
model.data_set(Xtest, Ttest,100,1);
model.test();
model.show_stat();
return 0;
}