Changeset 6578


Ignore:
Timestamp:
07/21/11 11:11:05 (8 years ago)
Author:
gkronber
Message:

#1474: added parameters for neural network regression algorithm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkRegression.cs

    r6577 r6578  
    4242  [StorableClass]
    4343  public sealed class NeuralNetworkRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
     44    private const string DecayParameterName = "Decay";
     45    private const string HiddenLayersParameterName = "HiddenLayers";
     46    private const string NodesInFirstHiddenLayerParameterName = "NodesInFirstHiddenLayer";
     47    private const string NodesInSecondHiddenLayerParameterName = "NodesInSecondHiddenLayer";
     48    private const string RestartsParameterName = "Restarts";
    4449    private const string NeuralNetworkRegressionModelResultName = "Neural network regression solution";
     50
     51    #region parameter properties
     52    public IFixedValueParameter<DoubleValue> DecayParameter {
     53      get { return (IFixedValueParameter<DoubleValue>)Parameters[DecayParameterName]; }
     54    }
     55    public ConstrainedValueParameter<IntValue> HiddenLayersParameter {
     56      get { return (ConstrainedValueParameter<IntValue>)Parameters[HiddenLayersParameterName]; }
     57    }
     58    public IFixedValueParameter<IntValue> NodesInFirstHiddenLayerParameter {
     59      get { return (IFixedValueParameter<IntValue>)Parameters[NodesInFirstHiddenLayerParameterName]; }
     60    }
     61    public IFixedValueParameter<IntValue> NodesInSecondHiddenLayerParameter {
     62      get { return (IFixedValueParameter<IntValue>)Parameters[NodesInSecondHiddenLayerParameterName]; }
     63    }
     64    public IFixedValueParameter<IntValue> RestartsParameter {
     65      get { return (IFixedValueParameter<IntValue>)Parameters[RestartsParameterName]; }
     66    }
     67    #endregion
     68
     69    #region properties
     70    public double Decay {
     71      get { return DecayParameter.Value.Value; }
     72      set {
     73        if (value < 0.001 || value > 100) throw new ArgumentException("The decay parameter should be set to a value between 0.001 and 100.", "Decay");
     74        DecayParameter.Value.Value = value;
     75      }
     76    }
     77    public int HiddenLayers {
     78      get { return HiddenLayersParameter.Value.Value; }
     79      set {
     80        if (value < 0 || value > 2) throw new ArgumentException("The number of hidden layers should be set to 0, 1, or 2.", "HiddenLayers");
     81        HiddenLayersParameter.Value = (from v in HiddenLayersParameter.ValidValues
     82                                       where v.Value == value
     83                                       select v)
     84                                      .Single();
     85      }
     86    }
     87    public int NodesInFirstHiddenLayer {
     88      get { return NodesInFirstHiddenLayerParameter.Value.Value; }
     89      set {
     90        if (value < 1) throw new ArgumentException("The number of nodes in the first hidden layer must be at least one.", "NodesInFirstHiddenLayer");
     91        NodesInFirstHiddenLayerParameter.Value.Value = value;
     92      }
     93    }
     94    public int NodesInSecondHiddenLayer {
     95      get { return NodesInSecondHiddenLayerParameter.Value.Value; }
     96      set {
     97        if (value < 1) throw new ArgumentException("The number of nodes in the first second layer must be at least one.", "NodesInSecondHiddenLayer");
     98        NodesInSecondHiddenLayerParameter.Value.Value = value;
     99      }
     100    }
     101    public int Restarts {
     102      get { return RestartsParameter.Value.Value; }
     103      set {
     104        if (value < 0) throw new ArgumentException("The number of restarts must be positive.", "Restarts");
     105        RestartsParameter.Value.Value = value;
     106      }
     107    }
     108    #endregion
     109
     110
    45111    [StorableConstructor]
    46112    private NeuralNetworkRegression(bool deserializing) : base(deserializing) { }
     
    50116    public NeuralNetworkRegression()
    51117      : base() {
     118      var validHiddenLayerValues = new ItemSet<IntValue>(new IntValue[] { new IntValue(0), new IntValue(1), new IntValue(2) });
     119      var selectedHiddenLayerValue = (from v in validHiddenLayerValues
     120                                      where v.Value == 1
     121                                      select v)
     122                                     .Single();
     123      Parameters.Add(new FixedValueParameter<DoubleValue>(DecayParameterName, "The decay parameter for the training phase of the neural network. This parameter determines the strengh of regularization and should be set to a value between 0.001 (weak regularization) to 100 (very strong regularization). The correct value should be determined via cross-validation.", new DoubleValue(1)));
     124      Parameters.Add(new ConstrainedValueParameter<IntValue>(HiddenLayersParameterName, "The number of hidden layers for the neural network (0, 1, or 2)", validHiddenLayerValues, selectedHiddenLayerValue));
     125      Parameters.Add(new FixedValueParameter<IntValue>(NodesInFirstHiddenLayerParameterName, "The number of nodes in the first hidden layer. This value is not used if the number of hidden layers is zero.", new IntValue(10)));
     126      Parameters.Add(new FixedValueParameter<IntValue>(NodesInSecondHiddenLayerParameterName, "The number of nodes in the second hidden layer. This value is not used if the number of hidden layers is zero or one.", new IntValue(10)));
     127      Parameters.Add(new FixedValueParameter<IntValue>(RestartsParameterName, "The number of restarts for learning.", new IntValue(2)));
     128
    52129      Problem = new RegressionProblem();
    53130    }
     
    61138    #region neural network
    62139    protected override void Run() {
    63       double decay = 0.01;
    64       int nLayers = 2;
    65       int nHidden1 = 10;
    66       int nHidden2 = 10;
    67       int nRestarts = 5;
    68140      double rmsError, avgRelError;
    69       var solution = CreateNeuralNetworkRegressionSolution(Problem.ProblemData, nLayers, nHidden1, nHidden2, decay, nRestarts, out rmsError, out avgRelError);
     141      var solution = CreateNeuralNetworkRegressionSolution(Problem.ProblemData, HiddenLayers, NodesInFirstHiddenLayer, NodesInSecondHiddenLayer, Decay, Restarts, out rmsError, out avgRelError);
    70142      Results.Add(new Result(NeuralNetworkRegressionModelResultName, "The neural network regression solution.", solution));
    71143      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)));
     
    105177
    106178      rmsError = alglib.mlprmserror(multiLayerPerceptron, inputMatrix, nRows);
    107       avgRelError = alglib.mlpavgerror(multiLayerPerceptron, inputMatrix, nRows);
     179      avgRelError = alglib.mlpavgrelerror(multiLayerPerceptron, inputMatrix, nRows);
    108180
    109181      return new NeuralNetworkRegressionSolution(problemData, new NeuralNetworkModel(multiLayerPerceptron, targetVariable, allowedInputVariables));
Note: See TracChangeset for help on using the changeset viewer.