private void ComputeGradient(Gradients gradients, List <double> inputs, List <double> requiredOutputs) { Activities(inputs); for (int i = NumberOfLayers() - 1; i >= 1; i--) { NeuralLayer currentLayer = GetLayer(i); if (currentLayer.IsLayerTop()) { for (int j = 0; j < currentLayer.NumberOfNeurons(); j++) { Neuron currentNeuron = currentLayer.GetNeuron(j); gradients.SetThreshold(i, j, currentNeuron.Output * (1 - currentNeuron.Output) * (currentNeuron.Output - requiredOutputs[j])); } for (int j = 0; j < currentLayer.NumberOfNeurons(); j++) { Neuron currentNeuron = currentLayer.GetNeuron(j); for (int k = 0; k < currentNeuron.NumberOfInputs(); k++) { NeuralInput currentInput = currentNeuron.GetInput(k); gradients.SetWeight(i, j, k, gradients.GetThreshold(i, j) * currentLayer.LowerLayer().GetNeuron(k).Output); } } } else { for (int j = 0; j < currentLayer.NumberOfNeurons(); j++) { double aux = 0; for (int ia = 0; ia < currentLayer.UpperLayer().NumberOfNeurons(); ia++) { aux += gradients.GetThreshold(i + 1, ia) * currentLayer.UpperLayer().GetNeuron(ia).GetInput(j).Weight; } gradients.SetThreshold(i, j, currentLayer.GetNeuron(j).Output *(1 - currentLayer.GetNeuron(j).Output) * aux); } for (int j = 0; j < currentLayer.NumberOfNeurons(); j++) { Neuron currentNeuron = currentLayer.GetNeuron(j) ; for (int k = 0; k < currentNeuron.NumberOfInputs(); k++) { NeuralInput currentInput = currentNeuron.GetInput(k); gradients.SetWeight(i, j, k, gradients.GetThreshold(i, j) * currentLayer.LowerLayer().GetNeuron(k).Output); } } } } }
private void LoadFromXml(string filePath) { Console.WriteLine("NeuralNetwork : loading network topology from file " + filePath); // Read XML to XmlDocument XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(filePath); string rootNodeName = xmlDocument.Name; if (rootNodeName != "neuralNetwork") { throw new InvalidOperationException( "[Error] NN-Load: Parse error in XML file, neural network couldn't be loaded."); } var nodeList = xmlDocument.ChildNodes; for (int i = 0; i < nodeList.Count; i++) { var nodeStructure = nodeList.Item(i); if (nodeStructure != null && nodeStructure.Name == "structure") { var nodeStructureContent = nodeStructure.ChildNodes; for (int j = 0; j < nodeStructureContent.Count; j++) { var nodeLayer = nodeStructureContent.Item(j); if (nodeLayer != null && nodeLayer.Name == "layer") { NeuralLayer neuralLayer = new NeuralLayer(this); listLayers.Add(neuralLayer); var nodeLayerContent = nodeLayer.ChildNodes; for (int k = 0; k < nodeLayerContent.Count; k++) { var nodeNeuron = nodeLayerContent.Item(k); if (nodeNeuron != null && nodeNeuron.Name == "neuron") { Neuron neuron = new Neuron(double.Parse(nodeNeuron.Attributes["threshold"].ToString()), neuralLayer); neuralLayer.listNeurons.Add(neuron); var nodeNeuronContent = nodeNeuron.ChildNodes; for (int l = 0; l < nodeNeuronContent.Count; l++) { var nodeNeuralInput = nodeNeuronContent.Item(l); if (nodeNeuralInput != null && nodeNeuralInput.Name == "input") { var neuralInput = new NeuralInput(double.Parse(nodeNeuralInput.Attributes["weight"].ToString()), neuron); neuron.listInputs.Add(neuralInput); } } } } } } } } }