Exemplo n.º 1
0
 public void Display(MNISTReader reader)
 {
     for (var i = 0; i < 10; i++)
     {
         Console.WriteLine("=========" + ArrayUtil.IndexOfLargest(reader.Data[i].Ideal));
         Dump(reader.Data[i].Input);
     }
 }
Exemplo n.º 2
0
        static void Main(string[] args)

        {
            Torch.SetSeed(1);

            var cwd = Environment.CurrentDirectory;

            //var device = Device.CPU; //Torch.IsCudaAvailable() ? Device.CUDA : Device.CPU;
            var device = Torch.IsCudaAvailable() ? Device.CUDA : Device.CPU;

            Console.WriteLine($"Running on {device.Type.ToString()}");

            if (device.Type == DeviceType.CUDA)
            {
                _trainBatchSize *= 4;
                _testBatchSize  *= 4;
                _epochs         *= 16;
            }

            var sourceDir = _dataLocation;
            var targetDir = Path.Combine(_dataLocation, "test_data");

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "train-images-idx3-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "train-labels-idx1-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "t10k-images-idx3-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "t10k-labels-idx1-ubyte.gz"), targetDir);
            }

            using (var train = new MNISTReader(targetDir, "train", _trainBatchSize, device: device, shuffle: true))
                using (var test = new MNISTReader(targetDir, "t10k", _testBatchSize, device: device))
                    //using (var model = new Model("model", device))
                    using (var model = GetModel(device))
                        using (var optimizer = NN.Optimizer.SGD(model.parameters(), 0.01, 0.5))
                        {
                            Stopwatch sw = new Stopwatch();
                            sw.Start();

                            for (var epoch = 1; epoch <= _epochs; epoch++)
                            {
                                Train(model, optimizer, nll_loss(), train, epoch, _trainBatchSize, train.Size);
                                Test(model, nll_loss(reduction: NN.Reduction.Sum), test, test.Size);

                                Console.WriteLine($"Pre-GC memory:  {GC.GetTotalMemory(false)}");
                                GC.Collect();
                                Console.WriteLine($"Post-GC memory: {GC.GetTotalMemory(false)}");
                            }

                            sw.Stop();
                            Console.WriteLine($"Elapsed time: {sw.Elapsed.TotalSeconds} s.");
                            Console.ReadLine();
                        }
        }
        static void Main(string[] args)
        {
            Helper.PrintLine("NNDL-NumSharp");
            Helper.PrintLine("检查数据文件...");
            if (!CheckDataFiles())
            {
                Exit(-1);
            }

            Helper.PrintLine("读取训练数据...");
            MNISTReader reader      = new MNISTReader();
            var         trainImages = reader.ReadMatrixsFlattened(TrainImagesPath).Take(1000).Select(array => new NDArray(array, new[] { array.Length, 1 })).ToArray();
            var         trainLabels = reader.ReadValues(TrainLabelsPath).Take(1000).ToArray();

            Helper.PrintLine("创建神经网络...");
            var network = new Network(new[] { 784, 30, 10 });

            Stopwatch stopwatch = new Stopwatch();

            Helper.PrintLine("随机梯度下降算法训练神经网络...");
            Helper.PrintSplit();
            stopwatch.Start();

            network.StochasticGradientDescent(
                trainImages.Zip(trainLabels.Select(value => { var values = Enumerable.Repeat(0.0, 10).Cast <double>().ToArray(); values[value] = 1; return(new NDArray(values, new[] { values.Length, 1 })); })),
                10,
                20,
                3.0);

            stopwatch.Stop();
            Helper.PrintSplit();
            Helper.PrintLine($"神经网络训练结束,耗时:{stopwatch.Elapsed.ToString()}");

            Helper.PrintSplit();
            Helper.PrintLine("预测:");
            var output = network.FeedForward(trainImages[0]).Array as double[];

            Helper.PrintLine($"答案:{trainLabels[0]}");
            Helper.PrintLine($"预测结果:\n\t{string.Join("\n\t", Enumerable.Range(0, output.Length).Select(index => $"{index} => {output[index]:P2}"))}");

            Exit();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            var dataset     = args.Length > 0 ? args[0] : "mnist";
            var datasetPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "..", "Downloads", dataset);

            Torch.SetSeed(1);

            var cwd = Environment.CurrentDirectory;

            var device = Torch.IsCudaAvailable() ? Device.CUDA : Device.CPU;

            Console.WriteLine($"Running MNIST on {device.Type.ToString()}");
            Console.WriteLine($"Dataset: {dataset}");

            var sourceDir = datasetPath;
            var targetDir = Path.Combine(datasetPath, "test_data");

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "train-images-idx3-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "train-labels-idx1-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "t10k-images-idx3-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "t10k-labels-idx1-ubyte.gz"), targetDir);
            }

            if (device.Type == DeviceType.CUDA)
            {
                _trainBatchSize *= 4;
                _testBatchSize  *= 4;
            }

            var model = new Model("model", device);

            var normImage = TorchVision.Transforms.Normalize(new double[] { 0.1307 }, new double[] { 0.3081 }, device: device);

            using (MNISTReader train = new MNISTReader(targetDir, "train", _trainBatchSize, device: device, shuffle: true, transform: normImage),
                   test = new MNISTReader(targetDir, "t10k", _testBatchSize, device: device, transform: normImage)) {
                TrainingLoop(dataset, device, model, train, test);
            }
        }
Exemplo n.º 5
0
        public void Play()
        {
            var trainX = MNISTReader.ReadImagesToTensor4("Data\\train-images.idx3-ubyte", 60000);
            var trainY = MNISTReader.ReadLabelsToMatrix("Data\\train-labels.idx1-ubyte", 60000);
            var testX  = MNISTReader.ReadImagesToTensor4("Data\\t10k-images.idx3-ubyte", 10000);
            var testY  = MNISTReader.ReadLabelsToMatrix("Data\\t10k-labels.idx1-ubyte", 10000);

            var nn = new NeuralNetwork()
                     .AddLayer(new ConvLayer(10, 5, 1))
                     .AddReLU()
                     .AddLayer(new MaxPooling(2))
                     .AddLayer(new ConvLayer(20, 5, 1))
                     .AddReLU()
                     .AddLayer(new MaxPooling(2))
                     .AddLayer(new FlattenLayer())
                     .AddFullLayer(100)
                     .AddSigmoid()
                     .AddFullLayer(50)
                     .AddSigmoid()
                     .AddFullLayer(10)
                     .AddSoftmaxWithCrossEntropyLoss()
                     .UseAdam();

            var cate = trainY
                       .GetRawValues()
                       .Distinct()
                       .OrderBy(a => a)
                       .Select(a => a.ToString())
                       .ToList();

            var codec = new OneHotCodec(cate);
            var norm  = new ZScoreNorm(trainX);

            var trainer = new Trainer(nn, 64, 10, true)
            {
                LabelCodec = codec,
                Normalizer = norm,
            };

            trainer.StartTrain(trainX, trainY, testX, testY);
        }
Exemplo n.º 6
0
        public static void Init(string dataPath)
        {
            //Loading DATA
            TestImages       = MNISTReader.ReadTest(dataPath);
            TrainingImages   = MNISTReader.ReadTraining(dataPath);
            ValidationImages = MNISTReader.ReadValidation(dataPath);

            param = new Parameters();

            //Defining the default parameters
            param.layers_size      = new int[] { 784, 100, 10 };
            param.nblayers         = param.layers_size.Length;
            param.eta              = 1;
            param.batchsize        = 10;
            param.costfunction     = Parameters.Costfunction.CROSSENTROPY;
            param.regularization   = true;
            param.lambda           = 3;
            param.stopafternbsteps = 20;

            //Creates a brain on the given parameters
            brain = new Brain(param);
        }
Exemplo n.º 7
0
        internal static void Main(string[] args)
        {
            var cwd = Environment.CurrentDirectory;

            var dataset     = args.Length > 0 ? args[0] : "mnist";
            var datasetPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "..", "Downloads", dataset);

            var _ = torch.random.manual_seed(1);

            //var device = torch.CPU;
            var device = torch.cuda.is_available() ? torch.CUDA : torch.CPU;

            Console.WriteLine($"\n  Running AdversarialExampleGeneration on {device.type.ToString()}\n");
            Console.WriteLine($"Dataset: {dataset}");

            if (device.type == DeviceType.CUDA)
            {
                _trainBatchSize *= 4;
                _testBatchSize  *= 4;
                _epochs         *= 4;
            }

            var sourceDir = _dataLocation;
            var targetDir = Path.Combine(_dataLocation, "test_data");

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "train-images-idx3-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "train-labels-idx1-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "t10k-images-idx3-ubyte.gz"), targetDir);
                Utils.Decompress.DecompressGZipFile(Path.Combine(sourceDir, "t10k-labels-idx1-ubyte.gz"), targetDir);
            }

            MNIST.Model model = null;

            var normImage = torchvision.transforms.Normalize(new double[] { 0.1307 }, new double[] { 0.3081 }, device: (Device)device);

            using (var test = new MNISTReader(targetDir, "t10k", _testBatchSize, device: device, transform: normImage)) {
                var modelFile = dataset + ".model.bin";

                if (!File.Exists(modelFile))
                {
                    // We need the model to be trained first, because we want to start with a trained model.
                    Console.WriteLine($"\n  Running MNIST on {device.type.ToString()} in order to pre-train the model.");

                    model = new MNIST.Model("model", device);

                    using (var train = new MNISTReader(targetDir, "train", _trainBatchSize, device: device, shuffle: true, transform: normImage)) {
                        MNIST.TrainingLoop(dataset, (Device)device, model, train, test);
                    }

                    Console.WriteLine("Moving on to the Adversarial model.\n");
                }
                else
                {
                    model = new MNIST.Model("model", torch.CPU);
                    model.load(modelFile);
                }

                model.to((Device)device);
                model.eval();

                var epsilons = new double[] { 0, 0.05, 0.1, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50 };

                foreach (var ε in epsilons)
                {
                    var attacked = Test(model, nll_loss(), ε, test, test.Size);
                    Console.WriteLine($"Epsilon: {ε:F2}, accuracy: {attacked:P2}");
                }
            }
        }
Exemplo n.º 8
0
        internal static void TrainingLoop(string dataset, Device device, Model model, MNISTReader train, MNISTReader test)
        {
            if (device.Type == DeviceType.CUDA)
            {
                _epochs *= 4;
            }

            var optimizer = NN.Optimizer.Adam(model.parameters());

            var scheduler = NN.Optimizer.StepLR(optimizer, 1, 0.7, last_epoch: 5);

            Stopwatch sw = new Stopwatch();

            sw.Start();

            for (var epoch = 1; epoch <= _epochs; epoch++)
            {
                Train(model, optimizer, nll_loss(reduction: Reduction.Mean), device, train, epoch, train.BatchSize, train.Size);
                Test(model, nll_loss(reduction: NN.Reduction.Sum), device, test, test.Size);

                Console.WriteLine($"End-of-epoch memory use: {GC.GetTotalMemory(false)}");
            }

            sw.Stop();
            Console.WriteLine($"Elapsed time: {sw.Elapsed.TotalSeconds:F1} s.");

            Console.WriteLine("Saving model to '{0}'", dataset + ".model.bin");
            model.save(dataset + ".model.bin");
        }
Exemplo n.º 9
0
        public static void Main()
        {
            int       count     = 0;
            int       dataSize  = 10000;
            IFunction activator = new Sigmoid();
            IDataset  dataset   = new MNISTReader("Data/train-images", "Data/train-labels", dataSize);

            double[][] inputTests   = dataset.GetDataset();
            double[][] outputTests  = dataset.GetLabelset();
            int[]      neuronCounts = new int[] { dataset.GetDataSize(), 500, 100, dataset.GetLabelSize() };
            double     dataRatio    = 0.5;
            double     learningRate = 0.1;

            Network        magicBox     = new Network(neuronCounts, activator, dataset, dataRatio);
            NetworkGraphic graphicMagic = new NetworkGraphic(0, 250, 800, 100, magicBox);
            bool           paused       = true;
            bool           drawstuff    = true;

            GraphGraphic      networkGraph = new GraphGraphic(250, 200, 400, 100, "Error", "Iteration");
            InputLayerGraphic inputGraphic = new InputLayerGraphic(250, 400, 200, dataset.GetDataSize());

            //Open the game window
            SwinGame.OpenGraphicsWindow("Neuralnet Project", 800, 600);


            //Run the game loop
            while (false == SwinGame.WindowCloseRequested())
            {
                //Fetch the next batch of UI interaction
                SwinGame.ProcessEvents();

                //Clear the screen and draw the framerate
                SwinGame.ClearScreen(Color.White);
                SwinGame.DrawFramerate(0, 0);
                if (drawstuff)
                {
                    networkGraph.draw();
                    graphicMagic.Draw();
                    inputGraphic.draw();

                    // Going to find the highest value (the one the neural net thinks it is) and print it!
                    double maxValue = magicBox.Output.Max();
                    int    maxIndex = magicBox.Output.ToList().IndexOf(maxValue);
                    SwinGame.DrawText(Convert.ToString(maxIndex), Color.Black, 650, 225);
                }
                else
                {
                    SwinGame.DrawText("Busy training...", Color.Black, 250, 250);
                }
                if (SwinGame.KeyReleased(KeyCode.vk_p))
                {
                    drawstuff = !drawstuff;
                }
                if (SwinGame.MouseDown(MouseButton.LeftButton))
                {
                    Point2D mouse = SwinGame.MousePosition();
                    inputGraphic.check((int)mouse.X, (int)mouse.Y, 2);
                }
                if (SwinGame.KeyReleased(KeyCode.vk_UP))
                {
                    magicBox.WhatIs(inputGraphic.output());
                    inputGraphic.reset();
                }
                if (SwinGame.KeyReleased(KeyCode.vk_n))
                {
                    magicBox.Train(2, learningRate);
                }
                if (!paused || SwinGame.KeyReleased(KeyCode.vk_RIGHT))
                {
                    count++;
                    if (count >= dataSize)
                    {
                        count = 0;
                    }
                    magicBox.Input(inputTests[count]);
                    magicBox.Feedforward();
                    magicBox.Backpropagate(outputTests[count], learningRate);
                    if (drawstuff)
                    {
                        networkGraph.pushValue(magicBox.SingleTest());
                    }
                }

                if (SwinGame.KeyReleased(KeyCode.vk_SPACE))
                {
                    paused = !paused;
                }

                SwinGame.DrawText(Convert.ToString(count), Color.Black, 50, 225);

                //Draw onto the screen
                SwinGame.RefreshScreen(300);
            }
        }
Exemplo n.º 10
0
 public void Scaffold_Reader()
 {
     sizelimit = 100;
     reader    = new MNISTReader("Data/train-images", "Data/train-labels", sizelimit);
 }