private static void Main() { List <string> inputList = new List <string> { "input1", "input2" }; List <string> outputList = new List <string> { "output1", "output2" }; Console.WriteLine("Basic test"); Console.WriteLine(); NeuralNetwork network1 = new NeuralNetwork(inputList, outputList, 2, 2); network1.HiddenNeurons[0].InputLinks[0].Weight = 1; network1.HiddenNeurons[2].InputLinks[0].Weight = 1; network1.OutputNeurons[0].InputLinks[0].Weight = 1; network1.Feedforward(new Dictionary <string, double> { { "input1", 2 }, { "input2", 3 } }); Console.WriteLine(network1); Console.WriteLine(); Console.WriteLine("Copying test"); Console.WriteLine(); NeuralNetwork network2 = network1.Copy(); network2.Feedforward(new Dictionary <string, double> { { "input1", 2 }, { "input2", 3 } }); Console.WriteLine(network2); Console.WriteLine(); Console.WriteLine("Random weights test"); Console.WriteLine(); NeuralNetwork network3 = network2.Copy(); network3.Feedforward(new Dictionary <string, double> { { "input1", 2 }, { "input2", 3 } }); Console.WriteLine(network3); Console.WriteLine(); network3.HiddenNeurons[0].InputLinks[0].Weight = -0.083920758234613382; network3.HiddenNeurons[0].InputLinks[1].Weight = -0.10348547168394251; network3.HiddenNeurons[1].InputLinks[0].Weight = 0.9864023685598281; network3.HiddenNeurons[1].InputLinks[1].Weight = 0.15295621398799475; network3.HiddenNeurons[2].InputLinks[0].Weight = -0.070737256110288152; network3.HiddenNeurons[2].InputLinks[1].Weight = -1.0365333983959844; network3.HiddenNeurons[3].InputLinks[0].Weight = 0.46109638325388069; network3.HiddenNeurons[3].InputLinks[1].Weight = -0.25149451732354522; network3.OutputNeurons[0].InputLinks[0].Weight = 0.50648030282771972; network3.OutputNeurons[0].InputLinks[1].Weight = 0.51670017869582552; network3.OutputNeurons[1].InputLinks[0].Weight = 0.23281424843657786; network3.OutputNeurons[1].InputLinks[1].Weight = -0.60550942224139681; network3.Feedforward(new Dictionary <string, double> { { "input1", 2 }, { "input2", 3 } }); Console.WriteLine(network3); Console.WriteLine(); Console.WriteLine("xml serialization test"); Console.WriteLine(); network3.Serialize("network.xml"); NeuralNetwork network4 = NeuralNetwork.Deserialize("network.xml"); network4.Feedforward(new Dictionary <string, double> { { "input1", 2 }, { "input2", 3 } }); Console.WriteLine(network4); Console.WriteLine(); Console.WriteLine("file save/load test"); Console.WriteLine(); network4.ExtraProperties = new Dictionary <string, string> { { "propDouble", 5.3.ToString() }, { "propFloat", 1.1f.ToString() }, { "propString", "abcd" }, }; network4.SaveToFile("network.txt"); NeuralNetwork network5 = NeuralNetwork.LoadFromFile("network.txt"); foreach (string propName in network5.ExtraProperties.Keys) { Console.WriteLine($"{propName} {network5.ExtraProperties[propName]}"); } network5.Feedforward(new Dictionary <string, double> { { "input1", 2 }, { "input2", 3 } }); Console.WriteLine(network5); Console.WriteLine(); Console.WriteLine("Adding input neuron test"); Console.WriteLine(); network5.AddInputNeuron("newInput", true); network5.HiddenNeurons[0].SetWeight("newInput", 0.333); network5.HiddenNeurons[1].SetWeight("newInput", -0.333); network5.Feedforward(new Dictionary <string, double> { { "input1", 2 }, { "input2", 3 }, { "newInput", 5.5 } }); Console.WriteLine(network5); Console.WriteLine(); Console.WriteLine("Removing input neuron test"); Console.WriteLine(); network5.RemoveInputNeuron("input1"); network5.Feedforward(new Dictionary <string, double> { { "input2", 2 }, { "newInput", 3 } }); Console.WriteLine(network5); Console.WriteLine(); Console.WriteLine("Adding output neuron test"); Console.WriteLine(); Neuron newOutputNeuron = network5.AddOutputNeuron("newOutput", true); newOutputNeuron.SetWeights(new List <double> { 0.555, -0.555 }); network5.Feedforward(new Dictionary <string, double> { { "input2", 2 }, { "newInput", 3 } }); Console.WriteLine(network5); Console.WriteLine(); Console.WriteLine("Removing output neuron test"); Console.WriteLine(); network5.RemoveOutputNeuron("output1"); network5.Feedforward(new Dictionary <string, double> { { "input2", 2 }, { "newInput", 3 } }); Console.WriteLine(network5); Console.WriteLine(); Console.WriteLine("Diff test"); Console.WriteLine(); NeuralNetwork network6 = network5.Copy(); network6.Mutate(1, 1); network6.Diff(network5); foreach (Neuron neuron in network6.AllNeurons) { Console.WriteLine($"{neuron.Name}"); foreach (double weightDelta in neuron.WeightDeltas) { Console.WriteLine($" {weightDelta}"); } } Console.WriteLine(network6); network6.Push(0.1); network6.Feedforward(new Dictionary <string, double> { { "input2", 2 }, { "newInput", 3 } }); Console.WriteLine(network6); }
/// <summary> /// Loads neural network from txt file. /// </summary> /// <param name="filename">Name of the file.</param> /// <returns>Neural network loaded from the file.</returns> #pragma warning disable SA1204 // Static elements should appear before instance elements public static NeuralNetwork LoadFromFile(string filename) #pragma warning restore SA1204 // Static elements should appear before instance elements { // object which will be returned NeuralNetwork loadedNetwork = new NeuralNetwork(); using (StreamReader reader = new StreamReader(filename)) { // loading network properties loadedNetwork.Id = int.Parse(reader.ReadLine().Split(' ')[1]); // loading extra properties while (true) { string nextLine = reader.ReadLine(); // list of properties ends with empty line if (nextLine == null || nextLine == string.Empty) { break; } loadedNetwork.ExtraProperties.Add(nextLine.Split(' ')[0], nextLine.Split(' ')[1]); } // first pass, loading list of neurons and creating them while (true) { string nextLine = reader.ReadLine(); // checking if list has ended if (nextLine == null || nextLine == string.Empty) { break; } // skipping lines with weights if (nextLine.StartsWith(" ")) { continue; } // creating new neuron Neuron newNeuron = null; string[] neuronHeader = nextLine.Split(' '); switch (neuronHeader[0]) { case "Input": newNeuron = loadedNetwork.AddInputNeuron(string.Empty); break; case "Hidden": newNeuron = loadedNetwork.AddHiddenNeuron(string.Empty); break; case "Output": newNeuron = loadedNetwork.AddOutputNeuron(string.Empty); break; } newNeuron.Id = int.Parse(neuronHeader[1]); newNeuron.Name = neuronHeader[2]; } // reset position to the beginning of the file reader.DiscardBufferedData(); reader.BaseStream.Seek(0, SeekOrigin.Begin); // skipping network parameters and empty line in the beginning while (true) { string nextLine = reader.ReadLine(); if (nextLine == string.Empty) { break; } } // second pass, loading neurons while (true) { // cheking if list has ended string nextLine = reader.ReadLine(); if (nextLine == null || nextLine == string.Empty) { break; } // finding neuron string[] neuronHeader = nextLine.Split(' '); int neuronId = int.Parse(neuronHeader[1]); Neuron neuron = loadedNetwork.GetNeuronById(neuronId); // loading weights and bias while (true) { string[] weightOrBiasLine = reader.ReadLine().Trim().Split(' '); string label = weightOrBiasLine[0]; double value = double.Parse(weightOrBiasLine[1]); if (label == "w") { int connection = int.Parse(weightOrBiasLine[2]); Neuron anotherNeuron = loadedNetwork.GetNeuronById(connection); Neuron.InputLink newInputLink = loadedNetwork.Connect(anotherNeuron, neuron); newInputLink.Weight = value; } else if (label == "b") { neuron.Bias = value; break; // line with bias is the last line of the neuron } } } } return(loadedNetwork); }