public void ComputeTest1()
        {
            double[][] inputs =
            {
                new double[] { 1, 4, 2, 0, 1 },
                new double[] { 1, 3, 2, 0, 1 },
                new double[] { 3, 0, 1, 1, 1 },
                new double[] { 3, 0, 1, 0, 1 },
                new double[] { 0, 5, 5, 5, 5 },
                new double[] { 1, 5, 5, 5, 5 },
                new double[] { 1, 0, 0, 0, 0 },
                new double[] { 1, 0, 0, 0, 0 },
            };

            int[] outputs =
            {
                0, 0,
                1, 1,
                2, 2,
                3, 3,
            };


            IKernel kernel = new Polynomial(2);
            var     msvm   = new MultilabelSupportVectorMachine(5, kernel, 4);
            var     smo    = new MultilabelSupportVectorLearning(msvm, inputs, outputs);

            smo.Algorithm = (svm, classInputs, classOutputs, i, j) =>
                            new SequentialMinimalOptimization(svm, classInputs, classOutputs)
            {
                Complexity = 1
            };

            Assert.AreEqual(0, msvm.GetLastKernelEvaluations());

            double error = smo.Run();

            Assert.AreEqual(0, error);


            int[] evals = new int[inputs.Length];
            for (int i = 0; i < inputs.Length; i++)
            {
                double   expected = outputs[i];
                double[] responses; msvm.Compute(inputs[i], out responses);
                int      actual; responses.Max(out actual);
                Assert.AreEqual(expected, actual);
                evals[i] = msvm.GetLastKernelEvaluations();
            }

            for (int i = 0; i < evals.Length; i++)
            {
                Assert.AreEqual(msvm.SupportVectorUniqueCount, evals[i]);
            }
        }
Example #2
0
        public void LinearComputeTest1()
        {
            double[][] inputs =
            {
                new double[] { 1, 4, 2, 0, 1 },
                new double[] { 1, 3, 2, 0, 1 },
                new double[] { 3, 0, 1, 1, 1 },
                new double[] { 3, 0, 1, 0, 1 },
                new double[] { 0, 5, 5, 5, 5 },
                new double[] { 1, 5, 5, 5, 5 },
                new double[] { 1, 0, 0, 0, 0 },
                new double[] { 1, 0, 0, 0, 0 },
            };

            int[] outputs =
            {
                0, 0,
                1, 1,
                2, 2,
                3, 3,
            };


            var msvm = new MultilabelSupportVectorMachine(5, 4);
            var smo  = new MultilabelSupportVectorLearning(msvm, inputs, outputs);

            smo.Algorithm = (svm, classInputs, classOutputs, i, j) =>
                            new LinearNewtonMethod(svm, classInputs, classOutputs)
            {
                Complexity = 1
            };

            Assert.AreEqual(0, msvm.GetLastKernelEvaluations());

#if DEBUG
            smo.ParallelOptions.MaxDegreeOfParallelism  = 1;
            msvm.ParallelOptions.MaxDegreeOfParallelism = 1;
#endif

            double error = smo.Run();
            Assert.AreEqual(0.125, error);


            int[] evals = new int[inputs.Length];
            int[] y     = new int[inputs.Length];
            for (int i = 0; i < inputs.Length; i++)
            {
                double   expected = outputs[i];
                double[] responses;
                msvm.Compute(inputs[i], out responses);
                int actual;
                responses.Max(out actual);
                y[i] = actual;
                if (i < 6)
                {
                    Assert.AreEqual(expected, actual);
                    evals[i] = msvm.GetLastKernelEvaluations();
                }
                else
                {
                    Assert.AreNotEqual(expected, actual);
                    evals[i] = msvm.GetLastKernelEvaluations();
                }
            }

            for (int i = 0; i < evals.Length; i++)
            {
                Assert.AreEqual(0, evals[i]);
            }

            for (int i = 0; i < inputs.Length; i++)
            {
                int actual;
                msvm.Scores(inputs[i], out actual);
                Assert.AreEqual(y[i], actual);
            }
        }