Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkModel.cs @ 12790

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

#2435: updated alglib to version 3.9.0

File size: 9.4 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 model for regression and classification
33  /// </summary>
34  [StorableClass]
35  [Item("NeuralNetworkModel", "Represents a neural network for regression and classification.")]
36  public sealed class NeuralNetworkModel : NamedItem, INeuralNetworkModel {
37
38    private alglib.multilayerperceptron multiLayerPerceptron;
39    public alglib.multilayerperceptron MultiLayerPerceptron {
40      get { return multiLayerPerceptron; }
41      set {
42        if (value != multiLayerPerceptron) {
43          if (value == null) throw new ArgumentNullException();
44          multiLayerPerceptron = 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 NeuralNetworkModel(bool deserializing)
58      : base(deserializing) {
59      if (deserializing)
60        multiLayerPerceptron = new alglib.multilayerperceptron();
61    }
62    private NeuralNetworkModel(NeuralNetworkModel original, Cloner cloner)
63      : base(original, cloner) {
64      multiLayerPerceptron = new alglib.multilayerperceptron();
65
66      multiLayerPerceptron.innerobj.columnmeans = (double[])original.multiLayerPerceptron.innerobj.columnmeans.Clone();
67      multiLayerPerceptron.innerobj.columnsigmas = (double[])original.multiLayerPerceptron.innerobj.columnsigmas.Clone();
68      multiLayerPerceptron.innerobj.derror = (double[])original.multiLayerPerceptron.innerobj.derror.Clone();
69      multiLayerPerceptron.innerobj.dfdnet = (double[])original.multiLayerPerceptron.innerobj.dfdnet.Clone();
70      multiLayerPerceptron.innerobj.neurons = (double[])original.multiLayerPerceptron.innerobj.neurons.Clone();
71      multiLayerPerceptron.innerobj.nwbuf = (double[])original.multiLayerPerceptron.innerobj.nwbuf.Clone();
72      multiLayerPerceptron.innerobj.structinfo = (int[])original.multiLayerPerceptron.innerobj.structinfo.Clone();
73      multiLayerPerceptron.innerobj.weights = (double[])original.multiLayerPerceptron.innerobj.weights.Clone();
74      multiLayerPerceptron.innerobj.x = (double[])original.multiLayerPerceptron.innerobj.x.Clone();
75      multiLayerPerceptron.innerobj.y = (double[])original.multiLayerPerceptron.innerobj.y.Clone();
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 NeuralNetworkModel(alglib.multilayerperceptron multiLayerPerceptron, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
82      : base() {
83      this.name = ItemName;
84      this.description = ItemDescription;
85      this.multiLayerPerceptron = multiLayerPerceptron;
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 NeuralNetworkModel(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.mlpprocess(multiLayerPerceptron, 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.mlpprocess(multiLayerPerceptron, 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 INeuralNetworkRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
140      return new NeuralNetworkRegressionSolution(new RegressionProblemData(problemData), this);
141    }
142    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
143      return CreateRegressionSolution(problemData);
144    }
145    public INeuralNetworkClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
146      return new NeuralNetworkClassificationSolution(new ClassificationProblemData(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 double[,] MultiLayerPerceptronChunks {
164      get {
165        // ignore this property in alglib version >= 3.9.0
166        return null;
167      }
168      set {
169        // ignore this property in alglib version >= 3.9.0
170      }
171    }
172    [Storable]
173    private double[] MultiLayerPerceptronColumnMeans {
174      get {
175        return multiLayerPerceptron.innerobj.columnmeans;
176      }
177      set {
178        multiLayerPerceptron.innerobj.columnmeans = value;
179      }
180    }
181    [Storable]
182    private double[] MultiLayerPerceptronColumnSigmas {
183      get {
184        return multiLayerPerceptron.innerobj.columnsigmas;
185      }
186      set {
187        multiLayerPerceptron.innerobj.columnsigmas = value;
188      }
189    }
190    [Storable]
191    private double[] MultiLayerPerceptronDError {
192      get {
193        return multiLayerPerceptron.innerobj.derror;
194      }
195      set {
196        multiLayerPerceptron.innerobj.derror = value;
197      }
198    }
199    [Storable]
200    private double[] MultiLayerPerceptronDfdnet {
201      get {
202        return multiLayerPerceptron.innerobj.dfdnet;
203      }
204      set {
205        multiLayerPerceptron.innerobj.dfdnet = value;
206      }
207    }
208    [Storable]
209    private double[] MultiLayerPerceptronNeurons {
210      get {
211        return multiLayerPerceptron.innerobj.neurons;
212      }
213      set {
214        multiLayerPerceptron.innerobj.neurons = value;
215      }
216    }
217    [Storable]
218    private double[] MultiLayerPerceptronNwbuf {
219      get {
220        return multiLayerPerceptron.innerobj.nwbuf;
221      }
222      set {
223        multiLayerPerceptron.innerobj.nwbuf = value;
224      }
225    }
226    [Storable]
227    private int[] MultiLayerPerceptronStuctinfo {
228      get {
229        return multiLayerPerceptron.innerobj.structinfo;
230      }
231      set {
232        multiLayerPerceptron.innerobj.structinfo = value;
233      }
234    }
235    [Storable]
236    private double[] MultiLayerPerceptronWeights {
237      get {
238        return multiLayerPerceptron.innerobj.weights;
239      }
240      set {
241        multiLayerPerceptron.innerobj.weights = value;
242      }
243    }
244    [Storable]
245    private double[] MultiLayerPerceptronX {
246      get {
247        return multiLayerPerceptron.innerobj.x;
248      }
249      set {
250        multiLayerPerceptron.innerobj.x = value;
251      }
252    }
253    [Storable]
254    private double[] MultiLayerPerceptronY {
255      get {
256        return multiLayerPerceptron.innerobj.y;
257      }
258      set {
259        multiLayerPerceptron.innerobj.y = value;
260      }
261    }
262    #endregion
263  }
264}
Note: See TracBrowser for help on using the repository browser.