/// <summary>
        ///   Converts <see cref="SupportVectorMachineLearningConfigurationFunction"/>
        ///   into a lambda function that can be passed to the <see cref=" OneVsOneLearning{TInput, TBinary, TModel}.Learner"/>
        ///   property of a <see cref="MulticlassSupportVectorLearning"/> learning algorithm.
        /// </summary>
        ///
        public Func <InnerParameters, InnerLearning> Convert(SupportVectorMachineLearningConfigurationFunction conf)
        {
            return(delegate(InnerParameters parameters)
            {
                int[] y = Classes.ToMinusOnePlusOne(parameters.Outputs);
                var machine = (KernelSupportVectorMachine)parameters.Model;
                ISupportVectorMachineLearning r = conf(machine, parameters.Inputs, y, parameters.Pair.Class1, parameters.Pair.Class2);

                var c = r as ISupervisedLearning <SupportVectorMachine <IKernel <double[]> >, double[], bool>;
                if (c != null)
                {
                    return c;
                }


                // TODO: The following checks exist only to provide support to previous way of using
                // the library and should be removed after a few releases.
                var svc = r as ISupportVectorMachineLearning;
                if (svc != null)
                {
                    svc.Run();
                    return null;
                }

                throw new Exception();
            });
        }
Ejemplo n.º 2
0
        public double TrainClassifierWithParameters(
            ClassificationData trainingData,
            IKernel kernel = null,
            SupportVectorMachineLearningConfigurationFunction algorithm = null)
        {
            double classifierError = 0;

            // Use default parameters if no valid new
            // values are provided.
            if (kernel == null)
            {
                kernel = new Linear();
            }
            if (algorithm == null)
            {
                algorithm = (SVM, inputData, outputData, i, j) =>
                            new SequentialMinimalOptimization(SVM, inputData, outputData)
                {
                    // Avoid OutOfMemory exception.
                    CacheSize = 1000
                }
            }
            ;

            // Create a new SVM classifier.
            SVMachine = new MulticlassSupportVectorMachine(
                trainingData.InputAttributeNumber,
                kernel,
                trainingData.OutputPossibleValues);

            // Create an algorithm to be learned by the SVM.
            SVMachineLearning = new MulticlassSupportVectorLearning(
                SVMachine,
                trainingData.InputData,
                trainingData.OutputData);
            SVMachineLearning.Algorithm = algorithm;

            // Run the learning algorithm.
            classifierError = SVMachineLearning.Run(true);

            return(classifierError);
        }