convolutional_layer.h
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// C O N V O L U T I O N A L 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 CONVOLUTIONALLAYER_H
10#define CONVOLUTIONALLAYER_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#include <ctype.h>
21#include <stdexcept>
22
23// OpenNN includes
24
25#include "layer.h"
26#include "config.h"
27
28namespace OpenNN
29{
30
31struct ConvolutionalLayerForwardPropagation;
32struct ConvolutionalLayerBackPropagation;
33
34class PoolingLayer;
35class PerceptronLayer;
36class ProbabilisticLayer;
37
39{
40
41public:
42
44
45 enum class ActivationFunction{Threshold, SymmetricThreshold, Logistic, HyperbolicTangent, Linear, RectifiedLinear, ExponentialLinear, ScaledExponentialLinear, SoftPlus, SoftSign, HardSigmoid};
46
47 enum class ConvolutionType{Valid, Same};
48
49 // Constructors
50
51 explicit ConvolutionalLayer();
52
53 explicit ConvolutionalLayer(const Tensor<Index, 1>&, const Tensor<Index, 1>&);
54
55 // Destructor
56
57 // Get methods
58
59 bool is_empty() const;
60
61 const Tensor<type, 1>& get_biases() const;
62
63 const Tensor<type, 4>& get_synaptic_weights() const;
64
65 Index get_synaptic_weights_number() const;
66
68
69 Tensor<Index, 1> get_outputs_dimensions() const;
70
71 Tensor<Index, 1> get_input_variables_dimensions() const;
72
73 Index get_outputs_rows_number() const;
74
75 Index get_outputs_columns_number() const;
76
77 ConvolutionType get_convolution_type() const;
78
79 Index get_column_stride() const;
80
81 Index get_row_stride() const;
82
83 Index get_kernels_number() const;
84 Index get_kernels_channels_number() const;
85 Index get_kernels_rows_number() const;
86 Index get_kernels_columns_number() const;
87
88 Index get_padding_width() const;
89 Index get_padding_height() const;
90
91 Index get_inputs_channels_number() const;
92 Index get_inputs_rows_number() const;
93 Index get_inputs_columns_number() const;
94
95 Index get_inputs_number() const;
96 Index get_neurons_number() const;
97
98 Tensor<type, 1> get_parameters() const;
99 Index get_parameters_number() const;
100
101 // Set methods
102
103 void set(const Tensor<Index, 1>&, const Tensor<Index, 1>&);
104
105 void set(const Tensor<type, 4>&, const Tensor<type, 4>&, const Tensor<type, 1>&);
106
108
109 void set_biases(const Tensor<type, 1>&);
110
111 void set_synaptic_weights(const Tensor<type, 4>&);
112
113 void set_convolution_type(const ConvolutionType&);
114
115 void set_parameters(const Tensor<type, 1>&, const Index& index);
116
117 void set_row_stride(const Index&);
118
119 void set_column_stride(const Index&);
120
121 // Initialization
122
123 void set_biases_constant(const type&);
124
125 void set_synaptic_weights_constant(const type&);
126
127 void set_parameters_constant(const type&);
128
129 void set_parameters_random();
130
131 // Padding
132
133 void insert_padding(const Tensor<type, 4>&, Tensor<type, 4>&);
134
135 // Combinations
136
137 void calculate_convolutions(const Tensor<type, 4>&, Tensor<type, 4>&) const;
138
139 void calculate_convolutions(const Tensor<type, 4>&,
140 const Tensor<type, 2>&,
141 const Tensor<type, 4>&,
142 Tensor<type, 4>&) const;
143
144 // Activation
145
146 void calculate_activations(const Tensor<type, 4>&, Tensor<type, 4>&) const;
147
148 void calculate_activations_derivatives(const Tensor<type, 4>&, Tensor<type, 4>&, Tensor<type, 4>&) const;
149
150 // Outputs
151
152 void calculate_outputs(const Tensor<type, 4>&, Tensor<type, 4>&);
153
154 void forward_propagate(const Tensor<type, 4>&, LayerForwardPropagation*);
155 void forward_propagate(const Tensor<type, 2>&, LayerForwardPropagation*);
156
157 void forward_propagate(const Tensor<type, 4>&, Tensor<type, 1>, LayerForwardPropagation*);
158 void forward_propagate(const Tensor<type, 2>&, Tensor<type, 1>, LayerForwardPropagation*);
159
160 // Delta methods
161
162 void calculate_hidden_delta(Layer*,
163 LayerForwardPropagation*,
164 const Tensor<type, 2>&,
165 Tensor<type, 2>&) const;
166
167
168 void calculate_hidden_delta_convolutional(ConvolutionalLayer*,
169 const Tensor<type, 4>&,
170 const Tensor<type, 4>&,
171 const Tensor<type, 4>&,
172 Tensor<type, 2>&) const;
173
174 void calculate_hidden_delta_pooling(PoolingLayer*,
175 const Tensor<type, 4>&,
176 const Tensor<type, 4>&,
177 const Tensor<type, 2>&,
178 Tensor<type, 2>&) const;
179
180 void calculate_hidden_delta_perceptron(const PerceptronLayer*,
181 const Tensor<type, 4>&,
182 const Tensor<type, 2>&,
183 const Tensor<type, 2>&,
184 Tensor<type, 2>&) const;
185
186 void calculate_hidden_delta_probabilistic(ProbabilisticLayer*,
187 const Tensor<type, 4>&,
188 const Tensor<type, 4>&,
189 const Tensor<type, 2>&,
190 Tensor<type, 2>&) const;
191
192 // Gradient methods
193
194 void calculate_error_gradient(const Tensor<type, 4>&,
195 LayerForwardPropagation*,
196 LayerBackPropagation&) const;
197
198 void calculate_error_gradient(const Tensor<type, 2>&,
199 LayerForwardPropagation*,
200 LayerBackPropagation&) const;
201
202 void insert_gradient(LayerBackPropagation*,
203 const Index&,
204 Tensor<type, 1>&) const;
205
206 void to_2d(const Tensor<type, 4>&, Tensor<type, 2>&) const;
207
208protected:
209
211
212 Tensor<type, 4> synaptic_weights;
213
216
217 Tensor<type, 1> biases;
218
219 Index row_stride = 1;
220
221 Index column_stride = 1;
222
223 Tensor<Index, 1> input_variables_dimensions;
224
225 ConvolutionType convolution_type = ConvolutionType::Valid;
226
227 ActivationFunction activation_function = ActivationFunction::RectifiedLinear;
228
229#ifdef OPENNN_CUDA
230 #include "../../opennn-cuda/opennn-cuda/convolutional_layer_cuda.h"
231#endif
232
233};
234
235
237{
238 // Default constructor
239
242 {
243 }
244
245 // Constructor
246
247 explicit ConvolutionalLayerForwardPropagation(const Index& new_batch_samples_number, Layer* new_layer_pointer)
249 {
250 set(new_batch_samples_number, new_layer_pointer);
251 }
252
253
254 void set(const Index& new_batch_samples_number, Layer* new_layer_pointer)
255 {
256 layer_pointer = new_layer_pointer;
257
258 const Index neurons_number = layer_pointer->get_neurons_number();
259
260 const Index kernels_number = static_cast<ConvolutionalLayer*>(layer_pointer)->get_kernels_number();
261 const Index outputs_rows_number = static_cast<ConvolutionalLayer*>(layer_pointer)->get_outputs_rows_number();
262 const Index outputs_columns_number = static_cast<ConvolutionalLayer*>(layer_pointer)->get_outputs_columns_number();
263
264 batch_samples_number = new_batch_samples_number;
265
266 combinations.resize(batch_samples_number, kernels_number, outputs_rows_number, outputs_columns_number);
267 activations.resize(batch_samples_number, kernels_number, outputs_rows_number, outputs_columns_number);
268
269 activations_derivatives.resize(batch_samples_number, neurons_number, neurons_number, neurons_number);
270 }
271
272
273 void print() const
274 {
275
276 }
277
278
279 Tensor<type, 4> combinations;
280 Tensor<type, 4> activations;
281 Tensor<type, 4> activations_derivatives;
282};
283
284
286{
287 const Index neurons_number = layer_pointer->get_neurons_number();
288 const Index inputs_nmumber = layer_pointer->get_inputs_number();
289
291 {
292 }
293
294
295 explicit ConvolutionalLayerBackPropagation(const Index& new_batch_samples_number, Layer* new_layer_pointer)
297 {
298 set(new_batch_samples_number, new_layer_pointer);
299 }
300
301
302 void set(const Index& new_batch_samples_number, Layer* new_layer_pointer)
303 {
304 }
305
306
307 void print() const
308 {
309 }
310
311 Tensor<type, 4> delta;
312
313 Tensor<type, 4> biases_derivatives;
314
315 Tensor<type, 4> synaptic_weights_derivatives;
316};
317
318
319}
320
321#endif
322
323
324// OpenNN: Open Neural Networks Library.
325// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
326//
327// This library is free software; you can redistribute it and/or
328// modify it under the terms of the GNU Lesser General Public
329// License as published by the Free Software Foundation; either
330// version 2.1 of the License, or any later version.
331//
332// This library is distributed in the hope that it will be useful,
333// but WITHOUT ANY WARRANTY; without even the implied warranty of
334// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
335// Lesser General Public License for more details.
336
337// You should have received a copy of the GNU Lesser General Public
338// License along with this library; if not, write to the Free Software
339
340// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Tensor< Index, 1 > get_input_variables_dimensions() const
Returns the dimension of the input variables.
void set_parameters_constant(const type &)
Index get_synaptic_weights_number() const
Returns the number of layer's synaptic weights.
Index get_column_stride() const
Returns the column stride.
Index get_inputs_number() const
Returns the number of inputs.
Index get_kernels_number() const
Returns the number of kernels of the layer.
Index get_outputs_rows_number() const
Returns the number of rows the result of applying the layer's kernels to an image will have.
void set_biases_constant(const type &)
Tensor< type, 4 > synaptic_weights
This tensor containing conection strengths from a layer's inputs to its neurons.
Index get_inputs_channels_number() const
Returns the number of channels of the input.
Index get_kernels_channels_number() const
Returns the number of channels of the layer's kernels.
ActivationFunction
Enumeration of available activation functions for the convolutional layer.
bool is_empty() const
Returns a boolean, true if convolutional layer is empty and false otherwise.
void set_synaptic_weights(const Tensor< type, 4 > &)
Index get_inputs_rows_number() const
Returns the number of rows of the input.
void set_activation_function(const ActivationFunction &)
Index get_inputs_columns_number() const
Returns the number of columns of the input.
void set(const Tensor< Index, 1 > &, const Tensor< Index, 1 > &)
void insert_padding(const Tensor< type, 4 > &, Tensor< type, 4 > &)
const Tensor< type, 1 > & get_biases() const
Returns the layer's biases.
Index get_outputs_columns_number() const
Returns the number of columns the result of applying the layer's kernels to an image will have.
void set_column_stride(const Index &)
Index get_padding_width() const
Returns the total number of columns of zeroes to be added to an image before applying a kernel,...
Index get_neurons_number() const
Returns the number of neurons.
ConvolutionType get_convolution_type() const
Returns the padding option.
void set_biases(const Tensor< type, 1 > &)
Index get_kernels_rows_number() const
Returns the number of rows of the layer's kernels.
Tensor< Index, 1 > get_outputs_dimensions() const
Returns a vector containing the number of channels, rows and columns of the result of applying the la...
void calculate_activations(const Tensor< type, 4 > &, Tensor< type, 4 > &) const
Calculates activations.
ActivationFunction get_activation_function() const
Returns the convolutional layer's activation function.
Index get_row_stride() const
Returns the row stride.
void set_synaptic_weights_constant(const type &)
Index get_padding_height() const
Returns the total number of rows of zeroes to be added to an image before applying a kernel,...
const Tensor< type, 4 > & get_synaptic_weights() const
Returns the layer's synaptic weights.
Index get_kernels_columns_number() const
Returns the number of columns of the layer's kernels.
void set_convolution_type(const ConvolutionType &)
void calculate_convolutions(const Tensor< type, 4 > &, Tensor< type, 4 > &) const
Calculate convolutions.
Index get_parameters_number() const
Returns the number of parameters of the layer.
void calculate_hidden_delta_perceptron(const PerceptronLayer *, const Tensor< type, 4 > &, const Tensor< type, 2 > &, const Tensor< type, 2 > &, Tensor< type, 2 > &) const
void calculate_outputs(const Tensor< type, 4 > &, Tensor< type, 4 > &)
void set_parameters(const Tensor< type, 1 > &, const Index &index)
Tensor< type, 1 > get_parameters() const
Returns the layer's parameters in the form of a vector.
This abstract class represents the concept of layer of neurons in OpenNN.
Definition: layer.h:53
LayerBackPropagation()
Default constructor.
Definition: layer.h:305
LayerForwardPropagation()
Default constructor.
Definition: layer.h:285