Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkModel.cs @ 17912

Last change on this file since 17912 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 9.4 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 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 : ClassificationModel, 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    public override IEnumerable<string> VariablesUsedForPrediction {
51      get { return allowedInputVariables; }
52    }
53
54    [Storable]
55    private string[] allowedInputVariables;
56    [Storable]
57    private double[] classValues;
58    [StorableConstructor]
59    private NeuralNetworkModel(bool deserializing)
60      : base(deserializing) {
61      if (deserializing)
62        multiLayerPerceptron = new alglib.multilayerperceptron();
63    }
64    private NeuralNetworkModel(NeuralNetworkModel original, Cloner cloner)
65      : base(original, cloner) {
66      multiLayerPerceptron = new alglib.multilayerperceptron();
67      multiLayerPerceptron.innerobj.chunks = (double[,])original.multiLayerPerceptron.innerobj.chunks.Clone();
68      multiLayerPerceptron.innerobj.columnmeans = (double[])original.multiLayerPerceptron.innerobj.columnmeans.Clone();
69      multiLayerPerceptron.innerobj.columnsigmas = (double[])original.multiLayerPerceptron.innerobj.columnsigmas.Clone();
70      multiLayerPerceptron.innerobj.derror = (double[])original.multiLayerPerceptron.innerobj.derror.Clone();
71      multiLayerPerceptron.innerobj.dfdnet = (double[])original.multiLayerPerceptron.innerobj.dfdnet.Clone();
72      multiLayerPerceptron.innerobj.neurons = (double[])original.multiLayerPerceptron.innerobj.neurons.Clone();
73      multiLayerPerceptron.innerobj.nwbuf = (double[])original.multiLayerPerceptron.innerobj.nwbuf.Clone();
74      multiLayerPerceptron.innerobj.structinfo = (int[])original.multiLayerPerceptron.innerobj.structinfo.Clone();
75      multiLayerPerceptron.innerobj.weights = (double[])original.multiLayerPerceptron.innerobj.weights.Clone();
76      multiLayerPerceptron.innerobj.x = (double[])original.multiLayerPerceptron.innerobj.x.Clone();
77      multiLayerPerceptron.innerobj.y = (double[])original.multiLayerPerceptron.innerobj.y.Clone();
78      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
79      if (original.classValues != null)
80        this.classValues = (double[])original.classValues.Clone();
81    }
82    public NeuralNetworkModel(alglib.multilayerperceptron multiLayerPerceptron, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues = null)
83      : base(targetVariable) {
84      this.name = ItemName;
85      this.description = ItemDescription;
86      this.multiLayerPerceptron = multiLayerPerceptron;
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 = dataset.ToArray(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        // NOTE: mlpprocess changes data in multiLayerPerceptron and is therefore not thread-save!
109        lock (multiLayerPerceptron) {
110          alglib.mlpprocess(multiLayerPerceptron, x, ref y);
111        }
112        yield return y[0];
113      }
114    }
115
116    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
117      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
118
119      int n = inputData.GetLength(0);
120      int columns = inputData.GetLength(1);
121      double[] x = new double[columns];
122      double[] y = new double[classValues.Length];
123
124      for (int row = 0; row < n; row++) {
125        for (int column = 0; column < columns; column++) {
126          x[column] = inputData[row, column];
127        }
128        // NOTE: mlpprocess changes data in multiLayerPerceptron and is therefore not thread-save!
129        lock (multiLayerPerceptron) {
130          alglib.mlpprocess(multiLayerPerceptron, x, ref y);
131        }
132        // find class for with the largest probability value
133        int maxProbClassIndex = 0;
134        double maxProb = y[0];
135        for (int i = 1; i < y.Length; i++) {
136          if (maxProb < y[i]) {
137            maxProb = y[i];
138            maxProbClassIndex = i;
139          }
140        }
141        yield return classValues[maxProbClassIndex];
142      }
143    }
144
145    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
146      return new NeuralNetworkRegressionSolution(this, new RegressionProblemData(problemData));
147    }
148    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
149      return new NeuralNetworkClassificationSolution(this, new ClassificationProblemData(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        return multiLayerPerceptron.innerobj.chunks;
166      }
167      set {
168        multiLayerPerceptron.innerobj.chunks = value;
169      }
170    }
171    [Storable]
172    private double[] MultiLayerPerceptronColumnMeans {
173      get {
174        return multiLayerPerceptron.innerobj.columnmeans;
175      }
176      set {
177        multiLayerPerceptron.innerobj.columnmeans = value;
178      }
179    }
180    [Storable]
181    private double[] MultiLayerPerceptronColumnSigmas {
182      get {
183        return multiLayerPerceptron.innerobj.columnsigmas;
184      }
185      set {
186        multiLayerPerceptron.innerobj.columnsigmas = value;
187      }
188    }
189    [Storable]
190    private double[] MultiLayerPerceptronDError {
191      get {
192        return multiLayerPerceptron.innerobj.derror;
193      }
194      set {
195        multiLayerPerceptron.innerobj.derror = value;
196      }
197    }
198    [Storable]
199    private double[] MultiLayerPerceptronDfdnet {
200      get {
201        return multiLayerPerceptron.innerobj.dfdnet;
202      }
203      set {
204        multiLayerPerceptron.innerobj.dfdnet = value;
205      }
206    }
207    [Storable]
208    private double[] MultiLayerPerceptronNeurons {
209      get {
210        return multiLayerPerceptron.innerobj.neurons;
211      }
212      set {
213        multiLayerPerceptron.innerobj.neurons = value;
214      }
215    }
216    [Storable]
217    private double[] MultiLayerPerceptronNwbuf {
218      get {
219        return multiLayerPerceptron.innerobj.nwbuf;
220      }
221      set {
222        multiLayerPerceptron.innerobj.nwbuf = value;
223      }
224    }
225    [Storable]
226    private int[] MultiLayerPerceptronStuctinfo {
227      get {
228        return multiLayerPerceptron.innerobj.structinfo;
229      }
230      set {
231        multiLayerPerceptron.innerobj.structinfo = value;
232      }
233    }
234    [Storable]
235    private double[] MultiLayerPerceptronWeights {
236      get {
237        return multiLayerPerceptron.innerobj.weights;
238      }
239      set {
240        multiLayerPerceptron.innerobj.weights = value;
241      }
242    }
243    [Storable]
244    private double[] MultiLayerPerceptronX {
245      get {
246        return multiLayerPerceptron.innerobj.x;
247      }
248      set {
249        multiLayerPerceptron.innerobj.x = value;
250      }
251    }
252    [Storable]
253    private double[] MultiLayerPerceptronY {
254      get {
255        return multiLayerPerceptron.innerobj.y;
256      }
257      set {
258        multiLayerPerceptron.innerobj.y = value;
259      }
260    }
261    #endregion
262  }
263}
Note: See TracBrowser for help on using the repository browser.