Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2550


Ignore:
Timestamp:
12/10/09 15:57:14 (15 years ago)
Author:
gkronber
Message:

Implemented #812 (Static methods for SVM operators).

Location:
trunk/sources/HeuristicLab.SupportVectorMachines/3.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/PredictorBuilder.cs

    r2440 r2550  
    6161      double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    6262
    63      
     63
    6464      ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true);
    6565      var inputVariableNames = from x in inputVariables
    6666                               select ((StringData)x).Data;
    6767
    68       double mean = ds.GetMean(targetVariable, start, end);
    69       double range = ds.GetRange(targetVariable, start, end);
    70 
    71       Predictor predictor = new Predictor(model, targetVariable, inputVariableNames, minTimeOffset, maxTimeOffset);
    72       predictor.LowerPredictionLimit = mean - punishmentFactor * range;
    73       predictor.UpperPredictionLimit = mean + punishmentFactor * range;
     68      var predictor = CreatePredictor(model, ds, targetVariable, inputVariableNames, punishmentFactor, start, end, minTimeOffset, maxTimeOffset);
    7469      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Predictor"), predictor));
    7570      return null;
    7671    }
     72
     73    public static Predictor CreatePredictor(SVMModel model, Dataset ds, string targetVariable, IEnumerable<string> inputVariables, double punishmentFactor,
     74      int start, int end, int minTimeOffset, int maxTimeOffset) {
     75      Predictor predictor = new Predictor(model, targetVariable, inputVariables, minTimeOffset, maxTimeOffset);
     76      double mean = ds.GetMean(targetVariable, start, end);
     77      double range = ds.GetRange(targetVariable, start, end);
     78      predictor.LowerPredictionLimit = mean - punishmentFactor * range;
     79      predictor.UpperPredictionLimit = mean + punishmentFactor * range;
     80      return predictor;
     81    }
    7782  }
    7883}
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs

    r2440 r2550  
    7171      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
    7272      string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
    73       int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
    7473      ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true);
    7574      var inputVariableNames = from x in inputVariables
     
    8483      string svmKernelType = GetVariableValue<StringData>("SVMKernelType", scope, true).Data;
    8584
    86       //extract SVM parameters from scope and set them
    87       SVM.Parameter parameter = new SVM.Parameter();
    88       parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
    89       parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), svmKernelType, true);
    90       parameter.C = GetVariableValue<DoubleData>("SVMCost", scope, true).Data;
    91       parameter.Nu = GetVariableValue<DoubleData>("SVMNu", scope, true).Data;
    92       parameter.Gamma = GetVariableValue<DoubleData>("SVMGamma", scope, true).Data;
    93       parameter.CacheSize = 500;
    94       parameter.Probability = false;
     85      double cost = GetVariableValue<DoubleData>("SVMCost", scope, true).Data;
     86      double nu = GetVariableValue<DoubleData>("SVMNu", scope, true).Data;
     87      double gamma = GetVariableValue<DoubleData>("SVMGamma", scope, true).Data;
    9588
    96       SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariableIndex, inputVariableNames, start, end, minTimeOffset, maxTimeOffset);
    97       SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    98       SVM.Problem scaledProblem = rangeTransform.Scale(problem);
    99 
    100       SVM.Model model = StartTraining(scaledProblem, parameter);
    101       if (!abortRequested) {
    102         //persist variables in scope
    103         SVMModel modelData = new SVMModel();
    104         modelData.Model = model;
    105         modelData.RangeTransform = rangeTransform;
    106         scope.AddVariable(new Variable(scope.TranslateName("SVMModel"), modelData));
    107         return null;
    108       } else {
    109         return new AtomicOperation(this, scope);
    110       }
    111     }
    112 
    113     private SVM.Model StartTraining(SVM.Problem scaledProblem, SVM.Parameter parameter) {
    114       SVM.Model model = null;
     89      SVMModel modelData = null;
    11590      lock (locker) {
    11691        if (!abortRequested) {
    11792          trainingThread = new Thread(() => {
    118             model = SVM.Training.Train(scaledProblem, parameter);
     93            modelData = TrainModel(dataset, targetVariable, inputVariableNames,
     94                                   start, end, minTimeOffset, maxTimeOffset,
     95                                   svmType, svmKernelType,
     96                                   cost, nu, gamma);
    11997          });
    12098          trainingThread.Start();
     
    125103        trainingThread = null;
    126104      }
     105
     106
     107      if (!abortRequested) {
     108        //persist variables in scope
     109        scope.AddVariable(new Variable(scope.TranslateName("SVMModel"), modelData));
     110        return null;
     111      } else {
     112        return new AtomicOperation(this, scope);
     113      }
     114    }
     115
     116    public static SVMModel TrainRegressionModel(
     117      Dataset dataset, string targetVariable, IEnumerable<string> inputVariables,
     118      int start, int end,
     119      double cost, double nu, double gamma) {
     120      return TrainModel(dataset, targetVariable, inputVariables, start, end, 0, 0, "NU_SVR", "RBF", cost, nu, gamma);
     121    }
     122
     123    public static SVMModel TrainModel(
     124      Dataset dataset, string targetVariable, IEnumerable<string> inputVariables,
     125      int start, int end,
     126      int minTimeOffset, int maxTimeOffset,
     127      string svmType, string kernelType,
     128      double cost, double nu, double gamma) {
     129      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
     130
     131      //extract SVM parameters from scope and set them
     132      SVM.Parameter parameter = new SVM.Parameter();
     133      parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
     134      parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
     135      parameter.C = cost;
     136      parameter.Nu = nu;
     137      parameter.Gamma = gamma;
     138      parameter.CacheSize = 500;
     139      parameter.Probability = false;
     140
     141
     142      SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariableIndex, inputVariables, start, end, minTimeOffset, maxTimeOffset);
     143      SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
     144      SVM.Problem scaledProblem = rangeTransform.Scale(problem);
     145      var model = new SVMModel();
     146
     147      model.Model = SVM.Training.Train(scaledProblem, parameter);
     148      model.RangeTransform = rangeTransform;
     149
    127150      return model;
    128151    }
Note: See TracChangeset for help on using the changeset viewer.