Exemplo n.º 1
0
        private static void TrainNetwork(FeedForwardNeuralNet net)
        {
            List <double[]> trainingInput    = new List <double[]>();
            List <double[]> trainingOutput   = new List <double[]>();
            List <double[]> validationInput  = new List <double[]>();
            List <double[]> validationOutput = new List <double[]>();

            int i = 0;

            foreach (var image in MnistReader.ReadTrainingData())
            {
                double[] input  = Encode(image.Data);
                double[] output = Encode(image.Label);

                if (i < 50000)
                {
                    trainingInput.Add(input);
                    trainingOutput.Add(output);
                }
                else
                {
                    validationInput.Add(input);
                    validationOutput.Add(output);
                }

                i++;
            }

            Console.WriteLine("Started Training:");
            net.Train(trainingInput, trainingOutput, 30, 10, 5.0, validationInput, validationOutput);
            Console.WriteLine("Finished training.");
        }
Exemplo n.º 2
0
        public static void ReadDataFromFile()
        {
            Console.WriteLine("Reading training data ... ");
            foreach (var data in MnistReader.ReadTrainingData())
            {
                TrainData.Add(data);
            }

            Console.WriteLine("Reading test data ... ");
            foreach (var data in MnistReader.ReadTestData())
            {
                TestData.Add(data);
            }
        }
Exemplo n.º 3
0
        public void Train(double targetErrorRate = 0.05)
        {
            int    count      = 0;
            int    sum        = 0;
            int    epochs     = 0;
            double totalError = 1.0;

            while (totalError > targetErrorRate)
            {
                epochs++;
                _errors.Clear();
                foreach (Image image in MnistReader.ReadTrainingData())
                {
                    count++;
                    _inputLayer.SetNeurons(image.Data);
                    SetNewDesiredValues(image.Label);
                    FeedForward();
                    _errors.Add(CalculateError());
                    BackPropagate();


                    if (count == 1000)
                    {
                        sum  += count;
                        count = 0;

                        Console.WriteLine("Images read: " + sum);
                    }
                }


                totalError = CalculateTotalError();
                Console.WriteLine("Current total error: " + totalError + "\tEpoch " + epochs);
            }

            Console.WriteLine("Finished training with total error of: " + totalError);
        }
Exemplo n.º 4
0
        private static void MnistTest()
        {
            var model = new SequentialModel(
                new DenseLayer(784, 800),
                //new ActivationLayer(new Sigmoid(), 800),
                new ActivationLayer(new LeakyReLU(0.5d), 800),
                new DenseLayer(800, 10),
                new ActivationLayer(new Sigmoid(), 10)
                );

            var mnistTrain = MnistReader.ReadTrainingData()
                             .Select(mnistImage =>
            {
                var image = new double[mnistImage.Width * mnistImage.Height];

                for (int i = 0; i < mnistImage.Height; i++)
                {
                    for (int j = 0; j < mnistImage.Width; j++)
                    {
                        image[(i * mnistImage.Width) + j] = mnistImage.Image[i, j] / 255d;
                    }
                }

                var label = new double[10];

                label[mnistImage.Label] = 1;

                return(image, label);
            }).ToArray();

            var rng = new Random();

            (double[] Input, double[] ExpectedOutput) dataSource() => mnistTrain[rng.Next(0, mnistTrain.Length - 1)];

            //using var window = new RenderWindow(new VideoMode(600, 600), "Map");
            //window.Clear(Color.Black);
            //var (exampleInput, exampleExpectedOutput) = dataSource();
            //var img = new Image(28, 28);
            //for (uint i = 0; i < 28; i++)
            //{
            //    for (uint j = 0; j < 28; j++)
            //    {
            //        var brightness = (byte)(exampleInput[(j * 28) + i] * 255d);
            //        img.SetPixel(i, j, new Color(brightness, brightness, brightness));
            //    }
            //}
            //window.Draw(new Sprite(new Texture(img)) { Scale = new Vector2f(600 / 28f, 600 / 28f) });
            //window.Draw(new Text("Actual: " + Array.IndexOf(exampleExpectedOutput, 1), new Font(Resources.Arial)));
            //window.Display();

            model.Train(
                dataSource: dataSource,
                epochs: 60000,
                batchSize: 1000,
                learningRate: 0.001d,
                errorFunction: new MeanSquareError(),
                callback: (i, error, metric) => Console.WriteLine("[" + i.ToString().PadLeft(5) + "] (" + (metric * 100d)?.ToString("0.00").PadLeft(6) + "%) Error: " + error),
                metric: (expected, actual) =>
            {
                if (expected.Length == 0)
                {
                    throw new ArgumentException("Expected array to be non-empty.", nameof(expected));
                }
                if (expected.Length != actual.Length)
                {
                    throw new ArgumentException("Expected array of same size as " + nameof(expected) + ".", nameof(actual));
                }

                var maxExpected = 0;
                var maxActual   = 0;

                for (int i = 1; i < expected.Length; i++)
                {
                    if (expected[i] > expected[maxExpected])
                    {
                        maxExpected = i;
                    }
                    if (actual[i] > actual[maxActual])
                    {
                        maxActual = i;
                    }
                }

                return(maxExpected == maxActual ? 1 : 0);
            });
        }