Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/10/12 11:02:37 (12 years ago)
Author:
gkronber
Message:

#1944 changed SVR and SVC algorithms in HeuristicLab to use most recent LibSVM version.

Location:
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj

    r8600 r8609  
    105105      <Private>False</Private>
    106106    </Reference>
    107     <Reference Include="LibSVM-1.6.3, Version=1.6.3.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    108       <HintPath>..\..\bin\LibSVM-1.6.3.dll</HintPath>
     107    <Reference Include="LibSVM-3.12, Version=3.12.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     108      <HintPath>..\..\bin\LibSVM-3.12.dll</HintPath>
    109109      <Private>False</Private>
    110110    </Reference>
     
    361361  -->
    362362  <PropertyGroup>
    363    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     363    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    364364set ProjectDir=$(ProjectDir)
    365365set SolutionDir=$(SolutionDir)
     
    368368call PreBuildEvent.cmd
    369369</PreBuildEvent>
    370 <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     370    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
    371371export ProjectDir=$(ProjectDir)
    372372export SolutionDir=$(SolutionDir)
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/ISupportVectorMachineModel.cs

    r7259 r8609  
    2020#endregion
    2121
    22 using HeuristicLab.Optimization;
    2322using HeuristicLab.Problems.DataAnalysis;
    24 using HeuristicLab.Core;
    25 using System.Collections.Generic;
     23using LibSVM;
    2624
    2725namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3028  /// </summary>
    3129  public interface ISupportVectorMachineModel : IDataAnalysisModel, IRegressionModel, IClassificationModel {
    32     SVM.Model Model { get; }
    33     SVM.RangeTransform RangeTransform { get; }
     30    svm_model Model { get; }
     31    RangeTransform RangeTransform { get; }
    3432    Dataset SupportVectors { get; }
    3533  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Plugin.cs.frame

    r8452 r8609  
    4646  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Classification", "3.4")]
    4747  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression", "3.4")]
    48   [PluginDependency("HeuristicLab.LibSVM", "1.6.3")]
     48  [PluginDependency("HeuristicLab.LibSVM", "3.12")]
    4949  [PluginDependency("HeuristicLab.Random", "3.3")]
    5050  public class HeuristicLabAlgorithmsDataAnalysisPlugin : PluginBase {
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassification.cs

    r8139 r8609  
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Problems.DataAnalysis;
     32using LibSVM;
    3233
    3334namespace HeuristicLab.Algorithms.DataAnalysis {
     
    123124      Results.Add(new Result("Support vector classification solution", "The support vector classification solution.", solution));
    124125      Results.Add(new Result("Training accuracy", "The accuracy of the SVR solution on the training partition.", new DoubleValue(trainingAccuracy)));
    125       Results.Add(new Result("Test ", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));
     126      Results.Add(new Result("Test accuracy", "The accuracy of the SVR solution on the test partition.", new DoubleValue(testAccuracy)));
    126127      Results.Add(new Result("Number of support vectors", "The number of support vectors of the SVR solution.", new IntValue(nSv)));
    127128    }
     
    135136
    136137      //extract SVM parameters from scope and set them
    137       SVM.Parameter parameter = new SVM.Parameter();
    138       parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
    139       parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
     138      svm_parameter parameter = new svm_parameter();
     139      parameter.svm_type = GetSvmType(svmType);
     140      parameter.kernel_type = GetKernelType(kernelType);
    140141      parameter.C = cost;
    141       parameter.Nu = nu;
    142       parameter.Gamma = gamma;
    143       parameter.CacheSize = 500;
    144       parameter.Probability = false;
     142      parameter.nu = nu;
     143      parameter.gamma = gamma;
     144      parameter.cache_size = 500;
     145      parameter.probability = 0;
     146      parameter.eps = 0.001;
     147      parameter.degree = 3;
     148      parameter.shrinking = 1;
     149      parameter.coef0 = 0;
    145150
     151
     152      var weightLabels = new List<int>();
     153      var weights = new List<double>();
    146154      foreach (double c in problemData.ClassValues) {
    147155        double wSum = 0.0;
     
    151159          }
    152160        }
    153         parameter.Weights.Add((int)c, wSum);
     161        weightLabels.Add((int)c);
     162        weights.Add(wSum);
    154163      }
     164      parameter.weight_label = weightLabels.ToArray();
     165      parameter.weight = weights.ToArray();
    155166
    156167
    157       SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
    158       SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    159       SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
    160       var svmModel = SVM.Training.Train(scaledProblem, parameter);
     168      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
     169      RangeTransform rangeTransform = RangeTransform.Compute(problem);
     170      svm_problem scaledProblem = rangeTransform.Scale(problem);
     171      var svmModel = svm.svm_train(scaledProblem, parameter);
    161172      var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
    162173      var solution = new SupportVectorClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
    163174
    164       nSv = svmModel.SupportVectorCount;
     175      nSv = svmModel.SV.Length;
    165176      trainingAccuracy = solution.TrainingAccuracy;
    166177      testAccuracy = solution.TestAccuracy;
     
    168179      return solution;
    169180    }
     181
     182    private static int GetSvmType(string svmType) {
     183      if (svmType == "NU_SVC") return svm_parameter.NU_SVC;
     184      if (svmType == "C_SVC") return svm_parameter.C_SVC;
     185      throw new ArgumentException("Unknown SVM type");
     186    }
     187
     188    private static int GetKernelType(string kernelType) {
     189      if (kernelType == "LINEAR") return svm_parameter.LINEAR;
     190      if (kernelType == "POLY") return svm_parameter.POLY;
     191      if (kernelType == "SIGMOID") return svm_parameter.SIGMOID;
     192      if (kernelType == "RBF") return svm_parameter.RBF;
     193      throw new ArgumentException("Unknown kernel type");
     194    }
    170195    #endregion
    171196  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs

    r8528 r8609  
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3030using HeuristicLab.Problems.DataAnalysis;
    31 using SVM;
     31using LibSVM;
    3232
    3333namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3939  public sealed class SupportVectorMachineModel : NamedItem, ISupportVectorMachineModel {
    4040
    41     private SVM.Model model;
     41    private svm_model model;
    4242    /// <summary>
    4343    /// Gets or sets the SVM model.
    4444    /// </summary>
    45     public SVM.Model Model {
     45    public svm_model Model {
    4646      get { return model; }
    4747      set {
     
    5757    /// Gets or sets the range transformation for the model.
    5858    /// </summary>
    59     private SVM.RangeTransform rangeTransform;
    60     public SVM.RangeTransform RangeTransform {
     59    private RangeTransform rangeTransform;
     60    public RangeTransform RangeTransform {
    6161      get { return rangeTransform; }
    6262      set {
     
    7171    public Dataset SupportVectors {
    7272      get {
    73         var data = new double[Model.SupportVectorCount, allowedInputVariables.Count()];
    74         for (int i = 0; i < Model.SupportVectorCount; i++) {
    75           var sv = Model.SupportVectors[i];
     73        var data = new double[Model.sv_coef.Length, allowedInputVariables.Count()];
     74        for (int i = 0; i < Model.sv_coef.Length; i++) {
     75          var sv = Model.SV[i];
    7676          for (int j = 0; j < sv.Length; j++) {
    77             data[i, j] = sv[j].Value;
     77            data[i, j] = sv[j].value;
    7878          }
    7979        }
     
    101101        this.classValues = (double[])original.classValues.Clone();
    102102    }
    103     public SupportVectorMachineModel(SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)
     103    public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)
    104104      : this(model, rangeTransform, targetVariable, allowedInputVariables) {
    105105      this.classValues = classValues.ToArray();
    106106    }
    107     public SupportVectorMachineModel(SVM.Model model, SVM.RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)
     107    public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)
    108108      : base() {
    109109      this.name = ItemName;
     
    161161    private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) {
    162162      // calculate predictions for the currently requested rows
    163       SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
    164       SVM.Problem scaledProblem = SVM.Scaling.Scale(RangeTransform, problem);
    165 
    166       for (int i = 0; i < scaledProblem.Count; i++) {
    167         yield return SVM.Prediction.Predict(Model, scaledProblem.X[i]);
     163      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
     164      svm_problem scaledProblem = rangeTransform.Scale(problem);
     165
     166      for (int i = 0; i < problem.l; i++) {
     167        yield return svm.svm_predict(Model, scaledProblem.x[i]);
    168168      }
    169169    }
     
    183183      get {
    184184        using (MemoryStream stream = new MemoryStream()) {
    185           SVM.Model.Write(stream, Model);
     185          svm.svm_save_model(new StreamWriter(stream), Model);
    186186          stream.Seek(0, System.IO.SeekOrigin.Begin);
    187187          StreamReader reader = new StreamReader(stream);
     
    191191      set {
    192192        using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
    193           model = SVM.Model.Read(stream);
     193          model = svm.svm_load_model(new StreamReader(stream));
    194194        }
    195195      }
     
    199199      get {
    200200        using (MemoryStream stream = new MemoryStream()) {
    201           SVM.RangeTransform.Write(stream, RangeTransform);
     201          RangeTransform.Write(stream, RangeTransform);
    202202          stream.Seek(0, System.IO.SeekOrigin.Begin);
    203203          StreamReader reader = new StreamReader(stream);
     
    207207      set {
    208208        using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
    209           RangeTransform = SVM.RangeTransform.Read(stream);
     209          RangeTransform = RangeTransform.Read(stream);
    210210        }
    211211      }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineUtil.cs

    r7259 r8609  
    2323using System.Linq;
    2424using HeuristicLab.Problems.DataAnalysis;
     25using LibSVM;
    2526
    2627namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3233    /// <param name="rowIndices">The rows of the dataset that should be contained in the resulting SVM-problem</param>
    3334    /// <returns>A problem data type that can be used to train a support vector machine.</returns>
    34     public static SVM.Problem CreateSvmProblem(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) {
     35    public static svm_problem CreateSvmProblem(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, IEnumerable<int> rowIndices) {
    3536      double[] targetVector =
    3637        dataset.GetDoubleValues(targetVariable, rowIndices).ToArray();
    3738
    38       SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];
    39       List<SVM.Node> tempRow;
     39      svm_node[][] nodes = new svm_node[targetVector.Length][];
     40      List<svm_node> tempRow;
    4041      int maxNodeIndex = 0;
    4142      int svmProblemRowIndex = 0;
    4243      List<string> inputVariablesList = inputVariables.ToList();
    4344      foreach (int row in rowIndices) {
    44         tempRow = new List<SVM.Node>();
     45        tempRow = new List<svm_node>();
    4546        int colIndex = 1; // make sure the smallest node index for SVM = 1
    4647        foreach (var inputVariable in inputVariablesList) {
     
    4950          // => don't add NaN values in the dataset to the sparse SVM matrix representation
    5051          if (!double.IsNaN(value)) {
    51             tempRow.Add(new SVM.Node(colIndex, value)); // nodes must be sorted in ascending ordered by column index
     52            tempRow.Add(new svm_node() { index = colIndex, value = value }); // nodes must be sorted in ascending ordered by column index
    5253            if (colIndex > maxNodeIndex) maxNodeIndex = colIndex;
    5354          }
     
    5758      }
    5859
    59       return new SVM.Problem(targetVector.Length, targetVector, nodes, maxNodeIndex);
     60      return new svm_problem() { l = targetVector.Length, y = targetVector, x = nodes };
    6061    }
    6162  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegression.cs

    r8139 r8609  
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3131using HeuristicLab.Problems.DataAnalysis;
     32using LibSVM;
    3233
    3334namespace HeuristicLab.Algorithms.DataAnalysis {
     
    143144
    144145      //extract SVM parameters from scope and set them
    145       SVM.Parameter parameter = new SVM.Parameter();
    146       parameter.SvmType = (SVM.SvmType)Enum.Parse(typeof(SVM.SvmType), svmType, true);
    147       parameter.KernelType = (SVM.KernelType)Enum.Parse(typeof(SVM.KernelType), kernelType, true);
     146      svm_parameter parameter = new svm_parameter();
     147      parameter.svm_type = GetSvmType(svmType);
     148      parameter.kernel_type = GetKernelType(kernelType);
    148149      parameter.C = cost;
    149       parameter.Nu = nu;
    150       parameter.Gamma = gamma;
    151       parameter.P = epsilon;
    152       parameter.CacheSize = 500;
    153       parameter.Probability = false;
     150      parameter.nu = nu;
     151      parameter.gamma = gamma;
     152      parameter.p = epsilon;
     153      parameter.cache_size = 500;
     154      parameter.probability = 0;
     155      parameter.eps = 0.001;
     156      parameter.degree = 3;
     157      parameter.shrinking = 1;
     158      parameter.coef0 = 0;
    154159
    155160
    156       SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
    157       SVM.RangeTransform rangeTransform = SVM.RangeTransform.Compute(problem);
    158       SVM.Problem scaledProblem = SVM.Scaling.Scale(rangeTransform, problem);
    159       var svmModel = SVM.Training.Train(scaledProblem, parameter);
    160       nSv = svmModel.SupportVectorCount;
     161
     162      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
     163      RangeTransform rangeTransform = RangeTransform.Compute(problem);
     164      svm_problem scaledProblem = rangeTransform.Scale(problem);
     165      var svmModel = svm.svm_train(scaledProblem, parameter);
     166      nSv = svmModel.SV.Length;
    161167      var model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables);
    162168      var solution = new SupportVectorRegressionSolution(model, (IRegressionProblemData)problemData.Clone());
     
    165171      return solution;
    166172    }
     173
     174    private static int GetSvmType(string svmType) {
     175      if (svmType == "NU_SVR") return svm_parameter.NU_SVR;
     176      if (svmType == "EPSILON_SVR") return svm_parameter.EPSILON_SVR;
     177      throw new ArgumentException("Unknown SVM type");
     178    }
     179
     180    private static int GetKernelType(string kernelType) {
     181      if (kernelType == "LINEAR") return svm_parameter.LINEAR;
     182      if (kernelType == "POLY") return svm_parameter.POLY;
     183      if (kernelType == "SIGMOID") return svm_parameter.SIGMOID;
     184      if (kernelType == "RBF") return svm_parameter.RBF;
     185      throw new ArgumentException("Unknown kernel type");
     186    }
    167187    #endregion
    168188  }
Note: See TracChangeset for help on using the changeset viewer.