Skip to content
Dimitar Rusev edited this page Jun 10, 2018 · 2 revisions

Use:

This is how to use the framework

Creation

var nn = new Network(
   0.03 // Learning rate,
   new int[] { 2, 6, 2, 1 } // Layers,
   new string[] { "linear", "bob", "bob", "sigmoid" } // Activation functions
);

Alternatively, you can use just one activation function for the whole network:

var nn = new Network(
   0.03,
   new int[] { 2, 6, 2, 1 },
   new string[] { "sigmoid" } // Note: even the input will go through sigmoid 
);

You can also create your own list of layers, or derived of such and pass it to the network:

public List<Layer> layers = new List<Layer>() { get; set; }
public Network nn { get; set; }
// ... //
var l1 = CreateLayer(0); this.layers.Add(l1);
var l2 = CreateLayer(1); this.layers.Add(l2);
var l3 = CreateLayer(2); this.layers.Add(l3);

for(i = 1; i < this.layers.Count - 1; i++)
{
    
    this.layers[i].Network = ref nn;
    this.layers[i].Configure(i == this.Layers.Count - 1); // Whether layer is last
    this.layers[i].PreviousLayer = this.layers[i - 1];
    this.layers[i].PreviousLAyer.NextLayer = this.layers[i];
}

nn = new Network(this.layers);

MyLayer CreateLayer(int index)
{
   return new MyLayer($"{index}", 10, "sigmoid");
}

// class MyLayer : Layer
public MyLayer(string name, int neuronCount, string activationFunction) { }

The training dataset

The dataset must be Dictionary<double[], double[]>

var trainingSet = new Dictionary<double[], double[]>()
{
    { new double[] { 0, 0 }, new double[] { 0 } },
    { new double[] { 0, 1 }, new double[] { 1 } },
    { new double[] { 1, 0 }, new double[] { 1 } },
    { new double[] { 1, 1 }, new double[] { 0 } }
}; // XOR

Training

This is fairly simple:

var sw = Stopwatch.StartNew();
nn.Train(10_000, true, trainingSet); //epochs, whether to randomize the data, training set
Console.WriteLine(sw.ElapsedMilliseconds);

Testing

I'm too lazy to write a testing method, so:

foreach (var item in trainingSet)
{
    (double[] result, double[] error)  = nn.ForwardPropagate(item.Key, item.Value);
    Console.WriteLine($"Result: {result[0]}, Expected: {item.Value[0]}, Error: {error[0]}");
}