Пример #1
0
 public void Run()
 {
     activation = 0;
     for (synapticIndex = 0; synapticIndex < TotalInputSynaptics; synapticIndex++)
     {
         synaticConnection = _ListOfInputSynaptics[synapticIndex];
         activation        = activation + synaticConnection._SourceNeuron._Value * synaticConnection._Weight;
     }
     _Value = _ContainerLayer._InternalMemory.ThresholdFunction(activation, _ContainerLayer._NeuralNetwork._LearningRate);
 }
Пример #2
0
        public void CreateNetwork(string topoloy, float learningRate)
        {
            #region Initialization
            RaiseTaskStartedEvent(this, "Constructing Network Topology: " + topoloy);
            _ListOfLayers                         = new List <Layer>();
            _ListOfNeurons                        = new List <Neuron>();
            _ListOfSynapticConnections            = new List <SynapticConnection>();
            _SourceTargetNeuronSynapticDictionary = new Dictionary <string, SynapticConnection>();
            int neuronid, layerid, synapticid;
            neuronid       = 0;
            layerid        = 0;
            synapticid     = 0;
            _TopoloyString = topoloy;
            char[]   sep             = { ',' };
            string[] toplogyarray    = topoloy.Split(sep);
            int      numberofneurons = int.Parse(toplogyarray[0]);
            int      index;
            Neuron   neuron;
            float    progress;
            #endregion Initialization
            _LearningRate = learningRate;
            #region InputLayer Code
            layerid++;
            _InputLayer = new InputLayer(layerid, this);
            _ListOfLayers.Add(_InputLayer);
            RaiseTaskProgressEvent(this, 0.00f, "Adding Neurons to Input Layer");
            for (index = 1; index <= numberofneurons; index++)
            {
                neuronid++;
                neuron = new Neuron(neuronid, _InputLayer);
                _InputLayer.AddNeurons(neuron);
                _ListOfNeurons.Add(neuron);
                progress = ((float)(index) / (float)(numberofneurons)) * 100;
                RaiseTaskProgressEvent(this, progress, "");
            }
            RaiseTaskProgressEvent(this, 0.00f, _InputLayer._ListOfNeurons.Count.ToString() + " Neuron(s) Has Been Added in InputLayer");
            #endregion InputLayer Code

            #region HiddenLayer Code
            int         hiddenLayerIndex;
            HiddenLayer hiddenLayer;

            for (hiddenLayerIndex = 1; hiddenLayerIndex < toplogyarray.Length - 1; hiddenLayerIndex++)
            {
                layerid++;
                RaiseTaskProgressEvent(this, 0.00f, "Adding Neurons to Hidden Layer No.: " + hiddenLayerIndex.ToString());
                numberofneurons = int.Parse(toplogyarray[hiddenLayerIndex]);
                hiddenLayer     = new HiddenLayer(layerid, this);
                for (index = 1; index <= numberofneurons; index++)
                {
                    neuronid++;
                    neuron = new Neuron(neuronid, hiddenLayer);
                    hiddenLayer.AddNeurons(neuron);
                    _ListOfNeurons.Add(neuron);
                    progress = ((float)(index) / (float)(numberofneurons)) * 100;
                    RaiseTaskProgressEvent(this, progress, "");
                }
                _ListOfLayers.Add(hiddenLayer);
                progress = ((float)(hiddenLayerIndex) / (float)(toplogyarray.Length - 1)) * 100;
                RaiseTaskProgressEvent(this, progress, "");
                RaiseTaskProgressEvent(this, 0.00f, hiddenLayer._ListOfNeurons.Count.ToString() + " Neuron(s) Has Been Added in HiddenLayer No.: " + hiddenLayerIndex.ToString());
            }
            #endregion HiddenLayer Code

            #region OutputLayer Code
            RaiseTaskProgressEvent(this, 0.00f, "Adding Neurons to OutputLayer");
            layerid++;
            _OutputLayer = new OutputLayer(layerid, this);
            neuronid++;
            neuron = new Neuron(neuronid, _OutputLayer);
            _OutputLayer.AddNeurons(neuron);
            _ListOfLayers.Add(_OutputLayer);
            _ListOfNeurons.Add(neuron);
            RaiseTaskProgressEvent(this, 100.00f, "");
            RaiseTaskProgressEvent(this, 0.00f, _OutputLayer._ListOfNeurons.Count.ToString() + " Neuron(s) Has Been Added in OutputLayer");
            #endregion OutputLayer Code

            Neuron             sourceNeuron, targetNeuron;
            int                sourceNeuronIndex, targetNeuronIndex;
            int                sourceLayerIndex, targetLayerIndex;
            int                totalSourceNeurons, totalTargetNeurons;
            int                totalSourceLayer, totalTargetLayer;
            Layer              sourceLayer, targetLayer;
            SynapticConnection synapticConnection;
            float              weight;
            Random             rnd     = new Random();
            Random             rndSign = new Random();

            #region Input - First Hidden Synapyic Connection Creation Code
            RaiseTaskProgressEvent(this, 0.00f, "Creating Synaptic Connection Between Input - Hidden Layer No.: 1");
            sourceLayer        = _InputLayer;
            targetLayer        = _ListOfLayers[1];
            totalSourceNeurons = sourceLayer._ListOfNeurons.Count;
            totalTargetNeurons = targetLayer._ListOfNeurons.Count;
            for (sourceNeuronIndex = 0; sourceNeuronIndex < totalSourceNeurons; sourceNeuronIndex++)
            {
                sourceNeuron = sourceLayer._ListOfNeurons[sourceNeuronIndex];
                for (targetNeuronIndex = 0; targetNeuronIndex < totalTargetNeurons; targetNeuronIndex++)
                {
                    weight = (float)rnd.NextDouble();
                    if (((float)rndSign.NextDouble()) < 0.5)
                    {
                        weight = weight * -1;
                    }

                    targetNeuron = targetLayer._ListOfNeurons[targetNeuronIndex];
                    synapticid++;
                    synapticConnection = new SynapticConnection(synapticid, sourceNeuron, targetNeuron, weight);
                    sourceNeuron._ListOfOutputSynaptics.Add(synapticConnection);
                    targetNeuron._ListOfInputSynaptics.Add(synapticConnection);
                    _ListOfSynapticConnections.Add(synapticConnection);
                    _SourceTargetNeuronSynapticDictionary.Add(sourceNeuron.ID.ToString() + "-" + targetNeuron.ID.ToString(), synapticConnection);
                }
                progress = ((float)(sourceNeuronIndex + 1) / (float)(totalSourceNeurons)) * 100;
                RaiseTaskProgressEvent(this, progress, "");
            }
            #endregion Input - First Hidden Synapyic Connection Creation Code

            #region Hidden to Hidden Synapyic Connection Creation Code
            totalSourceLayer = _ListOfLayers.Count - 3;
            totalTargetLayer = _ListOfLayers.Count - 1;
            for (sourceLayerIndex = 1; sourceLayerIndex <= totalSourceLayer; sourceLayerIndex++)
            {
                sourceLayer = _ListOfLayers[sourceLayerIndex];
                //for (targetLayerIndex = sourceLayerIndex + 1; targetLayerIndex < totalTargetLayer; targetLayerIndex++)
                //{
                targetLayerIndex = sourceLayerIndex + 1;
                RaiseTaskProgressEvent(this, 0.00f, "Creating Synaptic Connection Between Hidden Layer No.: " + sourceLayerIndex.ToString() + " - Hidden Layer No.: " + targetLayerIndex.ToString());

                targetLayer        = _ListOfLayers[targetLayerIndex];
                totalSourceNeurons = sourceLayer._ListOfNeurons.Count;
                totalTargetNeurons = targetLayer._ListOfNeurons.Count;
                for (sourceNeuronIndex = 0; sourceNeuronIndex < totalSourceNeurons; sourceNeuronIndex++)
                {
                    sourceNeuron = sourceLayer._ListOfNeurons[sourceNeuronIndex];
                    for (targetNeuronIndex = 0; targetNeuronIndex < totalTargetNeurons; targetNeuronIndex++)
                    {
                        weight = (float)rnd.NextDouble();
                        if (((float)rndSign.NextDouble()) < 0.5)
                        {
                            weight = weight * -1;
                        }

                        targetNeuron = targetLayer._ListOfNeurons[targetNeuronIndex];
                        synapticid++;
                        synapticConnection = new SynapticConnection(synapticid, sourceNeuron, targetNeuron, weight);
                        sourceNeuron._ListOfOutputSynaptics.Add(synapticConnection);
                        targetNeuron._ListOfInputSynaptics.Add(synapticConnection);
                        _ListOfSynapticConnections.Add(synapticConnection);
                        _SourceTargetNeuronSynapticDictionary.Add(sourceNeuron.ID.ToString() + "-" + targetNeuron.ID.ToString(), synapticConnection);
                    }
                }
                //}
                progress = ((float)(sourceLayerIndex) / (float)(totalSourceLayer)) * 100;
                RaiseTaskProgressEvent(this, progress, "");
            }
            #endregion Hidden to Hidden Synapyic Connection Creation Code

            #region Hidden to Output Synapyic Connection Creation Code
            RaiseTaskProgressEvent(this, 0.00f, "Creating Synaptic Connection Between Last Hidden Layer and Output Layer");
            sourceLayer        = _ListOfLayers[_ListOfLayers.Count - 2];
            targetLayer        = _OutputLayer;
            totalSourceNeurons = sourceLayer._ListOfNeurons.Count;
            totalTargetNeurons = targetLayer._ListOfNeurons.Count;
            for (sourceNeuronIndex = 0; sourceNeuronIndex < totalSourceNeurons; sourceNeuronIndex++)
            {
                sourceNeuron = sourceLayer._ListOfNeurons[sourceNeuronIndex];
                for (targetNeuronIndex = 0; targetNeuronIndex < totalTargetNeurons; targetNeuronIndex++)
                {
                    weight = (float)rnd.NextDouble();
                    if (((float)rndSign.NextDouble()) < 0.5)
                    {
                        weight = weight * -1;
                    }

                    targetNeuron = targetLayer._ListOfNeurons[targetNeuronIndex];
                    synapticid++;
                    synapticConnection = new SynapticConnection(synapticid, sourceNeuron, targetNeuron, weight);
                    sourceNeuron._ListOfOutputSynaptics.Add(synapticConnection);
                    targetNeuron._ListOfInputSynaptics.Add(synapticConnection);
                    _ListOfSynapticConnections.Add(synapticConnection);
                    _SourceTargetNeuronSynapticDictionary.Add(sourceNeuron.ID.ToString() + "-" + targetNeuron.ID.ToString(), synapticConnection);
                }
                progress = ((float)(sourceNeuronIndex + 1) / (float)(totalSourceNeurons)) * 100;
                RaiseTaskProgressEvent(this, progress, "");
            }
            #endregion Hidden to Output Synapyic Connection Creation Code
            RaiseTaskProgressEvent(this, 0.00f, "Total Synaptic Connections in Network (Network ID = " + ID.ToString() + ") are = " + _ListOfSynapticConnections.Count.ToString());
            RaiseTaskCompleted(this, null, "Topology Construction Completed...");
        }
Пример #3
0
        public void Run()
        {
            if (TrainingDataTable == null)
            {
                throw new Exception("Training Engine (ID = " + ID.ToString() + " cannot start training because TrainingDataTable is Empty/NULL/Invalid");
            }
            _MCCCalculator.Reset();
            _TSSE = 0.00f;
            _RMSE = 0.00f;
            for (patternIndex = 0; patternIndex < InputPatternCount; patternIndex++)
            {
                //Feed Farword
                patternRow = _TrainingData.Rows[patternIndex];
                _RuntimeEngine.PatternRow = patternRow;
                y = _RuntimeEngine.Run();
                //Feed Backword
                //OutputLayer:- dpj=(Tpj-Ypj) Ypj (1-Ypj)
                t = float.Parse(patternRow[_OutputAttribute].ToString());
                _MCCCalculator.Update(t, y);
                error  = ((t - y) * y * (1 - y));
                neuron = _NeuralNetwork._OutputLayer._ListOfNeurons[0];
                _NeuronErrorDictionary[neuron.ID] = error;
                //Compute TSSE and RMSE
                _TSSE = _TSSE + ((t - y) * (t - y)) / 2;
                _RMSE = (float)Math.Sqrt((2 * _TSSE) / InputPatternCount);
                //HiddenLayer:- dpj= Ypj (1-Ypj) Sum(&pk Wkj); where &pk is the error post synaptic neuron k
                totalLayers = _NeuralNetwork._ListOfLayers.Count;
                for (hiddenLayerIndex = totalLayers - 2; hiddenLayerIndex > 0; hiddenLayerIndex--)
                {
                    layer        = _NeuralNetwork._ListOfLayers[hiddenLayerIndex];
                    totalNeurons = layer._ListOfNeurons.Count;
                    for (neuronIndex = 0; neuronIndex < totalNeurons; neuronIndex++)
                    {
                        neuron = layer._ListOfNeurons[neuronIndex];
                        y      = neuron._Value;

                        postSynapticErrorSignal = 0;
                        postLayer = _NeuralNetwork._ListOfLayers[hiddenLayerIndex + 1];
                        for (postSynapticNeuronIndex = 0; postSynapticNeuronIndex < postLayer._ListOfNeurons.Count; postSynapticNeuronIndex++)
                        {
                            postSynapticNeuron      = postLayer._ListOfNeurons[postSynapticNeuronIndex];
                            postSynapticNeuronError = _NeuronErrorDictionary[postSynapticNeuron.ID];
                            postSynapticConnection  = _NeuralNetwork._SourceTargetNeuronSynapticDictionary[neuron.ID.ToString() + "-" + postSynapticNeuron.ID.ToString()];
                            postSynapticErrorSignal = postSynapticErrorSignal + (postSynapticNeuronError * postSynapticConnection._Weight);
                        }
                        error = y * (1 - y) * postSynapticErrorSignal;
                        _NeuronErrorDictionary[neuron.ID] = error;
                    }
                }
                //Change Weights
                for (synapticIndex = 0; synapticIndex < _NeuralNetwork._ListOfSynapticConnections.Count; synapticIndex++)
                {
                    synaptic         = _NeuralNetwork._ListOfSynapticConnections[synapticIndex];
                    deltaWeight      = synaptic._SourceNeuron._Value * _NeuronErrorDictionary[synaptic._TargetNeuron.ID] * _NeuralNetwork._LearningRate;
                    synaptic._Weight = synaptic._Weight + deltaWeight + (_Momentum * _PreviousWeightDictionary[synaptic.ID]);
                    _PreviousWeightDictionary[synaptic.ID] = deltaWeight;
                }
                //Progress report
                // _NeuralNetwork.RaiseTaskProgressEvent(this ,((((float)(patternIndex + 1)) / ((float)(InputPatternCount))) * 100) ,"Training Engine Learning Pattern No: " + patternIndex.ToString() + " of " + InputPatternCount.ToString());
            }
        }