Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs @ 18242

Last change on this file since 18242 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 11.2 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    public INeuralNetworkEnsembleClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
153      return new NeuralNetworkEnsembleClassificationSolution(problemData, this);
154    }
155    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
156      return CreateClassificationSolution(problemData);
157    }
158
159    #region events
160    public event EventHandler Changed;
161    private void OnChanged(EventArgs e) {
162      var handlers = Changed;
163      if (handlers != null)
164        handlers(this, e);
165    }
166    #endregion
167
168    #region persistence
169    [Storable]
170    private double[] MultiLayerPerceptronEnsembleColumnMeans {
171      get {
172        return mlpEnsemble.innerobj.columnmeans;
173      }
174      set {
175        mlpEnsemble.innerobj.columnmeans = value;
176      }
177    }
178    [Storable]
179    private double[] MultiLayerPerceptronEnsembleColumnSigmas {
180      get {
181        return mlpEnsemble.innerobj.columnsigmas;
182      }
183      set {
184        mlpEnsemble.innerobj.columnsigmas = value;
185      }
186    }
187    [Storable]
188    private double[] MultiLayerPerceptronEnsembleDfdnet {
189      get {
190        return mlpEnsemble.innerobj.dfdnet;
191      }
192      set {
193        mlpEnsemble.innerobj.dfdnet = value;
194      }
195    }
196    [Storable]
197    private int MultiLayerPerceptronEnsembleSize {
198      get {
199        return mlpEnsemble.innerobj.ensemblesize;
200      }
201      set {
202        mlpEnsemble.innerobj.ensemblesize = value;
203      }
204    }
205    [Storable]
206    private bool MultiLayerPerceptronEnsembleIsSoftMax {
207      get {
208        return mlpEnsemble.innerobj.issoftmax;
209      }
210      set {
211        mlpEnsemble.innerobj.issoftmax = value;
212      }
213    }
214    [Storable]
215    private double[] MultiLayerPerceptronEnsembleNeurons {
216      get {
217        return mlpEnsemble.innerobj.neurons;
218      }
219      set {
220        mlpEnsemble.innerobj.neurons = value;
221      }
222    }
223    [Storable]
224    private int MultiLayerPerceptronEnsembleNin {
225      get {
226        return mlpEnsemble.innerobj.nin;
227      }
228      set {
229        mlpEnsemble.innerobj.nin = value;
230      }
231    }
232    [Storable]
233    private int MultiLayerPerceptronEnsembleNout {
234      get {
235        return mlpEnsemble.innerobj.nout;
236      }
237      set {
238        mlpEnsemble.innerobj.nout = value;
239      }
240    }
241    [Storable]
242    private bool MultiLayerPerceptronEnsemblePostprocessing {
243      get {
244        return mlpEnsemble.innerobj.postprocessing;
245      }
246      set {
247        mlpEnsemble.innerobj.postprocessing = value;
248      }
249    }
250    [Storable]
251    private int MultiLayerPerceptronEnsembleSerializedLen {
252      get {
253        return mlpEnsemble.innerobj.serializedlen;
254      }
255      set {
256        mlpEnsemble.innerobj.serializedlen = value;
257      }
258    }
259    [Storable]
260    private double[] MultiLayerPerceptronEnsembleSerializedMlp {
261      get {
262        return mlpEnsemble.innerobj.serializedmlp;
263      }
264      set {
265        mlpEnsemble.innerobj.serializedmlp = value;
266      }
267    }
268    [Storable]
269    private int[] MultiLayerPerceptronStuctinfo {
270      get {
271        return mlpEnsemble.innerobj.structinfo;
272      }
273      set {
274        mlpEnsemble.innerobj.structinfo = value;
275      }
276    }
277    [Storable]
278    private double[] MultiLayerPerceptronEnsembleTmpMeans {
279      get {
280        return mlpEnsemble.innerobj.tmpmeans;
281      }
282      set {
283        mlpEnsemble.innerobj.tmpmeans = value;
284      }
285    }
286    [Storable]
287    private double[] MultiLayerPerceptronEnsembleTmpSigmas {
288      get {
289        return mlpEnsemble.innerobj.tmpsigmas;
290      }
291      set {
292        mlpEnsemble.innerobj.tmpsigmas = value;
293      }
294    }
295    [Storable]
296    private double[] MultiLayerPerceptronEnsembleTmpWeights {
297      get {
298        return mlpEnsemble.innerobj.tmpweights;
299      }
300      set {
301        mlpEnsemble.innerobj.tmpweights = value;
302      }
303    }
304    [Storable]
305    private int MultiLayerPerceptronEnsembleWCount {
306      get {
307        return mlpEnsemble.innerobj.wcount;
308      }
309      set {
310        mlpEnsemble.innerobj.wcount = value;
311      }
312    }
313
314    [Storable]
315    private double[] MultiLayerPerceptronWeights {
316      get {
317        return mlpEnsemble.innerobj.weights;
318      }
319      set {
320        mlpEnsemble.innerobj.weights = value;
321      }
322    }
323    [Storable]
324    private double[] MultiLayerPerceptronY {
325      get {
326        return mlpEnsemble.innerobj.y;
327      }
328      set {
329        mlpEnsemble.innerobj.y = value;
330      }
331    }
332    #endregion
333  }
334}
Note: See TracBrowser for help on using the repository browser.