Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs @ 6603

Last change on this file since 6603 was 6603, checked in by mkommend, 13 years ago

#1600: Added possibility to create regression solutions from regression models.

File size: 10.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
35  [Item("NeuralNetworkEnsembleModel", "Represents a neural network ensemble for regression and classification.")]
36  public sealed class NeuralNetworkEnsembleModel : NamedItem, INeuralNetworkEnsembleModel {
37
38    private alglib.mlpensemble mlpEnsemble;
39    public alglib.mlpensemble MultiLayerPerceptronEnsemble {
40      get { return mlpEnsemble; }
41      set {
42        if (value != mlpEnsemble) {
43          if (value == null) throw new ArgumentNullException();
44          mlpEnsemble = value;
45          OnChanged(EventArgs.Empty);
46        }
47      }
48    }
49
50    [Storable]
51    private string targetVariable;
52    [Storable]
53    private string[] allowedInputVariables;
54    [Storable]
55    private double[] classValues;
56    [StorableConstructor]
57    private NeuralNetworkEnsembleModel(bool deserializing)
58      : base(deserializing) {
59      if (deserializing)
60        mlpEnsemble = new alglib.mlpensemble();
61    }
62    private NeuralNetworkEnsembleModel(NeuralNetworkEnsembleModel original, Cloner cloner)
63      : base(original, cloner) {
64      mlpEnsemble = new alglib.mlpensemble();
65      mlpEnsemble.innerobj.columnmeans = (double[])original.mlpEnsemble.innerobj.columnmeans.Clone();
66      mlpEnsemble.innerobj.columnsigmas = (double[])original.mlpEnsemble.innerobj.columnsigmas.Clone();
67      mlpEnsemble.innerobj.dfdnet = (double[])original.mlpEnsemble.innerobj.dfdnet.Clone();
68      mlpEnsemble.innerobj.ensemblesize = original.mlpEnsemble.innerobj.ensemblesize;
69      mlpEnsemble.innerobj.issoftmax = original.mlpEnsemble.innerobj.issoftmax;
70      mlpEnsemble.innerobj.neurons = (double[])original.mlpEnsemble.innerobj.neurons.Clone();
71      mlpEnsemble.innerobj.nin = original.mlpEnsemble.innerobj.nin;
72      mlpEnsemble.innerobj.nout = original.mlpEnsemble.innerobj.nout;
73      mlpEnsemble.innerobj.postprocessing = original.mlpEnsemble.innerobj.postprocessing;
74      mlpEnsemble.innerobj.serializedlen = original.mlpEnsemble.innerobj.serializedlen;
75      mlpEnsemble.innerobj.serializedmlp = (double[])original.mlpEnsemble.innerobj.serializedmlp.Clone();
76      mlpEnsemble.innerobj.structinfo = (int[])original.mlpEnsemble.innerobj.structinfo.Clone();
77      mlpEnsemble.innerobj.tmpmeans = (double[])original.mlpEnsemble.innerobj.tmpmeans.Clone();
78      mlpEnsemble.innerobj.tmpsigmas = (double[])original.mlpEnsemble.innerobj.tmpsigmas.Clone();
79      mlpEnsemble.innerobj.tmpweights = (double[])original.mlpEnsemble.innerobj.tmpweights.Clone();
80      mlpEnsemble.innerobj.wcount = original.mlpEnsemble.innerobj.wcount;
81      mlpEnsemble.innerobj.weights = (double[])original.mlpEnsemble.innerobj.weights.Clone();
82      mlpEnsemble.innerobj.y = (double[])original.mlpEnsemble.innerobj.y.Clone();
83      targetVariable = original.targetVariable;
84      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
85      if (original.classValues != null)
86        this.classValues = (double[])original.classValues.Clone();
87    }
88    public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
89      : base() {
90      this.name = ItemName;
91      this.description = ItemDescription;
92      this.mlpEnsemble = mlpEnsemble;
93      this.targetVariable = targetVariable;
94      this.allowedInputVariables = allowedInputVariables.ToArray();
95      if (classValues != null)
96        this.classValues = (double[])classValues.Clone();
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new NeuralNetworkEnsembleModel(this, cloner);
101    }
102
103    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
104      double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
105
106      int n = inputData.GetLength(0);
107      int columns = inputData.GetLength(1);
108      double[] x = new double[columns];
109      double[] y = new double[1];
110
111      for (int row = 0; row < n; row++) {
112        for (int column = 0; column < columns; column++) {
113          x[column] = inputData[row, column];
114        }
115        alglib.mlpeprocess(mlpEnsemble, x, ref y);
116        yield return y[0];
117      }
118    }
119
120    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
121      double[,] inputData = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
122
123      int n = inputData.GetLength(0);
124      int columns = inputData.GetLength(1);
125      double[] x = new double[columns];
126      double[] y = new double[classValues.Length];
127
128      for (int row = 0; row < n; row++) {
129        for (int column = 0; column < columns; column++) {
130          x[column] = inputData[row, column];
131        }
132        alglib.mlpeprocess(mlpEnsemble, x, ref y);
133        // find class for with the largest probability value
134        int maxProbClassIndex = 0;
135        double maxProb = y[0];
136        for (int i = 1; i < y.Length; i++) {
137          if (maxProb < y[i]) {
138            maxProb = y[i];
139            maxProbClassIndex = i;
140          }
141        }
142        yield return classValues[maxProbClassIndex];
143      }
144    }
145
146    public INeuralNetworkEnsembleRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
147      return new NeuralNetworkEnsembleRegressionSolution(problemData, this);
148    }
149    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
150      return CreateRegressionSolution(problemData);
151    }
152
153    #region events
154    public event EventHandler Changed;
155    private void OnChanged(EventArgs e) {
156      var handlers = Changed;
157      if (handlers != null)
158        handlers(this, e);
159    }
160    #endregion
161
162    #region persistence
163    [Storable]
164    private double[] MultiLayerPerceptronEnsembleColumnMeans {
165      get {
166        return mlpEnsemble.innerobj.columnmeans;
167      }
168      set {
169        mlpEnsemble.innerobj.columnmeans = value;
170      }
171    }
172    [Storable]
173    private double[] MultiLayerPerceptronEnsembleColumnSigmas {
174      get {
175        return mlpEnsemble.innerobj.columnsigmas;
176      }
177      set {
178        mlpEnsemble.innerobj.columnsigmas = value;
179      }
180    }
181    [Storable]
182    private double[] MultiLayerPerceptronEnsembleDfdnet {
183      get {
184        return mlpEnsemble.innerobj.dfdnet;
185      }
186      set {
187        mlpEnsemble.innerobj.dfdnet = value;
188      }
189    }
190    [Storable]
191    private int MultiLayerPerceptronEnsembleSize {
192      get {
193        return mlpEnsemble.innerobj.ensemblesize;
194      }
195      set {
196        mlpEnsemble.innerobj.ensemblesize = value;
197      }
198    }
199    [Storable]
200    private bool MultiLayerPerceptronEnsembleIsSoftMax {
201      get {
202        return mlpEnsemble.innerobj.issoftmax;
203      }
204      set {
205        mlpEnsemble.innerobj.issoftmax = value;
206      }
207    }
208    [Storable]
209    private double[] MultiLayerPerceptronEnsembleNeurons {
210      get {
211        return mlpEnsemble.innerobj.neurons;
212      }
213      set {
214        mlpEnsemble.innerobj.neurons = value;
215      }
216    }
217    [Storable]
218    private int MultiLayerPerceptronEnsembleNin {
219      get {
220        return mlpEnsemble.innerobj.nin;
221      }
222      set {
223        mlpEnsemble.innerobj.nin = value;
224      }
225    }
226    [Storable]
227    private int MultiLayerPerceptronEnsembleNout {
228      get {
229        return mlpEnsemble.innerobj.nout;
230      }
231      set {
232        mlpEnsemble.innerobj.nout = value;
233      }
234    }
235    [Storable]
236    private bool MultiLayerPerceptronEnsemblePostprocessing {
237      get {
238        return mlpEnsemble.innerobj.postprocessing;
239      }
240      set {
241        mlpEnsemble.innerobj.postprocessing = value;
242      }
243    }
244    [Storable]
245    private int MultiLayerPerceptronEnsembleSerializedLen {
246      get {
247        return mlpEnsemble.innerobj.serializedlen;
248      }
249      set {
250        mlpEnsemble.innerobj.serializedlen = value;
251      }
252    }
253    [Storable]
254    private double[] MultiLayerPerceptronEnsembleSerializedMlp {
255      get {
256        return mlpEnsemble.innerobj.serializedmlp;
257      }
258      set {
259        mlpEnsemble.innerobj.serializedmlp = value;
260      }
261    }
262    [Storable]
263    private int[] MultiLayerPerceptronStuctinfo {
264      get {
265        return mlpEnsemble.innerobj.structinfo;
266      }
267      set {
268        mlpEnsemble.innerobj.structinfo = value;
269      }
270    }
271    [Storable]
272    private double[] MultiLayerPerceptronEnsembleTmpMeans {
273      get {
274        return mlpEnsemble.innerobj.tmpmeans;
275      }
276      set {
277        mlpEnsemble.innerobj.tmpmeans = value;
278      }
279    }
280    [Storable]
281    private double[] MultiLayerPerceptronEnsembleTmpSigmas {
282      get {
283        return mlpEnsemble.innerobj.tmpsigmas;
284      }
285      set {
286        mlpEnsemble.innerobj.tmpsigmas = value;
287      }
288    }
289    [Storable]
290    private double[] MultiLayerPerceptronEnsembleTmpWeights {
291      get {
292        return mlpEnsemble.innerobj.tmpweights;
293      }
294      set {
295        mlpEnsemble.innerobj.tmpweights = value;
296      }
297    }
298    [Storable]
299    private int MultiLayerPerceptronEnsembleWCount {
300      get {
301        return mlpEnsemble.innerobj.wcount;
302      }
303      set {
304        mlpEnsemble.innerobj.wcount = value;
305      }
306    }
307
308    [Storable]
309    private double[] MultiLayerPerceptronWeights {
310      get {
311        return mlpEnsemble.innerobj.weights;
312      }
313      set {
314        mlpEnsemble.innerobj.weights = value;
315      }
316    }
317    [Storable]
318    private double[] MultiLayerPerceptronY {
319      get {
320        return mlpEnsemble.innerobj.y;
321      }
322      set {
323        mlpEnsemble.innerobj.y = value;
324      }
325    }
326    #endregion
327  }
328}
Note: See TracBrowser for help on using the repository browser.