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]);
            }
        }
Beispiel #2
0
        /// <summary>
        ///   Compute the error ratio.
        /// </summary>
        ///
        public double ComputeError(double[][] inputs, int[][] expectedOutputs)
        {
            // Compute errors
            int count = 0;

            for (int i = 0; i < inputs.Length; i++)
            {
                int[] actual   = msvm.Compute(inputs[i]);
                int[] expected = expectedOutputs[i];

                for (int j = 0; j < actual.Length; j++)
                {
                    if (actual[j] != expected[j])
                    {
                        Interlocked.Increment(ref count);
                    }
                }
            }

            // Return misclassification error ratio
            return(count / (double)(inputs.Length * msvm.Classes));
        }
Beispiel #3
0
        public void SerializeTest1()
        {
            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 Linear();
            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
            };

            double error = smo.Run();

            Assert.AreEqual(0, error);

            int count = 0; // Compute errors

            for (int i = 0; i < inputs.Length; i++)
            {
                double[] responses;
                msvm.Compute(inputs[i], out responses);
                int y; responses.Max(out y);
                if (y != outputs[i])
                {
                    count++;
                }
            }

            double expected = (double)count / inputs.Length;

            Assert.AreEqual(msvm.Inputs, 5);
            Assert.AreEqual(msvm.Classes, 4);
            Assert.AreEqual(4, msvm.Machines.Length);


            MemoryStream stream = new MemoryStream();

            // Save the machines
            msvm.Save(stream);

            // Rewind
            stream.Seek(0, SeekOrigin.Begin);

            // Reload the machines
            var target = MultilabelSupportVectorMachine.Load(stream);

            double actual;

            count = 0; // Compute errors
            for (int i = 0; i < inputs.Length; i++)
            {
                double[] responses;
                target.Compute(inputs[i], out responses);
                int y; responses.Max(out y);
                if (y != outputs[i])
                {
                    count++;
                }
            }

            actual = (double)count / inputs.Length;


            Assert.AreEqual(expected, actual);

            Assert.AreEqual(msvm.Inputs, target.Inputs);
            Assert.AreEqual(msvm.Classes, target.Classes);
            for (int i = 0; i < msvm.Machines.Length; i++)
            {
                var a = msvm[i];
                var b = target[i];

                Assert.IsTrue(a.SupportVectors.IsEqual(b.SupportVectors));
            }
        }
Beispiel #4
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);
            }
        }