Example #1
0
        private static void Main()
        {
            Console.Title = "Digit Recognizer - Engine";

            var learningRate         = 0.00035;
            var epochs               = 10;
            var regularizationFactor = 15.0;

            LearningPipeline pipeline = new LearningPipeline()
                                        .UseGradientClipping()
                                        .UseL2Regularization(regularizationFactor)
                                        .UseDropout(0.5)
                                        .SetWeightsInitializer(InitializerType.RandomInitialization)
                                        .SetEpochCount(epochs);

            var layers = new List <NnLayer>
            {
                new NnLayer(784, 200, new LeakyRelu()),
                new NnLayer(200, 100, new LeakyRelu()),
                new NnLayer(100, 10, new Softmax())
            };

            var nn = new NeuralNetwork(layers, learningRate);

            var optimizer = new MomentumOptimizer(nn, new CrossEntropy(), 0.93);

            var provider = new BatchDataProvider(DirectoryHelper.ExpandedTrainLabelsPath, DirectoryHelper.ExpandedTrainImagesPath, 100);

            pipeline.Add(optimizer);

            pipeline.Add(nn);

            pipeline.Add(provider);

            PredictionModel model = pipeline.Run();

            var provider1 = new BatchDataProvider(DirectoryHelper.TestLabelsPath, DirectoryHelper.TestImagesPath, 10000);
            var acc       = 0.0;

            MnistImageBatch data = provider1.GetData();

            List <double[]> predictions = data.Pixels.Select(pixels => model.Predict(pixels)).ToList();

            for (var i = 0; i < data.Labels.Length; i++)
            {
                if (data.Labels[i] == predictions[i].ArgMax())
                {
                    acc++;
                }
            }

            acc /= 10000.0;

            Console.WriteLine($"Accuracy on the test data is: {acc:P2}");

            string basePath = Path.GetFullPath(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) +
                                               DirectoryHelper.ModelsFolder);

            string modelName = $"{Guid.NewGuid()}-{acc:N4}.nn";

            string filename = $"{basePath}/{modelName}";

            model.Save(filename);

            Console.ReadKey();
        }