public void learn_linear_multiclass() { #region doc_learn_multiclass // In this example, we will learn a multi-class SVM using the one-vs-one (OvO) // approach. The OvO approacbh can decompose decision problems involving multiple // classes into a series of binary ones, which can then be solved using SVMs. // Ensure we have reproducible results Accord.Math.Random.Generator.Seed = 0; // We will try to learn a classifier // for the Fisher Iris Flower dataset var iris = new Iris(); double[][] inputs = iris.Instances; // get the flower characteristics int[] outputs = iris.ClassLabels; // get the expected flower classes // We will use mini-batches of size 32 to learn a SVM using SGD var batches = MiniBatches.Create(batchSize: 32, maxIterations: 1000, shuffle: ShuffleMethod.EveryEpoch, input: inputs, output: outputs); // Now, we can create a multi-class teaching algorithm for the SVMs var teacher = new MulticlassSupportVectorLearning <Linear, double[]> { // We will use SGD to learn each of the binary problems in the multi-class problem Learner = (p) => new StochasticGradientDescent <Linear, double[], LogisticLoss>() { LearningRate = 1e-3, MaxIterations = 1 // so the gradient is only updated once after each mini-batch } }; // The following line is only needed to ensure reproducible results. Please remove it to enable full parallelization teacher.ParallelOptions.MaxDegreeOfParallelism = 1; // (Remove, comment, or change this line to enable full parallelism) // Now, we can start training the model on mini-batches: foreach (var batch in batches) { teacher.Learn(batch.Inputs, batch.Outputs); } // Get the final model: var svm = teacher.Model; // Now, we should be able to use the model to predict // the classes of all flowers in Fisher's Iris dataset: int[] prediction = svm.Decide(inputs); // And from those predictions, we can compute the model accuracy: var cm = new GeneralConfusionMatrix(expected: outputs, predicted: prediction); double accuracy = cm.Accuracy; // should be approximately 0.973 #endregion Assert.AreEqual(0.97333333333333338, cm.Accuracy); Assert.AreEqual(150, batches.NumberOfSamples); Assert.AreEqual(32, batches.MiniBatchSize); Assert.AreEqual(213, batches.CurrentEpoch); Assert.AreEqual(1001, batches.CurrentIteration); Assert.AreEqual(82, batches.CurrentSample); }
public void learn_linear_nonlinear() { #region doc_learn_nonlinear // In this example, we will show how its possible to learn a // non-linear SVM using a linear algorithm by using a explicit // expansion of the kernel function: // Ensure we have reproducible results Accord.Math.Random.Generator.Seed = 0; // We will try to learn a classifier // for the Fisher Iris Flower dataset var iris = new WisconsinDiagnosticBreastCancer(); double[][] inputs = iris.Features; // get the flower characteristics int[] outputs = iris.ClassLabels; // get the expected flower classes // We will use mini-batches of size 32 to learn a SVM using SGD var batches = MiniBatches.Create(batchSize: 32, maxIterations: 1000, shuffle: ShuffleMethod.EveryEpoch, input: inputs, output: outputs); // We will use an explicit Polynomial kernel expansion var polynomial = new Polynomial(2); // Now, we can create a multi-class teaching algorithm for the SVMs var teacher = new MulticlassSupportVectorLearning <Linear, double[]> { // We will use SGD to learn each of the binary problems in the multi-class problem Learner = (p) => new AveragedStochasticGradientDescent <Linear, double[], LogisticLoss>() { LearningRate = 1e-3, MaxIterations = 1 // so the gradient is only updated once after each mini-batch } }; // The following line is only needed to ensure reproducible results. Please remove it to enable full parallelization teacher.ParallelOptions.MaxDegreeOfParallelism = 1; // (Remove, comment, or change this line to enable full parallelism) // Now, we can start training the model on mini-batches: foreach (var batch in batches) { teacher.Learn(polynomial.Transform(batch.Inputs), batch.Outputs); } // Get the final model: var svm = teacher.Model; // The following line is only needed to ensure reproducible results. Please remove it to enable full parallelization svm.ParallelOptions.MaxDegreeOfParallelism = 1; // (Remove, comment, or change this line to enable full parallelism) // Now, we should be able to use the model to predict // the classes of all flowers in Fisher's Iris dataset: int[] prediction = svm.Decide(polynomial.Transform(inputs)); // And from those predictions, we can compute the model accuracy: var cm = new GeneralConfusionMatrix(expected: outputs, predicted: prediction); double accuracy = cm.Accuracy; // should be approximately 0.92 #endregion Assert.AreEqual(0.92091388400702989, cm.Accuracy); Assert.AreEqual(569, batches.NumberOfSamples); Assert.AreEqual(32, batches.MiniBatchSize); Assert.AreEqual(56, batches.CurrentEpoch); Assert.AreEqual(1001, batches.CurrentIteration); Assert.AreEqual(168, batches.CurrentSample); }