Exemplo n.º 1
0
        /// <summary>
        /// Creates Polinominal Kernel,
        /// </summary>
        /// <param name="vectors">array of vectors</param>
        /// <param name="degree">polinominal Degree</param>
        /// <param name="coef">coeficient</param>
        /// <param name="gamma">Gamma</param>
        public PolinominalKernel(double degree, double coef, double gamma)
        {
            Degree = degree;
            Coef   = coef;
            Gamma  = gamma;

            linKernel = new LinearKernel();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RbfKernel"/> class.
 /// </summary>
 /// <param name="gamma">The gamma parameter .</param>
 public RbfKernel(float gamma)
 {
     Gamma     = gamma;
     linKernel = new LinearKernel();
 }
Exemplo n.º 3
0
        public override void SearchParams(Problem <SparseVec> problem,
                                          out float C,
                                          out IKernel <SparseVec> kernel)
        {
            //   throw new NotImplementedException();
            Debug.WriteLine("Starting Parameter Selection for Linear kernel");

            //create range for penalty parameter C
            IList <double> rangeC = PowerRange(MinCPower, MaxCPower, PowerBase, PowerStep);

            double crossValidation = double.MinValue;
            double maxC            = double.MinValue;

            //we can set this kernel, because we only looking for one parameter C
            LinearKernel bestKernel = new LinearKernel();
            object       lockObj    = new object();


            List <SparseVec>[] foldsElements;
            List <float>[]     foldsLabels;
            Validation <SparseVec> .MakeFoldsSplit(problem, NrFolds, out foldsElements, out foldsLabels);

            Parallel.ForEach(rangeC, paramC =>
            {
                //do cross validation
                Stopwatch timer = Stopwatch.StartNew();

                Validation <SparseVec> valid = new Validation <SparseVec>();
                valid.Evaluator       = new DualEvaluator <SparseVec>();
                valid.TrainingProblem = problem;

                //but here kernel should be different because
                // in CSVM.Init we set the problem to kernel
                valid.Kernel = new LinearKernel();
                valid.C      = (float)paramC;

                double acc = valid.CrossValidateOnFolds(problem.ElementsCount, foldsElements, foldsLabels);

                //old code
                //var tmpKernel = new LinearKernel();

                //double acc = Validation.CrossValidateOnFolds(problem.ElementsCount,
                //                                             foldsElements,
                //                                             foldsLabels, tmpKernel,
                //                                             (float)paramC);

                lock (lockObj)
                {
                    if (acc > crossValidation)
                    {
                        crossValidation = acc;
                        maxC            = paramC;
                        // bestKernel = tmpKernel;
                    }
                }

                Debug.WriteLine(
                    string.Format(
                        "CrossValidation time={0},C={1}->{2:0.#####}",
                        timer.Elapsed, paramC, acc));
            });

            C      = (float)maxC;
            kernel = bestKernel;


            Debug.WriteLine("\n");
            Debug.WriteLine("-------------- Grid Search summary ------------");
            Debug.WriteLine(string.Format("Max accuracy={0} c={1} ", crossValidation, C));
        }