示例#1
0
 private void BackPropagate(Neuron outputNeuron)
 {
     for (var neuronIndex = 0; neuronIndex < HiddenLayer.Neurons.Count; neuronIndex++)
     {
         var neuron = HiddenLayer.Neurons[neuronIndex];
         neuron.Error = Sigmoid.Derivative(neuron.Output) * outputNeuron.Error * outputNeuron.Weights[neuronIndex];
         neuron.AdjustWeights();
     }
 }
示例#2
0
        public void Train(List <DataSet> dataSets)
        {
            if (!ValidateDataSets(dataSets))
            {
                throw new Exception("The data set is invalid for this network.");
            }

            var epoch = 0;

            while (epoch < MaxEpochs)
            {
                foreach (var dataSet in dataSets)
                {
                    //Input Initialization
                    foreach (var neuron in InputLayer.Neurons)
                    {
                        neuron.Inputs[0] = dataSet.Values[InputLayer.Neurons.IndexOf(neuron)];
                    }

                    //Forward Propagation Through Hidden Layer
                    foreach (var neuron in HiddenLayer.Neurons)
                    {
                        neuron.Inputs = InputLayer.Neurons.Select(x => x.Output).ToArray();
                    }

                    //Output Layer
                    foreach (var neuron in OutputLayer.Neurons)
                    {
                        neuron.Inputs = HiddenLayer.Neurons.Select(x => x.Output).ToArray();
                        neuron.Error  = Sigmoid.Derivative(neuron.Output) * (dataSet.Results[OutputLayer.Neurons.IndexOf(neuron)] - neuron.Output);
                        neuron.AdjustWeights();

                        //Back Propagation
                        BackPropagate(neuron);
                    }
                }

                epoch++;
            }
        }