Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/07/09 11:58:21 (15 years ago)
Author:
gkronber
Message:

Updated LibSVM project to latest version. #774

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/LibSVM/Training.cs

    r1819 r2415  
    2323namespace SVM
    2424{
    25     /// <remarks>
     25    /// <summary>
    2626    /// Class containing the routines to train SVM models.
    27     /// </remarks>
     27    /// </summary>
    2828    public static class Training
    2929    {
     30        /// <summary>
     31        /// Whether the system will output information to the console during the training process.
     32        /// </summary>
     33        public static bool IsVerbose
     34        {
     35            get
     36            {
     37                return Procedures.IsVerbose;
     38            }
     39            set
     40            {
     41                Procedures.IsVerbose = value;
     42            }
     43        }
     44
    3045        private static double doCrossValidation(Problem problem, Parameter parameters, int nr_fold)
    3146        {
    3247            int i;
    3348            double[] target = new double[problem.Count];
    34             Dictionary<int, double>[] confidence = new Dictionary<int, double>[problem.Count];
    35             Procedures.svm_cross_validation(problem, parameters, nr_fold, target, confidence);
    36             if (parameters.Probability)
    37             {
    38                 List<RankPair> ranks = new List<RankPair>();
    39                 for (i = 0; i < target.Length; i++)
    40                     ranks.Add(new RankPair(confidence[i][1], problem.Y[i]));
    41                 PerformanceEvaluator eval = new PerformanceEvaluator(ranks);
    42                 return eval.AuC*eval.AP;
     49            Procedures.svm_cross_validation(problem, parameters, nr_fold, target);
     50            int total_correct = 0;
     51            double total_error = 0;
     52            double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
     53            if (parameters.SvmType == SvmType.EPSILON_SVR || parameters.SvmType == SvmType.NU_SVR)
     54            {
     55                for (i = 0; i < problem.Count; i++)
     56                {
     57                    double y = problem.Y[i];
     58                    double v = target[i];
     59                    total_error += (v - y) * (v - y);
     60                    sumv += v;
     61                    sumy += y;
     62                    sumvv += v * v;
     63                    sumyy += y * y;
     64                    sumvy += v * y;
     65                }
     66                return(problem.Count * sumvy - sumv * sumy) / (Math.Sqrt(problem.Count * sumvv - sumv * sumv) * Math.Sqrt(problem.Count * sumyy - sumy * sumy));
    4367            }
    4468            else
    45             {
    46                 int total_correct = 0;
    47                 double total_error = 0;
    48                 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
    49                 if (parameters.SvmType == SvmType.EPSILON_SVR || parameters.SvmType == SvmType.NU_SVR)
    50                 {
    51                     for (i = 0; i < problem.Count; i++)
    52                     {
    53                         double y = problem.Y[i];
    54                         double v = target[i];
    55                         total_error += (v - y) * (v - y);
    56                         sumv += v;
    57                         sumy += y;
    58                         sumvv += v * v;
    59                         sumyy += y * y;
    60                         sumvy += v * y;
    61                     }
    62                 }
    63                 else
    64                     for (i = 0; i < problem.Count; i++)
    65                         if (target[i] == problem.Y[i])
    66                             ++total_correct;
    67                 return (double)total_correct / problem.Count;
    68             }
    69 
     69                for (i = 0; i < problem.Count; i++)
     70                    if (target[i] == problem.Y[i])
     71                        ++total_correct;
     72            return (double)total_correct / problem.Count;
    7073        }
    7174        /// <summary>
     
    9699        public static double PerformCrossValidation(Problem problem, Parameter parameters, int nrfold)
    97100        {
    98             Procedures.svm_check_parameter(problem, parameters);
    99             return doCrossValidation(problem, parameters, nrfold);
     101            string error = Procedures.svm_check_parameter(problem, parameters);
     102            if (error == null)
     103                return doCrossValidation(problem, parameters, nrfold);
     104            else throw new Exception(error);
    100105        }
    101106
     
    108113        public static Model Train(Problem problem, Parameter parameters)
    109114        {
    110             Procedures.svm_check_parameter(problem, parameters);
    111 
    112             return Procedures.svm_train(problem, parameters);
     115            string error = Procedures.svm_check_parameter(problem, parameters);
     116
     117            if (error == null)
     118                return Procedures.svm_train(problem, parameters);
     119            else throw new Exception(error);
    113120        }
    114121
     
    190197
    191198                    case 'w':
    192                         ++parameters.WeightCount;
    193                         {
    194                             int[] old = parameters.WeightLabels;
    195                             parameters.WeightLabels = new int[parameters.WeightCount];
    196                             Array.Copy(old, 0, parameters.WeightLabels, 0, parameters.WeightCount - 1);
    197                         }
    198                         {
    199                             double[] old = parameters.Weights;
    200                             parameters.Weights = new double[parameters.WeightCount];
    201                             Array.Copy(old, 0, parameters.Weights, 0, parameters.WeightCount - 1);
    202                         }
    203 
    204                         parameters.WeightLabels[parameters.WeightCount - 1] = int.Parse(args[i - 1].Substring(2));
    205                         parameters.Weights[parameters.WeightCount - 1] = double.Parse(args[i]);
     199                        parameters.Weights[int.Parse(args[i - 1].Substring(2))] = double.Parse(args[1]);
    206200                        break;
    207201
Note: See TracChangeset for help on using the changeset viewer.