probabilistic_layer.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// P R O B A B I L I S T I C L A Y E R C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#ifndef PROBABILISTICLAYER_H
10#define PROBABILISTICLAYER_H
11
12// System includes
13
14#include <cmath>
15#include <cstdlib>
16#include <fstream>
17#include <iostream>
18#include <string>
19#include <sstream>
20
21// OpenNN includes
22
23#include "config.h"
24
25#include "layer.h"
26
27#include "opennn_strings.h"
28
29
30namespace OpenNN
31{
32
33struct ProbabilisticLayerForwardPropagation;
34struct ProbabilisticLayerBackPropagation;
35struct ProbabilisticLayerBackPropagationLM;
36
37#ifdef OPENNN_CUDA
38 #include "../../opennn-cuda/opennn-cuda/struct_probabilistic_layer_cuda.h"
39#endif
40
41
43
48
50{
51
52public:
53
54 // Constructors
55
56 explicit ProbabilisticLayer();
57
58 explicit ProbabilisticLayer(const Index&, const Index&);
59
60 // Destructor
61
62 virtual ~ProbabilisticLayer();
63
64 // Enumerations
65
67
68 enum class ActivationFunction{Binary, Logistic, Competitive, Softmax};
69
70 // Get methods
71
72 Index get_inputs_number() const;
73 Index get_neurons_number() const;
74
75 Index get_biases_number() const;
76 Index get_synaptic_weights_number() const;
77
78 const type& get_decision_threshold() const;
79
81 string write_activation_function() const;
82 string write_activation_function_text() const;
83
84 const bool& get_display() const;
85
86 // Set methods
87
88 void set();
89 void set(const Index&, const Index&);
90 void set(const ProbabilisticLayer&);
91
92 void set_inputs_number(const Index&);
93 void set_neurons_number(const Index&);
94
95 void set_biases(const Tensor<type, 2>&);
96 void set_synaptic_weights(const Tensor<type, 2>&);
97
98 void set_parameters(const Tensor<type, 1>&, const Index& index=0);
99 void set_decision_threshold(const type&);
100
102 void set_activation_function(const string&);
103
104 virtual void set_default();
105
106 // Parameters
107
108 const Tensor<type, 2>& get_biases() const;
109 const Tensor<type, 2>& get_synaptic_weights() const;
110
111 Tensor<type, 2> get_biases(Tensor<type, 1>&) const;
112 Tensor<type, 2> get_synaptic_weights(Tensor<type, 1>&) const;
113
114 Index get_parameters_number() const;
115 Tensor<type, 1> get_parameters() const;
116
117 // Display messages
118
119 void set_display(const bool&);
120
121 // Parameters initialization methods
122
123 void set_biases_constant(const type&);
124 void set_synaptic_weights_constant(const type&);
125 void set_synaptic_weights_constant_Glorot();
126
127 void set_parameters_constant(const type&);
128
130
131 void insert_parameters(const Tensor<type, 1>&, const Index&);
132
133 // Combinations
134
135 void calculate_combinations(const Tensor<type, 2>&,
136 const Tensor<type, 2>&,
137 const Tensor<type, 2>&,
138 Tensor<type, 2>&) const;
139
140 // Activations
141
142 void calculate_activations(const Tensor<type, 2>&, Tensor<type, 2>&) const;
143
144 void calculate_activations_derivatives(const Tensor<type, 2>&,
145 Tensor<type, 2>&,
146 Tensor<type, 3>&) const;
147
148 // Outputs
149
150 Tensor<type, 2> calculate_outputs(const Tensor<type, 2>&);
151
152 void forward_propagate(const Tensor<type, 2>&,
154
155 void forward_propagate(const Tensor<type, 2>&,
156 Tensor<type, 1>,
158
159 // Gradient methods
160
161 void calculate_error_gradient(const Tensor<type, 2>&,
163 LayerBackPropagation*) const;
164
165 void insert_gradient(LayerBackPropagation*, const Index&, Tensor<type, 1>&) const;
166
167 // Squared errors methods
168
169 void calculate_squared_errors_Jacobian_lm(const Tensor<type, 2>&,
172
173 void insert_squared_errors_Jacobian_lm(LayerBackPropagationLM*,
174 const Index&,
175 Tensor<type, 2>&) const;
176
177 // Expression methods
178
179 string write_binary_expression(const Tensor<string, 1>&, const Tensor<string, 1>&) const;
180 string write_logistic_expression(const Tensor<string, 1>&, const Tensor<string, 1>&) const;
181 string write_competitive_expression(const Tensor<string, 1>&, const Tensor<string, 1>&) const;
182 string write_softmax_expression(const Tensor<string, 1>&, const Tensor<string, 1>&) const;
183 string write_no_probabilistic_expression(const Tensor<string, 1>&, const Tensor<string, 1>&) const;
184
185 string write_expression(const Tensor<string, 1>&, const Tensor<string, 1>&) const;
186 string write_combinations(const Tensor<string, 1>&) const;
187 string write_activations(const Tensor<string, 1>&) const;
188
189 string write_expression_c() const;
190 string write_combinations_c() const;
191 string write_activations_c() const;
192
193 string write_expression_python() const;
194 string write_combinations_python() const;
195 string write_activations_python() const;
196
197 // Serialization methods
198
199 void from_XML(const tinyxml2::XMLDocument&);
200
201 void write_XML(tinyxml2::XMLPrinter&) const;
202
203
204protected:
205
208
209 Tensor<type, 2> biases;
210
212
213 Tensor<type, 2> synaptic_weights;
214
216
217 ActivationFunction activation_function = ActivationFunction::Logistic;
218
219 type decision_threshold;
220
222
223 bool display = true;
224
225#ifdef OPENNN_CUDA
226 #include "../../opennn-cuda/opennn-cuda/probabilistic_layer_cuda.h"
227#else
228};
229#endif
230
232{
233 // Constructor
234
236 {
237 }
238
239 // Constructor
240
241 explicit ProbabilisticLayerForwardPropagation(const Index& new_batch_samples_number, Layer* new_layer_pointer)
243 {
244 set(new_batch_samples_number, new_layer_pointer);
245 }
246
247 void set(const Index& new_batch_samples_number, Layer* new_layer_pointer)
248 {
249 layer_pointer = new_layer_pointer;
250
251 batch_samples_number = new_batch_samples_number;
252
253 const Index neurons_number = layer_pointer->get_neurons_number();
254
255 combinations.resize(batch_samples_number, neurons_number);
256
257 activations.resize(batch_samples_number, neurons_number);
258
259 activations_derivatives.resize(batch_samples_number, neurons_number, neurons_number);
260 }
261
262
263 void print() const
264 {
265 cout << "Combinations:" << endl;
266 cout << combinations << endl;
267
268 cout << "Activations:" << endl;
269 cout << activations << endl;
270
271 cout << "Activations derivatives:" << endl;
272 cout << activations_derivatives << endl;
273 }
274
275 Tensor<type, 2> combinations;
276 Tensor<type, 2> activations;
277 Tensor<type, 3> activations_derivatives;
278};
279
280
282{
284 {
285
286 }
287
288
289 explicit ProbabilisticLayerBackPropagationLM(const Index& new_batch_samples_number, Layer* new_layer_pointer)
291 {
292 set(new_batch_samples_number, new_layer_pointer);
293 }
294
295
296 void set(const Index& new_batch_samples_number, Layer* new_layer_pointer)
297 {
298 layer_pointer = new_layer_pointer;
299
300 batch_samples_number = new_batch_samples_number;
301
302 const Index neurons_number = layer_pointer->get_neurons_number();
303 const Index parameters_number = layer_pointer->get_parameters_number();
304
305 delta.resize(batch_samples_number, neurons_number);
306 delta_row.resize(neurons_number);
307
308 squared_errors_Jacobian.resize(batch_samples_number, parameters_number);
309
310 error_combinations_derivatives.resize(batch_samples_number, neurons_number);
311 }
312
313 void print() const
314 {
315 cout << "Delta:" << endl;
316 cout << delta << endl;
317
318 cout << "Squared errors Jacobian: " << endl;
319 cout << squared_errors_Jacobian << endl;
320 }
321
322 Tensor<type, 2> delta;
323 Tensor<type, 1> delta_row;
324
325 Tensor<type, 2> error_combinations_derivatives;
326
327 Tensor<type, 2> squared_errors_Jacobian;
328};
329
330
331
333{
335 {
336
337 }
338
339
340 explicit ProbabilisticLayerBackPropagation(const Index& new_batch_samples_number, Layer* new_layer_pointer)
342 {
343 set(new_batch_samples_number, new_layer_pointer);
344 }
345
346
347 void set(const Index& new_batch_samples_number, Layer* new_layer_pointer)
348 {
349 layer_pointer = new_layer_pointer;
350
351 batch_samples_number = new_batch_samples_number;
352
353 const Index neurons_number = layer_pointer->get_neurons_number();
354 const Index inputs_number = layer_pointer->get_inputs_number();
355
356 biases_derivatives.resize(neurons_number);
357
358 synaptic_weights_derivatives.resize(inputs_number, neurons_number);
359
360 delta.resize(batch_samples_number, neurons_number);
361 delta_row.resize(neurons_number);
362
363 error_combinations_derivatives.resize(batch_samples_number, neurons_number);
364 }
365
366 void print() const
367 {
368 cout << "Delta:" << endl;
369 cout << delta << endl;
370
371 cout << "Biases derivatives:" << endl;
372 cout << biases_derivatives << endl;
373
374 cout << "Synaptic weights derivatives:" << endl;
375 cout << synaptic_weights_derivatives << endl;
376 }
377
378 Tensor<type, 2> delta;
379 Tensor<type, 1> delta_row;
380
381 Tensor<type, 2> error_combinations_derivatives;
382
383 Tensor<type, 2> synaptic_weights_derivatives;
384 Tensor<type, 1> biases_derivatives;
385};
386
387}
388
389#endif
390
391
392// OpenNN: Open Neural Networks Library.
393// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
394//
395// This library is free software; you can redistribute it and/or
396// modify it under the terms of the GNU Lesser General Public
397// License as published by the Free Software Foundation; either
398// version 2.1 of the License, or any later version.
399//
400// This library is distributed in the hope that it will be useful,
401// but WITHOUT ANY WARRANTY; without even the implied warranty of
402// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
403// Lesser General Public License for more details.
404
405// You should have received a copy of the GNU Lesser General Public
406// License along with this library; if not, write to the Free Software
407
408// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
This abstract class represents the concept of layer of neurons in OpenNN.
Definition: layer.h:53
virtual Index get_inputs_number() const
Returns the number of inputs.
Definition: layer.cpp:153
This class represents a layer of probabilistic neurons.
void set_parameters_constant(const type &)
string write_softmax_expression(const Tensor< string, 1 > &, const Tensor< string, 1 > &) const
Index get_synaptic_weights_number() const
Returns the number of layer's synaptic weights.
const bool & get_display() const
Index get_inputs_number() const
Returns the number of inputs.
const ActivationFunction & get_activation_function() const
string write_activation_function_text() const
void set_biases_constant(const type &)
string write_logistic_expression(const Tensor< string, 1 > &, const Tensor< string, 1 > &) const
void from_XML(const tinyxml2::XMLDocument &)
bool display
Display messages to screen.
ActivationFunction
Enumeration of available methods for interpreting variables as probabilities.
void set_activation_function(const ActivationFunction &)
const Tensor< type, 2 > & get_biases() const
Returns the biases of the layer.
const type & get_decision_threshold() const
Returns the decision threshold.
string write_no_probabilistic_expression(const Tensor< string, 1 > &, const Tensor< string, 1 > &) const
Tensor< type, 2 > calculate_outputs(const Tensor< type, 2 > &)
string write_competitive_expression(const Tensor< string, 1 > &, const Tensor< string, 1 > &) const
string write_binary_expression(const Tensor< string, 1 > &, const Tensor< string, 1 > &) const
void set_decision_threshold(const type &)
void set_synaptic_weights_constant(const type &)
Tensor< type, 2 > synaptic_weights
This matrix containing conection strengths from a layer's inputs to its neurons.
void write_XML(tinyxml2::XMLPrinter &) const
const Tensor< type, 2 > & get_synaptic_weights() const
Returns the synaptic weights of the layer.
Index get_parameters_number() const
Returns the number of parameters(biases and synaptic weights) of the layer.
ActivationFunction activation_function
Activation function variable.
Tensor< type, 1 > get_parameters() const
LayerBackPropagation()
Default constructor.
Definition: layer.h:305
LayerBackPropagationLM()
Default constructor.
Definition: layer.h:323
LayerForwardPropagation()
Default constructor.
Definition: layer.h:285