Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6579


Ignore:
Timestamp:
07/21/11 11:45:19 (13 years ago)
Author:
gkronber
Message:

#1474: implemented neural networks for classification.

Location:
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4
Files:
2 edited
3 copied

Legend:

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

    r6577 r6579  
    112112    <Compile Include="HeuristicLabAlgorithmsDataAnalysisPlugin.cs" />
    113113    <Compile Include="FixedDataAnalysisAlgorithm.cs" />
     114    <Compile Include="Interfaces\INeuralNetworkClassificationSolution.cs" />
    114115    <Compile Include="Interfaces\INeuralNetworkRegressionSolution.cs" />
    115116    <Compile Include="Interfaces\INeuralNetworkModel.cs" />
     
    134135    <Compile Include="Linear\MultinomialLogitClassificationSolution.cs" />
    135136    <Compile Include="Linear\MultinomialLogitModel.cs" />
     137    <Compile Include="NeuralNetwork\NeuralNetworkClassification.cs" />
     138    <Compile Include="NeuralNetwork\NeuralNetworkClassificationSolution.cs" />
    136139    <Compile Include="NeuralNetwork\NeuralNetworkModel.cs" />
    137140    <Compile Include="NeuralNetwork\NeuralNetworkRegression.cs" />
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/INeuralNetworkClassificationSolution.cs

    r6577 r6579  
    2626namespace HeuristicLab.Algorithms.DataAnalysis {
    2727  /// <summary>
    28   /// Interface to represent a neural network regression solution
     28  /// Interface to represent a neural network classification solution
    2929  /// </summary>
    30   public interface INeuralNetworkRegressionSolution : IRegressionSolution {
     30  public interface INeuralNetworkClassificationSolution : IClassificationSolution {
    3131    new INeuralNetworkModel Model { get; }
    3232  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkClassification.cs

    r6578 r6579  
    3636namespace HeuristicLab.Algorithms.DataAnalysis {
    3737  /// <summary>
    38   /// Neural network regression data analysis algorithm.
     38  /// Neural network classification data analysis algorithm.
    3939  /// </summary>
    40   [Item("Neural Network Regression", "Neural network regression data analysis algorithm (wrapper for ALGLIB).")]
     40  [Item("Neural Network Classification", "Neural network classification data analysis algorithm (wrapper for ALGLIB).")]
    4141  [Creatable("Data Analysis")]
    4242  [StorableClass]
    43   public sealed class NeuralNetworkRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
     43  public sealed class NeuralNetworkClassification : FixedDataAnalysisAlgorithm<IClassificationProblem> {
    4444    private const string DecayParameterName = "Decay";
    4545    private const string HiddenLayersParameterName = "HiddenLayers";
     
    4747    private const string NodesInSecondHiddenLayerParameterName = "NodesInSecondHiddenLayer";
    4848    private const string RestartsParameterName = "Restarts";
    49     private const string NeuralNetworkRegressionModelResultName = "Neural network regression solution";
     49    private const string NeuralNetworkRegressionModelResultName = "Neural network classification solution";
    5050
    5151    #region parameter properties
     
    110110
    111111    [StorableConstructor]
    112     private NeuralNetworkRegression(bool deserializing) : base(deserializing) { }
    113     private NeuralNetworkRegression(NeuralNetworkRegression original, Cloner cloner)
     112    private NeuralNetworkClassification(bool deserializing) : base(deserializing) { }
     113    private NeuralNetworkClassification(NeuralNetworkClassification original, Cloner cloner)
    114114      : base(original, cloner) {
    115115    }
    116     public NeuralNetworkRegression()
     116    public NeuralNetworkClassification()
    117117      : base() {
    118118      var validHiddenLayerValues = new ItemSet<IntValue>(new IntValue[] { new IntValue(0), new IntValue(1), new IntValue(2) });
     
    127127      Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of restarts for learning.", new IntValue(2)));
    128128
    129       Problem = new RegressionProblem();
     129      Problem = new ClassificationProblem();
    130130    }
    131131    [StorableHook(HookType.AfterDeserialization)]
     
    133133
    134134    public override IDeepCloneable Clone(Cloner cloner) {
    135       return new NeuralNetworkRegression(this, cloner);
     135      return new NeuralNetworkClassification(this, cloner);
    136136    }
    137137
    138138    #region neural network
    139139    protected override void Run() {
    140       double rmsError, avgRelError;
    141       var solution = CreateNeuralNetworkRegressionSolution(Problem.ProblemData, HiddenLayers, NodesInFirstHiddenLayer, NodesInSecondHiddenLayer, Decay, Restarts, out rmsError, out avgRelError);
     140      double rmsError, avgRelError, relClassError;
     141      var solution = CreateNeuralNetworkClassificationSolution(Problem.ProblemData, HiddenLayers, NodesInFirstHiddenLayer, NodesInSecondHiddenLayer, Decay, Restarts, out rmsError, out avgRelError, out relClassError);
    142142      Results.Add(new Result(NeuralNetworkRegressionModelResultName, "The neural network regression solution.", solution));
    143143      Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the neural network regression solution on the training set.", new DoubleValue(rmsError)));
    144144      Results.Add(new Result("Average relative error", "The average of relative errors of the neural network regression solution on the training set.", new PercentValue(avgRelError)));
     145      Results.Add(new Result("Relative classification error", "The percentage of misclassified samples.", new PercentValue(relClassError)));
    145146    }
    146147
    147     public static IRegressionSolution CreateNeuralNetworkRegressionSolution(IRegressionProblemData problemData, int nLayers, int nHiddenNodes1, int nHiddenNodes2, double decay, int restarts,
    148       out double rmsError, out double avgRelError) {
     148    public static IClassificationSolution CreateNeuralNetworkClassificationSolution(IClassificationProblemData problemData, int nLayers, int nHiddenNodes1, int nHiddenNodes2, double decay, int restarts,
     149      out double rmsError, out double avgRelError, out double relClassError) {
    149150      Dataset dataset = problemData.Dataset;
    150151      string targetVariable = problemData.TargetVariable;
     
    153154      double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
    154155      if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
    155         throw new NotSupportedException("Neural network regression does not support NaN or infinity values in the input dataset.");
    156 
    157       double targetMin = problemData.Dataset.GetEnumeratedVariableValues(targetVariable).Min();
    158       targetMin = targetMin - targetMin * 0.1; // -10%
    159       double targetMax = problemData.Dataset.GetEnumeratedVariableValues(targetVariable).Max();
    160       targetMax = targetMax + targetMax * 0.1; // + 10%
     156        throw new NotSupportedException("Neural network classification does not support NaN or infinity values in the input dataset.");
    161157
    162158      alglib.multilayerperceptron multiLayerPerceptron = null;
     159      int numberOfClasses = problemData.ClassValues.Count();
    163160      if (nLayers == 0) {
    164         alglib.mlpcreater0(allowedInputVariables.Count(), 1, targetMin, targetMax, out multiLayerPerceptron);
     161        alglib.mlpcreatec0(allowedInputVariables.Count(), numberOfClasses, out multiLayerPerceptron);
    165162      } else if (nLayers == 1) {
    166         alglib.mlpcreater1(allowedInputVariables.Count(), nHiddenNodes1, 1, targetMin, targetMax, out multiLayerPerceptron);
     163        alglib.mlpcreatec1(allowedInputVariables.Count(), nHiddenNodes1, numberOfClasses, out multiLayerPerceptron);
    167164      } else if (nLayers == 2) {
    168         alglib.mlpcreater2(allowedInputVariables.Count(), nHiddenNodes1, nHiddenNodes2, 1, targetMin, targetMax, out multiLayerPerceptron);
     165        alglib.mlpcreatec2(allowedInputVariables.Count(), nHiddenNodes1, nHiddenNodes2, numberOfClasses, out multiLayerPerceptron);
    169166      } else throw new ArgumentException("Number of layers must be zero, one, or two.", "nLayers");
    170167      alglib.mlpreport rep;
    171168      int nRows = inputMatrix.GetLength(0);
     169      int nFeatures = inputMatrix.GetLength(1) - 1;
     170      double[] classValues = dataset.GetVariableValues(targetVariable).Distinct().OrderBy(x => x).ToArray();
     171      int nClasses = classValues.Count();
     172      // map original class values to values [0..nClasses-1]
     173      Dictionary<double, double> classIndizes = new Dictionary<double, double>();
     174      for (int i = 0; i < nClasses; i++) {
     175        classIndizes[classValues[i]] = i;
     176      }
     177      for (int row = 0; row < nRows; row++) {
     178        inputMatrix[row, nFeatures] = classIndizes[inputMatrix[row, nFeatures]];
     179      }
    172180
    173181      int info;
    174182      // using mlptrainlm instead of mlptraines or mlptrainbfgs because only one parameter is necessary
    175183      alglib.mlptrainlm(multiLayerPerceptron, inputMatrix, nRows, decay, restarts, out info, out rep);
    176       if (info != 2) throw new ArgumentException("Error in calculation of neural network regression solution");
     184      if (info != 2) throw new ArgumentException("Error in calculation of neural network classification solution");
    177185
    178186      rmsError = alglib.mlprmserror(multiLayerPerceptron, inputMatrix, nRows);
    179187      avgRelError = alglib.mlpavgrelerror(multiLayerPerceptron, inputMatrix, nRows);
     188      relClassError = alglib.mlpclserror(multiLayerPerceptron, inputMatrix, nRows) / (double)nRows;
    180189
    181       return new NeuralNetworkRegressionSolution(problemData, new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables));
     190      return new NeuralNetworkClassificationSolution(problemData, new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables, problemData.ClassValues.ToArray()));
    182191    }
    183192    #endregion
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkClassificationSolution.cs

    r6577 r6579  
    3131namespace HeuristicLab.Algorithms.DataAnalysis {
    3232  /// <summary>
    33   /// Represents a neural network solution for a regression problem which can be visualized in the GUI.
     33  /// Represents a neural network solution for a classification problem which can be visualized in the GUI.
    3434  /// </summary>
    35   [Item("NeuralNetworkRegressionSolution", "Represents a neural network solution for a regression problem which can be visualized in the GUI.")]
     35  [Item("NeuralNetworkClassificationSolution", "Represents a neural network solution for a classification problem which can be visualized in the GUI.")]
    3636  [StorableClass]
    37   public sealed class NeuralNetworkRegressionSolution : RegressionSolution, INeuralNetworkRegressionSolution {
     37  public sealed class NeuralNetworkClassificationSolution : ClassificationSolution, INeuralNetworkClassificationSolution {
    3838
    3939    public new INeuralNetworkModel Model {
     
    4343
    4444    [StorableConstructor]
    45     private NeuralNetworkRegressionSolution(bool deserializing) : base(deserializing) { }
    46     private NeuralNetworkRegressionSolution(NeuralNetworkRegressionSolution original, Cloner cloner)
     45    private NeuralNetworkClassificationSolution(bool deserializing) : base(deserializing) { }
     46    private NeuralNetworkClassificationSolution(NeuralNetworkClassificationSolution original, Cloner cloner)
    4747      : base(original, cloner) {
    4848    }
    49     public NeuralNetworkRegressionSolution(IRegressionProblemData problemData, INeuralNetworkModel nnModel)
     49    public NeuralNetworkClassificationSolution(IClassificationProblemData problemData, INeuralNetworkModel nnModel)
    5050      : base(nnModel, problemData) {
    5151    }
    5252
    5353    public override IDeepCloneable Clone(Cloner cloner) {
    54       return new NeuralNetworkRegressionSolution(this, cloner);
     54      return new NeuralNetworkClassificationSolution(this, cloner);
    5555    }
    5656  }
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkRegression.cs

    r6578 r6579  
    177177
    178178      rmsError = alglib.mlprmserror(multiLayerPerceptron, inputMatrix, nRows);
    179       avgRelError = alglib.mlpavgrelerror(multiLayerPerceptron, inputMatrix, nRows);
     179      avgRelError = alglib.mlpavgrelerror(multiLayerPerceptron, inputMatrix, nRows);     
    180180
    181181      return new NeuralNetworkRegressionSolution(problemData, new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables));
Note: See TracChangeset for help on using the changeset viewer.