Ejemplo n.º 1
0
        public static void testSVMWrapperPackage() //rafal parsal
        {
            libSVM_Problem Problem = libSVM_Problem.Load("../../../LibSVMFull/testSvmWrapper/data/train.dat");

            GuiPreferences.Instance.setLog("trainnig data loaded");
            Problem.Save("../../../LibSVMFull/testSvmWrapper/data/train_saved");
            GuiPreferences.Instance.setLog("training data saved");
            libSVM_Extension svm       = new libSVM_Extension();
            libSVM_Parameter Parameter = new libSVM_Parameter();

            Parameter.svm_type    = SVM_TYPE.C_SVC;
            Parameter.kernel_type = KERNEL_TYPE.LINEAR;

            svm = new libSVM_Extension();
            svm.Train(Problem, Parameter);
            libSVM_Problem Test = libSVM_Problem.Load("../../../LibSVMFull/testSvmWrapper/data/test.dat");

            svm.GetAccuracyFromTest(Test);
            double accuracy = Preferences.Instance.svmWrapper.output.accuracy;

            GuiPreferences.Instance.setLog("Predicted accuracy from testing set: " + accuracy.ToString());
            svm.Dispose();


            svm = new libSVM_Extension();
            //svm.TrainAuto(10, Problem, Parameter, libSVM_Grid.C(), libSVM_Grid.gamma(), libSVM_Grid.p(), libSVM_Grid.nu(), libSVM_Grid.coef0(), libSVM_Grid.degree());
            libSVM_Grid grid = new libSVM_Grid();

            accuracy = svm.TrainAuto(10, Problem, Parameter, grid, null, null, null, null, null);
            GuiPreferences.Instance.setLog("Predicted accuracy from 10 cross fold validation: " + accuracy.ToString());
            svm.Save("../../../LibSVMFull/testSvmWrapper/data/model_file");
            GuiPreferences.Instance.setLog("10 cfv best model saved");
            svm.Dispose();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load problem from a file in libSVM format
        /// </summary>
        /// <param name="_filename">name of file</param>
        public static libSVM_Problem Load(string _filename)
        {
            libSVM_Problem problem = new libSVM_Problem();

            problem.Reload(_filename);

            return(problem);
        }
Ejemplo n.º 3
0
        public void TrainSplitExample()
        {
            Preferences.Instance.svmWrapper.Train(Preferences.Instance.ProblemFinal, Parameter);
            libSVM_Problem Test = libSVM_Problem.Load("../../../LibSVMFull/testSvmWrapper/data/test.dat");

            Preferences.Instance.svmWrapper.GetAccuracyFromTest(Test);
            double accuracy = Preferences.Instance.svmWrapper.output.accuracy;

            GuiPreferences.Instance.setLog("Predicted accuracy from testing set: " + accuracy.ToString());
            //disposeSVM();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Train model
        /// </summary>
        /// <param name="_problem">samples and labels</param>
        /// <param name="_parameter">model parameters</param>
        public void Train(libSVM_Problem _problem, libSVM_Parameter _parameter)
        {
            Dispose();

            __problem_ptr   = libSVM_Problem_to_svm_problem_ptr(_problem);
            __parameter_ptr = libSVM_Parameter_to_svm_parameter_ptr(_parameter);

            IntPtr error_ptr = svm_check_parameter(__problem_ptr, __parameter_ptr);

            if (error_ptr != IntPtr.Zero)
            {
                throw new Exception(ptr_to_string(error_ptr));
            }

            __model_ptr = svm_train(__problem_ptr, __parameter_ptr);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_problem"></param>
        /// <returns></returns>
        private IntPtr libSVM_Problem_to_svm_problem_ptr(libSVM_Problem _problem)
        {
            if (_problem == null)
            {
                throw new Exception("libSVMProblem not initialized");
            }

            if (_problem.samples == null)
            {
                throw new Exception("libSVMProblem.samples = null");
            }
            if (_problem.labels == null)
            {
                throw new Exception("libSVMProblem.labels = null");
            }
            if (_problem.samples.Length != _problem.labels.Length)
            {
                throw new Exception("libSVMProblem.samples.Length != libSVMProblem.labels.length");
            }

            IntPtr      problem_ptr = IntPtr.Zero;
            svm_problem problem     = new svm_problem();

            problem.l = _problem.samples.Length;
            problem.y = array_to_ptr(_problem.labels);
            problem.x = Alloc_ptr(Marshal.SizeOf(typeof(IntPtr)) * problem.l);

            IntPtr xptr  = problem.x;
            int    ixptr = xptr.ToInt32();

            for (int i = 0; i < problem.l; i++)
            {
                IntPtr svm_nodes = sample_to_svm_nodes_ptr(_problem.samples[i]);

                xptr = new IntPtr(ixptr);
                Marshal.StructureToPtr(svm_nodes, xptr, false);

                ixptr += Marshal.SizeOf(typeof(IntPtr));
            }

            problem_ptr = Alloc_ptr(Marshal.SizeOf(typeof(svm_problem)));
            Marshal.StructureToPtr(problem, problem_ptr, false);

            return(problem_ptr);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// wrapper function for GetAccuracy, the name was not logical and i have to maintain a subversion pristine copy.
        /// </summary>
        /// <param name="problem">problem</param>
        /// <returns>accuracy in %</returns>
        ///
        public double GetAccuracyFromTestSingleSample(libSVM_Problem problem, libSVM svmModel)
        {
            if (problem.samples == null || problem.samples.Length == 0)
            {
                throw new Exception("GetAccuracy no samples");
            }
            if (problem.labels == null || problem.labels.Length == 0)
            {
                throw new Exception("GetAccuracy no labels");
            }

            if (problem.samples.Length != problem.labels.Length)
            {
                throw new Exception("GetAccuracy labels.length!=samples.length");
            }

            output = new libSVM_output();

            int    correctPredictions = 0;
            double pred;

            int index = problem.samples.Length - 1;

            if (problem.samples[index] != null && problem.samples[index].Count > 0)
            {
                pred = svmModel.Predict(problem.samples[index]);
                return(pred);

                /*output.setValue(index, pred);
                 * if (pred == problem.labels[index])
                 *  correctPredictions++;*/
            }


            output.accuracy = correctPredictions / (double)problem.samples.Length * 100.0;
            return(1.0);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// GetAccuracy for problem, returning predicted labels.
        /// </summary>
        /// <param name="problem">problem</param>
        /// <returns>accuracy in %</returns>
        ///
        new public void GetAccuracy(libSVM_Problem problem)
        {
            if (problem.samples == null || problem.samples.Length == 0)
            {
                throw new Exception("GetAccuracy no samples");
            }
            if (problem.labels == null || problem.labels.Length == 0)
            {
                throw new Exception("GetAccuracy no labels");
            }

            if (problem.samples.Length != problem.labels.Length)
            {
                throw new Exception("GetAccuracy labels.length!=samples.length");
            }

            output = new libSVM_output();

            int    correctPredictions = 0;
            double pred;

            for (int i = 0; i < problem.samples.Length; i++)
            {
                if (problem.samples[i] != null && problem.samples[i].Count > 0)
                {
                    pred = Predict(problem.samples[i]);
                    output.setValue(i, pred);
                    if (pred == problem.labels[i])
                    {
                        correctPredictions++;
                    }
                }
            }

            output.accuracy = correctPredictions / (double)problem.samples.Length * 100.0;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Trains model automatically using users grids. if your svm_type/kernel_type does not need it then set it to null
        /// </summary>
        /// <param name="fold">folds for samples >=2 </param>
        /// <param name="problem">samples and labels</param>
        /// <param name="parameter">model parameters</param>
        /// <param name="grid_c">grid for C</param>
        /// <param name="grid_gamma">grid for gamma</param>
        /// <param name="grid_p">grid for p</param>
        /// <param name="grid_nu">grid for nu</param>
        /// <param name="grid_coef0">grid for coef</param>
        /// <param name="grid_degree">grid for degree</param>
        /// <returns>Total Accuracy in %</returns>
        public void TrainAutoBestTotal(int fold, libSVM_Problem problem, libSVM_Parameter parameter, libSVM_Grid grid_c, libSVM_Grid grid_gamma, libSVM_Grid grid_p, libSVM_Grid grid_nu, libSVM_Grid grid_coef0, libSVM_Grid grid_degree)
        {
            Dispose();

            if (problem == null)
            {
                throw new Exception("libSVMProblem not initialized");
            }
            if (problem.samples == null)
            {
                throw new Exception("libSVMProblem.samples = null");
            }
            if (problem.labels == null)
            {
                throw new Exception("libSVMProblem.labels = null");
            }
            if (problem.samples.Length != problem.labels.Length)
            {
                throw new Exception("libSVMProblem.samples.Length != libSVMProblem.labels.length");
            }
            if (parameter == null)
            {
                throw new Exception("libSVMParameter not initialized");
            }
            if (parameter.weight == null && parameter.weight_label != null)
            {
                throw new Exception("libSVMParameter.weight = null and libSVMParameter.weight_label != null");
            }
            if (parameter.weight != null && parameter.weight_label == null)
            {
                throw new Exception("libSVMParameter.weight_label = null and libSVMParameter.weight != null");
            }
            if (parameter.weight != null && parameter.weight_label != null && parameter.weight_label.Length != parameter.weight.Length)
            {
                throw new Exception("libSVMParameter.weight_label.Length != libSVMParameter.weight.Length");
            }
            if (fold < 2 || fold > problem.samples.Length)
            {
                throw new Exception("fold < 2 || fold > nr_samples");
            }

            libSVM_Grid _grid_gamma  = grid_gamma;
            libSVM_Grid _grid_coef0  = grid_coef0;
            libSVM_Grid _grid_degree = grid_degree;

            if (parameter.kernel_type == KERNEL_TYPE.LINEAR ||
                parameter.kernel_type == KERNEL_TYPE.PRECOMPUTED)
            {
                _grid_gamma  = new libSVM_Grid();
                _grid_coef0  = new libSVM_Grid();
                _grid_degree = new libSVM_Grid();
            }
            else if (parameter.kernel_type == KERNEL_TYPE.POLY)
            {
                if (_grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }
                if (_grid_coef0 == null)
                {
                    throw new Exception("grid_coef0 not set");
                }
                if (_grid_degree == null)
                {
                    throw new Exception("grid_degree not set");
                }
            }
            else if (parameter.kernel_type == KERNEL_TYPE.RBF)
            {
                if (_grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }

                _grid_coef0  = new libSVM_Grid();
                _grid_degree = new libSVM_Grid();
            }
            else if (parameter.kernel_type == KERNEL_TYPE.SIGMOID)
            {
                if (_grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }
                if (_grid_coef0 == null)
                {
                    throw new Exception("grid_coef not set");
                }
                _grid_degree = new libSVM_Grid();
            }
            else
            {
                throw new Exception("unknown kernel_type");
            }

            libSVM_Grid _grid_c  = grid_c;
            libSVM_Grid _grid_p  = grid_p;
            libSVM_Grid _grid_nu = grid_nu;

            if (parameter.svm_type == SVM_TYPE.C_SVC ||
                parameter.svm_type == SVM_TYPE.ONE_CLASS)
            {
                if (_grid_c == null)
                {
                    throw new Exception("grid_C not set");
                }

                _grid_nu = new libSVM_Grid();
                _grid_p  = new libSVM_Grid();
            }
            else if (parameter.svm_type == SVM_TYPE.NU_SVC || parameter.svm_type == SVM_TYPE.NU_SVR)
            {
                if (_grid_nu == null)
                {
                    throw new Exception("grid_nu not set");
                }

                _grid_c = new libSVM_Grid();
                _grid_p = new libSVM_Grid();
            }
            else if (parameter.svm_type == SVM_TYPE.EPSILON_SVR)
            {
                if (_grid_p == null)
                {
                    throw new Exception("grid_p not set");
                }

                _grid_c  = new libSVM_Grid();
                _grid_nu = new libSVM_Grid();
            }
            else
            {
                throw new Exception("unknown svm_type");
            }

            double c  = _grid_c.min;
            double p  = _grid_p.min;
            double nu = _grid_nu.min;

            double gamma  = _grid_gamma.min;
            double coef0  = _grid_coef0.min;
            double degree = _grid_degree.min;
            int    f      = 0;

            int test_probe   = 0;
            int test_samples = 0;
            //ori
            int total_test_probe             = 0;
            int total_test_samples           = 0;
            libSVM_Parameter train_parameter = new libSVM_Parameter();

            //copy parameters set by user;
            train_parameter.weight       = parameter.weight;
            train_parameter.weight_label = parameter.weight_label;
            train_parameter.shrinking    = parameter.shrinking;
            train_parameter.svm_type     = parameter.svm_type;
            train_parameter.kernel_type  = parameter.kernel_type;
            train_parameter.probability  = parameter.probability;
            train_parameter.cache_size   = parameter.cache_size;

            libSVM_Problem train_problem = new libSVM_Problem();
            libSVM_Problem test_problem  = new libSVM_Problem();

            //ori
            libSVM_output this_output = new libSVM_output();

            //fold
            do
            {
                train_problem.labels  = new double[1];
                train_problem.samples = new SortedDictionary <int, double> [1];

                test_problem.labels  = new double[1];
                test_problem.samples = new SortedDictionary <int, double> [1];

                int j = 0;
                int k = 0;
                for (int i = 0; i < problem.samples.Length; i++)
                {
                    if ((i + 1 + f) % fold == 0)
                    {
                        Array.Resize(ref test_problem.labels, j + 1);
                        Array.Resize(ref test_problem.samples, j + 1);
                        this_output.setValue(i, 999);

                        test_problem.labels[j]  = problem.labels[i];
                        test_problem.samples[j] = problem.samples[i];
                        j++;
                    }
                    else
                    {
                        Array.Resize(ref train_problem.labels, k + 1);
                        Array.Resize(ref train_problem.samples, k + 1);

                        train_problem.labels[k]  = problem.labels[i];
                        train_problem.samples[k] = problem.samples[i];
                        k++;
                    }
                }

                //p
                p = _grid_p.min;
                do
                {
                    //nu
                    nu = _grid_nu.min;
                    do
                    {
                        //gamma
                        gamma = _grid_gamma.min;
                        do
                        {
                            //coef0
                            coef0 = _grid_coef0.min;
                            do
                            {
                                //degree

                                degree = _grid_degree.min;
                                do
                                {
                                    //c

                                    c = _grid_c.min;
                                    do
                                    {
                                        //svm_train alters problem_ptr so it's necessary to create it every time;
                                        IntPtr this_problem_ptr = libSVM_Problem_to_svm_problem_ptr(train_problem);

                                        //set generated parameters
                                        train_parameter.C      = c;
                                        train_parameter.p      = p;
                                        train_parameter.nu     = nu;
                                        train_parameter.gamma  = gamma;
                                        train_parameter.degree = (int)degree;
                                        train_parameter.coef0  = coef0;

                                        IntPtr this_parameter_ptr = libSVM_Parameter_to_svm_parameter_ptr(train_parameter);

                                        IntPtr error_ptr = svm_check_parameter(this_problem_ptr, this_parameter_ptr);

                                        if (error_ptr != IntPtr.Zero)
                                        {
                                            throw new Exception(ptr_to_string(error_ptr));
                                        }

                                        IntPtr this_model_ptr = svm_train(this_problem_ptr, this_parameter_ptr);

                                        int this_test_probe = 0;

                                        //count propperly recognized test_problem.samples
                                        for (int i = 0; i < test_problem.labels.Length; i++)
                                        {
                                            IntPtr svm_nodes_ptr = sample_to_svm_nodes_ptr(test_problem.samples[i]);

                                            //ori

                                            double testPrediction = svm_predict(this_model_ptr, svm_nodes_ptr);

                                            this_output.setValue(this_output.getKeyByIndex(i), testPrediction);
                                            total_test_samples++; // can also be problem.labels.length
                                            if (test_problem.labels[i] == testPrediction)
                                            {
                                                this_test_probe++;
                                                total_test_probe++;
                                            }

                                            Free_ptr(ref svm_nodes_ptr);
                                        }

                                        //if first run then just copy this
                                        if (_model_ptr == IntPtr.Zero)
                                        {
                                            _model_ptr     = this_model_ptr;
                                            _parameter_ptr = this_parameter_ptr;
                                            _problem_ptr   = this_problem_ptr;

                                            test_probe   = this_test_probe;
                                            test_samples = test_problem.samples.Length;

                                            //ori
                                            output = this_output;

                                            //!-uncommenting this gets mapping of predicted labels only for best model
                                            this_output = new libSVM_output();

                                            //!-uncommenting this gets only best model and not runs on all 10 folds (unless best is 10th)
                                            //exit when no better solution could be found
                                            if (test_probe == test_problem.samples.Length)
                                            {
                                                goto leave;
                                            }
                                        }
                                        else
                                        //if model was better than previous then free previous data and copy this
                                        if (this_test_probe > test_probe)
                                        {
                                            Free_svm_parameter_ptr(ref _parameter_ptr);
                                            Free_svm_problem_ptr(ref _problem_ptr);
                                            svm_free_and_destroy_model(ref _model_ptr);

                                            _parameter_ptr = this_parameter_ptr;
                                            _problem_ptr   = this_problem_ptr;
                                            _model_ptr     = this_model_ptr;

                                            test_probe   = this_test_probe;
                                            test_samples = test_problem.samples.Length;

                                            //ori
                                            output = this_output;

                                            //!-uncommenting this gets mapping of predicted labels only for best model
                                            this_output = new libSVM_output();

                                            //!-uncommenting this gets only best model and not runs on all 10 folds (unless best is 10th)
                                            //exit when no better solution could be found;
                                            if (test_probe == test_problem.samples.Length)
                                            {
                                                goto leave;
                                            }
                                        }
                                        //if not then free this model
                                        else
                                        {
                                            Free_svm_problem_ptr(ref this_problem_ptr);
                                            Free_svm_parameter_ptr(ref this_parameter_ptr);
                                            svm_free_and_destroy_model(ref this_model_ptr);

                                            //!-uncommenting this gets mapping of predicted labels only for best model
                                            this_output = new libSVM_output();
                                        }

                                        c *= _grid_c.step;
                                    } while (c < _grid_c.max);

                                    degree *= _grid_degree.step;
                                } while (degree < _grid_degree.max);

                                coef0 *= _grid_coef0.step;
                            } while (coef0 < _grid_coef0.max);

                            gamma *= _grid_gamma.step;
                        } while (gamma < _grid_gamma.max);

                        nu *= _grid_nu.step;
                    } while (nu < _grid_nu.max);

                    p *= _grid_p.step;
                } while (p < _grid_p.max);

                f++;
            } while (f < fold);

leave:
            //return new double []{100.0 * test_probe / (double) test_samples,100.0 * total_test_probe / (double)total_test_samples};
            output.accuracy = 100.0 * test_probe / (double)test_samples;
            return;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// wrapper function for GetAccuracy, the name was not logical and i have to maintain a subversion pristine copy.
 /// </summary>
 /// <param name="problem">problem</param>
 /// <returns>accuracy in %</returns>
 ///
 public void GetAccuracyFromTest(libSVM_Problem problem)
 {
     GetAccuracy(problem);
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Trains model automatically using users grids. if your svm_type/kernel_type does not need it then set it to null
        /// </summary>
        /// <param name="_fold">folds for samples >=2 </param>
        /// <param name="_problem">samples and labels</param>
        /// <param name="_parameter">model parameters</param>
        /// <param name="_grid_c">grid for C</param>
        /// <param name="_grid_gamma">grid for gamma</param>
        /// <param name="_grid_p">grid for p</param>
        /// <param name="_grid_nu">grid for nu</param>
        /// <param name="_grid_coef0">grid for coef</param>
        /// <param name="_grid_degree">grid for degree</param>
        public void TrainAuto(int _fold, libSVM_Problem _problem, libSVM_Parameter _parameter, libSVM_Grid _grid_c, libSVM_Grid _grid_gamma, libSVM_Grid _grid_p, libSVM_Grid _grid_nu, libSVM_Grid _grid_coef0, libSVM_Grid _grid_degree)
        {
            Dispose();

            if (_problem == null)
            {
                throw new Exception("libSVMProblem not initialized");
            }
            if (_problem.samples == null)
            {
                throw new Exception("libSVMProblem.samples = null");
            }
            if (_problem.labels == null)
            {
                throw new Exception("libSVMProblem.labels = null");
            }
            if (_problem.samples.Length != _problem.labels.Length)
            {
                throw new Exception("libSVMProblem.samples.Length != libSVMProblem.labels.length");
            }
            if (_parameter == null)
            {
                throw new Exception("libSVMParameter not initialized");
            }
            if (_parameter.weight == null && _parameter.weight_label != null)
            {
                throw new Exception("libSVMParameter.weight = null and libSVMParameter.weight_label != null");
            }
            if (_parameter.weight != null && _parameter.weight_label == null)
            {
                throw new Exception("libSVMParameter.weight_label = null and libSVMParameter.weight != null");
            }
            if (_parameter.weight != null && _parameter.weight_label != null && _parameter.weight_label.Length != _parameter.weight.Length)
            {
                throw new Exception("libSVMParameter.weight_label.Length != libSVMParameter.weight.Length");
            }
            if (_fold < 2 || _fold > _problem.samples.Length)
            {
                throw new Exception("fold < 2 || fold > nr_samples");
            }

            libSVM_Grid grid_gamma  = _grid_gamma;
            libSVM_Grid grid_coef0  = _grid_coef0;
            libSVM_Grid grid_degree = _grid_degree;

            if (_parameter.kernel_type == KERNEL_TYPE.LINEAR ||
                _parameter.kernel_type == KERNEL_TYPE.PRECOMPUTED)
            {
                grid_gamma  = new libSVM_Grid();
                grid_coef0  = new libSVM_Grid();
                grid_degree = new libSVM_Grid();
            }
            else if (_parameter.kernel_type == KERNEL_TYPE.POLY)
            {
                if (grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }
                if (grid_coef0 == null)
                {
                    throw new Exception("grid_coef0 not set");
                }
                if (grid_degree == null)
                {
                    throw new Exception("grid_degree not set");
                }
            }
            else if (_parameter.kernel_type == KERNEL_TYPE.RBF)
            {
                if (grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }

                grid_coef0  = new libSVM_Grid();
                grid_degree = new libSVM_Grid();
            }
            else if (_parameter.kernel_type == KERNEL_TYPE.SIGMOID)
            {
                if (grid_gamma == null)
                {
                    throw new Exception("grid_gamma not set");
                }
                if (grid_coef0 == null)
                {
                    throw new Exception("grid_coef not set");
                }
                grid_degree = new libSVM_Grid();
            }
            else
            {
                throw new Exception("unknown kernel_type");
            }

            libSVM_Grid grid_c  = _grid_c;
            libSVM_Grid grid_p  = _grid_p;
            libSVM_Grid grid_nu = _grid_nu;

            if (_parameter.svm_type == SVM_TYPE.C_SVC ||
                _parameter.svm_type == SVM_TYPE.ONE_CLASS)
            {
                if (grid_c == null)
                {
                    throw new Exception("grid_C not set");
                }

                grid_nu = new libSVM_Grid();
                grid_p  = new libSVM_Grid();
            }
            else if (_parameter.svm_type == SVM_TYPE.NU_SVC || _parameter.svm_type == SVM_TYPE.NU_SVR)
            {
                if (grid_nu == null)
                {
                    throw new Exception("grid_nu not set");
                }

                grid_c = new libSVM_Grid();
                grid_p = new libSVM_Grid();
            }
            else if (_parameter.svm_type == SVM_TYPE.EPSILON_SVR)
            {
                if (grid_p == null)
                {
                    throw new Exception("grid_p not set");
                }

                grid_c  = new libSVM_Grid();
                grid_nu = new libSVM_Grid();
            }
            else
            {
                throw new Exception("unknown svm_type");
            }

            double c  = grid_c.min;
            double p  = grid_p.min;
            double nu = grid_nu.min;

            double gamma  = grid_gamma.min;
            double coef0  = grid_coef0.min;
            double degree = grid_degree.min;
            int    f      = 0;

            int nr_test_samples  = _problem.labels.Length / (int)_fold;
            int nr_train_samples = _problem.labels.Length - nr_test_samples;

            double test_probe = 0.0;

            libSVM_Parameter train_parameter = new libSVM_Parameter();

            //copy parameters set by user;
            train_parameter.weight       = _parameter.weight;
            train_parameter.weight_label = _parameter.weight_label;
            train_parameter.shrinking    = _parameter.shrinking;
            train_parameter.svm_type     = _parameter.svm_type;
            train_parameter.kernel_type  = _parameter.kernel_type;
            train_parameter.probability  = _parameter.probability;
            train_parameter.cache_size   = _parameter.cache_size;

            libSVM_Problem train_problem = new libSVM_Problem();
            libSVM_Problem test_problem  = new libSVM_Problem();

            //fold
            do
            {
                train_problem.labels  = new double[nr_train_samples];
                train_problem.samples = new double[nr_train_samples][];

                test_problem.labels  = new double[nr_test_samples];
                test_problem.samples = new double[nr_test_samples][];

                int j = 0;
                int k = 0;
                for (int i = 0; i < nr_train_samples + nr_test_samples; i++)
                {
                    if (i % _fold == 0)
                    {
                        test_problem.labels[j]  = _problem.labels[i];
                        test_problem.samples[j] = _problem.samples[i];
                        j++;
                    }
                    else
                    {
                        train_problem.labels[k]  = _problem.labels[i];
                        train_problem.samples[k] = _problem.samples[i];
                    }
                }

                //p
                p = grid_p.min;
                do
                {
                    //nu
                    nu = grid_nu.min;
                    do
                    {
                        //gamma
                        gamma = grid_gamma.min;
                        do
                        {
                            //coef0
                            coef0 = grid_coef0.min;
                            do
                            {
                                //degree

                                degree = grid_degree.min;
                                do
                                {
                                    //c

                                    c = grid_c.min;
                                    do
                                    {
                                        //svm_train alters problem_ptr so it's necessary to create it every time;
                                        IntPtr this_problem_ptr = libSVM_Problem_to_svm_problem_ptr(train_problem);

                                        //set generated parameters
                                        train_parameter.C      = c;
                                        train_parameter.p      = p;
                                        train_parameter.nu     = nu;
                                        train_parameter.gamma  = gamma;
                                        train_parameter.degree = (int)degree;
                                        train_parameter.coef0  = coef0;

                                        IntPtr this_parameter_ptr = libSVM_Parameter_to_svm_parameter_ptr(train_parameter);

                                        IntPtr error_ptr = svm_check_parameter(__problem_ptr, __parameter_ptr);

                                        if (error_ptr != IntPtr.Zero)
                                        {
                                            throw new Exception(ptr_to_string(error_ptr));
                                        }

                                        IntPtr this_model_ptr = svm_train(this_problem_ptr, this_parameter_ptr);

                                        double this_test_probe = 0.0;

                                        //count propperly recognized test_problem.samples
                                        for (int i = 0; i < test_problem.labels.Length; i++)
                                        {
                                            IntPtr svm_nodes_ptr = sample_to_svm_nodes_ptr(test_problem.samples[i]);

                                            if (test_problem.labels[i] == svm_predict(__model_ptr, svm_nodes_ptr))
                                            {
                                                this_test_probe++;
                                            }

                                            Free_ptr(ref svm_nodes_ptr);
                                        }

                                        //if first run then just copy this
                                        if (__model_ptr == IntPtr.Zero)
                                        {
                                            __model_ptr     = this_model_ptr;
                                            __parameter_ptr = this_parameter_ptr;
                                            __problem_ptr   = this_problem_ptr;

                                            test_probe = this_test_probe;
                                        }
                                        else
                                        //if model was better than previous then free previous data and copy this
                                        if (this_test_probe > test_probe)
                                        {
                                            Free_svm_parameter_ptr(ref __parameter_ptr);
                                            Free_svm_problem_ptr(ref __problem_ptr);
                                            svm_free_and_destroy_model(ref __model_ptr);

                                            __parameter_ptr = this_parameter_ptr;
                                            __problem_ptr   = this_problem_ptr;
                                            __model_ptr     = this_model_ptr;

                                            test_probe = this_test_probe;
                                        }
                                        //if not then free this model
                                        else
                                        {
                                            Free_svm_problem_ptr(ref this_problem_ptr);
                                            Free_svm_parameter_ptr(ref this_parameter_ptr);
                                            svm_free_and_destroy_model(ref this_model_ptr);
                                        }

                                        c *= grid_c.step;
                                    }while(c < grid_c.max);

                                    degree *= grid_degree.step;
                                }while(degree < grid_degree.max);

                                coef0 *= grid_coef0.step;
                            }while(coef0 < grid_coef0.max);

                            gamma *= grid_degree.step;
                        }while(gamma < grid_gamma.max);

                        nu *= grid_nu.step;
                    }while(nu < grid_nu.max);

                    p *= grid_p.step;
                }while(p < grid_p.max);

                f++;
            }while(f < _fold);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Trains model automatically, using default Grids for parameter value search
 /// Similar to OpenCV
 /// </summary>
 /// <param name="_fold">folds for samples >=2 </param>
 /// <param name="_problem">samples and labels</param>
 /// <param name="_parameter">model parameters</param>
 public void TrainAuto(int _fold, libSVM_Problem _problem, libSVM_Parameter _parameter)
 {
     TrainAuto(_fold, _problem, _parameter, libSVM_Grid.C(), libSVM_Grid.gamma(), libSVM_Grid.p(), libSVM_Grid.nu(), libSVM_Grid.coef(), libSVM_Grid.degree());
 }