Marcin Drobik

software journeyman notes

Generic Neural Network class

In the "Introduction to Neural Networks" series we worked on the specific example of XOR network. Because of that, the network layout was hardcoded so that It'd be very hard to change the number of nodes or layers.

With some refactoring I managed to make the code more generic and moved it to separate class (TrainableNerualNetwork - you can look it up on GitHub).

Example

The class is quite easy to work with:

[Test]
public void XOR_TrainableNetwork()
{

First define the training data. As previously we define the set of input and outputs for the XOR function:

    var traingData = Vector(0, 0)
        .Concat(Vector(0, 1))
        .Concat(Vector(1, 0))
        .Concat(Vector(1, 1));
    Matrix traingOutput = "0, 1, 1, 0";

    var X = traingData.Evaluate();
    var Y = traingOutput.Evaluate();

Instantiate the TrainableNeuralNetwork object by passing the number of nodes in each layer:

    var xorNetwork = new TrainableNeuralNetwork(2, 3, 2, 1);

Train the network with prepared training data:

    xorNetwork.Train(X, Y);

Network should output the same values as training data:

    MatrixAssert.AreEqual("0, 1, 1, 0", xorNetwork.Output);
}

Smaller network?

Is it possible to make the XOR network with smaller number of nodes and layers? Certainly! You can try running code above with following object:

var xorNetwork = new TrainableNeuralNetwork(2, 2, 1);

And you'll find that it still outputs correct data.

comments powered by Disqus