OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::Recurrent Class Referencefinal

Plain (Elman-style) recurrent layer over fixed-length sequences. More...

#include <recurrent_layer.h>

Inheritance diagram for opennn::Recurrent:
[legend]

Public Member Functions

 Recurrent (const Shape &input_shape={0, 0}, const Shape &output_shape={0})
 Constructs a Recurrent layer.
 
Shape get_input_shape () const override
 Returns the per-sample input shape (time_steps, input_features).
 
Shape get_output_shape () const override
 Returns the per-sample output shape.
 
const TensorViewget_biases () const
 Read-only access to the bias TensorView.
 
const TensorViewget_input_weights () const
 Read-only access to the input-to-hidden weight TensorView.
 
const TensorViewget_recurrent_weights () const
 Read-only access to the hidden-to-hidden weight TensorView.
 
const string & get_activation_function () const
 Name of the activation function applied to the hidden state.
 
vector< pair< Shape, Type > > get_parameter_specs () const override
 Specifications of the parameter tensors.
 
vector< pair< Shape, Type > > get_forward_specs (Index batch_size) const override
 Specifications of the forward intermediate buffers.
 
vector< pair< Shape, Type > > get_backward_specs (Index batch_size) const override
 Specifications of the backward intermediate buffers.
 
void set (const Shape &input_shape={}, const Shape &output_shape={})
 Re-initializes the layer.
 
void set_input_shape (const Shape &) override
 Updates the input shape and re-shapes weight tensors accordingly.
 
void set_output_shape (const Shape &) override
 Updates the output shape and re-shapes weight tensors accordingly.
 
void set_activation_function (const string &)
 Sets the activation function applied to the hidden state.
 
void forward_propagate (ForwardPropagation &, size_t, bool) noexcept override
 Forward pass: unrolls the recurrence over time_steps steps.
 
void back_propagate (ForwardPropagation &, BackPropagation &, size_t) const noexcept override
 Backward pass through time (BPTT) over the unrolled recurrence.
 
void read_JSON_body (const Json *) override
 Reads the layer-specific JSON body (shapes, activation name).
 
void write_JSON_body (JsonWriter &) const override
 Writes the layer-specific JSON body (shapes, activation name).
 
- Public Member Functions inherited from opennn::Layer
virtual ~Layer ()=default
 Virtual destructor; subclasses are owned via unique_ptr<Layer>.
 
const string & get_label () const
 Returns the user-assigned label of this layer.
 
const string & get_name () const
 Returns the canonical type name of this layer.
 
LayerType get_type () const
 Returns the LayerType enumerator for this layer.
 
void set_label (string new_label)
 Sets the human-readable label of this layer.
 
Index get_parameters_number () const
 Total number of trainable parameters in this layer.
 
virtual vector< Operator * > get_operators ()
 Returns the operators that compose this layer, if any.
 
virtual vector< pair< Shape, Type > > get_state_specs () const
 Specifications of the persistent state tensors of this layer.
 
vector< Shapeget_parameter_shapes () const
 Shape-only view of get_parameter_specs().
 
vector< Shapeget_state_shapes () const
 Shape-only view of get_state_specs().
 
vector< Shapeget_forward_shapes (Index b) const
 Shape-only view of get_forward_specs() for batch size b.
 
vector< Shapeget_backward_shapes (Index b) const
 Shape-only view of get_backward_specs() for batch size b.
 
vector< Typeget_parameter_dtypes () const
 Dtype-only view of get_parameter_specs().
 
vector< Typeget_forward_dtypes (Index b) const
 Dtype-only view of get_forward_specs() for batch size b.
 
vector< Typeget_backward_dtypes (Index b) const
 Dtype-only view of get_backward_specs() for batch size b.
 
virtual Activation::Function get_output_activation () const
 Activation function fused at the end of this layer, if any.
 
Index get_inputs_number () const
 Total number of scalar inputs per sample (product of input dims).
 
Index get_outputs_number () const
 Total number of scalar outputs per sample (product of output dims).
 
virtual void from_JSON (const JsonDocument &document)
 Loads the layer configuration (hyperparameters) from JSON.
 
virtual void load_state_from_JSON (const JsonDocument &document)
 Loads parameter and state tensors from a JSON document.
 
virtual void to_JSON (JsonWriter &writer) const
 Writes the layer configuration to JSON.
 
virtual void print () const
 Prints a human-readable summary of the layer to stdout.
 
bool get_is_trainable () const
 Whether this layer has trainable parameters.
 
Type get_compute_dtype () const
 Numerical type used for forward/backward computation.
 
void set_compute_dtype (Type new_compute_dtype)
 Sets the compute dtype and triggers on_compute_dtype_changed().
 
virtual void on_compute_dtype_changed ()
 Hook invoked after set_compute_dtype() mutates the dtype.
 
virtual float * link_parameters (float *pointer)
 Wires this layer's parameter TensorViews onto an external buffer.
 
virtual float * link_states (float *pointer)
 Wires this layer's state TensorViews onto an external buffer.
 
vector< TensorView > & get_parameter_views ()
 Mutable access to this layer's parameter TensorViews.
 
const vector< TensorView > & get_parameter_views () const
 Read-only access to this layer's parameter TensorViews.
 
vector< TensorView > & get_state_views ()
 Mutable access to this layer's state TensorViews.
 
const vector< TensorView > & get_state_views () const
 Read-only access to this layer's state TensorViews.
 
void redistribute_parameters_to_operators ()
 Forwards the current parameter views down to each composing Operator.
 
void redistribute_parameter_gradients_to_operators (vector< TensorView > &gradient_views)
 Forwards externally provided gradient views down to each Operator.
 
void redistribute_states_to_operators ()
 Forwards the current state views down to each composing Operator.
 

Additional Inherited Members

- Protected Member Functions inherited from opennn::Layer
 Layer ()=default
 Default constructor; only invoked by subclasses.
 
float * link_views (float *pointer, const vector< Shape > &shapes, vector< TensorView > &views, const char *tag) const
 Builds views over a contiguous float buffer using shapes.
 
void distribute_to_operators (vector< TensorView > &views, void(Operator::*link)(const vector< TensorView > &), vector< pair< Shape, Type > >(Operator::*specs)() const)
 Generic helper used by the redistribute_*_to_operators() routines.
 
- Protected Attributes inherited from opennn::Layer
string label = "my_layer"
 User-visible label for this layer instance (default "my_layer").
 
string name = "layer"
 Canonical type name set by the subclass (e.g. "dense").
 
LayerType layer_type = LayerType::Dense
 Layer type tag set by the subclass.
 
bool is_trainable = true
 True if the layer has parameters that participate in training.
 
bool is_first_layer = false
 True if this layer is the network's input layer.
 
Type compute_dtype = Type::FP32
 Numerical type used for forward and backward computation.
 
vector< TensorViewparameters
 Parameter TensorViews bound to the network's parameter arena.
 
vector< TensorViewstates
 State TensorViews bound to the network's state arena.
 
vector< unique_ptr< Layer > > layers
 Sub-layers, when this layer is itself a composite.
 

Detailed Description

Plain (Elman-style) recurrent layer over fixed-length sequences.

Inputs are rank-2 tensors (time_steps, input_features). At each step the hidden state is updated as h_t = activation(W_x * x_t + W_h * h_{t-1} + b), with shared weights and biases across all time steps.

The layer owns three parameter tensors (input weights, recurrent weights, biases) and applies a configurable elementwise activation.

Constructor & Destructor Documentation

◆ Recurrent()

opennn::Recurrent::Recurrent ( const Shape & input_shape = {0, 0},
const Shape & output_shape = {0} )

Constructs a Recurrent layer.

Parameters
input_shapePer-sample input shape (time_steps, input_features).
output_shapePer-sample output shape (number of hidden units).

Member Function Documentation

◆ back_propagate()

void opennn::Recurrent::back_propagate ( ForwardPropagation & ,
BackPropagation & ,
size_t  ) const
overridevirtualnoexcept

Backward pass through time (BPTT) over the unrolled recurrence.

Receives the forward intermediates, the BackPropagation buffer and this layer's index inside the network.

Reimplemented from opennn::Layer.

◆ forward_propagate()

void opennn::Recurrent::forward_propagate ( ForwardPropagation & ,
size_t ,
bool  )
overridevirtualnoexcept

Forward pass: unrolls the recurrence over time_steps steps.

Receives the ForwardPropagation buffer slice, this layer's index and the training flag.

Reimplemented from opennn::Layer.

◆ get_activation_function()

const string & opennn::Recurrent::get_activation_function ( ) const
inline

Name of the activation function applied to the hidden state.

◆ get_backward_specs()

vector< pair< Shape, Type > > opennn::Recurrent::get_backward_specs ( Index batch_size) const
overridevirtual

Specifications of the backward intermediate buffers.

Parameters
batch_sizeBatch size used for sizing.
Returns
Buffers used for time-unrolled gradient accumulation.

Reimplemented from opennn::Layer.

◆ get_biases()

const TensorView & opennn::Recurrent::get_biases ( ) const
inline

Read-only access to the bias TensorView.

◆ get_forward_specs()

vector< pair< Shape, Type > > opennn::Recurrent::get_forward_specs ( Index batch_size) const
overridevirtual

Specifications of the forward intermediate buffers.

Parameters
batch_sizeBatch size used for sizing.
Returns
Per-step combination and activation buffers plus the output.

Reimplemented from opennn::Layer.

◆ get_input_shape()

Shape opennn::Recurrent::get_input_shape ( ) const
inlineoverridevirtual

Returns the per-sample input shape (time_steps, input_features).

Implements opennn::Layer.

◆ get_input_weights()

const TensorView & opennn::Recurrent::get_input_weights ( ) const
inline

Read-only access to the input-to-hidden weight TensorView.

◆ get_output_shape()

Shape opennn::Recurrent::get_output_shape ( ) const
overridevirtual

Returns the per-sample output shape.

Returns
Shape with the number of hidden units.

Implements opennn::Layer.

◆ get_parameter_specs()

vector< pair< Shape, Type > > opennn::Recurrent::get_parameter_specs ( ) const
overridevirtual

Specifications of the parameter tensors.

Returns
Specs for input weights, recurrent weights and biases.

Reimplemented from opennn::Layer.

◆ get_recurrent_weights()

const TensorView & opennn::Recurrent::get_recurrent_weights ( ) const
inline

Read-only access to the hidden-to-hidden weight TensorView.

◆ read_JSON_body()

void opennn::Recurrent::read_JSON_body ( const Json * )
overridevirtual

Reads the layer-specific JSON body (shapes, activation name).

Reimplemented from opennn::Layer.

◆ set()

void opennn::Recurrent::set ( const Shape & input_shape = {},
const Shape & output_shape = {} )

Re-initializes the layer.

Parameters
input_shapePer-sample input shape (time_steps, input_features).
output_shapePer-sample output shape (hidden units).

◆ set_activation_function()

void opennn::Recurrent::set_activation_function ( const string & )

Sets the activation function applied to the hidden state.

Receives the activation name; see Activation::Function for the supported set.

◆ set_input_shape()

void opennn::Recurrent::set_input_shape ( const Shape & )
overridevirtual

Updates the input shape and re-shapes weight tensors accordingly.

Reimplemented from opennn::Layer.

◆ set_output_shape()

void opennn::Recurrent::set_output_shape ( const Shape & )
overridevirtual

Updates the output shape and re-shapes weight tensors accordingly.

Reimplemented from opennn::Layer.

◆ write_JSON_body()

void opennn::Recurrent::write_JSON_body ( JsonWriter & ) const
overridevirtual

Writes the layer-specific JSON body (shapes, activation name).

Reimplemented from opennn::Layer.