9#ifndef RECURRENTLAYER_H
10#define RECURRENTLAYER_H
24#include "tensor_utilities.h"
27#include "probabilistic_layer.h"
28#include "perceptron_layer.h"
33struct RecurrentLayerForwardPropagation;
34struct RecurrentLayerBackPropagation;
38 #include "../../opennn-cuda/opennn-cuda/struct_recurrent_layer_cuda.h"
52 Linear, RectifiedLinear, ExponentialLinear,
53 ScaledExponentialLinear, SoftPlus, SoftSign, HardSigmoid};
67 bool is_empty()
const;
76 Index get_timesteps()
const;
82 Index get_biases_number()
const;
83 Index get_input_weights_number()
const;
84 Index get_recurrent_weights_number()
const;
89 Tensor<type, 2>
get_biases(
const Tensor<type, 1>&)
const;
106 void set(
const Index&,
const Index&);
115 void set_input_shape(
const Tensor<Index, 1>&);
119 void set_timesteps(
const Index&);
121 void set_biases(
const Tensor<type, 1>&);
123 void set_input_weights(
const Tensor<type, 2>&);
125 void set_recurrent_weights(
const Tensor<type, 2>&);
154 void calculate_combinations(
const Tensor<type, 1>&,
155 const Tensor<type, 2>&,
156 const Tensor<type, 2>&,
157 const Tensor<type, 1>&,
158 Tensor<type, 1>&)
const;
160 void calculate_activations(
const Tensor<type, 1>&,
161 Tensor<type, 1>&)
const;
163 void calculate_activations_derivatives(
const Tensor<type, 1>&,
165 Tensor<type, 1>&)
const;
167 void calculate_activations_derivatives(
const Tensor<type, 2>&,
169 Tensor<type, 2>&)
const;
174 Tensor<type, 2> calculate_outputs(
const Tensor<type, 2>&);
196 void calculate_error_gradient(
const Tensor<type, 2>&,
200 void calculate_biases_error_gradient(
const Tensor<type, 2>&,
204 void calculate_input_weights_error_gradient(
const Tensor<type, 2>&,
208 void calculate_recurrent_weights_error_gradient(
const Tensor<type, 2>&,
214 string write_expression(
const Tensor<string, 1>&,
const Tensor<string, 1>&)
const;
216 string write_activation_function_expression()
const;
218 string write_expression_python()
const;
219 string write_combinations_python()
const;
237 Tensor<type, 2> input_weights;
247 Tensor<type, 1> hidden_states;
254 #include "../../opennn-cuda/opennn-cuda/recurrent_layer_cuda.h"
267 set(new_batch_samples_number, new_layer_pointer);
270 void set(
const Index& new_batch_samples_number,
Layer* new_layer_pointer)
272 layer_pointer = new_layer_pointer;
274 batch_samples_number = new_batch_samples_number;
276 const Index neurons_number = layer_pointer->get_neurons_number();
279 previous_activations.resize(neurons_number);
281 current_inputs.resize(inputs_number);
282 current_combinations.resize(neurons_number);
283 current_activations_derivatives.resize(neurons_number);
285 combinations.resize(batch_samples_number, neurons_number);
287 activations.resize(batch_samples_number, neurons_number);
289 activations_derivatives.resize(batch_samples_number, neurons_number);
296 Tensor<type, 1> previous_activations;
298 Tensor<type, 1> current_inputs;
299 Tensor<type, 1> current_combinations;
300 Tensor<type, 1> current_activations_derivatives;
302 Tensor<type, 2> combinations;
303 Tensor<type, 2> activations;
304 Tensor<type, 2> activations_derivatives;
317 set(new_batch_samples_number, new_layer_pointer);
321 void set(
const Index& new_batch_samples_number,
Layer* new_layer_pointer)
323 layer_pointer = new_layer_pointer;
325 batch_samples_number = new_batch_samples_number;
327 const Index neurons_number = layer_pointer->get_neurons_number();
330 current_layer_deltas.resize(neurons_number);
332 biases_derivatives.resize(neurons_number);
334 input_weights_derivatives.resize(inputs_number * neurons_number);
336 recurrent_weights_derivatives.resize(neurons_number * neurons_number);
338 delta.resize(batch_samples_number, neurons_number);
340 combinations_biases_derivatives.resize(neurons_number, neurons_number);
341 combinations_weights_derivatives.resize(inputs_number*neurons_number, neurons_number);
342 combinations_recurrent_weights_derivatives.resize(neurons_number*neurons_number, neurons_number);
351 Tensor<type, 1> current_layer_deltas;
353 Tensor<type, 1> biases_derivatives;
355 Tensor<type, 1> input_weights_derivatives;
357 Tensor<type, 1> recurrent_weights_derivatives;
359 Tensor<type, 2> combinations_biases_derivatives;
360 Tensor<type, 2> combinations_weights_derivatives;
361 Tensor<type, 2> combinations_recurrent_weights_derivatives;
363 Tensor<type, 2> delta;
This abstract class represents the concept of layer of neurons in OpenNN.
virtual Index get_inputs_number() const
Returns the number of inputs.
const Tensor< type, 2 > & get_recurrent_weights() const
string write_activation_function() const
void set_parameters_constant(const type &)
string write_activations_python() const
void set_input_weights_constant(const type &)
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
virtual ~RecurrentLayer()
Tensor< type, 2 > recurrent_weights
This matrix containing conection strengths from a recurrent layer inputs to its neurons.
bool display
Display messages to screen.
ActivationFunction
Enumeration of the available activation functions for the recurrent layer.
void set_activation_function(const ActivationFunction &)
void set_recurrent_weights_constant(const type &)
const Tensor< type, 2 > & get_input_weights() const
void set_inputs_number(const Index &)
Index get_neurons_number() const
Returns the size of the neurons vector.
void set_parameters_random()
const RecurrentLayer::ActivationFunction & get_activation_function() const
Returns the activation function of the layer.
void set_parameters(const Tensor< type, 1 > &, const Index &=0)
void set_display(const bool &)
void set_hidden_states_constant(const type &)
void initialize_input_weights_Glorot(const type &, const type &)
const Tensor< type, 1 > & get_hidden_states() const
Returns the hidden states of the layer.
Index get_parameters_number() const
Returns the number of parameters (biases and weights) of the layer.
ActivationFunction activation_function
Activation function variable.
Tensor< type, 1 > get_biases() const
void set_neurons_number(const Index &)
Tensor< type, 1 > get_parameters() const
LayerBackPropagation()
Default constructor.
LayerForwardPropagation()
Default constructor.