Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs @ 15325

Last change on this file since 15325 was 15142, checked in by gkronber, 7 years ago

#2697: merged r14843 (resolving conflicts in csproj file for HL.Algorithms.DataAnalysis because MCTS has been removed)

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