a C++ library for neural networks

NNF2 is a C++ library for feed-forward neural networks (also called multi-layer perceptrons).

Its modular design makes it easily extensible by users.

Batch training and saving of networks are supported.


Usage example

Programming a neural network to solve the classic XOR problem is as simple as:

// transfer functions

Sigmoid sigmoid(2.0f); // sigmoid of parameter 2.0

Heaviside heaviside; // heaviside of default parameter 0

// learning rate and initial weight range

float eps = 0.5f;

float range = 1.0f;

// input layer has 2 neurons, uses heaviside transfer function

InputLayer il(2, heaviside);

// hidden layer is connected to input layer, has 4 neurons, uses sigmoid,

// has learning rate 'eps' and initial weights in (-range, range)

Layer hl(&il, 2, sigmoid, eps, range);

// output layer is connected to hidden layer, has 1 neuron, uses heaviside

// transfer function

OutputLayer ol(&hl, 1, heaviside, eps, range);

// MLP network constructor takes input and output layers and

// a NULL-terminated list of hidden layers in the same order

// they were connected

MultiLayerPerceptron mlp(&il, &ol, &hl, NULL);

// a simple way to train the network is to generate some examples

// of input-output couples

for (int epochs = 0; epochs < 100; ++epochs)

     for (int i = 0; i <= 1; ++i)

          for (int j = 0; j <= 1; ++j) {

               float input[2] = {i, j};

               float desired_output = XOR(i, j);

               mlp.train(input, &desired_output);


// then we can test the network fitness so far

float input[2] = {1, 0};

float output;

mlp.compute(input, &output);

cout << "XOR(1, 0) = " << output << endl;

// or we can set up a text file for batch training

ifstream datafile("xor.txt");

// trains the network and returns true if it reached a mean square error

// under 0.001f in no more than 100000 epochs

bool success = mlp.optimize(datafile, 0.001f, 100000);

cout << "Success: " << success << endl;

// we can save the network on a text file

ofstream savefile("net.txt");



// load the network from a text file

ifstream loadfile("net.txt");



Used by

- An Exploration of Monophonic Instrument Classification Using Multi-Threaded Artificial Neural Networks, Marc J. Rubin, Master’s of Science Degree of Computer Science, The University of Tennessee, Knoxville, December 2009.

- ptBS reconstruction, Keith E. Turpin, Indiana University.

- Weather report Haaksbergen.


For any questions or bug reports contact <alessandro.presta@gmail.com>.