Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs @ 15802

Last change on this file since 15802 was 15018, checked in by gkronber, 8 years ago

#2520 introduced StorableConstructorFlag type for StorableConstructors

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
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("0d6dfe68-7903-4f2f-af46-e71ae2fbaf2d")]
35  [Item("NeuralNetworkEnsembleModel", "Represents a neural network ensemble for regression and classification.")]
36  public sealed class NeuralNetworkEnsembleModel : ClassificationModel, INeuralNetworkEnsembleModel {
37
38    private alglib.mlpensemble mlpEnsemble = new alglib.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    public override IEnumerable<string> VariablesUsedForPrediction {
51      get { return allowedInputVariables; }
52    }
53
54    [Storable]
55    private string targetVariable;
56    [Storable]
57    private string[] allowedInputVariables;
58    [Storable]
59    private double[] classValues;
60    [StorableConstructor]
61    private NeuralNetworkEnsembleModel(StorableConstructorFlag deserializing)
62      : base(deserializing) {
63    }
64    private NeuralNetworkEnsembleModel(NeuralNetworkEnsembleModel original, Cloner cloner)
65      : base(original, cloner) {
66      mlpEnsemble = new alglib.mlpensemble();
67      string serializedEnsemble;
68      alglib.mlpeserialize(original.mlpEnsemble, out serializedEnsemble);
69      alglib.mlpeunserialize(serializedEnsemble, out this.mlpEnsemble);
70      targetVariable = original.targetVariable;
71      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
72      if (original.classValues != null)
73        this.classValues = (double[])original.classValues.Clone();
74    }
75    public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
76      : base(targetVariable) {
77      this.name = ItemName;
78      this.description = ItemDescription;
79      this.mlpEnsemble = mlpEnsemble;
80      this.targetVariable = targetVariable;
81      this.allowedInputVariables = allowedInputVariables.ToArray();
82      if (classValues != null)
83        this.classValues = (double[])classValues.Clone();
84    }
85
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new NeuralNetworkEnsembleModel(this, cloner);
88    }
89
90    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
91      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
92
93      int n = inputData.GetLength(0);
94      int columns = inputData.GetLength(1);
95      double[] x = new double[columns];
96      double[] y = new double[1];
97
98      for (int row = 0; row < n; row++) {
99        for (int column = 0; column < columns; column++) {
100          x[column] = inputData[row, column];
101        }
102        alglib.mlpeprocess(mlpEnsemble, x, ref y);
103        yield return y[0];
104      }
105    }
106
107    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
108      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
109
110      int n = inputData.GetLength(0);
111      int columns = inputData.GetLength(1);
112      double[] x = new double[columns];
113      double[] y = new double[classValues.Length];
114
115      for (int row = 0; row < n; row++) {
116        for (int column = 0; column < columns; column++) {
117          x[column] = inputData[row, column];
118        }
119        alglib.mlpeprocess(mlpEnsemble, x, ref y);
120        // find class for with the largest probability value
121        int maxProbClassIndex = 0;
122        double maxProb = y[0];
123        for (int i = 1; i < y.Length; i++) {
124          if (maxProb < y[i]) {
125            maxProb = y[i];
126            maxProbClassIndex = i;
127          }
128        }
129        yield return classValues[maxProbClassIndex];
130      }
131    }
132
133    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
134      return new NeuralNetworkEnsembleRegressionSolution(this, new RegressionEnsembleProblemData(problemData));
135    }
136    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
137      return new NeuralNetworkEnsembleClassificationSolution(this, new ClassificationEnsembleProblemData(problemData));
138    }
139
140    #region events
141    public event EventHandler Changed;
142    private void OnChanged(EventArgs e) {
143      var handlers = Changed;
144      if (handlers != null)
145        handlers(this, e);
146    }
147    #endregion
148
149    #region persistence
150    [Storable]
151    private string MultiLayerPerceptronEnsembleNetwork {
152      get {
153        string serializedNetwork;
154        alglib.mlpeserialize(this.mlpEnsemble, out serializedNetwork);
155        return serializedNetwork;
156      }
157      set {
158        alglib.mlpeunserialize(value, out this.mlpEnsemble);
159      }
160    }
161
162    [Storable]
163    private double[] MultiLayerPerceptronEnsembleColumnMeans {
164      get { return mlpEnsemble.innerobj.columnmeans; }
165      set {
166        mlpEnsemble.innerobj.columnmeans = value;
167        mlpEnsemble.innerobj.network.columnmeans = value;
168      }
169    }
170    [Storable]
171    private double[] MultiLayerPerceptronEnsembleColumnSigmas {
172      get { return mlpEnsemble.innerobj.columnsigmas; }
173      set {
174        mlpEnsemble.innerobj.columnsigmas = value;
175        mlpEnsemble.innerobj.network.columnsigmas = value;
176      }
177    }
178    [Storable(AllowOneWay = true)]
179    private double[] MultiLayerPerceptronEnsembleDfdnet {
180      set {
181        mlpEnsemble.innerobj.network.dfdnet = value;
182      }
183    }
184    [Storable]
185    private int MultiLayerPerceptronEnsembleSize {
186      get { return mlpEnsemble.innerobj.ensemblesize; }
187      set {
188        mlpEnsemble.innerobj.ensemblesize = value;
189        mlpEnsemble.innerobj.ensemblesize = value;
190      }
191    }
192    [Storable(AllowOneWay = true)]
193    private double[] MultiLayerPerceptronEnsembleNeurons {
194      set { mlpEnsemble.innerobj.network.neurons = value; }
195    }
196    [Storable(AllowOneWay = true)]
197    private double[] MultiLayerPerceptronEnsembleSerializedMlp {
198      set {
199        mlpEnsemble.innerobj.network.dfdnet = value;
200      }
201    }
202    [Storable(AllowOneWay = true)]
203    private int[] MultiLayerPerceptronStuctinfo {
204      set {
205        mlpEnsemble.innerobj.network.structinfo = value;
206      }
207    }
208
209    [Storable]
210    private double[] MultiLayerPerceptronWeights {
211      get {
212        return mlpEnsemble.innerobj.weights;
213      }
214      set {
215        mlpEnsemble.innerobj.weights = value;
216        mlpEnsemble.innerobj.network.weights = value;
217      }
218    }
219    [Storable]
220    private double[] MultiLayerPerceptronY {
221      get {
222        return mlpEnsemble.innerobj.y;
223      }
224      set {
225        mlpEnsemble.innerobj.y = value;
226        mlpEnsemble.innerobj.network.y = value;
227      }
228    }
229    #endregion
230  }
231}
Note: See TracBrowser for help on using the repository browser.