perceptron_layer.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// P E R C E P T R O N 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 PERCEPTRONLAYER_H
10#define PERCEPTRONLAYER_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#include "layer.h"
25#include "probabilistic_layer.h"
26
27#include "opennn_strings.h"
28
29namespace OpenNN
30{
31
32struct PerceptronLayerForwardPropagation;
33struct PerceptronLayerBackPropagation;
34struct PerceptronLayerBackPropagationLM;
35
36#ifdef OPENNN_CUDA
37 #include "../../opennn-cuda/opennn-cuda/struct_perceptron_layer_cuda.h"
38#endif
39
40
42
47
48class PerceptronLayer : public Layer
49
50{
51
52public:
53
55
56 enum class ActivationFunction{Threshold, SymmetricThreshold, Logistic, HyperbolicTangent, Linear, RectifiedLinear,
57 ExponentialLinear, ScaledExponentialLinear, SoftPlus, SoftSign, HardSigmoid};
58
59 // Constructors
60
61 explicit PerceptronLayer();
62
63 explicit PerceptronLayer(const Index&, const Index&, const ActivationFunction& = PerceptronLayer::ActivationFunction::HyperbolicTangent);
64
65 // Destructor
66
67 virtual ~PerceptronLayer();
68
69 // Get methods
70
71 bool is_empty() const;
72
73 Index get_inputs_number() const;
74 Index get_neurons_number() const;
75
76 // Parameters
77
78 const Tensor<type, 2>& get_biases() const;
79 const Tensor<type, 2>& get_synaptic_weights() const;
80
81 Tensor<type, 2> get_biases(const Tensor<type, 1>&) const;
82 Tensor<type, 2> get_synaptic_weights(const Tensor<type, 1>&) const;
83
84 Index get_biases_number() const;
85 Index get_synaptic_weights_number() const;
86 Index get_parameters_number() const;
87 Tensor<type, 1> get_parameters() const;
88
89 // Activation functions
90
92
93 string write_activation_function() const;
94
95 // Display messages
96
97 const bool& get_display() const;
98
99 // Set methods
100
101 void set();
102 void set(const Index&, const Index&, const PerceptronLayer::ActivationFunction& = PerceptronLayer::ActivationFunction::HyperbolicTangent);
103
104 void set_default();
105 void set_name(const string&);
106
107 // Architecture
108
109 void set_inputs_number(const Index&);
110 void set_neurons_number(const Index&);
111
112 // Parameters
113
114 void set_biases(const Tensor<type, 2>&);
115 void set_synaptic_weights(const Tensor<type, 2>&);
116
117 void set_parameters(const Tensor<type, 1>&, const Index& index=0);
118
119 // Activation functions
120
122 void set_activation_function(const string&);
123
124 // Display messages
125
126 void set_display(const bool&);
127
128 // Parameters initialization methods
129 void set_biases_constant(const type&);
130 void set_synaptic_weights_constant(const type&);
131
132
133 void set_parameters_constant(const type&);
134
136
137 // Perceptron layer combinations
138
139 void calculate_combinations(const Tensor<type, 2>&,
140 const Tensor<type, 2>&,
141 const Tensor<type, 2>&,
142 Tensor<type, 2>&) const;
143
144 // Perceptron layer activations
145
146 void calculate_activations(const Tensor<type, 2>&,
147 Tensor<type, 2>&) const;
148
149 void calculate_activations_derivatives(const Tensor<type, 2>&,
150 Tensor<type, 2>&,
151 Tensor<type, 2>&) const;
152
153 // Perceptron layer outputs
154
155 Tensor<type, 2> calculate_outputs(const Tensor<type, 2>&);
156
157 void forward_propagate(const Tensor<type, 2>&,
159
160
161 void forward_propagate(const Tensor<type, 2>&,
162 Tensor<type, 1>,
164
165 // Delta methods
166
167 void calculate_hidden_delta(LayerForwardPropagation*,
169 LayerBackPropagation*) const;
170
171 void calculate_hidden_delta_perceptron(PerceptronLayerForwardPropagation*,
174
175 void calculate_hidden_delta_probabilistic(ProbabilisticLayerForwardPropagation*,
178
179 // Delta LM
180
181 void calculate_hidden_delta_lm(LayerForwardPropagation*,
184
185 void calculate_hidden_delta_perceptron_lm(PerceptronLayerForwardPropagation*,
188
189 void calculate_hidden_delta_probabilistic_lm(ProbabilisticLayerForwardPropagation*,
192
193 // Squared errors methods
194
195 void calculate_squared_errors_Jacobian_lm(const Tensor<type, 2>&,
198
199 void insert_squared_errors_Jacobian_lm(LayerBackPropagationLM*,
200 const Index&,
201 Tensor<type, 2>&) const;
202
203 // Gradient methods
204
205 void calculate_error_gradient(const Tensor<type, 2>&,
207 LayerBackPropagation*) const;
208
209 void insert_gradient(LayerBackPropagation*,
210 const Index&,
211 Tensor<type, 1>&) const;
212
213 // Expression methods
214
215 string write_expression(const Tensor<string, 1>&, const Tensor<string, 1>&) const;
216
217 string write_activation_function_expression() const;
218
219 string write_expression_c() const;
220 string write_combinations_c() const;
221 string write_activations_c() const;
222
223 string write_combinations_python() const;
224 string write_activations_python() const;
225 string write_expression_python() const;
226
227 // Serialization methods
228
229 void from_XML(const tinyxml2::XMLDocument&);
230 void write_XML(tinyxml2::XMLPrinter&) const;
231
232protected:
233
234 // MEMBERS
235
238
239 Tensor<type, 2> biases;
240
242
243 Tensor<type, 2> synaptic_weights;
244
246
248
250
251 bool display = true;
252
253#ifdef OPENNN_CUDA
254 #include "../../opennn-cuda/opennn-cuda/perceptron_layer_cuda.h"
255#else
256};
257#endif
258
260{
261 // Default constructor
262
264 {
265 }
266
267 explicit PerceptronLayerForwardPropagation(const Index& new_batch_samples_number, Layer* new_layer_pointer)
269 {
270 set(new_batch_samples_number, new_layer_pointer);
271 }
272
273 void set(const Index& new_batch_samples_number, Layer* new_layer_pointer)
274 {
275 layer_pointer = new_layer_pointer;
276
277 batch_samples_number = new_batch_samples_number;
278
279 const Index neurons_number = layer_pointer->get_neurons_number();
280
281 combinations.resize(batch_samples_number, neurons_number);
282
283 activations.resize(batch_samples_number, neurons_number);
284
285 activations_derivatives.resize(batch_samples_number, neurons_number);
286 }
287
288 void print() const
289 {
290 cout << "Combinations:" << endl;
291 cout << combinations << endl;
292
293 cout << "Activations:" << endl;
294 cout << activations << endl;
295
296 cout << "Activations derivatives:" << endl;
297 cout << activations_derivatives << endl;
298 }
299
300 Tensor<type, 2> combinations;
301 Tensor<type, 2> activations;
302 Tensor<type, 2> activations_derivatives;
303};
304
305
307{
308 // Default constructor
309
311 {
312
313 }
314
315
316 explicit PerceptronLayerBackPropagationLM(const Index& new_batch_samples_number, Layer* new_layer_pointer)
318 {
319 set(new_batch_samples_number, new_layer_pointer);
320 }
321
322
323 void set(const Index& new_batch_samples_number, Layer* new_layer_pointer)
324 {
325 layer_pointer = new_layer_pointer;
326
327 batch_samples_number = new_batch_samples_number;
328
329 const Index neurons_number = layer_pointer->get_neurons_number();
330 const Index parameters_number = layer_pointer->get_parameters_number();
331
332 delta.resize(batch_samples_number, neurons_number);
333
334 squared_errors_Jacobian.resize(batch_samples_number, parameters_number);
335 }
336
337 void print() const
338 {
339 cout << "Delta:" << endl;
340 cout << delta << endl;
341
342 cout << "Squared errors Jacobian: " << endl;
343 cout << squared_errors_Jacobian << endl;
344
345 }
346
347 Tensor<type, 2> delta;
348
349 Tensor<type, 2> squared_errors_Jacobian;
350};
351
352
353
355{
356 // Default constructor
357
359 {
360
361 }
362
363
364 explicit PerceptronLayerBackPropagation(const Index& new_batch_samples_number, Layer* new_layer_pointer)
366 {
367 set(new_batch_samples_number, new_layer_pointer);
368 }
369
370
371 void set(const Index& new_batch_samples_number, Layer* new_layer_pointer)
372 {
373 layer_pointer = new_layer_pointer;
374
375 batch_samples_number = new_batch_samples_number;
376
377 const Index neurons_number = layer_pointer->get_neurons_number();
378 const Index inputs_number = layer_pointer->get_inputs_number();
379
380 delta.resize(batch_samples_number, neurons_number);
381
382 biases_derivatives.resize(neurons_number);
383
384 synaptic_weights_derivatives.resize(inputs_number, neurons_number);
385 }
386
387 void print() const
388 {
389 cout << "Delta:" << endl;
390 cout << delta << endl;
391
392 cout << "Biases derivatives:" << endl;
393 cout << biases_derivatives << endl;
394
395 cout << "Synaptic weights derivatives:" << endl;
396 cout << synaptic_weights_derivatives << endl;
397 }
398
399 Tensor<type, 2> delta;
400
401 Tensor<type, 1> biases_derivatives;
402 Tensor<type, 2> synaptic_weights_derivatives;
403};
404
405
406
407}
408
409#endif
410
411
412// OpenNN: Open Neural Networks Library.
413// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
414//
415// This library is free software; you can redistribute it and/or
416// modify it under the terms of the GNU Lesser General Public
417// License as published by the Free Software Foundation; either
418// version 2.1 of the License, or any later version.
419//
420// This library is distributed in the hope that it will be useful,
421// but WITHOUT ANY WARRANTY; without even the implied warranty of
422// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
423// Lesser General Public License for more details.
424
425// You should have received a copy of the GNU Lesser General Public
426// License along with this library; if not, write to the Free Software
427
428// 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 perceptrons.
void set_parameters(const Tensor< type, 1 > &, const Index &index=0)
Sets the parameters of this layer.
string write_activation_function() const
void set_parameters_constant(const type &)
Index get_synaptic_weights_number() const
Returns the number of layer's synaptic weights.
string write_activations_python() const
const bool & get_display() const
Index get_inputs_number() const
Returns the number of inputs to the layer.
void set_biases_constant(const type &)
string write_expression(const Tensor< string, 1 > &, const Tensor< string, 1 > &) const
bool display
Display messages to screen.
ActivationFunction
Enumeration of available activation functions for the perceptron neuron model.
void set_activation_function(const ActivationFunction &)
const Tensor< type, 2 > & get_biases() const
Tensor< type, 2 > biases
void set_synaptic_weights(const Tensor< type, 2 > &)
void set_inputs_number(const Index &)
void set_biases(const Tensor< type, 2 > &)
Index get_neurons_number() const
Returns the number of neurons in the layer.
string write_activations_c() const
const PerceptronLayer::ActivationFunction & get_activation_function() const
void set_synaptic_weights_constant(const type &)
void set_display(const bool &)
Tensor< type, 2 > synaptic_weights
This matrix containing conection strengths from a layer's inputs to its neurons.
const Tensor< type, 2 > & get_synaptic_weights() const
Index get_parameters_number() const
Returns the number of parameters(biases and synaptic weights) of the layer.
ActivationFunction activation_function
Activation function variable.
void set_neurons_number(const Index &)
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