Пример #1
0
        public static void Run_Classify()
        {
            List <CDataRecord> records = LoadSample();

            DataTransformer <CDataRecord> dt = new DataTransformer <CDataRecord>();

            dt.DoFeaturesScaling(records);

            KernelSVM <CDataRecord> algorithm = new KernelSVM <CDataRecord>();

            algorithm.C = 20;
            ((GaussianKernel)algorithm.Kernel).Sigma = 0.01;

            algorithm.Train(records);

            Console.WriteLine("SVM(Gaussian Kernel) Model Built!");

            for (int i = 0; i < records.Count; i++)
            {
                CDataRecord rec = records[i] as CDataRecord;
                Console.WriteLine("rec: ");
                for (int j = 0; j < rec.Dimension; ++j)
                {
                    Console.WriteLine("X[" + j + "] = " + "[" + rec[j] + "] ");
                }
                Console.WriteLine("Label: " + "[" + rec.Label + "] ");
                Console.WriteLine("Predicted Label: " + algorithm.Predict(records[i]));
            }
        }
Пример #2
0
        public static void Run_Rank()
        {
            List <CDataRecord> records = LoadSample();

            DataTransformer <CDataRecord> dt = new DataTransformer <CDataRecord>();

            dt.DoFeaturesScaling(records);

            KernelSVM <CDataRecord> algorithm = new KernelSVM <CDataRecord>();

            algorithm.C = 20;                                //large value, high bias
            ((GaussianKernel)algorithm.Kernel).Sigma = 0.01; //low value, high bias

            algorithm.Train(records);

            Console.WriteLine("SVM (Gaussian Kernel) Model Built!");

            for (int i = 0; i < records.Count; i++)
            {
                CDataRecord rec = records[i] as CDataRecord;
                Console.WriteLine("rec: ");
                for (int j = 0; j < rec.Dimension; ++j)
                {
                    Console.WriteLine("X[" + j + " = " + rec[j] + "]");
                }
                Console.WriteLine("Label: " + rec.Label);

                List <KeyValuePair <string, double> > ranks = algorithm.Rank(records, rec);
                for (int k = 0; k < ranks.Count; ++k)
                {
                    Console.WriteLine("{0}: score = {1}", ranks[k].Key, ranks[k].Value);
                }
            }
        }
Пример #3
0
        public FrmSVM()
        {
            InitializeComponent();

            mSolverCommands.Add(btnSolver1);
            mSolverCommands.Add(btnSolver2);
            mSolverCommands.Add(btnSolver3);
            mSolverCommands.Add(btnSolver4);
            mSolverCommands.Add(btnSolver5);
            mSolverCommands.Add(btnSolver6);
            mSolverCommands.Add(btnSolver7);
            mSolverCommands.Add(btnSolver8);
            mSolverCommands.Add(btnSolver9);
            mSolverCommands.Add(btnSolver10);

            LinearSVM <CDataRecord> linear_svm = new LinearSVM <CDataRecord>();

            linear_svm.MaxSolverIteration = 100;
            linear_svm.Stepped           += (solution, k) =>
            {
                mTrainWorker.ReportProgress(k * 100 / linear_svm.MaxSolverIteration);
            };
            mSolverCommands[0].Text = "SVM (Linear Kernel)";
            mSolverCommands[0].Tag  = linear_svm;

            KernelSVM <CDataRecord> rbf_svm = new KernelSVM <CDataRecord>();

            rbf_svm.C = 20;
            rbf_svm.MaxSolverIteration             = 1000;
            ((GaussianKernel)rbf_svm.Kernel).Sigma = 0.01;
            rbf_svm.Stepped += (solution, k) =>
            {
                mTrainWorker.ReportProgress(k * 100 / rbf_svm.MaxSolverIteration);
            };

            mSolverCommands[1].Text = "SVM (RBF Kernel)";
            mSolverCommands[1].Tag  = rbf_svm;

            for (int i = 0; i < 2; ++i)
            {
                mSolverCommands[i].Click += (s1, e1) =>
                {
                    if (mTrainWorker != null && mTrainWorker.IsBusy)
                    {
                        MessageBox.Show("Training in process, please try again later");
                        return;
                    }

                    Button cmd = s1 as Button;
                    Classifier <CDataRecord, double> solver = cmd.Tag as Classifier <CDataRecord, double>;

                    double training_cost = -1, cv_cost = -1, testing_cost = -1;

                    DateTime curr_tick = DateTime.Now;
                    DateTime real_tick = curr_tick;

                    mTrainWorker = new BackgroundWorker();
                    mTrainWorker.WorkerSupportsCancellation = true;
                    mTrainWorker.WorkerReportsProgress      = true;
                    mTrainWorker.DoWork += (s2, e2) =>
                    {
                        List <CDataRecord> training_set = LoadDataSet(DataSetTypes.Training);
                        List <CDataRecord> cv_set       = LoadDataSet(DataSetTypes.CrossValidation);
                        List <CDataRecord> test_set     = LoadDataSet(DataSetTypes.Testing);
                        solver.Train(training_set);
                        solver.Predict(training_set);
                        solver.Predict(cv_set);
                        solver.Predict(test_set);

                        training_cost = solver.ComputeCost(training_set);
                        cv_cost       = solver.ComputeCost(cv_set);
                        testing_cost  = solver.ComputeCost(test_set);
                    };
                    mTrainWorker.RunWorkerCompleted += (s2, e2) =>
                    {
                        lblTrainProgress.Text = "...";
                        UpdateTabularDataView();
                        lblInformationStatus.Text = string.Format("Cost (Training): {0:0.00} Cost (CrossValidation): {1:0.00} Cost (Testing): {2:0.00}",
                                                                  training_cost, cv_cost, testing_cost);
                    };
                    mTrainWorker.ProgressChanged += (s2, e2) =>
                    {
                        real_tick = DateTime.Now;
                        TimeSpan ts = real_tick - curr_tick;
                        if (ts.TotalMilliseconds > 500)
                        {
                            curr_tick             = real_tick;
                            lblTrainProgress.Text = string.Format("{0}%", e2.ProgressPercentage);
                        }
                    };

                    mTrainWorker.RunWorkerAsync();
                };
            }

            for (int i = 2; i < mSolverCommands.Count; ++i)
            {
                mSolverCommands[i].Visible = false;
            }
        }