Changeset 2550 for trunk/sources/HeuristicLab.SupportVectorMachines
- Timestamp:
- 12/10/09 15:57:14 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.SupportVectorMachines/3.2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/PredictorBuilder.cs
r2440 r2550 61 61 double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data; 62 62 63 63 64 64 ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true); 65 65 var inputVariableNames = from x in inputVariables 66 66 select ((StringData)x).Data; 67 67 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); 74 69 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Predictor"), predictor)); 75 70 return null; 76 71 } 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 } 77 82 } 78 83 } -
trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs
r2440 r2550 71 71 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 72 72 string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data; 73 int targetVariableIndex = dataset.GetVariableIndex(targetVariable);74 73 ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true); 75 74 var inputVariableNames = from x in inputVariables … … 84 83 string svmKernelType = GetVariableValue<StringData>("SVMKernelType", scope, true).Data; 85 84 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; 95 88 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; 115 90 lock (locker) { 116 91 if (!abortRequested) { 117 92 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); 119 97 }); 120 98 trainingThread.Start(); … … 125 103 trainingThread = null; 126 104 } 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 127 150 return model; 128 151 }
Note: See TracChangeset
for help on using the changeset viewer.