Exemple #1
0
        private List <ImageData> GetPredictions(MultilabelSupportVectorMachine <Linear> machine1, MultilabelSupportVectorMachine <Linear> machine2)
        {
            var firstDigitInputs   = _inputCreator.ConvertToArrays(_bitmaps[0]);
            var secondDigitInputs  = _inputCreator.ConvertToArrays(_bitmaps[1]);
            var firstDigitPredict  = machine1.Decide(firstDigitInputs);
            var secondDigitPredict = machine2.Decide(secondDigitInputs);

            return(_predictionHandler.HandlePredictions(firstDigitPredict, secondDigitPredict, _targetImgPaths));
        }
Exemple #2
0
        public void RunTest()
        {
            Accord.Math.Random.Generator.Seed = 0;

            // Sample data
            //   The following is a simple auto association function
            //   in which each input correspond to its own class. This
            //   problem should be easily solved using a Linear kernel.

            // Sample input data
            double[][] inputs =
            {
                new double[] { 0, 0 },
                new double[] { 0, 1 },
                new double[] { 1, 0 },
                new double[] { 1, 1 },
            };

            // Outputs for each of the inputs
            int[][] outputs =
            {
                //       and   or   nand   xor
                new[] { -1, -1, +1, +1 },
                new[] { -1, +1, +1, -1 },
                new[] { -1, +1, +1, -1 },
                new[] { +1, +1, -1, +1 },
            };

            // Create a new Linear kernel
            IKernel linear = new Linear();

            // Create a new Multi-class Support Vector Machine for one input,
            //  using the linear kernel and four disjoint classes.
            var machine = new MultilabelSupportVectorMachine(inputs: 2, kernel: linear, classes: 4);

            // Create the Multi-class learning algorithm for the machine
            var teacher = new MultilabelSupportVectorLearning(machine, inputs, outputs);

            // Configure the learning algorithm to use SMO to train the
            //  underlying SVMs in each of the binary class subproblems.
            teacher.Algorithm = (svm, classInputs, classOutputs, i, j) =>
                                new SequentialMinimalOptimization(svm, classInputs, classOutputs)
            {
                // Create a hard SVM
                Complexity = 10000.0
            };

            // Run the learning algorithm
            double error = teacher.Run();

            // only xor is not learnable by
            // a hard-margin linear machine

            bool[][] pred  = machine.Decide(inputs);
            bool[][] train = Classes.Decide(outputs);

            var and = pred.GetColumn(0);

            Assert.IsTrue(and.IsEqual(train.GetColumn(0)));

            var or = pred.GetColumn(1);

            Assert.IsTrue(or.IsEqual(train.GetColumn(1)));

            var nand = pred.GetColumn(2);

            Assert.IsTrue(nand.IsEqual(train.GetColumn(2)));

            var xor = pred.GetColumn(3);

            Assert.IsFalse(xor.IsEqual(train.GetColumn(3)));


            Assert.AreEqual(2 / 16.0, error);
        }