Exemplo n.º 1
0
        public void Train(string imagesPath, string labelsPath, double learningRate)
        {
            foreach (var image in MnistReader.ReadData(imagesPath, labelsPath))
            {
                int[]         desiredOutputs = new int[10];
                Byte[]        inps           = image.Data.Cast <Byte>().ToArray();
                List <double> predictions    = new List <double>();
                List <double> inputs         = inps.Select(i => (double)i / 255.0).ToList();
                //Console.WriteLine((int)image.Label);
                desiredOutputs[image.Label] = 1;
                predictions = ForwardPass(inputs);
                BackwardPass(predictions, desiredOutputs, learningRate);
                //Console.WriteLine("Desired outputs ");
                //foreach (var elt in desiredOutputs)
                //{
                //  Console.Write(" " + elt);
                //}
                //Console.WriteLine();
                //predictions = ForwardPass(inputs);


                //Console.WriteLine("Predictions");
                //foreach (var elt in predictions)
                //{
                //  Console.Write(" " + elt);
                //}
                //BackwardPass(predictions, desiredOutputs);
            }
        }
Exemplo n.º 2
0
        public Tuple <double, double> evaluate(string imagesPath, string labelsPath)
        {
            var accuracyValue = 0;
            var numberImage   = 0;

            double loss = 0.0;

            foreach (var image in MnistReader.ReadData(imagesPath, labelsPath))
            {
                int[] desiredOuput = new int[10];
                desiredOuput[image.Label] = 1;
                numberImage += 1;

                Byte[]        inps        = image.Data.Cast <Byte>().ToArray();
                List <double> predictions = new List <double>();
                List <double> input       = inps.Select(i => (double)i / 255.0).ToList();
                predictions = ForwardPass(input);
                //foreach (var elt in predictions)
                //{
                //  Console.Write(" {0}", elt);
                //}
                //Console.WriteLine();
                var predictionValue = predictions.IndexOf(predictions.Max());
                var maxValue        = predictions.Max();
                //Console.WriteLine("Expected {0} : predicted {1}: max value {2} ", image.Label, predictionValue, maxValue);
                if (predictionValue == image.Label)
                {
                    accuracyValue += 1;
                }

                loss += Utils.calculate_loss(predictions, desiredOuput);
            }
            double averageLoss     = loss / numberImage;
            double averageAccuracy = (double)accuracyValue / numberImage;

            return(Tuple.Create(averageLoss, averageAccuracy));
        }