예제 #1
0
        public void Rbm_ClassifierDeepTest()
        {
            var dataPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\rbm_twoclass_sample.csv");

            LearningApi api = new LearningApi(this.getDescriptorForRbmTwoClassesClassifier(10));

            // Initialize data provider
            api.UseCsvDataProvider(dataPath, ';', false, 1);
            api.UseDefaultDataMapper();
            api.UseDeepRbm(0.01, 1000, new int[] { 10, 5, 2 });

            RbmDeepScore score = api.Run() as RbmDeepScore;

            double[][] testData = new double[5][];

            //
            // This test data contains two patterns. One is grouped at left and one at almost right.
            testData[0] = new double[] { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
            testData[1] = new double[] { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 };
            testData[2] = new double[] { 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 };
            testData[3] = new double[] { 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 };

            // This will be classified as third class.
            testData[4] = new double[] { 1, 1, 1, 0, 0, 1, 1, 1, 0, 0 };

            RbmDeepResult result = api.Algorithm.Predict(testData, api.Context) as RbmDeepResult;

            //
            // 2 * BIT1 + BIT2 of [0] and [1] should be same.
            // We don't know how RBM will classiffy data. We only expect that
            // same or similar pattern of data will be assigned to the same class.
            // Note, we have here two classes (two hiddne nodes).
            // First and second data sample are of the same class.
            // Third and fourth are also of same class. See data.

            // Here we check first classs.
            Assert.True(result.Results[0].ToArray()[1].HiddenNodesPredictions[0] == result.Results[1].ToArray()[1].HiddenNodesPredictions[0] &&
                        result.Results[0].ToArray()[1].HiddenNodesPredictions[1] == result.Results[1].ToArray()[1].HiddenNodesPredictions[1]);

            // Here is test for second class.
            Assert.True(result.Results[2].ToArray()[1].HiddenNodesPredictions[0] == result.Results[3].ToArray()[1].HiddenNodesPredictions[0] &&
                        result.Results[2].ToArray()[1].HiddenNodesPredictions[1] == result.Results[3].ToArray()[1].HiddenNodesPredictions[1]);
        }
예제 #2
0
        public void DigitRecognitionDeepTest(int iterations, double learningRate, int[] layers)
        {
            Debug.WriteLine($"{iterations}-{String.Join("", layers)}");

            LearningApi api = new LearningApi(getDescriptorForRbmTwoClassesClassifier(4096));

            // Initialize data provider
            // TODO: Describe Digit Dataset.
            api.UseCsvDataProvider(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\DigitDataset.csv"), ',', false, 0);
            api.UseDefaultDataMapper();

            api.UseDeepRbm(learningRate, iterations, layers);

            Stopwatch watch = new Stopwatch();

            watch.Start();
            RbmDeepScore score = api.Run() as RbmDeepScore;

            watch.Stop();

            var testData = RbmHandwrittenDigitUnitTests.ReadData(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\predictiondigitdata.csv"));

            var result               = api.Algorithm.Predict(testData, api.Context) as RbmDeepResult;
            var accList              = new double[result.Results.Count];
            var predictions          = new double[result.Results.Count][];
            var predictedHiddenNodes = new double[result.Results.Count][];
            var Time = watch.ElapsedMilliseconds / 1000;

            int i = 0;

            foreach (var item in result.Results)
            {
                predictions[i]          = item.First().VisibleNodesPredictions;
                predictedHiddenNodes[i] = item.Last().HiddenNodesPredictions;
                accList[i] = testData[i].GetHammingDistance(predictions[i]);
                i++;
            }

            RbmHandwrittenDigitUnitTests.WriteDeepResult(iterations, layers, accList, Time * 1000, predictedHiddenNodes);
            /// write predicted hidden nodes.......
            RbmHandwrittenDigitUnitTests.WriteOutputMatrix(iterations, layers, predictions, testData);
        }
예제 #3
0
        public void smileyTestDeepRbm(int iterations, double learningRate, int[] layers)
        {
            LearningApi api = new LearningApi(getDescriptorForRbm(1600));

            // Initialize data provider
            api.UseCsvDataProvider(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\Smiley.csv"), ',', false, 0);
            api.UseDefaultDataMapper();
            api.UseDeepRbm(learningRate, iterations, layers);

            Stopwatch watch = new Stopwatch();

            watch.Start();
            RbmDeepScore score = api.Run() as RbmDeepScore;

            watch.Stop();

            var testData = ReadData(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\SmileyTest.csv"));

            var result               = api.Algorithm.Predict(testData, api.Context) as RbmDeepResult;
            var accList              = new double[result.Results.Count];
            var predictions          = new double[result.Results.Count][];
            var predictedHiddenNodes = new double[result.Results.Count][];
            var Time = watch.ElapsedMilliseconds / 1000;

            int i = 0;

            foreach (var item in result.Results)
            {
                predictions[i]          = item.First().VisibleNodesPredictions;
                predictedHiddenNodes[i] = item.Last().HiddenNodesPredictions;
                accList[i] = testData[i].GetHammingDistance(predictions[i]);
                i++;
            }
            var ValTest  = calcDelta(predictions, testData);
            var lossTest = ValTest / (layers.First());

            Debug.WriteLine($"lossTest: {lossTest}");

            WriteDeepResult(iterations, layers, accList, Time / 60.0, predictedHiddenNodes);
            /// write predicted hidden nodes.......
            WriteOutputMatrix(iterations, layers, predictions, testData);
        }
예제 #4
0
        public void Rbm_ClassifierTest2()
        {
            var dataPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\rbm_sample2.csv");

            LearningApi api = new LearningApi(this.getDescriptorForRbmTwoClassesClassifier(21));

            // Initialize data provider
            api.UseCsvDataProvider(dataPath, ',', false, 1);
            api.UseDefaultDataMapper();
            api.UseDeepRbm(0.2, 10000, new int[] { 21, 9, 6, 2 });

            RbmResult score = api.Run() as RbmResult;

            var expectedResults = new Dictionary <int, List <double[]> >();

            // All test data, which belong to the sam class.
            List <double[]> testListClass1 = new List <double[]>();
            List <double[]> testListClass2 = new List <double[]>();

            //
            // This test data contains two patterns. One is grouped at left and one at almost right.
            // testListClass1 contains class 1
            // testListClass2 contains class 2
            testListClass1.Add(new double[] { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
            testListClass1.Add(new double[] { 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
            testListClass1.Add(new double[] { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });

            testListClass2.Add(new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0 });
            testListClass2.Add(new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0 });
            testListClass2.Add(new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 });

            expectedResults.Add(1, testListClass1);
            expectedResults.Add(2, testListClass2);

            validateClassificationResult(api, expectedResults);
        }