Esempio n. 1
0
        public void TestUnifiedIteratorYield()
        {
            string filename = ".unittestfile" + nameof(TestUnifiedIteratorYield);

            CreateCsvTempFile(filename);
            SigmaEnvironment.Clear();


            FileSource         source    = new FileSource(filename, Path.GetTempPath());
            CsvRecordExtractor extractor = (CsvRecordExtractor) new CsvRecordReader(source).Extractor(new CsvRecordExtractor(new Dictionary <string, int[][]> {
                ["inputs"] = new[] { new[] { 0 } }
            }));
            ExtractedDataset    dataset  = new ExtractedDataset("test", 2, new DiskCacheProvider(Path.GetTempPath() + "/" + nameof(TestUnifiedIteratorYield)), true, extractor);
            UnifiedIterator     iterator = new UnifiedIterator(dataset);
            SigmaEnvironment    sigma    = SigmaEnvironment.Create("test");
            IComputationHandler handler  = new CpuFloat32Handler();

            foreach (var block in iterator.Yield(handler, sigma))
            {
                Assert.AreEqual(new[] { 5.1f, 4.9f, 4.7f }, block["inputs"].GetDataAs <float>().GetValuesArrayAs <float>(0, 3));
            }

            dataset.Dispose();

            DeleteTempFile(filename);
        }
Esempio n. 2
0
        public void TestSigmaEnvironmentAlreadyCreated()
        {
            SigmaEnvironment.Clear();

            SigmaEnvironment.Create("test");

            Assert.Throws <ArgumentException>(() => SigmaEnvironment.Create("test"));
        }
Esempio n. 3
0
        public void TestSigmaEnvironmentCreate()
        {
            SigmaEnvironment.Clear();

            SigmaEnvironment sigma = SigmaEnvironment.Create("test");

            Assert.AreEqual("test", sigma.Name);
        }
Esempio n. 4
0
        private static CpuMultithreadedOperator CreateOperator()
        {
            SigmaEnvironment.Clear();
            RedirectGlobalsToTempPath();

            CpuMultithreadedOperator @operator = new CpuMultithreadedOperator(new CpuFloat32Handler(), 3, ThreadPriority.Normal);

            @operator.Trainer = new MockTrainer();
            @operator.Trainer.Initialise(@operator.Handler);
            @operator.Network = @operator.Trainer.Network;
            @operator.Sigma   = SigmaEnvironment.GetOrCreate("testificate-operatorcreate");

            return(@operator);
        }
Esempio n. 5
0
        public void TestMinibatchIteratorCreate()
        {
            string filename = ".unittestfile" + nameof(TestMinibatchIteratorCreate);

            CreateCsvTempFile(filename);
            SigmaEnvironment.Clear();

            FileSource         source    = new FileSource(filename, Path.GetTempPath());
            CsvRecordExtractor extractor = (CsvRecordExtractor) new CsvRecordReader(source).Extractor(new CsvRecordExtractor(new Dictionary <string, int[][]> {
                ["inputs"] = new[] { new[] { 0 } }
            }));
            ExtractedDataset dataset = new ExtractedDataset("test", 1, new DiskCacheProvider(Path.GetTempPath() + "/" + nameof(TestMinibatchIteratorYield)), true, extractor);

            Assert.Throws <ArgumentException>(() => new MinibatchIterator(-3, dataset));
            Assert.Throws <ArgumentNullException>(() => new MinibatchIterator(1, null));

            dataset.Dispose();

            DeleteTempFile(filename);
        }
Esempio n. 6
0
        public void TestUndividedIteratorYield()
        {
            string filename = ".unittestfile" + nameof(TestUndividedIteratorCreate);

            CreateCsvTempFile(filename);

            SigmaEnvironment.Clear();

            FileSource         source    = new FileSource(filename, Path.GetTempPath());
            CsvRecordExtractor extractor = (CsvRecordExtractor) new CsvRecordReader(source).Extractor(new CsvRecordExtractor(new Dictionary <string, int[][]> {
                ["inputs"] = new[] { new[] { 0 } }
            }));
            ExtractedDataset    dataset  = new ExtractedDataset("test", 2, new DiskCacheProvider(Path.GetTempPath() + "/" + nameof(TestUndividedIteratorCreate)), true, extractor);
            UndividedIterator   iterator = new UndividedIterator(dataset);
            SigmaEnvironment    sigma    = SigmaEnvironment.Create("test");
            IComputationHandler handler  = new CpuFloat32Handler();

            int index = 0;

            foreach (var block in iterator.Yield(handler, sigma))
            {
                if (index == 0)
                {
                    Assert.AreEqual(new float[] { 5.1f, 4.9f }, block["inputs"].GetDataAs <float>().GetValuesArrayAs <float>(0, 2));
                }
                else if (index == 1)
                {
                    Assert.AreEqual(new float[] { 4.7f }, block["inputs"].GetDataAs <float>().GetValuesArrayAs <float>(0, 1));
                }
                else
                {
                    Assert.Fail("There can be a maximum of two iterations, but this is yield iteration 3 (index 2).");
                }

                index++;
            }

            dataset.Dispose();

            DeleteTempFile(filename);
        }
Esempio n. 7
0
        public void TestMinibatchIteratorYield(int minibatchSize)
        {
            string filename = ".unittestfile" + nameof(TestMinibatchIteratorYield);

            CreateCsvTempFile(filename);
            SigmaEnvironment.Clear();

            FileSource         source    = new FileSource(filename, Path.GetTempPath());
            CsvRecordExtractor extractor = (CsvRecordExtractor) new CsvRecordReader(source).Extractor(new CsvRecordExtractor(new Dictionary <string, int[][]> {
                ["inputs"] = new[] { new[] { 0 } }
            }));
            ExtractedDataset    dataset  = new ExtractedDataset("test", 1, new DiskCacheProvider(Path.GetTempPath() + "/" + nameof(TestMinibatchIteratorYield)), true, extractor);
            MinibatchIterator   iterator = new MinibatchIterator(minibatchSize, dataset);
            IComputationHandler handler  = new CpuFloat32Handler();
            SigmaEnvironment    sigma    = SigmaEnvironment.Create("test");

            Assert.Throws <ArgumentNullException>(() => iterator.Yield(null, null).GetEnumerator().MoveNext());
            Assert.Throws <ArgumentNullException>(() => iterator.Yield(handler, null).GetEnumerator().MoveNext());
            Assert.Throws <ArgumentNullException>(() => iterator.Yield(null, sigma).GetEnumerator().MoveNext());

            int index = 0;

            foreach (var block in iterator.Yield(handler, sigma))
            {
                //pass through each more than 5 times to ensure consistency
                if (index++ > 20)
                {
                    break;
                }

                Assert.Contains(block["inputs"].GetValue <float>(0, 0, 0), new float[] { 5.1f, 4.9f, 4.7f });
            }

            dataset.Dispose();

            DeleteTempFile(filename);
        }
Esempio n. 8
0
        public static INetwork GenerateNetwork(double number)
        {
            if (_environment == null)
            {
                _environment = SigmaEnvironment.Create("TestAverageNetworkMergerEnvironment");
            }

            ITrainer trainer = _environment.CreateTrainer($"trainer{_count++}");

            Network net = new Network();

            net.Architecture = InputLayer.Construct(2, 2) + FullyConnectedLayer.Construct(2 * 2) + OutputLayer.Construct(2);

            trainer.Network = net;
            trainer.AddInitialiser("*.weights", new ConstantValueInitialiser(number));

            trainer.Operator = new CpuSinglethreadedOperator();

            trainer.Initialise(new CpuFloat32Handler());

            SigmaEnvironment.Clear();

            return(net);
        }
Esempio n. 9
0
        private static SigmaEnvironment ClearAndCreate(string identifier)
        {
            SigmaEnvironment.Clear();

            return(SigmaEnvironment.Create(identifier));
        }