Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2985


Ignore:
Timestamp:
03/09/10 20:26:03 (14 years ago)
Author:
gkronber
Message:

Fixed bugs in MLP operators, extended operators to work for time series prognosis and added pre-configured engine for time series prognosis with MLP. #882 (Artificial neural networks engine for time series prognosis)

Location:
trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/HeuristicLab.ArtificialNeuralNetworks-3.2.csproj

    r2808 r2985  
    138138    <None Include="HeuristicLab.snk" />
    139139    <None Include="Properties\AssemblyInfo.frame" />
     140    <Compile Include="MultiLayerPerceptronTimeSeries.cs" />
    140141    <Compile Include="Properties\AssemblyInfo.cs" />
    141142  </ItemGroup>
  • trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptron.cs

    r2562 r2985  
    6868
    6969      clone.inputVariables = new List<string>(inputVariables);
     70      clone.minTimeOffset = MinTimeOffset;
     71      clone.maxTimeOffset = MaxTimeOffset;
    7072
    7173      double[] ra = null;
     
    7880    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    7981      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    80 
     82      var minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset");
     83      minTimeOffsetAttr.Value = minTimeOffset.ToString();
     84      var maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset");
     85      maxTimeOffsetAttr.Value = maxTimeOffset.ToString();
     86      node.Attributes.Append(minTimeOffsetAttr);
     87      node.Attributes.Append(maxTimeOffsetAttr);
    8188      XmlNode networkInformation = document.CreateElement("NetworkInformation");
    8289      double[] ra = null;
     
    94101    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    95102      base.Populate(node, restoredObjects);
     103      minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value);
     104      maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value);
    96105      double[] ra = (from s in node.SelectSingleNode("NetworkInformation").InnerText.Split(';')
    97106                     select double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
  • trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptronEvaluator.cs

    r2562 r2985  
    5252                               select ((StringData)x).Data;
    5353      string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
    54       int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
     54      // int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
    5555      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
    5656      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
     
    6161      MultiLayerPerceptron model = GetVariableValue<MultiLayerPerceptron>("MultiLayerPerceptron", scope, true);
    6262
    63       double[,] values = new double[end - start, 2];
    64       for (int i = 0; i < end - start; i++) {
     63      double[] targetVector;
     64      double[,] inputMatrix;
     65      MultiLayerPerceptronRegressionOperator.PrepareDataset(dataset, targetVariable, inputVariableNames, start, end, minTimeOffset, maxTimeOffset,
     66        out inputMatrix, out targetVector);
     67
     68      double[,] values = new double[targetVector.Length, 2];
     69      for (int i = 0; i < targetVector.Length; i++) {
    6570        double[] output = new double[1];
    66         double[] inputRow = new double[dataset.Columns - 1];
    67         for (int c = 1; c < inputRow.Length; c++) {
    68           inputRow[c - 1] = dataset.GetValue(i + start, c);
     71        double[] inputRow = new double[inputMatrix.GetLength(1)];
     72        for (int c = 0; c < inputRow.Length; c++) {
     73          inputRow[c] = inputMatrix[i, c];
    6974        }
    7075        alglib.mlpbase.multilayerperceptron p = model.Perceptron;
    7176        alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output);
    7277        model.Perceptron = p;
    73         values[i, 0] = dataset.GetValue(start + i, targetVariableIndex);
     78        values[i, 0] = targetVector[i];
    7479        values[i, 1] = output[0];
    7580      }
  • trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptronRegression.cs

    r2570 r2985  
    123123      get {
    124124        if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
    125         IScope bestModelScope = engine.GlobalScope;
     125        IScope bestModelScope = engine.GlobalScope.SubScopes[0];
    126126        return CreateMlpModel(bestModelScope);
    127127      }
  • trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/MultiLayerPerceptronRegressionOperator.cs

    r2562 r2985  
    3636      AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
    3737      AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
     38      AddVariableInfo(new VariableInfo("ValidationSamplesStart", "Start of validation set", typeof(IntData), VariableKind.In));
     39      AddVariableInfo(new VariableInfo("ValidationSamplesEnd", "End of validation set", typeof(IntData), VariableKind.In));
    3840      AddVariableInfo(new VariableInfo("NumberOfHiddenLayerNeurons", "The number of nodes in the hidden layer.", typeof(IntData), VariableKind.In));
    3941      AddVariableInfo(new VariableInfo("MaxTimeOffset", "(optional) Maximal time offset for time-series prognosis", typeof(IntData), VariableKind.In));
     
    4850      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
    4951      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
     52
     53      int valStart = GetVariableValue<IntData>("ValidationSamplesStart", scope, true).Data;
     54      int valEnd = GetVariableValue<IntData>("ValidationSamplesEnd", scope, true).Data;
     55
     56
    5057      IntData maxTimeOffsetData = GetVariableValue<IntData>("MaxTimeOffset", scope, true, false);
    5158      int maxTimeOffset = maxTimeOffsetData == null ? 0 : maxTimeOffsetData.Data;
     
    5461      int nHiddenNodes = GetVariableValue<IntData>("NumberOfHiddenLayerNeurons", scope, true).Data;
    5562
    56       var perceptron = CreateModel(dataset, targetVariable, dataset.VariableNames, start, end, minTimeOffset, maxTimeOffset, nHiddenNodes);
     63      var perceptron = CreateModel(dataset, targetVariable, dataset.VariableNames, start, end, valStart, valEnd, minTimeOffset, maxTimeOffset, nHiddenNodes);
    5764      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MultiLayerPerceptron"), perceptron));
    5865      return null;
    5966    }
    6067
    61     public static MultiLayerPerceptron CreateModel(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, int start, int end, int nHiddenNodes) {
    62       return CreateModel(dataset, targetVariable, inputVariables, start, end, 0, 0, nHiddenNodes);
    63     }
     68    //public static MultiLayerPerceptron CreateModel(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, int start, int end, int nHiddenNodes) {
     69    //  return CreateModel(dataset, targetVariable, inputVariables, start, end, 0, 0, nHiddenNodes);
     70    //}
    6471
    6572    public static MultiLayerPerceptron CreateModel(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables,
    66         int start, int end,
     73        int start, int end, int valStart, int valEnd,
    6774        int minTimeOffset, int maxTimeOffset, int nHiddenNodes) {
    68       int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
    69       List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariableIndex, inputVariables.Select(x => dataset.GetVariableIndex(x)), start, end);
    70       List<int> allowedRows = CalculateAllowedRows(dataset, targetVariableIndex, allowedColumns, start, end, minTimeOffset, maxTimeOffset);
    71 
    72       double[,] inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows, minTimeOffset, maxTimeOffset);
    73       double[] targetVector = PrepareTargetVector(dataset, targetVariableIndex, allowedRows);
    74 
    75       var perceptron = TrainPerceptron(inputMatrix, targetVector, nHiddenNodes);
     75      double[,] inputMatrix;
     76      double[,] validationData;
     77      double[] targetVector;
     78      double[] validationTargetVector;
     79      PrepareDataset(dataset, targetVariable, inputVariables, start, end, minTimeOffset, maxTimeOffset, out inputMatrix, out targetVector);
     80      PrepareDataset(dataset, targetVariable, inputVariables, valStart, valEnd, minTimeOffset, maxTimeOffset, out validationData, out validationTargetVector);
     81      var perceptron = TrainPerceptron(inputMatrix, targetVector, nHiddenNodes, validationData, validationTargetVector);
    7682      return new MultiLayerPerceptron(perceptron, inputVariables, minTimeOffset, maxTimeOffset);
    7783    }
     
    7985
    8086
    81     private static alglib.mlpbase.multilayerperceptron TrainPerceptron(double[,] inputMatrix, double[] targetVector, int nHiddenNodes) {
     87    private static alglib.mlpbase.multilayerperceptron TrainPerceptron(double[,] inputMatrix, double[] targetVector, int nHiddenNodes, double[,] validationData, double[] validationTargetVector) {
    8288      int retVal = 0;
    8389      int n = targetVector.Length;
     90      int validationN = validationTargetVector.Length;
    8491      int p = inputMatrix.GetLength(1);
    8592      alglib.mlpbase.multilayerperceptron perceptron = new alglib.mlpbase.multilayerperceptron();
     
    93100        dataset[row, p - 1] = targetVector[row];
    94101      }
    95       alglib.mlptrain.mlptrainlbfgs(ref perceptron, ref dataset, n, 0.001, 2, 0.01, 0, ref retVal, ref report);
    96       if (retVal != 2) throw new ArgumentException("Error in training of multi layer perceptron");
     102      double[,] validationDataset = new double[validationN, p];
     103      for (int row = 0; row < validationN; row++) {
     104        for (int column = 0; column < p - 1; column++) {
     105          validationDataset[row, column] = validationData[row, column];
     106        }
     107        validationDataset[row, p - 1] = validationTargetVector[row];
     108      }
     109      //alglib.mlptrain.mlptrainlbfgs(ref perceptron, ref dataset, n, 0.001, 10, 0.01, 0, ref retVal, ref report);
     110      alglib.mlptrain.mlptraines(ref perceptron, ref dataset, n, ref validationDataset, validationN, 0.001, 10, ref retVal, ref report);
     111      if (retVal != 2 && retVal != 6) throw new ArgumentException("Error in training of multi layer perceptron");
    97112      return perceptron;
     113    }
     114
     115    public static void PrepareDataset(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, int start, int end, int minTimeOffset, int maxTimeOffset,
     116      out double[,] inputMatrix, out double[] targetVector) {
     117      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
     118      List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariableIndex, inputVariables.Select(x => dataset.GetVariableIndex(x)), start, end);
     119      List<int> allowedRows = CalculateAllowedRows(dataset, targetVariableIndex, allowedColumns, start, end, minTimeOffset, maxTimeOffset);
     120
     121      inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows, minTimeOffset, maxTimeOffset);
     122      targetVector = PrepareTargetVector(dataset, targetVariableIndex, allowedRows);
     123
    98124    }
    99125
     
    139165      int rowCount = allowedRows.Count;
    140166      int timeOffsetRange = (maxTimeOffset - minTimeOffset + 1);
    141       double[,] matrix = new double[rowCount, (allowedColumns.Count * timeOffsetRange) + 1];
     167      double[,] matrix = new double[rowCount, (allowedColumns.Count * timeOffsetRange)];
    142168      for (int row = 0; row < allowedRows.Count; row++)
    143169        for (int col = 0; col < allowedColumns.Count; col++) {
     
    145171            matrix[row, (col * timeOffsetRange) + (timeOffset - minTimeOffset)] = dataset.GetValue(allowedRows[row] + timeOffset, allowedColumns[col]);
    146172        }
    147       //add constant 1.0 in last column
    148       for (int i = 0; i < rowCount; i++)
    149         matrix[i, allowedColumns.Count * timeOffsetRange] = 1.0;
    150173      return matrix;
    151174    }
  • trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/Predictor.cs

    r2619 r2985  
    2727using System.Xml;
    2828using HeuristicLab.DataAnalysis;
     29using System.Linq;
    2930
    3031namespace HeuristicLab.ArtificialNeuralNetworks {
     
    4445      for (int i = 0; i < end - start; i++) {
    4546        double[] output = new double[1];
    46         double[] inputRow = new double[input.Columns - 1];
    47         for (int c = 1; c < inputRow.Length; c++) {
    48           inputRow[c - 1] = input.GetValue(i + start, c);
     47        double[] inputRow = new double[GetInputVariables().Count()];
     48        int c = 0;
     49        foreach (var inputVariable in GetInputVariables()) {
     50          int inputVariableIndex = input.GetVariableIndex(inputVariable);
     51          inputRow[c++] = input.GetValue(i + start, inputVariableIndex);
    4952        }
    50         alglib.mlpbase.multilayerperceptron p = perceptron.Perceptron;
    51         alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output);
    52         perceptron.Perceptron = p;
    53         yield return Math.Max(Math.Min(output[0], UpperPredictionLimit), LowerPredictionLimit);
     53        double estimatedValue;
     54        try {
     55          alglib.mlpbase.multilayerperceptron p = perceptron.Perceptron;
     56          alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output);
     57          perceptron.Perceptron = p;
     58          estimatedValue = output[0];
     59        }
     60        catch (ArithmeticException) {
     61          estimatedValue = UpperPredictionLimit;
     62        }
     63        yield return Math.Max(Math.Min(estimatedValue, UpperPredictionLimit), LowerPredictionLimit);
    5464      }
    5565    }
Note: See TracChangeset for help on using the changeset viewer.