Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18180 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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 HEAL.Attic;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  /// <summary>
32  /// Represents a neural network ensembel model for regression and classification
33  /// </summary>
34  [StorableType("51B29670-27BD-405C-A521-39814E4BD857")]
35  [Item("NeuralNetworkEnsembleModel", "Represents a neural network ensemble for regression and classification.")]
36  public sealed class NeuralNetworkEnsembleModel : ClassificationModel, INeuralNetworkEnsembleModel {
37
38    private object mlpEnsembleLocker = new object();
39    private alglib.mlpensemble mlpEnsemble;
40
41    public override IEnumerable<string> VariablesUsedForPrediction {
42      get { return allowedInputVariables; }
43    }
44
45    [Storable]
46    private string targetVariable;
47    [Storable]
48    private string[] allowedInputVariables;
49    [Storable]
50    private double[] classValues;
51    [StorableConstructor]
52    private NeuralNetworkEnsembleModel(StorableConstructorFlag _) : base(_) {
53      mlpEnsemble = new alglib.mlpensemble();
54    }
55    private NeuralNetworkEnsembleModel(NeuralNetworkEnsembleModel original, Cloner cloner)
56      : base(original, cloner) {
57      mlpEnsemble = new alglib.mlpensemble();
58      string serializedEnsemble;
59      alglib.mlpeserialize(original.mlpEnsemble, out serializedEnsemble);
60      alglib.mlpeunserialize(serializedEnsemble, out this.mlpEnsemble);
61      targetVariable = original.targetVariable;
62      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
63      if (original.classValues != null)
64        this.classValues = (double[])original.classValues.Clone();
65    }
66    public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
67      : base(targetVariable) {
68      this.name = ItemName;
69      this.description = ItemDescription;
70      this.mlpEnsemble = mlpEnsemble;
71      this.targetVariable = targetVariable;
72      this.allowedInputVariables = allowedInputVariables.ToArray();
73      if (classValues != null)
74        this.classValues = (double[])classValues.Clone();
75    }
76
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new NeuralNetworkEnsembleModel(this, cloner);
79    }
80
81    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
82      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
83
84      int n = inputData.GetLength(0);
85      int columns = inputData.GetLength(1);
86      double[] x = new double[columns];
87      double[] y = new double[1];
88
89      for (int row = 0; row < n; row++) {
90        for (int column = 0; column < columns; column++) {
91          x[column] = inputData[row, column];
92        }
93        // mlpeprocess writes data in mlpEnsemble and is therefore not thread-safe
94        lock (mlpEnsembleLocker) {
95          alglib.mlpeprocess(mlpEnsemble, x, ref y);
96        }
97        yield return y[0];
98      }
99    }
100
101    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
102      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
103
104      int n = inputData.GetLength(0);
105      int columns = inputData.GetLength(1);
106      double[] x = new double[columns];
107      double[] y = new double[classValues.Length];
108
109      for (int row = 0; row < n; row++) {
110        for (int column = 0; column < columns; column++) {
111          x[column] = inputData[row, column];
112        }
113        // mlpeprocess writes data in mlpEnsemble and is therefore not thread-safe
114        lock (mlpEnsembleLocker) {
115          alglib.mlpeprocess(mlpEnsemble, x, ref y);
116        }
117        // find class for with the largest probability value
118        int maxProbClassIndex = 0;
119        double maxProb = y[0];
120        for (int i = 1; i < y.Length; i++) {
121          if (maxProb < y[i]) {
122            maxProb = y[i];
123            maxProbClassIndex = i;
124          }
125        }
126        yield return classValues[maxProbClassIndex];
127      }
128    }
129
130
131    public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
132      return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
133    }
134
135    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
136      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
137
138      var regressionProblemData = problemData as IRegressionProblemData;
139      if (regressionProblemData != null)
140        return IsProblemDataCompatible(regressionProblemData, out errorMessage);
141
142      var classificationProblemData = problemData as IClassificationProblemData;
143      if (classificationProblemData != null)
144        return IsProblemDataCompatible(classificationProblemData, out errorMessage);
145
146      throw new ArgumentException("The problem data is not compatible with this neural network ensemble. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
147    }
148
149    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
150      return new NeuralNetworkEnsembleRegressionSolution(this, new RegressionEnsembleProblemData(problemData));
151    }
152    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
153      return new NeuralNetworkEnsembleClassificationSolution(this, new ClassificationEnsembleProblemData(problemData));
154    }
155
156    #region persistence
157    [Storable]
158    private string MultiLayerPerceptronEnsembleNetwork {
159      get {
160        string serializedNetwork;
161        alglib.mlpeserialize(this.mlpEnsemble, out serializedNetwork);
162        return serializedNetwork;
163      }
164      set {
165        alglib.mlpeunserialize(value, out this.mlpEnsemble);
166      }
167    }
168
169    [Storable]
170    private double[] MultiLayerPerceptronEnsembleColumnMeans {
171      get { return mlpEnsemble.innerobj.columnmeans; }
172      set {
173        mlpEnsemble.innerobj.columnmeans = value;
174        mlpEnsemble.innerobj.network.columnmeans = value;
175      }
176    }
177    [Storable]
178    private double[] MultiLayerPerceptronEnsembleColumnSigmas {
179      get { return mlpEnsemble.innerobj.columnsigmas; }
180      set {
181        mlpEnsemble.innerobj.columnsigmas = value;
182        mlpEnsemble.innerobj.network.columnsigmas = value;
183      }
184    }
185    [Storable(OldName = "MultiLayerPerceptronEnsembleDfdnet")]
186    private double[] MultiLayerPerceptronEnsembleDfdnet {
187      set {
188        mlpEnsemble.innerobj.network.dfdnet = value;
189      }
190    }
191    [Storable]
192    private int MultiLayerPerceptronEnsembleSize {
193      get { return mlpEnsemble.innerobj.ensemblesize; }
194      set {
195        mlpEnsemble.innerobj.ensemblesize = value;
196        mlpEnsemble.innerobj.ensemblesize = value;
197      }
198    }
199    [Storable(OldName = "MultiLayerPerceptronEnsembleNeurons")]
200    private double[] MultiLayerPerceptronEnsembleNeurons {
201      set { mlpEnsemble.innerobj.network.neurons = value; }
202    }
203    [Storable(OldName = "MultiLayerPerceptronEnsembleSerializedMlp")]
204    private double[] MultiLayerPerceptronEnsembleSerializedMlp {
205      set {
206        mlpEnsemble.innerobj.network.dfdnet = value;
207      }
208    }
209    [Storable(OldName = "MultiLayerPerceptronStuctinfo")]
210    private int[] MultiLayerPerceptronStuctinfo {
211      set {
212        mlpEnsemble.innerobj.network.structinfo = value;
213      }
214    }
215
216    [Storable]
217    private double[] MultiLayerPerceptronWeights {
218      get {
219        return mlpEnsemble.innerobj.weights;
220      }
221      set {
222        mlpEnsemble.innerobj.weights = value;
223        mlpEnsemble.innerobj.network.weights = value;
224      }
225    }
226    [Storable]
227    private double[] MultiLayerPerceptronY {
228      get {
229        return mlpEnsemble.innerobj.y;
230      }
231      set {
232        mlpEnsemble.innerobj.y = value;
233        mlpEnsemble.innerobj.network.y = value;
234      }
235    }
236    #endregion
237  }
238}
Note: See TracBrowser for help on using the repository browser.