Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            NeuralNetwork.NeuralNetwork nn    = new NeuralNetwork.NeuralNetwork();
            List <double[]>             input = new List <double[]>()
            {
                new double[] { 0, 0 },
                new double[] { 0, 1 },
                new double[] { 1, 0 },
                new double[] { 1, 1 }
            };
            List <double[]> output = new List <double[]>()
            {
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 0 },
                new double[] { 1 }
            };

            for (int i = 0; i < 100000; i++)
            {
                nn.Train(input, output);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            handler = new ConsoleEventDelegate(ConsoleEventCallback);
            SetConsoleCtrlHandler(handler, true);

            var form = new NeuralForm(NeuralNetwork);

            form.Show();

            var r = new Random(1);

            if (!File.Exists("neural.net"))
            {
                var ii = 0;
                while (ii++ <= 100000)
                {
                    var input = new double[8];

                    for (int i = 0; i < input.Length; i++)
                    {
                        input[i] = r.Next(100) > 50 ? 1 : 0;
                    }

                    var output = new double[input.Length];

                    for (int i = 0; i < input.Length; i++)
                    {
                        output[i] = input[i] == 1 ? 0 : 1;
                    }

                    NeuralNetwork.Train(input, output);



                    var networkOutput = new double[input.Length];
                    for (int j = 0; j < NeuralNetwork.Layers.Last().Neurons.Count; j++)
                    {
                        var neuron = NeuralNetwork.Layers.Last().Neurons[j];
                        networkOutput[j] = neuron.Value;
                    }

                    for (int j = 0; j < output.Length; j++)
                    {
                        if (experienceList.Count > 999)
                        {
                            experienceList.Remove(experienceList.First());
                        }
                        experienceList.Add(output[j] == Math.Round(networkOutput[j]) ? 1 : 0);
                    }

                    double temp = 0;
                    foreach (var exp in experienceList)
                    {
                        temp += exp;
                    }

                    double loss = 100 - ((temp / experienceList.Count) * 100);



                    if (ii % 100 == 0)
                    {
                        Console.CursorTop  = 0;
                        Console.CursorLeft = 0;
                        Console.WriteLine($"Iteration: {ii}  ");
                        form.UpdateNeural(NeuralNetwork, $"Loss: {loss:##.###}%");
                    }
                }
            }
            else
            {
                NeuralNetwork.Load("neural.net");
                form.Hide();
                form = new NeuralForm(NeuralNetwork);
                form.Show();
            }



            while (true)
            {
                Console.Clear();

                var input         = new double[8];
                var networkOutput = new double[8];
                var output        = new double[8];

                for (int i = 0; i < input.Length; i++)
                {
                    input[i] = r.Next(100) > 50 ? 1 : 0;
                }

                for (int i = 0; i < input.Length; i++)
                {
                    output[i] = input[i] == 1 ? 0 : 1;
                }

                var response = NeuralNetwork.Run(input);

                for (int i = 0; i < response.Length; i++)
                {
                    networkOutput[i] = Math.Round(response[i]);
                }
                DrawMap(input, networkOutput);

                for (int i = 0; i < 10; i++)
                {
                    NeuralNetwork.Train(input, output);
                }



                for (int j = 0; j < output.Length; j++)
                {
                    if (experienceList.Count > 999)
                    {
                        experienceList.Remove(experienceList.First());
                    }
                    experienceList.Add(output[j] == Math.Round(networkOutput[j]) ? 1 : 0);
                }

                double temp = 0;
                foreach (var exp in experienceList)
                {
                    temp += exp;
                }

                double loss = 100 - ((temp / experienceList.Count) * 100);



                form.UpdateNeural(NeuralNetwork, $"Loss: {loss:##.###}%");
                //Thread.Sleep(300);
            }
        }
Ejemplo n.º 3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Matrix.Matrix output  = new Matrix.Matrix(1, 1);
            double        sum     = 0;
            double        sumTest = 0;
            int           epochs  = 0;
            int           epoch   = 5000;


            LoadFromFile(data, target);
            LoadFromFileTest(dataTest, targetTest);
            SortX(data, target);
            this.Controls.Add(pv);
            this.Controls.Add(pv2);
            PlotModel pm = new PlotModel();

            pv.Model = pm;
            PlotModel pm2 = new PlotModel();

            pv2.Model = pm2;

            LineSeries punktySerii = new LineSeries
            {
                LineStyle    = LineStyle.None,
                MarkerType   = MarkerType.Diamond,
                MarkerSize   = 2,
                MarkerStroke = OxyColors.Red,
                Title        = "Punkty treningowe"
            };
            LineSeries punktySeriiTestu = new LineSeries
            {
                LineStyle    = LineStyle.None,
                MarkerType   = MarkerType.Circle,
                MarkerSize   = 2,
                MarkerStroke = OxyColors.Red,
                Title        = "Punkty wejściowe"
            };

            LineSeries punktySieci = new LineSeries()
            {
                LineStyle    = LineStyle.None,
                MarkerType   = MarkerType.Plus,
                MarkerSize   = 3,
                MarkerStroke = OxyColors.Blue,
                Title        = "Punkty wyjściowe"
            };

            LineSeries punktyBledu = new LineSeries()
            {
                LineStyle    = LineStyle.Solid,
                MarkerType   = MarkerType.Diamond,
                Color        = OxyColors.Red,
                MarkerSize   = 1,
                MarkerStroke = OxyColors.Red,
                Title        = "Zbior treningowy"
            };

            LineSeries punktyBleduTestowego = new LineSeries()
            {
                LineStyle    = LineStyle.Solid,
                MarkerType   = MarkerType.Diamond,
                Color        = OxyColors.Blue,
                MarkerSize   = 1,
                MarkerStroke = OxyColors.Blue,
                Title        = "Zbior testowy"
            };

            LineSeries punktyKresek = new LineSeries
            {
                Color           = OxyColors.Blue,
                StrokeThickness = 1
            };

            /*
             * for (int i = 0; i < data.Length; i++)
             * {
             *  if (data[i] >= 1)
             *      target[i] = data[i] * -2;
             * }
             */

            for (int i = 0; i < data.Length; i++)
            {
                punktySerii.Points.Add(new DataPoint(data[i], target[i]));
            }

            for (int i = 0; i < dataTest.Length; i++)
            {
                punktySeriiTestu.Points.Add(new DataPoint(dataTest[i], targetTest[i]));
            }



            Matrix.Matrix outputFF         = new Matrix.Matrix(1, 1);
            double[]      d                = new double[1];
            NeuralNetwork.NeuralNetwork nn = new NeuralNetwork.NeuralNetwork(1, 10, 1, true);

            //uczenie

            for (int i = 0; i < epoch; ++i)
            {
                sum     = 0;
                sumTest = 0;

                foreach (int j in Enumerable.Range(0, 81).OrderBy(x => rnd.Next()))
                {
                    d[0] = data[j];
                    double[] y = new double[1] {
                        target[j]
                    };
                    nn.Train(d, y);
                    // Console.Write("x: " + data[j]+ "y= ");
                    // nn.FeedForward(d).DisplayMatrix();
                    // Console.WriteLine();
                }
                Console.WriteLine("epoka: " + epochs);



                for (int j = 0; j < data.Length; j++)
                {
                    d[0]     = data[j];
                    outputFF = nn.FeedForward(d);
                    sum     += (outputFF.tab[0, 0] - target[j]) * (outputFF.tab[0, 0] - target[j]) / 2;
                }

                for (int j = 0; j < dataTest.Length; j++)
                {
                    d[0]     = dataTest[j];
                    outputFF = nn.FeedForward(d);
                    sumTest += (outputFF.tab[0, 0] - targetTest[j]) * (outputFF.tab[0, 0] - targetTest[j]) / 2;
                }

                EpochErrorTest.Add(new DataPoint(epochs, sumTest / dataTest.Length));
                EpochError.Add(new DataPoint(epochs, sum / data.Length));
                ++epochs;
            }

            /*
             * for (int i = 0; i < data.Length; ++i)
             * {
             *  d[0] = data[i];
             *  outputFF = nn.FeedForward(d);
             *  punktySieci.Points.Add(new DataPoint(d[0], outputFF.tab[0, 0]));
             *  outerSpace[i] = outputFF.tab[0, 0];
             *  //Console.WriteLine("x: " + d[0] + " y: " + outputFF.tab[0, 0]);
             *  //outputFF.DisplayMatrix();
             * }
             */

            for (int i = 0; i < dataTest.Length; ++i)
            {
                d[0]     = dataTest[i];
                outputFF = nn.FeedForward(d);
                punktySieci.Points.Add(new DataPoint(d[0], outputFF.tab[0, 0]));
                outerSpaceTest[i] = outputFF.tab[0, 0];
            }

            SortX(data, outerSpace);
            SortX(dataTest, outerSpaceTest);

            /*
             * for(int i = 0; i < data.Length; ++i)
             * {
             *  punktyKresek.Points.Add(new DataPoint(data[i], outerSpace[i]));
             * }
             *
             * for (int i = 0; i < dataTest.Length; ++i)
             * {
             *  punktyKresek.Points.Add(new DataPoint(dataTest[i], outerSpaceTest[i]));
             * }
             */
            //for error:

            for (int i = 0; i < epoch; ++i)
            {
                punktyBleduTestowego.Points.Add(EpochErrorTest[i]);
                punktyBledu.Points.Add(EpochError[i]);
            }

            pm2.Series.Add(punktyBleduTestowego);
            pm2.Series.Add(punktyBledu);
            pm2.Axes.Add(new OxyPlot.Axes.LinearAxis {
                Position = OxyPlot.Axes.AxisPosition.Bottom, MinimumPadding = 0.1, MaximumPadding = 0.1, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Title = "Epoka"
            });
            pm2.Axes.Add(new OxyPlot.Axes.LinearAxis {
                Position = OxyPlot.Axes.AxisPosition.Left, MinimumPadding = 0.1, MaximumPadding = 0.1, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.Dot, Title = "Błąd"
            });

            //for points

            pm.Series.Add(punktySerii);
            pm.Series.Add(punktySieci);
            pm.Series.Add(punktySeriiTestu);
            //pm.Series.Add(punktyKresekTestu);
            //pm.Series.Add(punktyKresek);
        }