/// <summary> /// Constructs a new Multi-label Support Vector Learning algorithm. /// </summary> /// public MultilabelSupportVectorLearning(MultilabelSupportVectorMachine machine, double[][] inputs, int[][] outputs) { // Initial argument checking if (machine == null) { throw new ArgumentNullException("machine"); } if (inputs == null) { throw new ArgumentNullException("inputs"); } if (outputs == null) { throw new ArgumentNullException("outputs"); } if (inputs.Length != outputs.Length) { throw new DimensionMismatchException("outputs", "The number of inputs and outputs does not match."); } if (machine.Inputs > 0) { // This machine has a fixed input vector size for (int i = 0; i < inputs.Length; i++) { if (inputs[i].Length != machine.Inputs) { throw new DimensionMismatchException("inputs", "The size of the input vectors does not match the expected number of inputs of the machine"); } } } for (int i = 0; i < outputs.Length; i++) { if (outputs[i].Length != machine.Classes) { throw new DimensionMismatchException("outputs", "The number of output values is outside of the expected range of class labels."); } for (int j = 0; j < outputs[i].Length; j++) { if (outputs[i][j] != -1 && outputs[i][j] != 1) { throw new ArgumentException("Output values should be either -1 or +1.", "outputs"); } } } // Machine this.msvm = machine; // Learning data this.inputs = inputs; this.outputs = outputs; }
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)); }
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]); } }
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); }
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); }
private void initialize(MultilabelSupportVectorMachine machine, double[][] inputs, int[][] outputs) { if (machine.Inputs > 0) { // This machine has a fixed input vector size for (int i = 0; i < inputs.Length; i++) { if (inputs[i].Length != machine.Inputs) { throw new DimensionMismatchException("inputs", "The size of the input vector at index " + i + " does not match the expected number of inputs of the machine." + " All input vectors for this machine must have length " + machine.Inputs); } } } for (int i = 0; i < outputs.Length; i++) { if (outputs[i].Length != machine.Classes) { throw new DimensionMismatchException("outputs", "Output vectors should have the same length as there are classes in the multi-label" + " machine. The output vector at position " + i + " has a different length."); } for (int j = 0; j < outputs[i].Length; j++) { if (outputs[i][j] != -1 && outputs[i][j] != 1) { throw new ArgumentOutOfRangeException("outputs", "Output values should be either -1 or +1. The output at index " + j + " of the output vector at position " + i + " violates this criteria."); } } } // Machine this.msvm = machine; // Learning data this.inputs = inputs; this.outputs = outputs; }
/// <summary> /// Constructs a new Multi-label Support Vector Learning algorithm. /// </summary> /// /// <param name="inputs">The input learning vectors for the machine learning algorithm.</param> /// <param name="machine">The <see cref="MulticlassSupportVectorMachine"/> to be trained.</param> /// <param name="outputs">The output labels associated with each of the input vectors. The /// class labels should be between 0 and the <see cref="MultilabelSupportVectorMachine.Classes"> /// number of classes in the multiclass machine</see>. In a multi-label SVM, multiple classes /// can be associated with a single input vector.</param> /// public MultilabelSupportVectorLearning(MultilabelSupportVectorMachine machine, double[][] inputs, int[] outputs) { // Initial argument checking if (machine == null) { throw new ArgumentNullException("machine"); } if (inputs == null) { throw new ArgumentNullException("inputs"); } if (outputs == null) { throw new ArgumentNullException("outputs"); } if (inputs.Length != outputs.Length) { throw new DimensionMismatchException("outputs", "The number of inputs and outputs does not match."); } int classes = machine.Classes; int[][] expanded = new int[outputs.Length][]; for (int i = 0; i < expanded.Length; i++) { int c = outputs[i]; if (c < 0 || c >= classes) { throw new ArgumentOutOfRangeException("outputs", "Output labels should be either positive and less than the number of classes" + " at the machine. The output at position " + i + " violates this criteria."); } int[] row = expanded[i] = new int[classes]; for (int j = 0; j < row.Length; j++) { row[j] = -1; } row[c] = 1; } initialize(machine, inputs, expanded); }
/// <summary> /// Constructs a new Multi-label Support Vector Learning algorithm. /// </summary> /// /// <param name="inputs">The input learning vectors for the machine learning algorithm.</param> /// <param name="machine">The <see cref="MulticlassSupportVectorMachine"/> to be trained.</param> /// <param name="outputs">The output labels associated with each of the input vectors. The /// class labels should be between 0 and the <see cref="MultilabelSupportVectorMachine.Classes"> /// number of classes in the multiclass machine</see>. In a multi-label SVM, multiple classes /// can be associated with a single input vector.</param> /// public MultilabelSupportVectorLearning(MultilabelSupportVectorMachine machine, double[][] inputs, int[][] outputs) { // Initial argument checking if (machine == null) { throw new ArgumentNullException("machine"); } if (inputs == null) { throw new ArgumentNullException("inputs"); } if (outputs == null) { throw new ArgumentNullException("outputs"); } if (inputs.Length != outputs.Length) { throw new DimensionMismatchException("outputs", "The number of inputs and outputs does not match."); } initialize(machine, inputs, outputs); }
/// <summary> /// Constructs a new Multi-label Support Vector Learning algorithm. /// </summary> /// /// <param name="inputs">The input learning vectors for the machine learning algorithm.</param> /// <param name="machine">The <see cref="MulticlassSupportVectorMachine"/> to be trained.</param> /// <param name="outputs">The output labels associated with each of the input vectors. The /// class labels should be between 0 and the <see cref="MultilabelSupportVectorMachine.Classes"> /// number of classes in the multiclass machine</see>. In a multi-label SVM, multiple classes /// can be associated with a single input vector.</param> /// public MultilabelSupportVectorLearning(MultilabelSupportVectorMachine machine, double[][] inputs, int[] outputs) { // Initial argument checking if (machine == null) throw new ArgumentNullException("machine"); if (inputs == null) throw new ArgumentNullException("inputs"); if (outputs == null) throw new ArgumentNullException("outputs"); if (inputs.Length != outputs.Length) throw new DimensionMismatchException("outputs", "The number of inputs and outputs does not match."); int classes = machine.Classes; int[][] expanded = new int[outputs.Length][]; for (int i = 0; i < expanded.Length; i++) { int c = outputs[i]; if (c < 0 || c >= classes) { throw new ArgumentOutOfRangeException("outputs", "Output labels should be either positive and less than the number of classes" + " at the machine. The output at position " + i + " violates this criteria."); } int[] row = expanded[i] = new int[classes]; for (int j = 0; j < row.Length; j++) row[j] = -1; row[c] = 1; } initialize(machine, inputs, expanded); }
public MultilabelSupportVectorLearning(MultilabelSupportVectorMachine model, double[][] input, int[] output) { this.Model = model; this.input = input; this.output = Jagged.OneHot <int>(output); }
public List <ImageData> Run(MultilabelSupportVectorMachine <Linear> machine1, MultilabelSupportVectorMachine <Linear> machine2) { LoadTargetImages(); return(GetPredictions(machine1, machine2)); }
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 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)); } }
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); } }
/// <summary> /// Constructs a new Multi-label Support Vector Learning algorithm. /// </summary> /// /// <param name="inputs">The input learning vectors for the machine learning algorithm.</param> /// <param name="machine">The <see cref="MulticlassSupportVectorMachine"/> to be trained.</param> /// <param name="outputs">The output labels associated with each of the input vectors. The /// class labels should be between 0 and the <see cref="MultilabelSupportVectorMachine.Classes"> /// number of classes in the multiclass machine</see>. In a multi-label SVM, multiple classes /// can be associated with a single input vector.</param> /// public MultilabelSupportVectorLearning(MultilabelSupportVectorMachine machine, double[][] inputs, int[][] outputs) { // Initial argument checking if (machine == null) throw new ArgumentNullException("machine"); if (inputs == null) throw new ArgumentNullException("inputs"); if (outputs == null) throw new ArgumentNullException("outputs"); if (inputs.Length != outputs.Length) throw new DimensionMismatchException("outputs", "The number of inputs and outputs does not match."); initialize(machine, inputs, outputs); }
/// <summary> /// Constructs a new Multi-label Support Vector Learning algorithm. /// </summary> /// public MultilabelSupportVectorLearning(MultilabelSupportVectorMachine machine, double[][] inputs, int[][] outputs) { // Initial argument checking if (machine == null) throw new ArgumentNullException("machine"); if (inputs == null) throw new ArgumentNullException("inputs"); if (outputs == null) throw new ArgumentNullException("outputs"); if (inputs.Length != outputs.Length) throw new DimensionMismatchException("outputs", "The number of inputs and outputs does not match."); if (machine.Inputs > 0) { // This machine has a fixed input vector size for (int i = 0; i < inputs.Length; i++) if (inputs[i].Length != machine.Inputs) throw new DimensionMismatchException("inputs", "The size of the input vectors does not match the expected number of inputs of the machine"); } for (int i = 0; i < outputs.Length; i++) { if (outputs[i].Length != machine.Classes) throw new DimensionMismatchException("outputs", "The number of output values is outside of the expected range of class labels."); for (int j = 0; j < outputs[i].Length; j++) if (outputs[i][j] != -1 && outputs[i][j] != 1) throw new ArgumentException("Output values should be either -1 or +1.", "outputs"); } // Machine this.msvm = machine; // Learning data this.inputs = inputs; this.outputs = outputs; }