Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs @ 16811

Last change on this file since 16811 was 16387, checked in by gkronber, 6 years ago

#2891: merged r15739 and r16168 from trunk to stable

File size: 7.8 KB
RevLine 
[6577]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6577]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  /// <summary>
[6580]32  /// Represents a neural network ensembel model for regression and classification
[6577]33  /// </summary>
34  [StorableClass]
[6580]35  [Item("NeuralNetworkEnsembleModel", "Represents a neural network ensemble for regression and classification.")]
[14027]36  public sealed class NeuralNetworkEnsembleModel : ClassificationModel, INeuralNetworkEnsembleModel {
[6577]37
[16387]38    private object mlpEnsembleLocker = new object();
[6580]39    private alglib.mlpensemble mlpEnsemble;
[6577]40
[14027]41    public override IEnumerable<string> VariablesUsedForPrediction {
42      get { return allowedInputVariables; }
43    }
44
[6577]45    [Storable]
46    private string targetVariable;
47    [Storable]
48    private string[] allowedInputVariables;
49    [Storable]
50    private double[] classValues;
51    [StorableConstructor]
[6580]52    private NeuralNetworkEnsembleModel(bool deserializing)
[6577]53      : base(deserializing) {
54      if (deserializing)
[6580]55        mlpEnsemble = new alglib.mlpensemble();
[6577]56    }
[6580]57    private NeuralNetworkEnsembleModel(NeuralNetworkEnsembleModel original, Cloner cloner)
[6577]58      : base(original, cloner) {
[6580]59      mlpEnsemble = new alglib.mlpensemble();
[7694]60      string serializedEnsemble;
61      alglib.mlpeserialize(original.mlpEnsemble, out serializedEnsemble);
62      alglib.mlpeunserialize(serializedEnsemble, out this.mlpEnsemble);
[6577]63      targetVariable = original.targetVariable;
64      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
65      if (original.classValues != null)
66        this.classValues = (double[])original.classValues.Clone();
67    }
[6580]68    public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
[14027]69      : base(targetVariable) {
[6577]70      this.name = ItemName;
71      this.description = ItemDescription;
[6580]72      this.mlpEnsemble = mlpEnsemble;
[6577]73      this.targetVariable = targetVariable;
74      this.allowedInputVariables = allowedInputVariables.ToArray();
75      if (classValues != null)
76        this.classValues = (double[])classValues.Clone();
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
[6580]80      return new NeuralNetworkEnsembleModel(this, cloner);
[6577]81    }
82
[12702]83    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
[15142]84      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
[6577]85
86      int n = inputData.GetLength(0);
87      int columns = inputData.GetLength(1);
88      double[] x = new double[columns];
89      double[] y = new double[1];
90
91      for (int row = 0; row < n; row++) {
92        for (int column = 0; column < columns; column++) {
93          x[column] = inputData[row, column];
94        }
[16387]95        // mlpeprocess writes data in mlpEnsemble and is therefore not thread-safe
96        lock (mlpEnsembleLocker) {
97          alglib.mlpeprocess(mlpEnsemble, x, ref y);
98        }
[6577]99        yield return y[0];
100      }
101    }
102
[14027]103    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
[15142]104      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
[6577]105
106      int n = inputData.GetLength(0);
107      int columns = inputData.GetLength(1);
108      double[] x = new double[columns];
109      double[] y = new double[classValues.Length];
110
111      for (int row = 0; row < n; row++) {
112        for (int column = 0; column < columns; column++) {
113          x[column] = inputData[row, column];
114        }
[16387]115        // mlpeprocess writes data in mlpEnsemble and is therefore not thread-safe
116        lock (mlpEnsembleLocker) {
117          alglib.mlpeprocess(mlpEnsemble, x, ref y);
118        }
[6577]119        // find class for with the largest probability value
120        int maxProbClassIndex = 0;
121        double maxProb = y[0];
122        for (int i = 1; i < y.Length; i++) {
123          if (maxProb < y[i]) {
124            maxProb = y[i];
125            maxProbClassIndex = i;
126          }
127        }
128        yield return classValues[maxProbClassIndex];
129      }
130    }
131
[14027]132    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
133      return new NeuralNetworkEnsembleRegressionSolution(this, new RegressionEnsembleProblemData(problemData));
[6603]134    }
[14027]135    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
136      return new NeuralNetworkEnsembleClassificationSolution(this, new ClassificationEnsembleProblemData(problemData));
[6603]137    }
[16387]138   
[6577]139    #region persistence
140    [Storable]
[7694]141    private string MultiLayerPerceptronEnsembleNetwork {
[6577]142      get {
[7694]143        string serializedNetwork;
144        alglib.mlpeserialize(this.mlpEnsemble, out serializedNetwork);
145        return serializedNetwork;
[6577]146      }
147      set {
[7694]148        alglib.mlpeunserialize(value, out this.mlpEnsemble);
149      }
150    }
151
152    [Storable]
153    private double[] MultiLayerPerceptronEnsembleColumnMeans {
154      get { return mlpEnsemble.innerobj.columnmeans; }
155      set {
[6580]156        mlpEnsemble.innerobj.columnmeans = value;
[7694]157        mlpEnsemble.innerobj.network.columnmeans = value;
[6577]158      }
159    }
160    [Storable]
[6580]161    private double[] MultiLayerPerceptronEnsembleColumnSigmas {
[7694]162      get { return mlpEnsemble.innerobj.columnsigmas; }
[6577]163      set {
[6580]164        mlpEnsemble.innerobj.columnsigmas = value;
[7694]165        mlpEnsemble.innerobj.network.columnsigmas = value;
[6577]166      }
167    }
[7694]168    [Storable(AllowOneWay = true)]
[6580]169    private double[] MultiLayerPerceptronEnsembleDfdnet {
[6577]170      set {
[7694]171        mlpEnsemble.innerobj.network.dfdnet = value;
[6577]172      }
173    }
174    [Storable]
[6580]175    private int MultiLayerPerceptronEnsembleSize {
[7694]176      get { return mlpEnsemble.innerobj.ensemblesize; }
[6577]177      set {
[6580]178        mlpEnsemble.innerobj.ensemblesize = value;
[7694]179        mlpEnsemble.innerobj.ensemblesize = value;
[6577]180      }
181    }
[7694]182    [Storable(AllowOneWay = true)]
[6580]183    private double[] MultiLayerPerceptronEnsembleNeurons {
[7694]184      set { mlpEnsemble.innerobj.network.neurons = value; }
[6577]185    }
[7694]186    [Storable(AllowOneWay = true)]
[6580]187    private double[] MultiLayerPerceptronEnsembleSerializedMlp {
188      set {
[7694]189        mlpEnsemble.innerobj.network.dfdnet = value;
[6580]190      }
191    }
[7694]192    [Storable(AllowOneWay = true)]
[6577]193    private int[] MultiLayerPerceptronStuctinfo {
194      set {
[7694]195        mlpEnsemble.innerobj.network.structinfo = value;
[6577]196      }
197    }
[6580]198
199    [Storable]
200    private double[] MultiLayerPerceptronWeights {
201      get {
202        return mlpEnsemble.innerobj.weights;
203      }
204      set {
205        mlpEnsemble.innerobj.weights = value;
[7694]206        mlpEnsemble.innerobj.network.weights = value;
[6580]207      }
208    }
209    [Storable]
[6577]210    private double[] MultiLayerPerceptronY {
211      get {
[6580]212        return mlpEnsemble.innerobj.y;
[6577]213      }
214      set {
[6580]215        mlpEnsemble.innerobj.y = value;
[7694]216        mlpEnsemble.innerobj.network.y = value;
[6577]217      }
218    }
219    #endregion
220  }
221}
Note: See TracBrowser for help on using the repository browser.