Skip to content

Custom layers and structures

Dimitar Rusev edited this page Jun 10, 2018 · 1 revision

Creating your own custom layer

  • Must inherit Layer

Standard layers for more complex networks, such as Convolutional and Recurrent are available in my other library: ComplexNeuralNetwork

Creating a more complex structure

For example: You need a layer, that simply scales its Input by a factor of a

public class MyLayer : Layer
{
    public int Factor { get; private set; }

    public MyLayer(int a, string name, int neuronCount, string activationFunction, Layer nextLayer = null, Layer previousLayer = null) : 
        base(name, neuronCount, activationFunction, nextLayer, previousLayer) 
    {
        this.Factor = a; 
        foreach(double weight in this.Weights)
            weight = this.Factor;

        // Set bias to 0
        for(int i = 0; i < this.Weights.Length; i++)
            this.Weights[i][this.Weights[i].Length - 1] = 0;
    }

    // You won't have to change weights when backpropagating, so you must implement a backwards method 
    // It is used to override the actions normally performed on a fully connected (dense) Layer
    public override void Backwards(double[] error = null)
    {
        int ln = this.Neurons.Count;
        for (int i = 0; i < ln - 1 && error != null; i++)
        {
             // We have -1 because the expected data does not cover the unused bias
             l.Neurons.Error[i] = error[i];
        }
        
        // and so on
    }
}

You would have to create the structure of the network itself, because by using the default constructor, you won't be able to add your custom layer:

var layers = new List<Layer>();
InputLayer i  = GetInputLayer(); // Method not included in the library
HiddenLayer h = GetHiddenLayer(); // Method not included in the library
MyLayer m     = GetMyLayer(); // Method not included in the library
OutputLayer o  = GetOutputLayer(); // Method not included in the library

layers.Add(i); 
layers.Add(h); 
layers.Add(m); 
layers.Add(o);

var nn = new Network(layers);
Clone this wiki locally