Exemple #1
0
        /// <summary>
        /// Create a MNIST trainer (writing recognition) will be added to an environemnt.
        /// </summary>
        /// <param name="sigma">The sigma environemnt this trainer will be assigned to.</param>
        /// <returns>The newly created trainer.</returns>
        private static ITrainer CreateMnistTrainer(SigmaEnvironment sigma)
        {
            ByteRecordReader mnistImageReader    = new ByteRecordReader(headerLengthBytes: 16, recordSizeBytes: 28 * 28, source: new CompressedSource(new MultiSource(new FileSource("train-images-idx3-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz"))));
            IRecordExtractor mnistImageExtractor = mnistImageReader.Extractor("inputs", new[] { 0L, 0L }, new[] { 28L, 28L }).Preprocess(new NormalisingPreprocessor(0, 255));

            ByteRecordReader mnistTargetReader    = new ByteRecordReader(headerLengthBytes: 8, recordSizeBytes: 1, source: new CompressedSource(new MultiSource(new FileSource("train-labels-idx1-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"))));
            IRecordExtractor mnistTargetExtractor = mnistTargetReader.Extractor("targets", new[] { 0L }, new[] { 1L }).Preprocess(new OneHotPreprocessor(minValue: 0, maxValue: 9));

            IDataset dataset = new Dataset("mnist-training", Dataset.BlockSizeAuto, mnistImageExtractor, mnistTargetExtractor);
            ITrainer trainer = sigma.CreateTrainer("test");

            trainer.Network = new Network
            {
                Architecture = InputLayer.Construct(28, 28)
                               + 2 * FullyConnectedLayer.Construct(28 * 28)
                               + FullyConnectedLayer.Construct(10)
                               + OutputLayer.Construct(10)
                               + SoftMaxCrossEntropyCostLayer.Construct()
            };

            trainer.TrainingDataIterator = new MinibatchIterator(8, dataset);
            trainer.Optimiser            = new AdagradOptimiser(baseLearningRate: 0.02);
            trainer.Operator             = new CpuSinglethreadedOperator();

            trainer.AddInitialiser("*.weights", new GaussianInitialiser(standardDeviation: 0.05f));
            trainer.AddInitialiser("*.bias*", new GaussianInitialiser(standardDeviation: 0.01f, mean: 0.03f));

            trainer.AddGlobalHook(new CurrentEpochIterationReporter(TimeStep.Every(1, TimeScale.Iteration)));

            return(trainer);
        }
Exemple #2
0
            /// <summary>
            /// Create an extracted MNIST training dataset, automatically download any required resources (may take a while).
            /// This dataset is normalised and one-hot-target-preprocessed.
            /// </summary>
            /// <param name="name">The optional name.</param>
            /// <returns>The MNIST dataset.</returns>
            public static IDataset Mnist(string name = "mnist")
            {
                IRecordExtractor mnistImageExtractor = new ByteRecordReader(headerLengthBytes: 16, recordSizeBytes: 28 * 28,
                                                                            source: new CompressedSource(new MultiSource(new FileSource("train-images-idx3-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz"))))
                                                       .Extractor("inputs", new[] { 0L, 0L }, new[] { 28L, 28L }).Preprocess(new NormalisingPreprocessor(0, 255));

                IRecordExtractor mnistTargetExtractor = new ByteRecordReader(headerLengthBytes: 8, recordSizeBytes: 1,
                                                                             source: new CompressedSource(new MultiSource(new FileSource("train-labels-idx1-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"))))
                                                        .Extractor("targets", new[] { 0L }, new[] { 1L }).Preprocess(new OneHotPreprocessor(minValue: 0, maxValue: 9));

                return(new ExtractedDataset(name, ExtractedDataset.BlockSizeAuto, false, mnistImageExtractor, mnistTargetExtractor));
            }
Exemple #3
0
        private static void SampleLoadExtractIterate()
        {
            SigmaEnvironment sigma = SigmaEnvironment.Create("test");

            sigma.Prepare();

            //var irisReader = new CsvRecordReader(new MultiSource(new FileSource("iris.data"), new UrlSource("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")));
            //IRecordExtractor irisExtractor = irisReader.Extractor("inputs2", new[] { 0, 3 }, "targets2", 4).AddValueMapping(4, "Iris-setosa", "Iris-versicolor", "Iris-virginica");
            //irisExtractor = irisExtractor.Preprocess(new OneHotPreprocessor(sectionName: "targets2", minValue: 0, maxValue: 2), new NormalisingPreprocessor(sectionNames: "inputs2", minInputValue: 0, maxInputValue: 6));

            ByteRecordReader mnistImageReader    = new ByteRecordReader(headerLengthBytes: 16, recordSizeBytes: 28 * 28, source: new CompressedSource(new MultiSource(new FileSource("train-images-idx3-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz"))));
            IRecordExtractor mnistImageExtractor = mnistImageReader.Extractor("inputs", new[] { 0L, 0L }, new[] { 28L, 28L }).Preprocess(new NormalisingPreprocessor(0, 255));

            ByteRecordReader mnistTargetReader    = new ByteRecordReader(headerLengthBytes: 8, recordSizeBytes: 1, source: new CompressedSource(new MultiSource(new FileSource("train-labels-idx1-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"))));
            IRecordExtractor mnistTargetExtractor = mnistTargetReader.Extractor("targets", new[] { 0L }, new[] { 1L }).Preprocess(new OneHotPreprocessor(minValue: 0, maxValue: 9));

            IComputationHandler handler = new CpuFloat32Handler();

            ExtractedDataset dataset = new ExtractedDataset("mnist-training", ExtractedDataset.BlockSizeAuto, mnistImageExtractor, mnistTargetExtractor);

            IDataset[] slices         = dataset.SplitRecordwise(0.8, 0.2);
            IDataset   trainingData   = slices[0];
            IDataset   validationData = slices[1];

            MinibatchIterator trainingIterator   = new MinibatchIterator(1, trainingData);
            MinibatchIterator validationIterator = new MinibatchIterator(1, validationData);

            while (true)
            {
                foreach (var block in trainingIterator.Yield(handler, sigma))
                {
                    Thread.Sleep(100);

                    PrintFormattedBlock(block, PrintUtils.AsciiGreyscalePalette);

                    Thread.Sleep(1000);
                }
            }

            //Random random = new Random();
            //INDArray array = new ADNDArray<float>(3, 1, 2, 2);

            //new GaussianInitialiser(0.05, 0.05).Initialise(array, Handler, random);

            //Console.WriteLine(array);

            //new ConstantValueInitialiser(1).Initialise(array, Handler, random);

            //Console.WriteLine(array);

            //dataset.InvalidateAndClearCaches();
        }
Exemple #4
0
        public void TestByteRecordReaderRead()
        {
            string filename = ".unittestfile" + nameof(TestByteRecordReaderCreate);

            CreateCsvTempFile(filename);

            FileSource source = new FileSource(filename, Path.GetTempPath());

            ByteRecordReader reader = new ByteRecordReader(source, 2, 1);

            Assert.Throws <InvalidOperationException>(() => reader.Read(1));

            reader.Prepare();

            Assert.AreEqual(new[] { new byte[] { 5 }, new byte[] { 3 }, new byte[] { 4 } }, reader.Read(4));

            source.Dispose();
            reader.Dispose();

            DeleteTempFile(filename);
        }
Exemple #5
0
        private static void SampleCachedFastIteration()
        {
            SigmaEnvironment sigma = SigmaEnvironment.Create("test");

            IDataSource dataSource = new CompressedSource(new MultiSource(new FileSource("train-images-idx3-ubyte.gz"), new UrlSource("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz")));

            ByteRecordReader mnistImageReader    = new ByteRecordReader(headerLengthBytes: 16, recordSizeBytes: 28 * 28, source: dataSource);
            IRecordExtractor mnistImageExtractor = mnistImageReader.Extractor("inputs", new[] { 0L, 0L }, new[] { 28L, 28L }).Preprocess(new NormalisingPreprocessor(0, 255));

            IDataset dataset = new ExtractedDataset("mnist-training", ExtractedDataset.BlockSizeAuto, mnistImageExtractor);

            IDataset[] slices       = dataset.SplitRecordwise(0.8, 0.2);
            IDataset   trainingData = slices[0];

            Stopwatch stopwatch = Stopwatch.StartNew();

            IDataIterator iterator = new MinibatchIterator(10, trainingData);

            foreach (var block in iterator.Yield(new CpuFloat32Handler(), sigma))
            {
                //PrintFormattedBlock(block, PrintUtils.AsciiGreyscalePalette);
            }

            Console.Write("\nFirst iteration took " + stopwatch.Elapsed + "\n+=+ Iterating over dataset again +=+ Dramatic pause...");

            ArrayUtils.Range(1, 10).ToList().ForEach(i =>
            {
                Thread.Sleep(500);
                Console.Write(".");
            });

            stopwatch.Restart();

            foreach (var block in iterator.Yield(new CpuFloat32Handler(), sigma))
            {
                //PrintFormattedBlock(block, PrintUtils.AsciiGreyscalePalette);
            }

            Console.WriteLine("Second iteration took " + stopwatch.Elapsed);
        }