Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13921 was 13921, checked in by bburlacu, 8 years ago

#2604: Revert changes to DataAnalysisSolution and IDataAnalysisSolution and implement the desired properties in model classes that implement IDataAnalysisModel, IRegressionModel and IClassificationModel.

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