Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16755 was 16387, checked in by gkronber, 6 years ago

#2891: merged r15739 and r16168 from trunk to stable

File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 object mlpEnsembleLocker = new object();
39    private alglib.mlpensemble mlpEnsemble;
40
41    public override IEnumerable<string> VariablesUsedForPrediction {
42      get { return allowedInputVariables; }
43    }
44
45    [Storable]
46    private string targetVariable;
47    [Storable]
48    private string[] allowedInputVariables;
49    [Storable]
50    private double[] classValues;
51    [StorableConstructor]
52    private NeuralNetworkEnsembleModel(bool deserializing)
53      : base(deserializing) {
54      if (deserializing)
55        mlpEnsemble = new alglib.mlpensemble();
56    }
57    private NeuralNetworkEnsembleModel(NeuralNetworkEnsembleModel original, Cloner cloner)
58      : base(original, cloner) {
59      mlpEnsemble = new alglib.mlpensemble();
60      string serializedEnsemble;
61      alglib.mlpeserialize(original.mlpEnsemble, out serializedEnsemble);
62      alglib.mlpeunserialize(serializedEnsemble, out this.mlpEnsemble);
63      targetVariable = original.targetVariable;
64      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
65      if (original.classValues != null)
66        this.classValues = (double[])original.classValues.Clone();
67    }
68    public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
69      : base(targetVariable) {
70      this.name = ItemName;
71      this.description = ItemDescription;
72      this.mlpEnsemble = mlpEnsemble;
73      this.targetVariable = targetVariable;
74      this.allowedInputVariables = allowedInputVariables.ToArray();
75      if (classValues != null)
76        this.classValues = (double[])classValues.Clone();
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new NeuralNetworkEnsembleModel(this, cloner);
81    }
82
83    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
84      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
85
86      int n = inputData.GetLength(0);
87      int columns = inputData.GetLength(1);
88      double[] x = new double[columns];
89      double[] y = new double[1];
90
91      for (int row = 0; row < n; row++) {
92        for (int column = 0; column < columns; column++) {
93          x[column] = inputData[row, column];
94        }
95        // mlpeprocess writes data in mlpEnsemble and is therefore not thread-safe
96        lock (mlpEnsembleLocker) {
97          alglib.mlpeprocess(mlpEnsemble, x, ref y);
98        }
99        yield return y[0];
100      }
101    }
102
103    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
104      double[,] inputData = dataset.ToArray(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[classValues.Length];
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        // mlpeprocess writes data in mlpEnsemble and is therefore not thread-safe
116        lock (mlpEnsembleLocker) {
117          alglib.mlpeprocess(mlpEnsemble, x, ref y);
118        }
119        // find class for with the largest probability value
120        int maxProbClassIndex = 0;
121        double maxProb = y[0];
122        for (int i = 1; i < y.Length; i++) {
123          if (maxProb < y[i]) {
124            maxProb = y[i];
125            maxProbClassIndex = i;
126          }
127        }
128        yield return classValues[maxProbClassIndex];
129      }
130    }
131
132    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
133      return new NeuralNetworkEnsembleRegressionSolution(this, new RegressionEnsembleProblemData(problemData));
134    }
135    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
136      return new NeuralNetworkEnsembleClassificationSolution(this, new ClassificationEnsembleProblemData(problemData));
137    }
138   
139    #region persistence
140    [Storable]
141    private string MultiLayerPerceptronEnsembleNetwork {
142      get {
143        string serializedNetwork;
144        alglib.mlpeserialize(this.mlpEnsemble, out serializedNetwork);
145        return serializedNetwork;
146      }
147      set {
148        alglib.mlpeunserialize(value, out this.mlpEnsemble);
149      }
150    }
151
152    [Storable]
153    private double[] MultiLayerPerceptronEnsembleColumnMeans {
154      get { return mlpEnsemble.innerobj.columnmeans; }
155      set {
156        mlpEnsemble.innerobj.columnmeans = value;
157        mlpEnsemble.innerobj.network.columnmeans = value;
158      }
159    }
160    [Storable]
161    private double[] MultiLayerPerceptronEnsembleColumnSigmas {
162      get { return mlpEnsemble.innerobj.columnsigmas; }
163      set {
164        mlpEnsemble.innerobj.columnsigmas = value;
165        mlpEnsemble.innerobj.network.columnsigmas = value;
166      }
167    }
168    [Storable(AllowOneWay = true)]
169    private double[] MultiLayerPerceptronEnsembleDfdnet {
170      set {
171        mlpEnsemble.innerobj.network.dfdnet = value;
172      }
173    }
174    [Storable]
175    private int MultiLayerPerceptronEnsembleSize {
176      get { return mlpEnsemble.innerobj.ensemblesize; }
177      set {
178        mlpEnsemble.innerobj.ensemblesize = value;
179        mlpEnsemble.innerobj.ensemblesize = value;
180      }
181    }
182    [Storable(AllowOneWay = true)]
183    private double[] MultiLayerPerceptronEnsembleNeurons {
184      set { mlpEnsemble.innerobj.network.neurons = value; }
185    }
186    [Storable(AllowOneWay = true)]
187    private double[] MultiLayerPerceptronEnsembleSerializedMlp {
188      set {
189        mlpEnsemble.innerobj.network.dfdnet = value;
190      }
191    }
192    [Storable(AllowOneWay = true)]
193    private int[] MultiLayerPerceptronStuctinfo {
194      set {
195        mlpEnsemble.innerobj.network.structinfo = value;
196      }
197    }
198
199    [Storable]
200    private double[] MultiLayerPerceptronWeights {
201      get {
202        return mlpEnsemble.innerobj.weights;
203      }
204      set {
205        mlpEnsemble.innerobj.weights = value;
206        mlpEnsemble.innerobj.network.weights = value;
207      }
208    }
209    [Storable]
210    private double[] MultiLayerPerceptronY {
211      get {
212        return mlpEnsemble.innerobj.y;
213      }
214      set {
215        mlpEnsemble.innerobj.y = value;
216        mlpEnsemble.innerobj.network.y = value;
217      }
218    }
219    #endregion
220  }
221}
Note: See TracBrowser for help on using the repository browser.