Exemplo n.º 1
0
        public void TestComputeInferOrLearnOnly()
        {
            var classifier = new SDRClassifier(new[] { 1 }, 1.0, 0.1);
            int recordNum  = 0;
            // Learn only
            var retVal = classifier.Compute <double>(recordNum, new Map <string, object> {
                { "bucketIdx", 4 }, { "actValue", 34.7 }
            },
                                                     new[] { 1, 5, 9 }, true, false);

            Assert.IsNull(retVal);

            // Infer only
            var retVal1 = classifier.Compute <double>(recordNum, new Map <string, object> {
                { "bucketIdx", 2 }, { "actValue", 14.2 }
            },
                                                      new[] { 1, 5, 9 }, false, true);

            recordNum += 1;
            var retVal2 = classifier.Compute <double>(recordNum, new Map <string, object> {
                { "bucketIdx", 3 }, { "actValue", 20.5 }
            },
                                                      new[] { 1, 5, 9 }, false, true);

            recordNum += 1;
            Assert.IsTrue((retVal1.GetStats(1)).SequenceEqual(retVal2.GetStats(1)));
        }
Exemplo n.º 2
0
        private Classification <double> _compute(SDRClassifier classifier, int recordNum, int[] pattern, int bucket, double value)
        {
            var classification = new Map <string, object> {
                { "bucketIdx", bucket }, { "actValue", value }
            };

            return(classifier.Compute <double>(recordNum, classification, pattern, true, true));
        }
Exemplo n.º 3
0
 public void TestComputeInferAndLearnFalse()
 {
     var classifier = new SDRClassifier(new[] { 1 }, 1.0, 0.1);
     int recordNum  = 0;
     // Learn only
     var retVal = classifier.Compute <double>(recordNum, new Map <string, object> {
         { "bucketIdx", 4 }, { "actValue", 34.7 }
     },
                                              new[] { 1, 5, 9 }, false, false);
 }
Exemplo n.º 4
0
        public void TestComputeResultTypes()
        {
            var classifier = new SDRClassifier(new[] { 1 }, 0.1, 0.1);
            var result     = classifier.Compute <double>(0, new Map <string, object> {
                { "bucketIdx", 4 }, { "actValue", 34.7 }
            }, new[] { 1, 5, 9 }, true, true);

            Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, result.StepSet()));
            Assert.AreEqual(1, result.GetActualValueCount());
            Assert.AreEqual(34.7, result.GetActualValue(0), 0.01);
        }
Exemplo n.º 5
0
        public void TestSingleValue0Steps()
        {
            var classifier = new SDRClassifier(new[] { 0 }, 1.0);
            // Enough times to perform Inference and learn associations
            Classification <double> retVal = null;

            for (int i = 0; i < 10; i++)
            {
                retVal = _compute(classifier, i, new[] { 1, 5 }, 0, 10);
            }
            Assert.AreEqual(retVal.GetActualValue(0), 10);
            Assert.IsTrue(retVal.GetStat(0, 0) > 0.9, "value of 1 must be greater then 0.9 and is " + retVal.GetStat(0, 0));
        }
Exemplo n.º 6
0
        public void TestComputeComplex()
        {
            var classifier = new SDRClassifier(new[] { 1 }, 1.0, 0.1);
            int recordNum  = 0;

            classifier.Compute <double>(recordNum, new Map <string, object> {
                { "bucketIdx", 4 }, { "actValue", 34.7 }
            },
                                        new[] { 1, 5, 9 }, true, true);
            recordNum += 1;

            classifier.Compute <double>(recordNum, new Map <string, object> {
                { "bucketIdx", 5 }, { "actValue", 41.7 }
            },
                                        new[] { 0, 6, 9, 11 }, true, true);
            recordNum += 1;

            classifier.Compute <double>(recordNum, new Map <string, object> {
                { "bucketIdx", 5 }, { "actValue", 44.9 }
            },
                                        new[] { 6, 9 }, true, true);
            recordNum += 1;

            classifier.Compute <double>(recordNum, new Map <string, object> {
                { "bucketIdx", 4 }, { "actValue", 42.9 }
            },
                                        new[] { 1, 5, 9 }, true, true);
            recordNum += 1;

            var result = classifier.Compute <double>(recordNum, new Map <string, object> {
                { "bucketIdx", 4 }, { "actValue", 34.7 }
            },
                                                     new[] { 1, 5, 9 }, true, true);

            recordNum += 1;

            Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, result.StepSet()));
            Assert.AreEqual(6, result.GetActualValueCount());
            double[] actValues = result.GetActualValues();

            Assert.AreEqual((double)actValues[4], 35.520000457763672, 0.0001);
            Assert.AreEqual((double)actValues[5], 42.020000457763672, 0.0001);
            double[] resultDoubles = (double[])result.GetStats(1);
            Assert.AreEqual(6, resultDoubles.Length);
            Assert.AreEqual(resultDoubles[0], 0.034234, 0.0001);
            Assert.AreEqual(resultDoubles[1], 0.034234, 0.0001);
            Assert.AreEqual(resultDoubles[2], 0.034234, 0.0001);
            Assert.AreEqual(resultDoubles[3], 0.034234, 0.0001);
            Assert.AreEqual(resultDoubles[4], 0.093058, 0.0001);
            Assert.AreEqual(resultDoubles[5], 0.770004, 0.0001);
        }
Exemplo n.º 7
0
        public void TestComputeWithMissingValue()
        {
            var classifier = new SDRClassifier(new[] { 1 }, 0.1, 0.1);
            var result     = classifier.Compute <double?>(0, new Map <string, object> {
                { "bucketIdx", null }, { "actValue", null }
            },
                                                          new[] { 1, 5, 9 }, true, true);

            Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, result.StepSet()));
            Assert.AreEqual(1, result.GetActualValueCount());
            double?[] actValues = result.GetActualValues();
            Assert.AreEqual(1, actValues.Length);
            Assert.AreEqual(null, actValues[0]);
        }
Exemplo n.º 8
0
        public void TestCompute1()
        {
            var classifier = new SDRClassifier(new[] { 1 }, 0.1, 0.1);
            var retVal     = classifier.Compute <double>(0, new Map <string, object> {
                { "bucketIdx", 4 }, { "actValue", 34.7 }
            },
                                                         new[] { 1, 5, 9 }, true, true);

            Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, retVal.StepSet()));
            Assert.AreEqual(1, retVal.GetActualValueCount());

            double[] actValues = retVal.GetActualValues();
            Assert.AreEqual(1, actValues.Length);
            Assert.IsInstanceOfType(actValues[0], typeof(double));
            Assert.AreEqual(actValues[0], 34.7, 0.0001);
        }
Exemplo n.º 9
0
        public void TestComputeCategory()
        {
            var classifier = new SDRClassifier(new[] { 1 }, 0.1, 0.1);

            classifier.Compute <string>(0, new Map <string, object> {
                { "bucketIdx", 4 }, { "actValue", "D" }
            },
                                        new[] { 1, 5, 9 }, true, true);
            var result = classifier.Compute <string>(1, new Map <string, object> {
                { "bucketIdx", 4 }, { "actValue", "D" }
            },
                                                     new[] { 1, 5, 9 }, true, true);

            Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, result.StepSet()));
            Assert.AreEqual(5, result.GetActualValueCount());
            double[] actValues = result.GetStats(1);
            Assert.AreEqual("D", result.GetActualValue(4));
        }
Exemplo n.º 10
0
        public void TestOverlapPattern()
        {
            var classifier = new SDRClassifier(new[] { 1 }, 10.0);

            _compute(classifier, 0, new[] { 1, 5 }, 9, 9);
            _compute(classifier, 1, new[] { 1, 5 }, 9, 9);
            var retVal = _compute(classifier, 2, new[] { 3, 5 }, 2, 2);

            // Since overlap - should be previous with high likelihood
            double[] actValues = retVal.GetActualValues();
            Assert.AreEqual(actValues[9], 9);
            double[] resultDoubles = retVal.GetStats(1);
            Assert.IsTrue(resultDoubles[9] > 0.9);

            retVal        = _compute(classifier, 3, new[] { 3, 5 }, 2, 2);
            resultDoubles = retVal.GetStats(1);
            // Second example: now new value should be more probable than old
            Assert.IsTrue(resultDoubles[2] > resultDoubles[9]);
        }
Exemplo n.º 11
0
        public void TestMultistepSingleValue()
        {
            var classifier = new SDRClassifier(new[] { 1, 2 });

            Classification <double> retVal = null;

            for (int i = 0; i < 10; i++)
            {
                retVal = _compute(classifier, i, new[] { 1, 5 }, 0, 10);
            }

            // Since overlap - should be previous with high likelihood
            double[] actValues = retVal.GetActualValues();

            Assert.AreEqual((double)actValues[0], 10);
            double[] resultDoubles1 = (double[])retVal.GetStats(1);
            double[] resultDoubles2 = (double[])retVal.GetStats(2);

            Assert.AreEqual(resultDoubles1[0], 1);
            Assert.AreEqual(resultDoubles2[0], 1);
        }
Exemplo n.º 12
0
        public void TestMultistepPredictions()
        {
            var classifier = new SDRClassifier(new[] { 0, 1 }, 1.0, 0.1, 0);

            int[] sdr1      = new[] { 1, 3, 5 };
            int[] sdr2      = new[] { 2, 4, 6 };
            int   recordNum = 0;

            for (int i = 0; i < 100; i++)
            {
                classifier.Compute <double>(recordNum: recordNum, patternNZ: sdr1,
                                            classification: new Map <string, object> {
                    { "bucketIdx", 0 }, { "actValue", 0 }
                },
                                            learn: true, infer: false);
                recordNum++;

                classifier.Compute <double>(recordNum: recordNum, patternNZ: sdr2,
                                            classification: new Map <string, object> {
                    { "bucketIdx", 1 }, { "actValue", 1.0 }
                },
                                            learn: true, infer: false);
                recordNum++;
            }

            var result1 = classifier.Compute <double>(recordNum: recordNum, patternNZ: sdr1,
                                                      classification: null, learn: false, infer: true);

            var result2 = classifier.Compute <double>(recordNum: recordNum, patternNZ: sdr2,
                                                      classification: null, learn: false, infer: true);

            Assert.AreEqual(1.0, result1.GetStats(0)[0], 0.01);
            Assert.AreEqual(0.0, result1.GetStats(0)[1], 0.01);
            Assert.AreEqual(0.0, result2.GetStats(0)[0], 0.01);
            Assert.AreEqual(1.0, result2.GetStats(0)[1], 0.01);
        }
Exemplo n.º 13
0
        public void TestInitialization()
        {
            var c = new SDRClassifier(new[] { 1 }, 0.1, 0.1);

            Assert.IsNotNull(c);
        }