Ejemplo n.º 1
0
        static void SimpleTest()
        {
            var neuralNetwork = new NeuronalNetwork();
            var network       = new SimpleNetwork(neuralNetwork, Sigmoid, Sigmoid);

            var layers = new int[5];

            layers[0] = 2;
            layers[1] = 3;
            layers[2] = 5;
            layers[3] = 4;
            layers[4] = 3;

            network.Init(layers);

            Console.WriteLine($"Created Network with {layers[0]} input neurons and {layers[layers.Length-1]} output neurons.");

            var consoleInput = string.Empty;
            var stopWatch    = new Stopwatch();

            do
            {
                Console.WriteLine("Please enter all input values:");
                for (var i = 0; i < layers[0]; i++)
                {
                    Console.Write($"{i}. > ");
                    consoleInput = Console.ReadLine();
                    if (double.TryParse(consoleInput, out var value))
                    {
                        network.SetInputValue(i, value);
                    }
                }

                Console.WriteLine("Process..");
                stopWatch.Reset();
                stopWatch.Start();

                network.Process();

                stopWatch.Stop();
                Console.WriteLine($"Finished! Time: {stopWatch.ElapsedTicks} Ticks ({stopWatch.ElapsedMilliseconds} ms)");

                Console.WriteLine("Output values:");
                for (var i = 0; i < layers[layers.Length - 1]; i++)
                {
                    Console.WriteLine($"{i}.: {network.GetOutputValue(i)}");
                }

                Console.Write("Enter anything to exit.");
                consoleInput = Console.ReadLine();
            } while (consoleInput == string.Empty);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //We create the training template

            TrainingTemplate xor_template = new TrainingTemplate();

            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                0, 0
            }, new List <double> {
                0
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                0, 1
            }, new List <double> {
                1
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                1, 0
            }, new List <double> {
                1
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                1, 1
            }, new List <double> {
                0
            }));

            //We create the network
            SimpleNetwork sn = new SimpleNetwork();

            // We create the neurons
            Neuron input1Neuron = new Neuron("input1", 0, 1);
            Neuron input2Neuron = new Neuron("input2", 0, 1);

            Neuron hidden1Neuron = new Neuron("hidden1", 0, 1);
            Neuron hidden2Neuron = new Neuron("hidden2", 0, 1);

            Neuron outputNeuron = new Neuron("output", 0, 1);

            //We asign them Sigmoid fire functions

            input1Neuron.fireRule = new SigmoidFireRule();
            input2Neuron.fireRule = new SigmoidFireRule();

            hidden1Neuron.fireRule = new SigmoidFireRule();
            hidden1Neuron.fireRule = new SigmoidFireRule();

            outputNeuron.fireRule = new SigmoidFireRule();

            // We bind them together

            input1Neuron.addNewInput("network_in_1", 1, 1);
            input2Neuron.addNewInput("network_in_2", 1, 1);

            hidden1Neuron.inputs.Add(input1Neuron.output);
            hidden1Neuron.inputs.Add(input2Neuron.output);

            hidden2Neuron.inputs.Add(input1Neuron.output);
            hidden2Neuron.inputs.Add(input2Neuron.output);

            outputNeuron.inputs.Add(hidden1Neuron.output);

            // We put them into layers

            sn.inputLayer.neurons.Add(input1Neuron);
            sn.inputLayer.neurons.Add(input2Neuron);

            sn.hiddenLayer.neurons.Add(hidden1Neuron);
            sn.hiddenLayer.neurons.Add(hidden2Neuron);

            sn.outputLayer.neurons.Add(outputNeuron);

            // We train

            double error = sn.train(xor_template, 5000, new ErrorHistory());

            Console.WriteLine(error);
            Console.ReadKey();
        }