Пример #1
0
        public override void DoTraining(IEnumerable <FeatureVectorWithLabelID> trainingSet, ILogBuilder logger)
        {
            IEnumerable <double[]> vectors = trainingSet.Select(x => x.vector.dimensions);
            IEnumerable <int>      labels  = trainingSet.Select(x => x.labelID);

            // Learn a machine
            if (model == mSVMModels.linear)
            {
                machine = teacher.Learn(vectors.ToArray(), labels.ToArray());
            }

            if (model == mSVMModels.gaussian)
            {
                machineGaussian = teacherGaussian.Learn(vectors.ToArray(), labels.ToArray());


                // Create the multi-class learning algorithm for the machine
                var calibration = new MulticlassSupportVectorLearning <Gaussian>()
                {
                    Model = machineGaussian, // We will start with an existing machine

                    // Configure the learning algorithm to use Platt's calibration
                    Learner = (param) => new ProbabilisticOutputCalibration <Gaussian>()
                    {
                        Model = param.Model // Start with an existing machine
                    }
                };
            }
        }
Пример #2
0
        /// <summary>
        /// Core machine learning method for parsing csv data, training the svm, and calculating the accuracy.
        /// </summary>
        /// <param name="path">string - path to csv file (training, csv, test).</param>
        /// <param name="count">int - max number of rows to process. This is useful for preparing learning curves, by using gradually increasing values. Use Int32.MaxValue to read all rows.</param>
        /// <param name="machine">MulticlassSupportVectorMachine - Leave null for initial training.</param>
        /// <returns>MulticlassSupportVectorMachine</returns>
        private static MulticlassSupportVectorMachine RunSvm(string path, int count, MulticlassSupportVectorMachine machine = null)
        {
            double[][] inputs;
            int[]      outputs;

            // Parse the csv file to get inputs and outputs.
            ReadData(path, count, out inputs, out outputs, new FrontLabelParser());

            if (machine == null)
            {
                // Training.
                MulticlassSupportVectorLearning teacher = null;

                // Create the svm.
                machine           = new MulticlassSupportVectorMachine(_pixelCount, new Gaussian(_sigma), _classCount);
                teacher           = new MulticlassSupportVectorLearning(machine, inputs, outputs);
                teacher.Algorithm = (svm, classInputs, classOutputs, i, j) => new SequentialMinimalOptimization(svm, classInputs, classOutputs)
                {
                    CacheSize = 0
                };

                // Train the svm.
                Utility.ShowProgressFor(() => teacher.Run(), "Training");
            }

            // Calculate accuracy.
            double accuracy = Utility.ShowProgressFor <double>(() => Accuracy.CalculateAccuracy(machine, inputs, outputs), "Calculating Accuracy");

            Console.WriteLine("Accuracy: " + Math.Round(accuracy * 100, 2) + "%");

            return(machine);
        }
        public void LinearTest()
        {
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] inputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 1, 1, 1 }, //  2
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] outputs = // those are the class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1, 1, 1,
                2, 2, 2, 2, 2,
            };

            // Create a new multi-class linear support vector machine for 3 classes
            var machine = new MulticlassSupportVectorMachine(inputs: 4, classes: 3);

            // Create a one-vs-one learning algorithm using LIBLINEAR's L2-loss SVC dual
            var teacher = new MulticlassSupportVectorLearning(machine, inputs, outputs)
            {
                Algorithm = (svm, classInputs, classOutputs, i, j) =>
                            new LinearDualCoordinateDescent(svm, classInputs, classOutputs)
                {
                    Loss = Loss.L2
                }
            };

#if DEBUG
            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;
#endif

            // Teach the machine
            double error = teacher.Run(); // should be 0.

            Assert.AreEqual(0, error);
            for (int i = 0; i < inputs.Length; i++)
            {
                error = machine.Compute(inputs[i]);
                double expected = outputs[i];
                Assert.AreEqual(expected, error);
            }
        }
Пример #4
0
        private static void TestPredictSparseMulticlassSVM()
        {
            Console.WriteLine("Downloading dataset");
            var news20 = new Accord.DataSets.News20(@"C:\Temp\");

            Sparse <double>[] inputs  = news20.Training.Item1;
            int[]             outputs = news20.Training.Item2.ToMulticlass();

            var learn = new MulticlassSupportVectorLearning <Linear, Sparse <double> >()
            {
                // using LIBLINEAR's L2-loss SVC dual for each SVM
                Learner = (p) => new LinearDualCoordinateDescent <Linear, Sparse <double> >()
                {
                    Loss       = Loss.L2,
                    Complexity = 1.0,
                    Tolerance  = 1e-4
                }
            };

            Console.WriteLine("Learning");
            Stopwatch sw  = Stopwatch.StartNew();
            var       svm = learn.Learn(inputs.Get(0, 1000), outputs.Get(0, 1000));

            Console.WriteLine(sw.Elapsed);

            Console.WriteLine("Predicting");
            sw = Stopwatch.StartNew();
            int[] predicted = svm.Decide(inputs);
            Console.WriteLine(sw.Elapsed);
        }
        void TranslateInstance(Frame frame)
        {
            // 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 MulticlassSupportVectorMachine(5, kernel, numOfClasses);

            // Create the Multi-class learning algorithm for the machine
            var teacher = new MulticlassSupportVectorLearning(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);

            // Run the learning algorithm
            double error = teacher.Run(); // output should be 0

            double[] distances = new double[5];
            distances = LeapEventListener.getDistances(frame);

            int decision = machine.Compute(distances); //svm AI

            output.Text = output.Text + Char2SvmClass.class2svm(decision);
        }
Пример #6
0
        public byte[] trainSVM(double[][] inputs, int[] outputs)
        {
            IKernel kernel = Gaussian.Estimate(inputs, inputs.Length / 4);

            var numComplexity = kernel.EstimateComplexity(inputs);

            double            complexity = numComplexity;
            double            tolerance  = (double)0.2;
            int               cacheSize  = (int)1000;
            SelectionStrategy strategy   = SelectionStrategy.SecondOrder;

            // Create the learning algorithm using the machine and the training data
            var ml = new MulticlassSupportVectorLearning <IKernel>()
            {
                // Configure the learning algorithm
                Learner = (param) => new SequentialMinimalOptimization <IKernel>()
                {
                    Complexity = complexity,
                    Tolerance  = tolerance,
                    CacheSize  = cacheSize,
                    Strategy   = strategy,
                    Kernel     = kernel
                }
            };

            var ksvm = ml.Learn(inputs, outputs);

            byte[] saved;
            Accord.IO.Serializer.Save(ksvm, out saved);
            return(saved);
        }
Пример #7
0
        private void SVMLearning(double[][] input, int[] output)
        {
            var teacher = new MulticlassSupportVectorLearning <Gaussian>()
            {
                Learner = (param) => new SequentialMinimalOptimization <Gaussian>()
                {
                    UseKernelEstimation = true
                }
            };

            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

            machine = teacher.Learn(input, output);

            int[] prediction = machine.Decide(input);

            PredictedData[2] = new double[prediction.Length];

            for (int i = 0; i < prediction.Length; i++)
            {
                PredictedData[2][i] = Convert.ToDouble(prediction[i]);
            }

            ErrorSVM = new ZeroOneLoss(output).Loss(prediction);
        }
        public void SparseLinearTest()
        {
            MulticlassSupportVectorMachine <Linear> svm1;
            MulticlassSupportVectorMachine <Linear, Sparse <double> > svm2;

            {
                Accord.Math.Random.Generator.Seed = 0;
                MemoryStream file = new MemoryStream(
                    Encoding.Default.GetBytes(Resources.iris_scale));
                var reader = new SparseReader(file, Encoding.Default);

                var        samples = reader.ReadDenseToEnd();
                double[][] x       = samples.Item1;
                int[]      y       = samples.Item2.ToMulticlass();

                var learner = new MulticlassSupportVectorLearning <Linear>()
                {
                    Learner = (p) => new LinearDualCoordinateDescent <Linear>()
                };

                svm1 = learner.Learn(x, y);
            }

            {
                Accord.Math.Random.Generator.Seed = 0;
                MemoryStream file = new MemoryStream(
                    Encoding.Default.GetBytes(Resources.iris_scale));

                // Create a new Sparse Sample Reader to read any given file,
                //  passing the correct dense sample size in the constructor
                var reader = new SparseReader(file, Encoding.Default);

                var samples         = reader.ReadSparseToEnd();
                Sparse <double>[] x = samples.Item1;
                int[]             y = samples.Item2.ToMulticlass();

                var learner = new MulticlassSupportVectorLearning <Linear, Sparse <double> >()
                {
                    Learner = (p) => new LinearDualCoordinateDescent <Linear, Sparse <double> >()
                };

                svm2 = learner.Learn(x, y);
            }

            Assert.AreEqual(svm1.Models.Length, svm2.Models.Length);
            for (int i = 0; i < svm1.Models.Length; i++)
            {
                var ma = svm1[i].Value;
                var mb = svm2[i].Value;

                Assert.IsTrue(ma.Weights.IsEqual(mb.Weights));
                Assert.AreEqual(ma.SupportVectors.Length, mb.SupportVectors.Length);
                for (int j = 0; j < ma.SupportVectors.Length; j++)
                {
                    double[] expected = ma.SupportVectors[j];
                    double[] actual   = mb.SupportVectors[j].ToDense(4);
                    Assert.IsTrue(expected.IsEqual(actual, 1e-5));
                }
            }
        }
        public int[]  ApplySVMByGussianKernel(double kernelDegree, double complexity, double epsilon, double tolerance,
                                              double[][] inputs, int[] outputs, int inputNumber, double[][] tests)
        {
            MulticlassSupportVectorMachine ksvm;

            int[]   SVMoutputs = new Int32[tests.Length];
            IKernel kernel;

            kernel = new Gaussian(kernelDegree);
            ksvm   = new MulticlassSupportVectorMachine(inputNumber, kernel, 2);
            MulticlassSupportVectorLearning ml = new MulticlassSupportVectorLearning(ksvm, inputs, outputs);

            ml.Algorithm = (svm, classInputs, classOutputs, i, j) =>
            {
                var smo = new SequentialMinimalOptimization(svm, classInputs, classOutputs);
                smo.Complexity = complexity;  /// Cost parameter for SVM
                smo.Epsilon    = epsilon;
                smo.Tolerance  = tolerance;
                return(smo);
            };

            double error = ml.Run();

            for (int i = 0; i < tests.Length; i++)
            {
                SVMoutputs[i] = (int)ksvm.Compute(tests[i]);
            }
            return(SVMoutputs);
        }
Пример #10
0
        private static void multiclassSvm(double[][] inputs, int[] outputs)
        {
            // Support vector machines are by definition binary classifiers. As such, in order
            // to apply them for classification problems with more than 2 classes we need to
            // use some tricks. There are two most famous ways to achieve a multi-class model
            // from a set of binary ones: one-vs-one and one-vs-the rest.

            // One-vs-one classifiers are true multi-class classifiers in the sense that they
            // can only predict one single class label for each sample. However, One-vs-rest
            // classifiers can predict either one class label or many, depending on how it is
            // used.

            // Create the multi-class learning algorithm as one-vs-one
            var teacher = new MulticlassSupportVectorLearning <Linear>()
            {
                Learner = (p) => new SequentialMinimalOptimization <Linear>()
                {
                    Complexity = 10000.0 // Create a hard SVM
                }
            };

            // Learn a multi-class SVM using the teacher
            var svm = teacher.Learn(inputs, outputs);

            // Get the predictions for the inputs
            int[] predicted = svm.Decide(inputs);

            // Create a confusion matrix to check the quality of the predictions:
            var cm = new ConfusionMatrix(predicted: predicted, expected: outputs);

            // Check the accuracy measure:
            double accuracy = cm.Accuracy; // (should be 1.0 or 100%)
        }
Пример #11
0
        private static void bagOfWords(int[][] inputs, int[] outputs)
        {
            var bow = new BagOfWords <int>();

            var quantizer = bow.Learn(inputs);

            double[][] histograms = quantizer.Transform(inputs);

            // One way to perform sequence classification with an SVM is to use
            // a kernel defined over sequences, such as DynamicTimeWarping.

            // Create the multi-class learning algorithm as one-vs-one with DTW:
            var teacher = new MulticlassSupportVectorLearning <ChiSquare, double[]>()
            {
                Learner = (p) => new SequentialMinimalOptimization <ChiSquare, double[]>()
                {
                    Complexity = 10000.0 // Create a hard SVM
                }
            };

            // Learn a multi-label SVM using the teacher
            var svm = teacher.Learn(histograms, outputs);

            // Get the predictions for the inputs
            int[] predicted = svm.Decide(histograms);

            // Create a confusion matrix to check the quality of the predictions:
            var cm = new ConfusionMatrix(predicted: predicted, expected: outputs);

            // Check the accuracy measure:
            double accuracy = cm.Accuracy;
        }
Пример #12
0
        public void LoadDataAndRunSvmGaussian(string testingDataAbsolutePath, bool testingDataHasHeaders)
        {
            _numberOfPredictions      = 0;
            _numberPredictionsCorrect = 0;

            if (ErrorHasOccured)
            {
                return;
            }

            try
            {
                using (StreamReader sr = new StreamReader(testingDataAbsolutePath))
                {
                    // Read the stream to a string, and write the string to the console.
                    string    line  = sr.ReadToEnd();
                    DataTable table = CsvReader.FromText(line, testingDataHasHeaders).ToTable();

                    _testingInputs  = table.ToJagged <double>(FeatureNames.ToArray());
                    _testingOutputs = table.Columns["Label"].ToArray <int>();

                    var teacher = new MulticlassSupportVectorLearning <Gaussian>()
                    {
                        // Configure the learning algorithm to use SMO to train the
                        //  underlying SVMs in each of the binary class subproblems.
                        Learner = (param) => new SequentialMinimalOptimization <Gaussian>()
                        {
                            // Estimate a suitable guess for the Gaussian kernel's parameters.
                            // This estimate can serve as a starting point for a grid search.
                            UseKernelEstimation = true
                        }
                    };

                    // Learn a machine
                    var machine = teacher.Learn(_trainingInputs, _trainingOutputs);

                    // Classify the samples using the model
                    int[] answers = machine.Decide(_testingInputs, _testingOutputs);

                    // Get class scores for each sample
                    double[] scores = machine.Score(_trainingInputs);

                    for (int item = 0; item < answers.Length; item++)
                    {
                        _numberOfPredictions++;
                        if (_testingOutputs[item] == answers[item])
                        {
                            _numberPredictionsCorrect++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                ErrorHasOccured    = true;
                FailureInformation = ex.Message;
                return;
            }
        }
Пример #13
0
        private static void dtw(int[][] inputs, int[] outputs)
        {
            // One way to perform sequence classification with an SVM is to use
            // a kernel defined over sequences, such as DynamicTimeWarping.

            // Create the multi-class learning algorithm as one-vs-one with DTW:
            var teacher = new MulticlassSupportVectorLearning <DynamicTimeWarping <Dirac <int>, int>, int[]>()
            {
                Learner = (p) => new SequentialMinimalOptimization <DynamicTimeWarping <Dirac <int>, int>, int[]>()
                {
                    Complexity = 10000.0 // Create a hard SVM
                }
            };

            // Learn a multi-label SVM using the teacher
            var svm = teacher.Learn(inputs, outputs);

            // Get the predictions for the inputs
            int[] predicted = svm.Decide(inputs);

            // Create a confusion matrix to check the quality of the predictions:
            var cm = new ConfusionMatrix(predicted: predicted, expected: outputs);

            // Check the accuracy measure:
            double accuracy = cm.Accuracy; // (should be 1.0 or 100%)
        }
        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 MulticlassSupportVectorMachine(5, 4);
            var smo  = new MulticlassSupportVectorLearning(msvm, inputs, outputs);

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

            msvm.ParallelOptions.MaxDegreeOfParallelism = 1;
            smo.ParallelOptions.MaxDegreeOfParallelism  = 1;

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

            double error = smo.Run();

            Assert.AreEqual(0, error);

            // Linear machines in compact form do not require kernel evaluations
            Assert.AreEqual(0, msvm.GetLastKernelEvaluations());

            for (int i = 0; i < inputs.Length; i++)
            {
                double expected = outputs[i];
                double actual   = msvm.Compute(inputs[i], MulticlassComputeMethod.Elimination);
                Assert.AreEqual(expected, actual);
                Assert.AreEqual(0, msvm.GetLastKernelEvaluations());
            }

            for (int i = 0; i < inputs.Length; i++)
            {
                double expected = outputs[i];
                double actual   = msvm.Compute(inputs[i], MulticlassComputeMethod.Voting);
                Assert.AreEqual(expected, actual);
                Assert.AreEqual(0, msvm.GetLastKernelEvaluations());
            }
        }
Пример #15
0
        // here the same setting is mantained, being the following another kernelSVM
        public void SVMPolinomialKernel()
        {
            Console.WriteLine("SottoProgramma chiamato: PolynomialKernel; Kernel = Polynomial, degree = 2.");
            this.clock = DateTime.Now;

            var teacher = new MulticlassSupportVectorLearning <Polynomial>()
            {
                Learner = (p) => new SequentialMinimalOptimization <Polynomial>()
                {
                    UseKernelEstimation = false,
                    Kernel = new Polynomial(degree: 2),
                    UseComplexityHeuristic = true
                }
            };

            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

            var machine = teacher.Learn(DataSets[1].ItemsFeatures, DataSets[1].CatIDs);

            int[] predicted = machine.Decide(DataSets[0].ItemsFeatures);

            double error = new ZeroOneLoss(DataSets[0].CatIDs).Loss(predicted);

            PrintReport(predicted, error, "Polynomial");

            Console.WriteLine("SottoProgramma PolynomialKernel terminato.\nErrore: {0}", error);
            Console.WriteLine("Tempo richiesto per l'operazione: " + (DateTime.Now - clock).TotalSeconds + " secondi.");
        }
        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);
        }
Пример #17
0
        public static void Train(string trainingFolder)
        {
            Console.WriteLine("Training SVM model with Cross-Validation...");

            (double[][] inputs, int[] output) = ReadData(trainingFolder);

            int crossValidateCount = Math.Min(maxCrossValidateCount, inputs.Count());

            Accord.Math.Random.Generator.Seed = 0;

            Console.WriteLine("Grid-Search...");
            var gscv = GridSearch <double[], int> .CrossValidate(
                ranges : new
            {
                Sigma = GridSearch.Range(fromInclusive: 0.00000001, toExclusive: 3),
            },

                learner : (p, ss) => new MulticlassSupportVectorLearning <Gaussian>
            {
                Kernel = new Gaussian(p.Sigma)
            },

                fit : (teacher, x, y, w) => teacher.Learn(x, y, w),

                loss : (actual, expected, r) => new ZeroOneLoss(expected).Loss(actual),

                folds : 10);

            //gscv.ParallelOptions.MaxDegreeOfParallelism = 1;

            var result = gscv.Learn(inputs.Take(crossValidateCount).ToArray(), output.Take(crossValidateCount).ToArray());

            var crossValidation = result.BestModel;

            double bestError     = result.BestModelError;
            double trainError    = result.BestModel.Training.Mean;
            double trainErrorVar = result.BestModel.Training.Variance;
            double valError      = result.BestModel.Validation.Mean;
            double valErrorVar   = result.BestModel.Validation.Variance;

            double bestSigma = result.BestParameters.Sigma;

            Console.WriteLine("Grid-Search Done.");

            Console.WriteLine("Using Sigma=" + bestSigma);

            // train model with best parameter
            var bestTeacher = new MulticlassSupportVectorLearning <Gaussian>
            {
                Kernel = new Gaussian(bestSigma)
            };

            MulticlassSupportVectorMachine <Gaussian> svm = bestTeacher.Learn(
                inputs.Take(traingRowsCount).ToArray(),
                output.Take(traingRowsCount).ToArray());

            // save model
            svm.Save(Path.Combine(trainingFolder, "model"), SerializerCompression.GZip);
        }
        public void multiclass_linear_new_usage()
        {
            #region doc_learn_ldcd
            // Let's say we have the following data to be classified
            // into three possible classes. Those are the samples:
            //
            double[][] inputs =
            {
                //               input         output
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 0, 0, 1, 0 }, //  0
                new double[] { 0, 1, 1, 0 }, //  0
                new double[] { 0, 1, 0, 0 }, //  0
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 0 }, //  1
                new double[] { 1, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 0, 0, 0, 1 }, //  1
                new double[] { 1, 1, 1, 1 }, //  2
                new double[] { 1, 0, 1, 1 }, //  2
                new double[] { 1, 1, 0, 1 }, //  2
                new double[] { 0, 1, 1, 1 }, //  2
                new double[] { 1, 1, 1, 1 }, //  2
            };

            int[] outputs = // those are the class labels
            {
                0, 0, 0, 0, 0,
                1, 1, 1, 1, 1,
                2, 2, 2, 2, 2,
            };

            // Create a one-vs-one multi-class SVM learning algorithm
            var teacher = new MulticlassSupportVectorLearning <Linear>()
            {
                // using LIBLINEAR's L2-loss SVC dual for each SVM
                Learner = (p) => new LinearDualCoordinateDescent()
                {
                    Loss = Loss.L2
                }
            };

            // Configure parallel execution options
            teacher.ParallelOptions.MaxDegreeOfParallelism = 1;

            // Learn a machine
            var machine = teacher.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            int[] predicted = machine.Decide(inputs);

            // Compute classification error
            double error = new ZeroOneLoss(outputs).Loss(predicted);
            #endregion

            Assert.AreEqual(0, error);
            Assert.IsTrue(predicted.IsEqual(outputs));
        }
 public void Reset()
 {
     //  m_bow = null;
     machine = null;
     teacher = null;
     m_trainImageFeatureVectors = null;
     m_classIdClassNameMap      = new Dictionary <int, string>();
 }
Пример #20
0
 public double TrainSVM(IKernel kernel, int classes, double[][] inputs, int[] outputs)
 {
     _svm           = new MulticlassSupportVectorMachine(inputs[0].Length, kernel, classes);
     _smo           = new MulticlassSupportVectorLearning(_svm, inputs, outputs);
     _smo.Algorithm = (svm, classInputs, classOutputs, i, j) =>
                      new SequentialMinimalOptimization(svm, classInputs, classOutputs);
     return(_smo.Run());
 }
Пример #21
0
        private void TrainAndPredict(double complexity, double gamma, int degree, double[][] trainingInputs, int[] trainingOutputs, double[][] validationInputs, out int[] predictedTraining, out int[] predictedValidation)
        {
            switch (Kernel)
            {
            case Kernel.Gaussian:
                var gaussianLearningKfold = new MulticlassSupportVectorLearning <Gaussian>
                {
                    Learner = p => new SequentialMinimalOptimization <Gaussian>
                    {
                        UseKernelEstimation    = false,
                        UseComplexityHeuristic = false,
                        Complexity             = complexity,
                        Token     = CancellationTokenSource.Token,
                        Tolerance = 0.01,
                        Kernel    = Gaussian.FromGamma(gamma),
                    }
                };
                var svmGaussian = gaussianLearningKfold.Learn(trainingInputs, trainingOutputs);
                predictedTraining   = svmGaussian.Decide(trainingInputs);
                predictedValidation = svmGaussian.Decide(validationInputs);
                break;

            case Kernel.Linear:
                var linearLearning = new MulticlassSupportVectorLearning <Linear>
                {
                    Learner = p => new LinearDualCoordinateDescent <Linear>
                    {
                        Complexity             = complexity,
                        UseComplexityHeuristic = false,
                        Token = CancellationTokenSource.Token
                    }
                };
                var svmLinear = linearLearning.Learn(trainingInputs, trainingOutputs);
                predictedTraining   = svmLinear.Decide(trainingInputs);
                predictedValidation = svmLinear.Decide(validationInputs);
                break;

            case Kernel.Polynomial:
                var polynomialLearning = new MulticlassSupportVectorLearning <Polynomial>
                {
                    Learner = p => new SequentialMinimalOptimization <Polynomial>
                    {
                        UseKernelEstimation    = false,
                        UseComplexityHeuristic = false,
                        Complexity             = complexity,
                        Token  = CancellationTokenSource.Token,
                        Kernel = new Polynomial(degree, 1)
                    }
                };
                var polynomialSvm = polynomialLearning.Learn(trainingInputs, trainingOutputs);
                predictedTraining   = polynomialSvm.Decide(trainingInputs);
                predictedValidation = polynomialSvm.Decide(validationInputs);
                break;

            default:
                throw new NotImplementedException();
            }
        }
        public void multiclass_new_usage_method_polynomial()
        {
            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 learner = new MulticlassSupportVectorLearning <Polynomial>()
            {
                Learner = (p) => new SequentialMinimalOptimization <Polynomial>()
                {
                    Model      = p.Model,
                    Complexity = 1,
                    Kernel     = new Polynomial(2)
                }
            };

            learner.ParallelOptions.MaxDegreeOfParallelism = 1;

            MulticlassSupportVectorMachine <Polynomial> msvm = learner.Learn(inputs, outputs);

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

            int[] evals = { 8, 8, 7, 7, 7, 7, 6, 6 };

            for (int i = 0; i < inputs.Length; i++)
            {
                double expected = outputs[i];
                msvm.Method = MulticlassComputeMethod.Elimination;
                double actual = msvm.Decide(inputs[i]);
                Assert.AreEqual(expected, actual);
                Assert.AreEqual(evals[i], msvm.GetLastKernelEvaluations());
            }

            for (int i = 0; i < inputs.Length; i++)
            {
                double expected = outputs[i];
                msvm.Method = MulticlassComputeMethod.Voting;
                double actual = msvm.Decide(inputs[i]);
                Assert.AreEqual(expected, actual);
                Assert.AreEqual(8, msvm.GetLastKernelEvaluations());
            }
        }
        public SVM(string TrainedDataInputFile)  //// Do training for all existing trained Data
        {
            _engine = new TesseractEngine(@"./tessdata3", "eng", EngineMode.TesseractAndCube);
            _engine.SetVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
            _engine.SetVariable("tessedit_char_blacklist", "¢§+~»~`!@#$%^&*()_+-={}[]|\\:\";\'<>?,./");

            string[]   TrainedData = Directory.GetFiles(TrainedDataInputFile, "*.png");
            double[][] inputs      = new double[TrainedData.Length][]; ///
            double[]   InputArray  = new double[784];
            int[]      Outputs     = new int[TrainedData.Length];

            for (int i = 0; i < TrainedData.Length; i++)
            {
                string   filename      = Path.GetFileNameWithoutExtension(TrainedData[i]);
                Bitmap   TrainingImage = new Bitmap(TrainedData[i]);
                string[] split         = filename.Split('.');
                for (int j = 0; j < 28; j++)
                {
                    for (int k = 0; k < 28; k++)
                    {
                        if ((!TrainingImage.GetPixel(j, k).Name.Equals("ffffffff")))
                        {
                            InputArray[j * 28 + k] = 1;
                        }
                        else
                        {
                            InputArray[j * 28 + k] = 0;
                        }
                    }
                }

                inputs[i]  = InputArray;
                Outputs[i] = Convert.ToInt32(split[0]);
                InputArray = new double[784];
            }

            IKernel kernel;

            kernel = new Polynomial(2, 0);
            ksvm   = new MulticlassSupportVectorMachine(784, kernel, 2);
            MulticlassSupportVectorLearning ml = new MulticlassSupportVectorLearning(ksvm, inputs, Outputs);

            double complexity = 1;   ///// set these three parameters Carefuly later
            double epsilon    = 0.001;
            double tolerance  = 0.2;

            ml.Algorithm = (svm, classInputs, classOutputs, i, j) =>
            {
                var smo = new SequentialMinimalOptimization(svm, classInputs, classOutputs);
                smo.Complexity = complexity;  /// Cost parameter for SVM
                smo.Epsilon    = epsilon;
                smo.Tolerance  = tolerance;
                return(smo);
            };

            // Train the machines. It should take a while.
            double error = ml.Run();
        }
        public double v3_0_1()
        {
            var ksvm = new MulticlassSupportVectorMachine(784, new Polynomial(2), 10);
            var smo  = new MulticlassSupportVectorLearning(ksvm, problem.Training.Inputs, problem.Training.Output);

            smo.Algorithm = (svm, x, y, i, j) => new SequentialMinimalOptimization(svm, x, y);

            return(smo.Run(computeError: false));
        }
Пример #25
0
        public void multilabelSVM()
        {
            var teacher = new MulticlassSupportVectorLearning <Gaussian>()
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (param) => new SequentialMinimalOptimization <Gaussian>()
                {
                    // Estimate a suitable guess for the Gaussian kernel's parameters.
                    // This estimate can serve as a starting point for a grid search.
                    UseKernelEstimation = true
                }
            };

            // Learn a machine
            var machine = teacher.Learn(inputs, outputs);


            // Create the multi-class learning algorithm for the machine
            var calibration = new MulticlassSupportVectorLearning <Gaussian>()
            {
                Model = machine, // We will start with an existing machine

                // Configure the learning algorithm to use Platt's calibration
                Learner = (param) => new ProbabilisticOutputCalibration <Gaussian>()
                {
                    Model = param.Model // Start with an existing machine
                }
            };


            // Configure parallel execution options
            calibration.ParallelOptions.MaxDegreeOfParallelism = 1;

            // Learn a machine
            calibration.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            int[] predicted = machine.Decide(inputs);

            // Get class scores for each sample
            double[] scores = machine.Score(inputs);

            // Get log-likelihoods (should be same as scores)
            double[][] logl = machine.LogLikelihoods(inputs);

            // Get probability for each sample
            double[][] prob = machine.Probabilities(inputs);

            // Compute classification error
            double error = new ZeroOneLoss(outputs).Loss(predicted);
            double loss  = new CategoryCrossEntropyLoss(outputs).Loss(prob);

            message += "SVM Validacja\n";
            message += "error " + error.ToString() + "\n";
            message += "loss " + loss.ToString() + "\n\n";
        }
Пример #26
0
        private void TreeTraining(double[][] inputs, int[] outputs)
        {
            if (doTraining)
            {
                Console.WriteLine("Start tree training.");

                teacher = new MulticlassSupportVectorLearning <Gaussian>()
                {
                    Learner = (param) => new SequentialMinimalOptimization <Gaussian>()
                    {
                        UseKernelEstimation = true
                    }
                };

                int        sampleCount        = inputs.Length;
                int        sampleCountDivided = sampleCount / 2;
                double[][] inputs_training    = new double[sampleCountDivided][];
                int[]      outputs_training   = new int[sampleCountDivided];
                double[][] inputs_validating  = new double[sampleCountDivided][];
                int[]      outputs_validating = new int[sampleCountDivided];

                for (int i = 0; i < sampleCountDivided; i++)
                {
                    inputs_training[i]    = inputs[2 * i];
                    inputs_validating[i]  = inputs[2 * i + 1];
                    outputs_training[i]   = outputs[2 * i];
                    outputs_validating[i] = outputs[2 * i + 1];
                }

                Console.WriteLine("Learning.");
                machine = teacher.Learn(inputs_training, outputs_training);

                Console.WriteLine("Deciding.");
                int[] predicted = machine.Decide(inputs_validating);

                /*
                 * double[] scores = machine.Score(inputs);
                 *
                 * double error = new ZeroOneLoss(outputs).Loss(predicted);
                 *
                 * Console.WriteLine(error);
                 * Console.WriteLine(answer);
                 *
                 * for(int i = 0; i < sampleCountDivided; i++)
                 * {
                 *  Console.WriteLine("{0:D} {1:D}", outputs_validating[i], predicted[i]);
                 * }
                 */
                Console.WriteLine("Training done.");

                machine.Save(modelPath);
            }
            else
            {
                machine = Serializer.Load <MulticlassSupportVectorMachine <Gaussian> >(modelPath);
            }
        }
Пример #27
0
        /// <summary>
        ///   Loads the stored gestures and learns a SVM using those data.
        /// </summary>
        ///
        private void btnLearn_Click(object sender, EventArgs e)
        {
            if (gridSamples.Rows.Count == 0)
            {
                MessageBox.Show("Please load or insert some data first.");
                return;
            }

            BindingList <Sequence> samples = database.Samples;
            BindingList <String>   classes = database.Classes;

            double[][] inputs  = new double[samples.Count][];
            int[]      outputs = new int[samples.Count];

            for (int i = 0; i < inputs.Length; i++)
            {
                inputs[i]  = samples[i].Input;
                outputs[i] = samples[i].Output;
            }


            // Creates a new learning machine. Please note how the number of inputs is given
            // as zero: this means the machine will accept variable-length sequences as input.
            //
            svm = new MulticlassSupportVectorMachine <DynamicTimeWarping>(inputs: 0,
                                                                          kernel: new DynamicTimeWarping(2), classes: classes.Count);


            // Create the learning algorithm to teach the multiple class classifier
            var teacher = new MulticlassSupportVectorLearning <DynamicTimeWarping>()
            {
                // Setup the learning algorithm for each 1x1 subproblem
                Learner = (param) => new SequentialMinimalOptimization <DynamicTimeWarping>()
                {
                    Kernel = new DynamicTimeWarping(2),
                }
            };


            // Run the learning algorithm
            this.svm = teacher.Learn(inputs, outputs);


            // Classify all training instances
            foreach (var sample in database.Samples)
            {
                sample.RecognizedAs = svm.Decide(sample.Input);
            }

            foreach (DataGridViewRow row in gridSamples.Rows)
            {
                var sample = row.DataBoundItem as Sequence;
                row.DefaultCellStyle.BackColor = (sample.RecognizedAs == sample.Output) ?
                                                 Color.LightGreen : Color.White;
            }
        }
Пример #28
0
    void Start()
    {
        // Generate always same random numbers
        //Accord.Math.Random.Generator.Seed = 0;

        // The following is a simple auto association function in which
        // the last column of each input correspond to its own class. This
        // problem should be easily solved using a Linear kernel.

        // Sample input data
        double[][] inputs =
        {
            new double[] { 1, 2, 8 },
            new double[] { 6, 2, 3 },
            new double[] { 1, 1, 1 },
            new double[] { 7, 6, 7 },
        };

        // Output for each of the inputs
        int[] outputs = { 0, 3, 1, 2 };


        // Create the multi-class learning algorithm for the machine
        var teacher = new MulticlassSupportVectorLearning <Linear>()
        {
            // Configure the learning algorithm to use SMO to train the
            //  underlying SVMs in each of the binary class subproblems.
            Learner = (param) => new SequentialMinimalOptimization <Linear>()
            {
                // If you would like to use other kernels, simply replace
                // the generic parameter to the desired kernel class, such
                // as for example, Polynomial or Gaussian:

                Kernel = new Linear()         // use the Linear kernel
            }
        };

        // Estimate the multi-class support vector machine using one-vs-one method
        MulticlassSupportVectorMachine <Linear> ovo = teacher.Learn(inputs, outputs);

        // Obtain class predictions for each sample
        int[] predicted = ovo.Decide(inputs);


        miText.text = "Input : " +
                      " {1, 2, 0 } \n" +
                      " {6, 2, 3 }\n" +
                      " {1, 1, 1 }\n" +
                      " { 7, 6, 2 }\n\n" +
                      "Predicted Guess: ";
        for (int i = 0; i < predicted.Length; i++)
        {
            Console.WriteLine(predicted[i]);
            miText.text += predicted [i];
        }
    }
        public void learn_test_wavelet()
        {
            #region doc_learn_wavelet
            // Generate always same random numbers
            Accord.Math.Random.Generator.Seed = 0;

            // The following is a simple auto association function in which
            // the last column of each input correspond to its own class. This
            // problem should be easily solved using a Linear kernel.

            // Sample input data
            int[][] inputs =
            {
                new int[] { 1, 2, 0 },
                new int[] { 6, 2, 3 },
                new int[] { 1, 1, 1 },
                new int[] { 7, 6, 2 },
            };

            // Output for each of the inputs
            int[] outputs = { 0, 3, 1, 2 };

            // Create the multi-class learning algorithm for the machine
            var teacher = new MulticlassSupportVectorLearning <Wavelet, int[]>()
            {
                // Configure the learning algorithm to use SMO to train the
                //  underlying SVMs in each of the binary class subproblems.
                Learner = (param) => new SequentialMinimalOptimization <Wavelet, int[]>()
                {
                    // If you would like to use other kernels, simply replace
                    // the generic parameter to the desired kernel class, such
                    // as for example, Wavelet:

                    Kernel = new Wavelet(invariant: true) // use the Wavelet kernel
                }
            };

            // Estimate the multi-class support vector machine using one-vs-one method
            var ovo = teacher.Learn(inputs, outputs);

            // Obtain class predictions for each sample
            int[] predicted = ovo.Decide(inputs);

            // Compute classification error
            double error = new ZeroOneLoss(outputs).Loss(predicted);
            #endregion

            Assert.AreEqual(0, error);
            Assert.IsTrue(predicted.IsEqual(outputs));
            var s0 = ovo.Scores(inputs[0]);
            var s1 = ovo.Scores(inputs[1]);
            var s2 = ovo.Scores(inputs[2]);
            Assert.IsTrue(s0.IsEqual(new double[] { 1, -1, -1, -1 }, 1e-2));
            Assert.IsTrue(s1.IsEqual(new double[] { -1, -1, -1, 1 }, 1e-2));
            Assert.IsTrue(s2.IsEqual(new double[] { -1.18, 1.18, -1, -1 }, 1e-2));
        }
Пример #30
0
        public void Setup()
        {
            ksvm = new MulticlassSupportVectorMachine <Polynomial>(
                inputs: 2, kernel: new Polynomial(2), classes: 10);

            smo = new MulticlassSupportVectorLearning <Polynomial>()
            {
                Model = ksvm
            };
        }