public void RunTest() { Accord.Math.Tools.SetupGenerator(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 Assert.AreEqual(2 / 16.0, error); }
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); double expected = smo.Run(); 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; int 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)); } }
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); 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]); }
private static void multilabelsvm() { // Sample data // The following is simple auto association function // where each input correspond to its own class. This // problem should be easily solved by a Linear kernel. // Sample input data double[][] inputs = { new double[] { 0 }, new double[] { 3 }, new double[] { 1 }, new double[] { 2 }, }; // Outputs for each of the inputs int[][] outputs = { new[] { -1, 1, -1 }, new[] { -1, -1, 1 }, new[] { 1, 1, -1 }, new[] { -1, -1, -1 }, }; // Create a new Linear kernel IKernel kernel = new Linear(); // Create a new Multi-class Support Vector Machine with one input, // using the linear kernel and for four disjoint classes. var machine = new MultilabelSupportVectorMachine(1, kernel, 3); // Create the Multi-label 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(); int[][] answers = inputs.Apply(machine.Compute); }
public void serialize_reload_new_version() { 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 expected = smo.Run(); // Save the machines var bytes = msvm.Save(); // Reload the machines var target = Serializer.Load<MultilabelSupportVectorMachine>(bytes); double actual; int 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.AreEqual(a.Threshold, b.Threshold); Assert.AreEqual(a.NumberOfInputs, b.NumberOfInputs); Assert.AreEqual(a.NumberOfOutputs, b.NumberOfOutputs); Assert.IsTrue(a.Weights.IsEqual(b.Weights)); Assert.IsTrue(a.SupportVectors.IsEqual(b.SupportVectors)); } }
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); } }