Exemple #1
0
        private List <EducationLayer> GetEducationNetwork(NeuralNetworkObj neuralNetworkObj)
        {
            if (this._educationNetwork != null)
            {
                return(this._educationNetwork);
            }

            List <EducationLayer> result = new List <EducationLayer>();

            foreach (Layer layer in neuralNetworkObj.GetListLayers())
            {
                EducationLayer educationLayer = new EducationLayer(layer.Id);
                foreach (NeuronObj neuron in layer.GetListNeurons())
                {
                    educationLayer.AddNeuron(new EducationNeuron(neuron));
                }

                result.Add(educationLayer);
            }

            for (int educationLayerId = 0; educationLayerId < result.Count - 1; educationLayerId++)
            {
                EducationLayer educationLayer     = result[educationLayerId];
                EducationLayer nextEducationLayer = result[educationLayerId + 1];

                foreach (EducationNeuron educationNeuron in educationLayer.GetListNeurons())
                {
                    foreach (EducationNeuron nextLayerEducationNeuron in nextEducationLayer.GetListNeurons())
                    {
                        Synapse synapse = this.GetSynapse(
                            neuralNetworkObj,
                            educationLayer.Id,
                            educationNeuron.NeuronObj.Id,
                            nextLayerEducationNeuron.NeuronObj.Id
                            );

                        if (synapse == null)
                        {
                            throw new Exception("Something goes wrong with id layers.");
                        }

                        educationNeuron.AddEducationSynapse(new EducationSynapse(nextLayerEducationNeuron, synapse));
                    }
                }
            }

            this._educationNetwork = result;

            return(this._educationNetwork);
        }
Exemple #2
0
        public NeuralNetworkObj GetTaughtNeuralNetwork(NeuralNetworkObj neuralNetworkObj, double[] inputVector, double[] expectedResultVector)
        {
            double[] resultVector = this.GetResultVector(neuralNetworkObj, inputVector);

            List <EducationLayer> educationNetwork = this.GetEducationNetwork(neuralNetworkObj);

            for (int educationLayerId = educationNetwork.Count - 1; educationLayerId >= 0; educationLayerId--)
            {
                EducationLayer educationLayer = educationNetwork[educationLayerId];
                foreach (EducationNeuron educationNeuron in educationLayer.GetListNeurons())
                {
                    NeuronObj neuron = educationNeuron.NeuronObj;
                    if (neuron.IsInLastLayer())
                    {
                        //deltaWeight0 = (OUT(ideal) - OUT(actual)) * f'(IN)
                        educationNeuron.WeightDelta = (expectedResultVector[neuron.Id] - resultVector[neuron.Id]) * activationFunc.GetDerivativeValue(neuron.GetInputData());
                    }
                    else
                    {
                        //deltaWeightH = f'(IN) * sum(Wi * delataWeighti)
                        double sumWeightDeltaInNextLayer = 0;
                        foreach (EducationSynapse educationSynapse in educationNeuron.GetEducationSynapses())
                        {
                            sumWeightDeltaInNextLayer += educationSynapse.Synapse.Weight * educationSynapse.EducationNeuron.WeightDelta;
                        }

                        educationNeuron.WeightDelta = this.activationFunc.GetDerivativeValue(neuron.GetInputData()) * sumWeightDeltaInNextLayer;

                        foreach (EducationSynapse educationSynapse in educationNeuron.GetEducationSynapses())
                        {
                            //grad(A, B) = deltaWeight(B) * OUT(A)
                            double grad = educationSynapse.EducationNeuron.WeightDelta * educationNeuron.NeuronObj.GetOutputData();
                            educationSynapse.Synapse.Weight += this.learningRate * grad;//todo moment a*deltaWi
                        }
                    }
                }
            }

            return(neuralNetworkObj);
        }