Esempio n. 1
0
        public static void ShowOutputs(Network network)
        {
            Console.WriteLine(":::::::::::OUTPUTS START::::::::::::::::::::::");

            for (int i = 0; i < network.Layers.Count; i++)
            {
                if (network.Layers[i].Neurons.Count > 0)
                {
                    foreach (Neuron neuron in network.Layers[i].Neurons)
                    {
                        Console.Write(neuron.Output + " ");
                    }

                    Console.WriteLine();
                }
            }
            Console.WriteLine(":::::::::::OUTPUTS END::::::::::::::::::::::");
        }
Esempio n. 2
0
        public static void ShowError(Network network)
        {
            Console.WriteLine(":::::::::::ERROR::::::::::::::::::::::");

            Console.WriteLine(network.Layers.Last().Neurons.First().Error);

            Console.WriteLine(":::::::::::ERROR end::::::::::::::::::::::");

        }
Esempio n. 3
0
        public static void Calculate(Network network, double[] inputs)
        {
            for (int i = 0; i < network.Layers.Count; i++)
            {
                if (i == 0)
                {
                    for (int k = 0; k < network.Layers[i].Neurons.Count; k++)
                    {
                        network.Layers[i].Neurons[k].Accept(inputs[k]);
                    }
                }
                else
                {
                    foreach (Neuron neuron in network.Layers[i].Neurons)
                    {
                        neuron.Reflex();
                    }
                }
            }

            //for (int i = 0; i < network.Layers.Count; i++)
            //{
            //    if (network.Layers[i].Neurons.Count > 0)
            //    {
            //        foreach (Neuron neuron in network.Layers[i].Neurons)
            //        {
            //            Console.Write(neuron.Output + " ");
            //        }

            //        Console.WriteLine();
            //    }
            //}
            //Console.WriteLine(":::::::::::::::::::::::::::::::::");
        }
Esempio n. 4
0
        public static void TrainNetwork(Network network, double rightAnswer)
        {
            double h = 0.1;

            double tempError = rightAnswer - network.Layers.Last().Neurons.FirstOrDefault().Output;
            double error = tempError * tempError * 0.5;

            //Console.WriteLine(" Error : " + tempError);

            network.Layers.Last().Neurons.FirstOrDefault().Error = tempError;

            for (int i = network.Layers.Count - 2; i >= 0; i--)
            {
                foreach (Neuron neuron in network.Layers[i].Neurons)
                {
                    double err = 0;
                    foreach (Link link in neuron.OutgoingLinks)
                    {
                        err += link.Neuron.Error * link.Weight;
                    }
                    neuron.Error = err * (1 - Math.Tanh(neuron.Output) * Math.Tanh(neuron.Output));

                    //Console.Write(neuron.Error);
                }
                //Console.WriteLine("EndLayer : ");
            }

            //Console.WriteLine("EndERROR COUNTING ||||||||||||||||||||||||||| : ");

            for (int i = 0; i < network.Layers.Count; i++)
            {
                foreach (Neuron neuron in network.Layers[i].Neurons)
                {
                    foreach (Link link in neuron.OutgoingLinks)
                    {
                        link.Weight = link.Weight + h * link.Neuron.Error * neuron.Output;
                        if (link.Neuron.IncomingLinks.Count > 0)
                        {
                            link.Neuron.IncomingLinks.First(f => f.Neuron.Id == neuron.Id).Weight = link.Weight;
                        }
                    }
                }
            }

            //for (int i = 0; i < network.Layers.Count - 1; i++)
            //{
            //    foreach (Neuron neuron in network.Layers[i].Neurons)
            //    {
            //        foreach (Link link in neuron.OutgoingLinks)
            //        {
            //            Console.Write(link.Weight + " ");
            //        }
            //        Console.WriteLine();
            //    }

            //    Console.WriteLine();
            //}
            //Console.WriteLine("|||||||||||||||||||||||||||||||||||||||||||||||||||");

        }
Esempio n. 5
0
 public static void RelaxNetwork(Network network)
 {
     for (int i = 0; i < network.Layers.Count; i++)
     {
         foreach (Neuron neuron in network.Layers[i].Neurons)
         {
             neuron.Relax();
         }
     }
 }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Random random = new Random();

            Network network = new Network();
            network.Layers.Add(new Layer());
            network.Layers.Add(new Layer());
            network.Layers.Add(new Layer());
            network.Layers.Add(new Layer());
            network.Layers.Add(new Layer());

            for (int i = 0; i < network.Layers.Count; i++)
            {
                if (i == 0)
                {
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                }
                if (i == 1)
                {
                    network.Layers[i].Neurons.Add(new Neuron());
                }
                if (i == 2)
                {
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                }
                if (i == 3)
                {
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                }
                if (i == 4)
                {
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                    network.Layers[i].Neurons.Add(new Neuron());
                }
                if (i == 5)
                {
                    network.Layers[i].Neurons.Add(new Neuron());
                }
            }

            #region Initialization

            for (int i = 0; i < network.Layers.Count - 1; i++)
            {
                foreach (Neuron neuron in network.Layers[i].Neurons)
                {
                    foreach (Neuron attachingNeuron in network.Layers[i + 1].Neurons)
                    {
                        neuron.Attach(attachingNeuron, random.NextDouble());
                    }
                }
            }

            for (int i = 0; i < network.Layers.Count - 1; i++)
            {
                foreach (Neuron neuron in network.Layers[i].Neurons)
                {
                    foreach (Link link in neuron.OutgoingLinks)
                    {
                        Console.Write(link.Weight + " ");
                    }
                    Console.WriteLine();
                }

                Console.WriteLine();
            }
            Console.WriteLine("|||||||||||||||||||||||||||||||||||||||||||||||||||");

            #endregion

            double[] inputs1 = new double[2];
            inputs1[0] = 1;
            inputs1[1] = 0;
            double answer1 = 0;

            double[] inputs2 = new double[2];
            inputs2[0] = 0;
            inputs2[1] = 0;
            double answer2 = 0;


            double[] inputs3 = new double[2];
            inputs3[0] = 1;
            inputs3[1] = 1;
            double answer3 = 1;

            double[] inputs4 = new double[2];
            inputs4[0] = 0;
            inputs4[1] = 1;
            double answer4 = 0;


            Calculate(network, inputs1);
            TrainNetwork(network, answer1);
            ShowError(network);
            //ShowOutputs(network);
            RelaxNetwork(network);


            //Calculate(network, inputs2);
            //TrainNetwork(network, answer2);
            //ShowError(network);
            //ShowOutputs(network);
            //RelaxNetwork(network);

            Calculate(network, inputs3);
            TrainNetwork(network, answer3);
            ShowError(network);
            //ShowOutputs(network);
            RelaxNetwork(network);

            Calculate(network, inputs4);
            TrainNetwork(network, answer4);
            ShowError(network);
            //ShowOutputs(network);
            RelaxNetwork(network);


            for (int i = 0; i < 300000; i++)
            {
                Calculate(network, inputs1);
                TrainNetwork(network, answer1);
                RelaxNetwork(network);

                //Calculate(network, inputs2);
                //TrainNetwork(network, answer2);
                //RelaxNetwork(network);

                Calculate(network, inputs3);
                TrainNetwork(network, answer3);
                RelaxNetwork(network);

                Calculate(network, inputs4);
                TrainNetwork(network, answer4);
                RelaxNetwork(network);
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("AFTER : ");
            Console.WriteLine();

            Calculate(network, inputs1);
            TrainNetwork(network, answer1);
            ShowError(network);
            //ShowOutputs(network);
            RelaxNetwork(network);


            //Calculate(network, inputs2);
            //TrainNetwork(network, answer2);
            //ShowError(network);
            //ShowOutputs(network);
            //RelaxNetwork(network);

            Calculate(network, inputs3);
            TrainNetwork(network, answer3);
            ShowError(network);
            //ShowOutputs(network);
            RelaxNetwork(network);

            Calculate(network, inputs4);
            TrainNetwork(network, answer4);
            ShowError(network);
            //ShowOutputs(network);
            RelaxNetwork(network);

            Console.ReadLine();
        }