Beispiel #1
0
        public void movieRecommendationTestCRbm(int iterations, double learningRate, int visNodes, int hidNodes)
        {
            Debug.WriteLine($"{iterations}-{visNodes}-{hidNodes}");

            LearningApi api = new LearningApi(getDescriptorForRbm(3898));

            // Initialize data provider
            api.UseCsvDataProvider(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\movieDatasetTrain.csv"), ',', false, 0);
            api.UseDefaultDataMapper();
            double[] featureVector = new double[] { 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85 };
            api.UseCRbm(featureVector, learningRate, iterations, visNodes, hidNodes);

            Stopwatch watch = new Stopwatch();

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

            watch.Stop();

            var hiddenNodes  = score.HiddenValues;
            var hiddenWeight = score.HiddenBisases;


            double[] learnedFeatures = new double[hidNodes];
            double[] hiddenWeights   = new double[hidNodes];
            for (int i = 0; i < hidNodes; i++)
            {
                learnedFeatures[i] = hiddenNodes[i];
                hiddenWeights[i]   = hiddenWeight[i];
            }

            StreamWriter tw = new StreamWriter($"PredictedDigit_I{iterations}_V{visNodes}_H{hidNodes}_learnedbias.txt");

            foreach (var item in score.HiddenBisases)
            {
                tw.WriteLine(item);
            }
            tw.Close();

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

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

            var predictedData = ((RbmResult)result).VisibleNodesPredictions;

            var predictedHiddenNodes = ((RbmResult)result).HiddenNodesPredictions;

            var acc = testData.GetHammingDistance(predictedData);

            WriteDeepResult(iterations, new int[] { visNodes, hidNodes }, learningRate, acc, watch.ElapsedMilliseconds * 1000, predictedHiddenNodes);

            WriteOutputMatrix(iterations, new int[] { visNodes, hidNodes }, learningRate, predictedData, testData);
        }
Beispiel #2
0
        public void SimpleCRBMTest()
        {
            LearningApi api = new LearningApi();

            api.UseActionModule <object, double[][]>((notUsed, ctx) =>
            {
                const int maxSamples = 12;
                ctx.DataDescriptor   = getDescriptorForRbm_sample1();
                double[][] data      = new double[maxSamples][];

                data[0] = new double[] { 1, 1, 0, 0, 0, 0 };  // A
                data[1] = new double[] { 0, 0, 1, 1, 0, 0 };  // B
                data[2] = new double[] { 0, 0, 0, 0, 1, 1 };  // C

                data[3] = new double[] { 1, 1, 0, 0, 0, 1 };  // noisy A
                data[4] = new double[] { 0, 0, 1, 1, 0, 0 };  // BRt
                data[5] = new double[] { 0, 0, 0, 0, 1, 1 };  // C

                data[6] = new double[] { 1, 0, 0, 0, 0, 0 };  // weak A
                data[7] = new double[] { 0, 0, 1, 0, 0, 0 };  // weak B
                data[8] = new double[] { 0, 0, 0, 0, 1, 0 };  // weak C

                data[9]  = new double[] { 1, 1, 0, 1, 0, 0 }; // noisy A
                data[10] = new double[] { 1, 0, 1, 1, 0, 0 }; // noisy B
                data[11] = new double[] { 0, 0, 1, 0, 1, 1 }; // noisy C
                return(data);
            });

            double[] featureVector = new double[] { 1, 0 };
            api.UseCRbm(featureVector, 0.01, 1000, 6, 3);

            RbmScore score = api.Run() as RbmScore;

            double[][] testData = new double[4][];

            Assert.True(score.Loss < 1.0);

            testData[0] = new double[] { 1, 1, 0, 0, 0, 0 };
            testData[1] = new double[] { 0, 0, 0, 0, 1, 1 };
            testData[2] = new double[] { 0, 1, 0, 0, 0, 0 };
            testData[3] = new double[] { 0, 0, 0, 0, 1, 0 };

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

            // NOT FINISHED.
            //Assert.True(result[0] == 1);
            //Assert.True(result[1] == 0);
            //Assert.True(result[2] == 0);
            //Assert.True(result[3] == 0);
            //Assert.True(result[4] == 1);
            //Assert.True(result[5] == 0);
        }
Beispiel #3
0
        public void smileyTestCRbm(int iterations, double learningRate, int visNodes, int hidNodes)
        {
            LearningApi api = new LearningApi(getDescriptorForRbm(1600));

            // Initialize data provider
            api.UseCsvDataProvider(Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\Smiley.csv"), ',', false, 0);
            api.UseDefaultDataMapper();
            double[] featureVector = new double[] { 0.1, 0.2 };
            api.UseCRbm(featureVector, learningRate, iterations, visNodes, hidNodes);

            Stopwatch watch = new Stopwatch();

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

            watch.Stop();

            var hiddenNodes  = score.HiddenValues;
            var hiddenWeight = score.HiddenBisases;


            double[] learnedFeatures = new double[hidNodes];
            double[] hiddenWeights   = new double[hidNodes];
            for (int i = 0; i < hidNodes; i++)
            {
                learnedFeatures[i] = hiddenNodes[i];
                hiddenWeights[i]   = hiddenWeight[i];
            }

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

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

            var predictedData = ((RbmResult)result).VisibleNodesPredictions;

            var predictedHiddenNodes = ((RbmResult)result).HiddenNodesPredictions;

            var acc = testData.GetHammingDistance(predictedData);

            var ValTest  = calcDelta(predictedData, testData);
            var lossTest = ValTest / (visNodes);

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

            WriteDeepResult(iterations, new int[] { visNodes, hidNodes }, acc, watch.ElapsedMilliseconds * 1000, predictedHiddenNodes);

            WriteOutputMatrix(iterations, new int[] { visNodes, hidNodes }, predictedData, testData);
        }
Beispiel #4
0
        public void CRbm_ClassifierTest()
        {
            var dataPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), @"RestrictedBolzmannMachine2\Data\rbm_twoclass_sample.csv");

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

            // Initialize data provider
            api.UseCsvDataProvider(dataPath, ';', false, 1);
            api.UseDefaultDataMapper();
            double[] featureVector = new double[] { 0.1, 0.2, 0.3 };
            api.UseCRbm(featureVector, 0.01, 1000, 10, 2);
            RbmResult score = api.Run() as RbmResult;

            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 };

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

            //
            // 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 same class. Third and fourth are also of same class.

            // Here we check first classs.
            Assert.True(2 * result.HiddenNodesPredictions[0][0] + result.HiddenNodesPredictions[0][1] ==
                        2 * result.HiddenNodesPredictions[1][0] + result.HiddenNodesPredictions[1][1]);

            // Here is test for second class.
            Assert.True(2 * result.HiddenNodesPredictions[2][0] + result.HiddenNodesPredictions[2][1] ==
                        2 * result.HiddenNodesPredictions[3][0] + result.HiddenNodesPredictions[3][1]);

            printVector("Weights", result.Weights);
        }