Click here to Skip to main content
15,895,709 members
Articles / Artificial Intelligence

More Neural Network mathematics and code (C#)

Rate me:
Please Sign up or sign in to vote.
4.96/5 (13 votes)
25 Nov 2012CPOL16 min read 34.4K   1.6K   69  
Running, Initializing and Training a Neural Network.
//NeuralNetwork, Lucas Allen, completed 21/11/2012.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NeuralNetwork
{
    //The NNetwork class is used for running and training a neural network.  
    public class NNetwork
    {
        //Neural Network variables 

        //•	n – The neural network has n neurons.
        int n;
        //•	i – The length of input vectors.
        int i;
        //•	o – The length of output vectors.
        int o;

        //•	W – An n*n matrix, where W[k][j] is the synapse weight from the jth neuron to the kth neuron.
        double?[][] W;
        //•	t – A vector of length n, where t[k] is the threshold/bias of the kth neuron. t[k] must be greater than zero when k >= i, so that neurons can’t activate themselves.
        //t[k] where k < i, can be set to anything because they correspond to the thresholds of the input neurons and are not used.
        double[] t;
        //•	a – A vector of length n, a[k] is 0 if the kth neuron is not activated and is 1 if it is.
        bool[] a;

        // Constructor for the NNetwork class, sets the neural network varialbles and resets the network
        public NNetwork(double?[][] weightMatrix, double[] thresholdVector, int inputVectorLength, int outputVectorLength)
        {
            n = weightMatrix.Length;
            i = inputVectorLength;
            o = outputVectorLength;

            W = weightMatrix;
            t = thresholdVector;
            a = new bool[n];

            Reset();
        }

        //Running a neural network this the process where a binary input vector (I) of length i is inputted into the network and a
        //binary output vector (O) of length o is outputted from the network. This is a three step process:
        //1)	for (k = 1 to i) a[k] = I[k]
        //2)	for (k = i+1 to n) if (DotProduct(W[k], a) >= t[k]) a[k] = 1
        //3)	for (k = n-o+1 to n) O[k + o - n] = a[k]
        public bool[] Run(bool[] inputVector)
        {
            bool[] outputVector = new bool[o];
            //1)	for (k = 1 to i) a[k] = I[k]
            for (int k = 0; k < i; k++) a[k] = inputVector[k];
            //2)	for (k = i+1 to n) if (DotProduct(W[k], a) >= t[k]) a[k] = 1
            for (int k = i; k < n; k++) if (DotProduct(k) >= t[k]) a[k] = true;
            //3)	for (k = n-o+1 to n) O[k + o - n] = a[k]
            for (int k = n - o; k < n; k++) outputVector[k + o - n] = a[k];
            return outputVector;

        }

        //Basic Learning algorithm
        //Execute this after running the neural network with the desired inputs
        public void Learn(bool[] outputVector, double learningRate)
        {
            //Regardless of the outputs given, change the output neurons to the desired output vector (O)
            for (int k = n - o; k < n; k++) a[k] = outputVector[k + o - n];
            // Ideally this should weaken unused connections (synapses) and strengthen the used ones. 
            for (int k = n - 1; k >= i; k--)
                for (int j = 0; j < n; j++)
                    if (a[j] && W[k][j] != null)
                        if (a[k]) W[k][j] += learningRate * (t[k] - DotProduct(k));
                        else W[k][j] -= learningRate * (t[k] - DotProduct(k));                    
        }

        //Reset: sets all elements of a to zero/false.
        //This allows the run algorithm to be executed again without interference from the previous times.
        public void Reset()
        {
            for (int k = 0; k < n; k++) a[k] = false;
        }

        //Dotproduct: finds the dot proctuct of the jth row of W and the a vector.
        private double DotProduct(int j)
        {
            double sum = 0;
            for (int k = 0; k < n; k++) if (a[k] && W[j][k] != null) sum += W[j][k].GetValueOrDefault();
            return sum;
        }

        //Returns the weight matrix (W)
        public double?[][] GetWeightMatrix()
        {
            return W;
        }

        //Returns the threshold vector (t)
        public double[] GetThresholdVector()
        {
            return t;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions