Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13777 was 12702, checked in by mkommend, 9 years ago

#2276: Merged changes to stable.

File size: 8.3 KB
RevLine 
[6577]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6577]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>
[6580]32  /// Represents a neural network ensembel model for regression and classification
[6577]33  /// </summary>
34  [StorableClass]
[6580]35  [Item("NeuralNetworkEnsembleModel", "Represents a neural network ensemble for regression and classification.")]
36  public sealed class NeuralNetworkEnsembleModel : NamedItem, INeuralNetworkEnsembleModel {
[6577]37
[6580]38    private alglib.mlpensemble mlpEnsemble;
39    public alglib.mlpensemble MultiLayerPerceptronEnsemble {
40      get { return mlpEnsemble; }
[6577]41      set {
[6580]42        if (value != mlpEnsemble) {
[6577]43          if (value == null) throw new ArgumentNullException();
[6580]44          mlpEnsemble = value;
[6577]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]
[6580]57    private NeuralNetworkEnsembleModel(bool deserializing)
[6577]58      : base(deserializing) {
59      if (deserializing)
[6580]60        mlpEnsemble = new alglib.mlpensemble();
[6577]61    }
[6580]62    private NeuralNetworkEnsembleModel(NeuralNetworkEnsembleModel original, Cloner cloner)
[6577]63      : base(original, cloner) {
[6580]64      mlpEnsemble = new alglib.mlpensemble();
[7694]65      string serializedEnsemble;
66      alglib.mlpeserialize(original.mlpEnsemble, out serializedEnsemble);
67      alglib.mlpeunserialize(serializedEnsemble, out this.mlpEnsemble);
[6577]68      targetVariable = original.targetVariable;
69      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
70      if (original.classValues != null)
71        this.classValues = (double[])original.classValues.Clone();
72    }
[6580]73    public NeuralNetworkEnsembleModel(alglib.mlpensemble mlpEnsemble, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
[6577]74      : base() {
75      this.name = ItemName;
76      this.description = ItemDescription;
[6580]77      this.mlpEnsemble = mlpEnsemble;
[6577]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) {
[6580]85      return new NeuralNetworkEnsembleModel(this, cloner);
[6577]86    }
87
[12702]88    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
[6577]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        }
[6580]100        alglib.mlpeprocess(mlpEnsemble, x, ref y);
[6577]101        yield return y[0];
102      }
103    }
104
[12702]105    public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
[6577]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        }
[6580]117        alglib.mlpeprocess(mlpEnsemble, x, ref y);
[6577]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
[6603]131    public INeuralNetworkEnsembleRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
[8528]132      return new NeuralNetworkEnsembleRegressionSolution(new RegressionEnsembleProblemData(problemData), this);
[6603]133    }
134    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
135      return CreateRegressionSolution(problemData);
136    }
[6604]137    public INeuralNetworkEnsembleClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
[8528]138      return new NeuralNetworkEnsembleClassificationSolution(new ClassificationEnsembleProblemData(problemData), this);
[6604]139    }
140    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
141      return CreateClassificationSolution(problemData);
142    }
[6603]143
[6577]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]
[7694]155    private string MultiLayerPerceptronEnsembleNetwork {
[6577]156      get {
[7694]157        string serializedNetwork;
158        alglib.mlpeserialize(this.mlpEnsemble, out serializedNetwork);
159        return serializedNetwork;
[6577]160      }
161      set {
[7694]162        alglib.mlpeunserialize(value, out this.mlpEnsemble);
163      }
164    }
165
166    [Storable]
167    private double[] MultiLayerPerceptronEnsembleColumnMeans {
168      get { return mlpEnsemble.innerobj.columnmeans; }
169      set {
[6580]170        mlpEnsemble.innerobj.columnmeans = value;
[7694]171        mlpEnsemble.innerobj.network.columnmeans = value;
[6577]172      }
173    }
174    [Storable]
[6580]175    private double[] MultiLayerPerceptronEnsembleColumnSigmas {
[7694]176      get { return mlpEnsemble.innerobj.columnsigmas; }
[6577]177      set {
[6580]178        mlpEnsemble.innerobj.columnsigmas = value;
[7694]179        mlpEnsemble.innerobj.network.columnsigmas = value;
[6577]180      }
181    }
[7694]182    [Storable(AllowOneWay = true)]
[6580]183    private double[] MultiLayerPerceptronEnsembleDfdnet {
[6577]184      set {
[7694]185        mlpEnsemble.innerobj.network.dfdnet = value;
[6577]186      }
187    }
188    [Storable]
[6580]189    private int MultiLayerPerceptronEnsembleSize {
[7694]190      get { return mlpEnsemble.innerobj.ensemblesize; }
[6577]191      set {
[6580]192        mlpEnsemble.innerobj.ensemblesize = value;
[7694]193        mlpEnsemble.innerobj.ensemblesize = value;
[6577]194      }
195    }
[7694]196    [Storable(AllowOneWay = true)]
[6580]197    private double[] MultiLayerPerceptronEnsembleNeurons {
[7694]198      set { mlpEnsemble.innerobj.network.neurons = value; }
[6577]199    }
[7694]200    [Storable(AllowOneWay = true)]
[6580]201    private double[] MultiLayerPerceptronEnsembleSerializedMlp {
202      set {
[7694]203        mlpEnsemble.innerobj.network.dfdnet = value;
[6580]204      }
205    }
[7694]206    [Storable(AllowOneWay = true)]
[6577]207    private int[] MultiLayerPerceptronStuctinfo {
208      set {
[7694]209        mlpEnsemble.innerobj.network.structinfo = value;
[6577]210      }
211    }
[6580]212
213    [Storable]
214    private double[] MultiLayerPerceptronWeights {
215      get {
216        return mlpEnsemble.innerobj.weights;
217      }
218      set {
219        mlpEnsemble.innerobj.weights = value;
[7694]220        mlpEnsemble.innerobj.network.weights = value;
[6580]221      }
222    }
223    [Storable]
[6577]224    private double[] MultiLayerPerceptronY {
225      get {
[6580]226        return mlpEnsemble.innerobj.y;
[6577]227      }
228      set {
[6580]229        mlpEnsemble.innerobj.y = value;
[7694]230        mlpEnsemble.innerobj.network.y = value;
[6577]231      }
232    }
233    #endregion
234  }
235}
Note: See TracBrowser for help on using the repository browser.