Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs @ 14877

Last change on this file since 14877 was 12509, checked in by mkommend, 9 years ago

#2276: Reintegrated branch for dataset refactoring.

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