示例#1
0
        private INeuron[] CreateNeurons(int neuronsCount, int inputCount, int outputCount, double bias, double excess, IActivation activation)
        {
            var neurons = new INeuron[neuronsCount];

            for (var i = 0; i < _inputCount; i++)
            {
                neurons[i] = new InputNeuron
                {
                    B = bias,
                };
            }
            for (var i = _inputCount; i < neuronsCount - _outputCount; i++)
            {
                neurons[i] = new HiddenNeuron
                {
                    Activation = activation,
                    L          = 0.5 + 0.25 * 1,
                    B          = bias,
                };
            }
            for (var i = neuronsCount - _outputCount; i < neuronsCount; i++)
            {
                neurons[i] = new OutputNeuron
                {
                    Activation = activation,
                    L          = 0.5 + 0.25 * 3,
                    B          = bias,
                };
            }
            return(neurons);
        }
示例#2
0
        public void TestIfNeuronCalculatesCorrectOutput()
        {
            double   weight1   = 0.5;
            double   weight2   = 2.5;
            Dendrite dendrite1 = new Dendrite(weight1);
            Dendrite dendrite2 = new Dendrite(weight2);

            // Give input to inputneurons
            InputNeuron n1 = new InputNeuron();
            InputNeuron n2 = new InputNeuron();

            n1.Output = 5;
            n2.Output = 2;

            dendrite1.CalculateValue(n1.Output);
            dendrite2.CalculateValue(n2.Output);

            // Make hidden neuron, assign dendrites
            HiddenNeuron n3 = new HiddenNeuron();

            n3.Dendrites.Add(dendrite1);
            n3.Dendrites.Add(dendrite2);
            n3.CalculateOutput();

            double expectedValue = 7.5;
            double actualValue   = n3.Output;

            Assert.AreEqual(expectedValue, actualValue);
        }
示例#3
0
        private NeuralNetwork.NeuralNetwork CreateNn()
        {
            NeuralNetwork.NeuralNetwork nn;

            nn = new NeuralNetwork.NeuralNetwork();

            InputLayer inputLayer = nn.CreateInputLayer();

            //inHeading = inputLayer.CreateNeuron("heading");
            //inVelocityAngle = inputLayer.CreateNeuron("v_angle");
            //inVelocityLength = inputLayer.CreateNeuron("v_length");
            inNextCheckpointDistance0  = inputLayer.CreateNeuron("c_dist0");
            inNextCheckpointDistance1  = inputLayer.CreateNeuron("c_dist1");
            inNextCheckpointDistance2  = inputLayer.CreateNeuron("c_dist2");
            inNextCheckpointDistance3  = inputLayer.CreateNeuron("c_dist3");
            inNextCheckpointAngle0     = inputLayer.CreateNeuron("c_angle0");
            inNextCheckpointAngle1     = inputLayer.CreateNeuron("c_angle1");
            inNextCheckpointAngle2     = inputLayer.CreateNeuron("c_angle2");
            inNextCheckpointAngle3     = inputLayer.CreateNeuron("c_angle3");
            inNextCheckpointAngle4     = inputLayer.CreateNeuron("c_angle4");
            inNextCheckpointAngle5     = inputLayer.CreateNeuron("c_angle5");
            inNextNextCheckpointAngle0 = inputLayer.CreateNeuron("nnc_angle0");
            inNextNextCheckpointAngle1 = inputLayer.CreateNeuron("nnc_angle1");
            inNextNextCheckpointAngle2 = inputLayer.CreateNeuron("nnc_angle2");
            inNextNextCheckpointAngle3 = inputLayer.CreateNeuron("nnc_angle3");
            inNextNextCheckpointAngle4 = inputLayer.CreateNeuron("nnc_angle4");
            inNextNextCheckpointAngle5 = inputLayer.CreateNeuron("nnc_angle5");
            //inNextCheckpointDistance = inputLayer.CreateNeuron("c_dist");

            OutputLayer outputLayer = nn.CreateOutputLayer();

            outHeading0 = outputLayer.CreateNeuron("o_heading0");
            outHeading1 = outputLayer.CreateNeuron("o_heading1");
            outHeading2 = outputLayer.CreateNeuron("o_heading2");
            outHeading3 = outputLayer.CreateNeuron("o_heading3");
            outHeading4 = outputLayer.CreateNeuron("o_heading4");
            outHeading5 = outputLayer.CreateNeuron("o_heading5");
            outThrust0  = outputLayer.CreateNeuron("o_thrust0");
            outThrust1  = outputLayer.CreateNeuron("o_thrust1");
            outThrust2  = outputLayer.CreateNeuron("o_thrust2");
            outThrust3  = outputLayer.CreateNeuron("o_thrust3");
            outThrust4  = outputLayer.CreateNeuron("o_thrust4");
            outThrust5  = outputLayer.CreateNeuron("o_thrust5");
            outThrust6  = outputLayer.CreateNeuron("o_thrust6");

            for (int i = 0; i < 3; i++)
            {
                HiddenLayer hiddenLayer = nn.CreateHiddenLayer();

                for (int j = 0; j < 32; j++)
                {
                    HiddenNeuron hiddenNeuron = hiddenLayer.CreateNeuron(string.Format("hidden[{0}][{1}]", i, j));
                }
            }

            nn.CreateFullConnections();
            nn.InitWithRandomValues();

            return(nn);
        }
示例#4
0
 /// <summary>
 /// Updates the statistics.
 /// </summary>
 /// <param name="neuron">The hidden neuron.</param>
 public void Update(HiddenNeuron neuron)
 {
     Input.Update(neuron.Statistics.InputStimuliStat);
     Reservoir.Update(neuron.Statistics.ReservoirStimuliStat);
     Total.Update(neuron.Statistics.TotalStimuliStat);
     return;
 }
示例#5
0
 /// <summary>
 /// Updates the anomalies.
 /// </summary>
 /// <param name="neuron">The hidden neuron.</param>
 /// <param name="numOfResSynapses">The number of reservoir synapses.</param>
 public void Update(HiddenNeuron neuron, int numOfResSynapses)
 {
     if (numOfResSynapses == 0)
     {
         ++NoResSynapses;
     }
     if (neuron.Statistics.ReservoirStimuliStat.NumOfNonzeroSamples == 0 && numOfResSynapses > 0)
     {
         ++NoResStimuli;
     }
     if (neuron.Statistics.AnalogSignalStat.NumOfNonzeroSamples == 0)
     {
         ++NoAnalogOutput;
     }
     if (neuron.Statistics.AnalogSignalStat.NumOfNonzeroSamples > 0 && neuron.Statistics.AnalogSignalStat.Span == 0)
     {
         ++ConstAnalogOutput;
     }
     if (neuron.Statistics.FiringStat.NumOfNonzeroSamples == 0)
     {
         ++NotFiring;
     }
     if (neuron.Statistics.FiringStat.NumOfNonzeroSamples > 0 && neuron.Statistics.FiringStat.Span == 0)
     {
         ++ConstFiring;
     }
     return;
 }
        private static void CalculateHidden(HiddenNeuron hidden)
        {
            var incomingSum = hidden.Incoming.Sum(i => i.From.Value * i.Weight);
            var withBias    = incomingSum + (hidden.BiasSynapse.From.Value * hidden.BiasSynapse.Weight);

            hidden.Value = hidden.ActivationFunction.Activate(withBias);
        }
示例#7
0
        public void NeuralNetwork_Neuron_HiddenNeuronIOTest()
        {
            HiddenNeuron HN = new HiddenNeuron(1, 1, 3);

            HN.InValue = new double[] { 0.10101, 0.20202, 0.30303 };
            Assert.AreEqual(HN.OutValue, 0.64704151179442626);
        }
示例#8
0
        // Initialization

        /// <summary>
        /// Create new Neural Network by given <see cref="NetworkLayout"/>
        /// </summary>
        /// <param name="networkLayout">NN layout</param>
        public NeuralNetwork(NetworkLayout networkLayout)
        {
            var ic = networkLayout.inputs;
            var oc = networkLayout.outputs;
            var ih = networkLayout.hidden;

            inputs  = new InputNeuron[ic];
            outputs = new Neuron[oc];
            hidden  = new HiddenNeuron[ih.Length][];

            // Initialize DNA
            genes = new float[networkLayout.TotalGenes];

            // Gene counter
            var g = 0;


            // Generate input layer
            for (var i = 0; i < ic; i++)
            {
                var neuron = inputs[i] = new InputNeuron();

                foreach (var gene in neuron.Genes)
                {
                    genes[g++] = gene;
                }
            }

            // Number of inputs for neuron in layer, which equals to number of neurons in previous layer
            var layerInputsCount = ic;

            // Generate hidden layers
            for (var i = 0; i < ih.Length; i++)
            {
                var hc    = ih[i];
                var layer = hidden[i] = new HiddenNeuron[hc];

                for (var j = 0; j < hc; j++)
                {
                    var neuron = layer[j] = new HiddenNeuron(layerInputsCount, networkLayout.activationFunction);
                    foreach (var gene in neuron.Genes)
                    {
                        genes[g++] = gene;
                    }
                }

                layerInputsCount = hc;
            }

            // Generate output layer
            for (var i = 0; i < oc; i++)
            {
                var neuron = outputs[i] = new Neuron(layerInputsCount);
                foreach (var gene in neuron.Genes)
                {
                    genes[g++] = gene;
                }
            }
        }
示例#9
0
 /// <summary>
 /// Updates the statistics.
 /// </summary>
 /// <param name="neuron">The hidden neuron.</param>
 /// <param name="inputSynapses">A collection of the input synapses.</param>
 /// <param name="internalSynapses">A collection of the internal synapses.</param>
 public void Update(HiddenNeuron neuron, IList <Synapse> inputSynapses, IList <Synapse> internalSynapses)
 {
     Synapses.Update(inputSynapses);
     Synapses.Update(internalSynapses);
     NeuronsAnomalies.Update(neuron, internalSynapses.Count);
     Pools[neuron.Location.PoolID].Update(neuron, inputSynapses, internalSynapses);
     return;
 }
示例#10
0
 public HiddenLayer(Activation activation, TrainingInfo trainInfo, int size)
 {
     var neurons = new Neuron[size + 1];
     for (int i = 0; i < size; i++)
         neurons[i] = new HiddenNeuron(activation, trainInfo);
     neurons[size] = new BiasNeuron();
     this.neurons = neurons;
 }
        public void HiddenNeuron_DoesNothing()
        {
            // Arrange
            var hiddenNeuron = new HiddenNeuron();

            // Assert
            Assert.IsNotNull(hiddenNeuron.Connections);
            Assert.IsTrue(hiddenNeuron.Connections.Count == 0);
        }
示例#12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_inSize"></param>
        /// <param name="_numHLay"></param>
        /// <param name="_hidSize"></param>
        /// <param name="_outSize"></param>
        public MultilayerNeuralNet(int _inSize, int _numHLay, int _hidSize, int _outSize)
        {
            InputLayerSize = _inSize;

            NumHiddenLayers = _numHLay;

            HiddenLayerSize = _hidSize;

            OutputLayerSize = _outSize;


            //Instantiate Jagged Arrays of Neurons

            int TotalLayers = NumHiddenLayers + 2;

            Neurons = new Neuron[TotalLayers][];

            Neurons[0] = new InputNeuron[InputLayerSize];

            for (int i = 0; i < NumHiddenLayers; i++)
            {
                Neurons[i + 1] = new HiddenNeuron[HiddenLayerSize];
            }

            Neurons[TotalLayers - 1] = new OutputNeuron[OutputLayerSize];

            //Instantiate the layers based on the inputs
            for (int i = 0; i < InputLayerSize; i++)
            {
                Neurons[0][i] = new InputNeuron(HiddenLayerSize);
            }

            for (int i = 0; i < NumHiddenLayers; i++)
            {
                for (int j = 0; j < HiddenLayerSize; j++)
                {
                    if (i == 0)
                    {
                        Neurons[i + 1][j] = new HiddenNeuron(InputLayerSize, HiddenLayerSize);
                    }
                    if (i == (NumHiddenLayers - 1))
                    {
                        Neurons[i + 1][j] = new HiddenNeuron(HiddenLayerSize, OutputLayerSize);
                    }
                    else
                    {
                        Neurons[i + 1][j] = new HiddenNeuron(HiddenLayerSize, HiddenLayerSize);
                    }
                }
            }

            for (int i = 0; i < OutputLayerSize; i++)
            {
                Neurons[NumHiddenLayers + 1][i] = new OutputNeuron(HiddenLayerSize);
            }
            //Get input values
        }
示例#13
0
        public static void Reset()
        {
            First  = new InputNeuron();
            Second = new InputNeuron();

            A = new HiddenNeuron(First, Second);
            B = new HiddenNeuron(First, Second);

            Output = new OutputNeuron(A, B);
        }
示例#14
0
        public static void Reset()
        {
            First = new InputNeuron();
            Second = new InputNeuron();

            A = new HiddenNeuron(First, Second);
            B = new HiddenNeuron(First, Second);

            Output = new OutputNeuron(A, B);
        }
示例#15
0
        public HiddenLayer(Activation activation, TrainingInfo trainInfo, int size)
        {
            var neurons = new Neuron[size + 1];

            for (int i = 0; i < size; i++)
            {
                neurons[i] = new HiddenNeuron(activation, trainInfo);
            }
            neurons[size] = new BiasNeuron();
            this.neurons  = neurons;
        }
示例#16
0
 /// <summary>
 /// Updates the statistics.
 /// </summary>
 /// <param name="neuron">The hidden neuron.</param>
 /// <param name="inputSynapses">A collection of the input synapses.</param>
 /// <param name="internalSynapses">A collection of the internal synapses.</param>
 public void Update(HiddenNeuron neuron, IList <Synapse> inputSynapses, IList <Synapse> internalSynapses)
 {
     ++NumOfNeurons;
     Activation.Update(neuron.Statistics.ActivationStat);
     Stimuli.Update(neuron);
     Synapses.Update(inputSynapses);
     Synapses.Update(internalSynapses);
     Signal.Update(neuron);
     NeuronsAnomalies.Update(neuron, internalSynapses.Count);
     return;
 }
示例#17
0
 public HiddenLayer(int size, int sizePrevious, object previousLayer, object nextLayer, double eps, double alpha)
 {
     Epsilon            = eps;
     Alpha              = alpha;
     Size               = size;
     this.previousLayer = previousLayer;
     this.nextLayer     = nextLayer;
     Neuron             = new HiddenNeuron[size];
     for (int i = 0; i < size; i++)
     {
         Neuron[i] = new HiddenNeuron(sizePrevious, i, nextLayer);
     }
 }
        public HiddenLayer(int n, NeuralNetwork neuralNetwork)
        {
            network = neuralNetwork;

            hiddenNeurons = n;

            neurons = new HiddenNeuron[hiddenNeurons];

            for (int i = 0; i < neurons.Length; i++)
            {
                neurons[i] = new HiddenNeuron(i, this);
            }
        }
        public void GetOutgoingConnections_WithoutOutgoingConnections_ReturnsEmpty()
        {
            // Arrange
            IHiddenNeuron       hiddenNeuron = new HiddenNeuron();
            IIncomingConnection inConnection = new IncomingConnection(hiddenNeuron);

            _neuron.Connections.Add(inConnection);

            // Act
            var outConnections = _neuron.GetOutgoingConnections();

            // Assert
            Assert.IsTrue(outConnections.Count() == 0);
        }
        public void HiddenLayer_WithSortOrderAndNeurons_InitializesProperty()
        {
            // Arrange
            var hiddenNeuron = new HiddenNeuron();

            _layer = new HiddenLayer(5, new List <IHiddenNeuron>()
            {
                hiddenNeuron
            });

            // Assert
            Assert.IsTrue(_layer.Neurons.Count() == 1);
            Assert.IsTrue(_layer.Neurons.First() == hiddenNeuron);
        }
        public void GenerateConnectionsWith_WithExistingConnectionHidden_DoesNotAddNewOutgoingConnection()
        {
            // Arrange
            var incomingHiddenNeuron = new HiddenNeuron();

            // Act
            _hiddenNeuron.GenerateConnectionsWith(incomingHiddenNeuron);
            _hiddenNeuron.GenerateConnectionsWith(incomingHiddenNeuron);
            _hiddenNeuron.GenerateConnectionsWith(incomingHiddenNeuron);
            _hiddenNeuron.GenerateConnectionsWith(incomingHiddenNeuron);

            // Assert
            Assert.IsTrue(incomingHiddenNeuron.GetOutgoingConnections().Count() == 1);
        }
        public void GetOutgoingConnections_WithOutgoingConnections_ReturnsConnections()
        {
            // Arrange
            IHiddenNeuron       hiddenNeuron  = new HiddenNeuron();
            IOutgoingConnection outConnection = new OutgoingConnection(hiddenNeuron);

            _neuron.Connections.Add(outConnection);

            // Act
            var outConnections = _neuron.GetOutgoingConnections();

            // Assert
            Assert.IsTrue(outConnections.Count() == 1);
            Assert.IsTrue(outConnections.First() == outConnection);
        }
        public void GenerateConnectionsWith_WithoutConnectionHidden_CreatesOutgoingConnection()
        {
            // Arrange
            var incomingHiddenNeuron = new HiddenNeuron();

            // Act
            var connectionsBefore = incomingHiddenNeuron.GetOutgoingConnections().Count();

            _hiddenNeuron.GenerateConnectionsWith(incomingHiddenNeuron);

            // Assert
            Assert.IsTrue(connectionsBefore == 0);
            Assert.IsTrue(incomingHiddenNeuron.GetOutgoingConnections().Count() == 1);
            Assert.IsTrue(incomingHiddenNeuron.GetOutgoingConnections().First().ToNeuron == _hiddenNeuron);
        }
        public void GetIncomingConnections_WithIncomingConnections_ReturnsConnections()
        {
            // Arrange
            IHiddenNeuron       hiddenNeuron = new HiddenNeuron();
            IIncomingConnection inConnection = new IncomingConnection(hiddenNeuron);

            _neuron.Connections.Add(inConnection);

            // Act
            var inConnections = _neuron.GetIncomingConnections();

            // Assert
            Assert.IsTrue(inConnections.Count() == 1);
            Assert.IsTrue(inConnections.First() == inConnection);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="_inSize"></param>
        /// <param name="_hidSize"></param>
        /// <param name="_outSize"></param>
        public SingleHiddenLayerNN(int _inSize, int _hidSize, int _outSize)
        {
            InputLayerSize = _inSize;

            HiddenLayerSize = _hidSize;

            OutputLayerSize = _outSize;

            for (int i = 0; i < InputLayerSize; i++)
            {
                int NumSynapse = HiddenLayerSize;

                InputNeuron InpN = new InputNeuron(NumSynapse);
                inputNeurons.Add(InpN);
            }

            for (int i = 0; i < HiddenLayerSize; i++)
            {
                int NumSynapse      = OutputLayerSize;
                int NumInputSynapse = InputLayerSize;

                HiddenNeuron HidN = new HiddenNeuron(NumInputSynapse, NumSynapse);
                hiddenNeurons.Add(HidN);
            }

            for (int i = 0; i < OutputLayerSize; i++)
            {
                int NumInputSynapse = HiddenLayerSize;

                OutputNeuron OutN = new OutputNeuron(NumInputSynapse);
                outputNeurons.Add(OutN);
            }

            for (int i = 0; i < InputLayerSize; i++)
            {
                Console.WriteLine("Please input Value " + (i + 1));
                inputNeurons[i]._inputValue = double.Parse(Console.ReadLine());
            }

            NetworkForwardInput();
            NetworkForwardHidden();

            double[] FinalOutPut = NetworkForwardOutput();

            FinalOutPutDisplay();
        }
示例#26
0
        public void HiddenLayer_InstantiedWithNumberOfNeurons_MakesCorrectNumberOfNeurons()
        {
            HiddenNeuron n1 = new HiddenNeuron();
            HiddenNeuron n2 = new HiddenNeuron();
            HiddenNeuron n3 = new HiddenNeuron();
            BiasNeuron   b  = new BiasNeuron();

            List <Neuron> l = new List <Neuron> {
                n1, n2, n3, b
            };

            int expectedValue = l.Count;

            int actualValue = hiddenLayer.Neurons.Count;

            Assert.AreEqual(expectedValue, actualValue);
        }
        /*
         * Computes output neurons value.
         */
        public double computeOutputNeuronValue()
        {
            int    index  = 0;
            double retVal = 0.0;

            foreach (OutputNeuron neuron in outputLayer)
            {
                foreach (double weight in neuron.Weights)
                {
                    HiddenNeuron hidNeuron = (HiddenNeuron)hiddenLayer[index];
                    neuron.Soma += weight * hidNeuron.Value;
                }
                neuron.Soma += neuron.Bias;
                neuron.Value = tansigAF(neuron.Soma);
                retVal       = neuron.Value;
            }
            return(retVal);
        }
 /*
  * Forward pass
  * Parameter - data for input
  */
 public double forwardPass(double[] data)
 {
     for (int j = 0; j < inputLayer.Count; j++)
     {
         InputNeuron inNeuron = (InputNeuron)inputLayer[j];
         inNeuron.Input = data[j];
         inputLayer[j]  = inNeuron;
     }
     for (int j = 0; j < hiddenLayer.Count; j++)
     {
         HiddenNeuron hidNeuron = (HiddenNeuron)hiddenLayer[j];
         hidNeuron.Soma = 0.0;
         hiddenLayer[j] = hidNeuron;
     }
     for (int j = 0; j < outputLayer.Count; j++)
     {
         OutputNeuron outNeuron = (OutputNeuron)outputLayer[j];
         outNeuron.Soma = 0.0;
         outputLayer[j] = outNeuron;
     }
     return(forwardPass());
 }
示例#29
0
            private void InitNetwork()
            {
                inputLayer = new List <Neuron>();
                if (NeuralDefines.USE_HIDDEN_LAYER)
                {
                    hiddenLayer = new List <Neuron>();
                }
                outputLayer = new List <Neuron>();

                inputLayer.Add(new InputNeuron(() => this.agentHealthInput / 100.0f));
                inputLayer.Add(new InputNeuron(() => this.enemyHealthInput / 100.0f));
                inputLayer.Add(new InputNeuron(() => this.enemyVisibilityInput));

                if (NeuralDefines.USE_HIDDEN_LAYER)
                {
                    for (int i = 0; i < NeuralDefines.HIDDEN_LAYER_SIZE; ++i)
                    {
                        Neuron neuron = new HiddenNeuron(NeuralDefines.SIGMOID_FUNCTION);
                        foreach (Neuron input in inputLayer)
                        {
                            neuron.AddInput(input);
                        }
                        hiddenLayer.Add(neuron);
                    }
                }

                foreach (OutputVariable var in Enum.GetValues(typeof(OutputVariable)))
                {
                    Neuron        neuron        = new OutputNeuron(NeuralDefines.SIGMOID_FUNCTION);
                    List <Neuron> previousLayer = NeuralDefines.USE_HIDDEN_LAYER ? hiddenLayer : inputLayer;
                    foreach (Neuron input in previousLayer)
                    {
                        neuron.AddInput(input);
                    }
                    outputLayer.Add(neuron);
                }
            }
示例#30
0
 public void growNeuralNetwork(string keywords)
 {
     /* buildNeuralNet ----- Method to be called in initializeController. Scans through the
      * lists of sensors and effectors, creating neurons for them. Links said neurons by
      * synapses in a manner specified by special keywords string from initializeController. */
     foreach (Sensor sen in sensors)
     {
         SensorNeuron sensorNeuron = new SensorNeuron(sen);
         sensorNeurons.Add(sensorNeuron);
     }
     if (rootController.hiddenNeurons)
     {
         for (int i = 0; i < sensors.Count; i++)
         {
             HiddenNeuron hiddenNeuron = new HiddenNeuron();
             hiddenNeurons.Add(hiddenNeuron);
         }
     }
     foreach (Effector eff in effectors)
     {
         EffectorNeuron effectorNeuron = new EffectorNeuron(eff);
         effectorNeurons.Add(effectorNeuron);
     }
     foreach (SensorNeuron senRon in sensorNeurons)
     {
         foreach (EffectorNeuron effRon in effectorNeurons)
         {
             if (senRon.checkConnectionTag(effRon))
             {
                 Synapse syn = new Synapse(senRon, effRon);
                 senRon.addSynapse(syn);
                 synapses.Add(syn);
             }
         }
     }
 }
        /*
         * Backpropagation
         * Parameters - Number of epochs, Learning rate, Target Error
         *              Input File Path, Output File Path
         *
         */
        public void backPropagate(int epoch, double learningRate, double targetError
                                  , string inputFilePath, string outputFilePath)
        {
            List <List <double> > trainData = new List <List <double> >();

            string[] inputContent = File.ReadAllLines(inputFilePath);

            List <double> outData = new List <double>();

            string[] outputContent = File.ReadAllLines(outputFilePath);

            int dataIndex = 0;

            foreach (string line in inputContent)
            {
                trainData.Add(new List <double>());
                string[] data = line.Split(',');
                foreach (string d in data)
                {
                    trainData[dataIndex].Add(Convert.ToDouble(d));
                }
                dataIndex++;
            }

            foreach (string line in outputContent)
            {
                outData.Add(Convert.ToDouble(line));
            }

            List <List <double> > hiddenNewWeights = new List <List <double> >();

            for (int i = 0; i < hiddenLayer.Count; i++)
            {
                hiddenNewWeights.Add(new List <double>());
                for (int j = 0; j < inputLayer.Count; j++)
                {
                    hiddenNewWeights[i].Add(0.5);
                }
            }

            List <double> outputNewWeights = new List <double>();

            for (int i = 0; i < hiddenLayer.Count; i++)
            {
                outputNewWeights.Add(0.5);
            }

            List <double> outputNewBias = new List <double>();

            for (int i = 0; i < outputLayer.Count; i++)
            {
                outputNewBias.Add(0.5);
            }

            List <double> hiddenNewBias = new List <double>();

            for (int i = 0; i < hiddenLayer.Count; i++)
            {
                hiddenNewBias.Add(0.5);
            }

            double[] backPropInputs = backPropInputs = new double[trainData[0].Count];

            int trainIndex = 0;

            double error = 1.0;

            for (int i = 0; i < epoch; i++)
            {
                if (error <= targetError)
                {
                    break;
                }

                List <double>         outputOldWeights = outputNewWeights;
                List <List <double> > hiddenOldWeights = hiddenNewWeights;
                List <double>         outputOldBias    = outputNewBias;
                List <double>         hiddenOldBias    = hiddenNewBias;

                // Update Output Layer
                for (int j = 0; j < outputLayer.Count; j++)
                {
                    OutputNeuron outNeuron = (OutputNeuron)outputLayer[j];
                    for (int k = 0; k < outNeuron.Weights.Count; k++)
                    {
                        outNeuron.Weights[k] = outputOldWeights[k];
                    }
                    outNeuron.Bias = outputOldBias[j];
                    outNeuron.Soma = 0.0;
                    outputLayer[j] = outNeuron;
                }

                // Update Hidden Layer
                for (int j = 0; j < hiddenLayer.Count; j++)
                {
                    HiddenNeuron hidNeuron = (HiddenNeuron)hiddenLayer[j];
                    for (int k = 0; k < hidNeuron.Weights.Count; k++)
                    {
                        hidNeuron.Weights[k] = hiddenOldWeights[j][k];
                    }
                    hidNeuron.Bias = hiddenOldBias[j];
                    hidNeuron.Soma = 0.0;
                    hiddenLayer[j] = hidNeuron;
                }

                for (int j = 0; j < backPropInputs.Length; j++)
                {
                    backPropInputs[j] = trainData[trainIndex][j];
                }

                for (int j = 0; j < inputLayer.Count; j++)
                {
                    InputNeuron inNeuron = (InputNeuron)inputLayer[j];
                    inNeuron.Input = backPropInputs[j];
                    inputLayer[j]  = inNeuron;
                }

                double backPropOut = outData[trainIndex];

                double output = forwardPass();

                error = 0.5 * (Math.Pow(backPropOut - output, 2));

                double dE_dOut    = output - backPropOut;
                double dOut_dNetO = 1 - (Math.Pow(output, 2));

                // Hidden to Output Weights
                int index = 0;
                foreach (HiddenNeuron neuron in hiddenLayer)
                {
                    double dNetO_dW = neuron.Value;
                    double dE_dW    = dE_dOut * dOut_dNetO * dNetO_dW;
                    outputNewWeights[index] = outputOldWeights[index] - (learningRate * dE_dW);
                    index++;
                }

                // Output Bias
                index = 0;
                foreach (OutputNeuron outNeuron in outputLayer)
                {
                    double dNetO_dB = 1.0;
                    double dE_dB    = dE_dOut * dOut_dNetO * dNetO_dB;
                    outputNewBias[index] = outputOldBias[index] - (learningRate * dE_dB);
                }

                List <double> dNetO_dOutH = new List <double>();

                // Hidden Neuron
                index = 0;
                foreach (HiddenNeuron neuron in hiddenLayer)
                {
                    OutputNeuron outNeuron = (OutputNeuron)outputLayer[0];
                    dNetO_dOutH.Add(outNeuron.Weights[index]);
                    index++;
                }

                List <List <double> > dOutH_dNetOH = new List <List <double> >();

                // Hidden Neuron Net Value
                index = 0;
                foreach (HiddenNeuron neuron in hiddenLayer)
                {
                    dOutH_dNetOH.Add(new List <double>());
                    dOutH_dNetOH[index].Add(1 - (Math.Pow(neuron.Value, 2)));
                    index++;
                }

                List <List <double> > dNetOH_dW = new List <List <double> >();

                // Net OH Weight
                index = 0;
                int backPropInputIndex = 0;
                foreach (HiddenNeuron hidNeuron in hiddenLayer)
                {
                    dNetOH_dW.Add(new List <double>());
                    foreach (InputNeuron inNeuron in inputLayer)
                    {
                        dNetOH_dW[index].Add(backPropInputs[backPropInputIndex]);
                        backPropInputIndex++;
                    }
                    backPropInputIndex = 0;
                    index++;
                }

                List <List <double> > dE_dWI = new List <List <double> >();

                // Input to Hidden Weight
                index = 0;
                int dNetOH_dW_Index = 0;
                foreach (HiddenNeuron hidNeuron in hiddenLayer)
                {
                    dE_dWI.Add(new List <double>());
                    foreach (InputNeuron inNeuron in inputLayer)
                    {
                        dE_dWI[index].Add(dE_dOut * dOut_dNetO * dNetO_dOutH[index] * dOutH_dNetOH[index][0]
                                          * dNetOH_dW[index][dNetOH_dW_Index]);
                        dNetOH_dW_Index++;
                    }
                    dNetOH_dW_Index = 0;
                    index++;
                }

                for (int j = 0; j < hiddenNewWeights.Count; j++)
                {
                    for (int k = 0; k < hiddenNewWeights[j].Count; k++)
                    {
                        hiddenNewWeights[j][k] = hiddenOldWeights[j][k]
                                                 - (learningRate * dE_dWI[j][k]);
                    }
                }

                List <double> dNetOH_dB = new List <double>();
                for (int j = 0; j < hiddenLayer.Count; j++)
                {
                    dNetOH_dB.Add(1.0);
                }

                List <double> dE_dBI = new List <double>();
                for (int j = 0; j < hiddenLayer.Count; j++)
                {
                    dE_dBI.Add(dE_dOut * dOut_dNetO * dNetO_dOutH[j] *
                               dOutH_dNetOH[j][0] * dNetOH_dB[j]);
                }

                for (int j = 0; j < hiddenOldBias.Count; j++)
                {
                    hiddenNewBias[j] = hiddenOldBias[j] - (learningRate * dE_dBI[j]);
                }

                trainIndex++;

                if (trainIndex > trainData.Count - 1)
                {
                    trainIndex = 0;
                }
            }
        }
示例#32
0
 /// <summary>
 /// Updates statistics
 /// </summary>
 /// <param name="neuron">The hidden neuron.</param>
 public void Update(HiddenNeuron neuron)
 {
     Analog.Update(neuron.Statistics.AnalogSignalStat);
     Spiking.Update(neuron.Statistics.FiringStat);
     return;
 }