Module containing implementation of recurrent neural network layers
This module implements the simple recurrent neural network (RNN) layer, which is designed to handle sequential data by maintaining a hidden state.
Simple RNN layer (equivalent to RNNCell of PyTorch):
where: - is input at time t - is hidden state at time t - is the activation function (e.g., tanh, relu) - matrices are learnable weights - vectors are learnable biases
Properties: - Processes sequential data with temporal dependencies - Maintains hidden state across time steps
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | hidden_size | |||
| integer, | intent(in), | optional | :: | input_size | ||
| logical, | intent(in), | optional | :: | use_bias | ||
| class(*), | intent(in), | optional | :: | activation | ||
| class(*), | intent(in), | optional | :: | kernel_initialiser | ||
| class(*), | intent(in), | optional | :: | bias_initialiser | ||
| integer, | intent(in), | optional | :: | verbose |
Type for simple RNN layer
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| class(base_actv_type), | public, | allocatable | :: | activation |
Activation function |
||
| class(base_init_type), | public, | allocatable | :: | bias_init |
Initialisers for kernel and bias |
||
| character(len=14), | public | :: | bias_initialiser | = | '' |
Initialisers for kernel and bias |
|
| integer, | public, | allocatable, dimension(:) | :: | bias_shape |
Shape of biases |
||
| type(graph_type), | public, | allocatable, dimension(:) | :: | graph |
Graph structure of input data |
||
| integer, | public | :: | hidden_size |
Size of hidden state |
|||
| type(array_type), | public, | pointer | :: | hidden_state | => | null() |
Hidden state |
| integer, | public | :: | id |
Unique identifier |
|||
| logical, | public | :: | inference | = | .false. |
Inference mode |
|
| integer, | public | :: | input_rank | = | 0 |
Rank of input data |
|
| integer, | public, | allocatable, dimension(:) | :: | input_shape |
Input shape |
||
| integer, | public | :: | input_size |
Size of input |
|||
| class(base_init_type), | public, | allocatable | :: | kernel_init |
Initialisers for kernel and bias |
||
| character(len=14), | public | :: | kernel_initialiser | = | '' |
Initialisers for kernel and bias |
|
| character(len=:), | public, | allocatable | :: | name |
Layer name |
||
| integer, | public | :: | num_params | = | 0 |
Number of learnable parameters |
|
| class(array_type), | public, | allocatable, dimension(:,:) | :: | output |
Output |
||
| integer, | public | :: | output_rank | = | 0 |
Rank of output data |
|
| integer, | public, | allocatable, dimension(:) | :: | output_shape |
Output shape |
||
| type(array_type), | public, | allocatable, dimension(:) | :: | params |
Learnable parameters |
||
| character(len=20), | public | :: | subtype | = | repeat(" ", 20) | ||
| integer, | public | :: | time_step |
Current time step |
|||
| character(len=4), | public | :: | type | = | 'base' |
Layer type |
|
| logical, | public | :: | use_bias | = | .false. |
Layer has bias |
|
| logical, | public | :: | use_graph_input | = | .false. |
Use graph input |
|
| logical, | public | :: | use_graph_output | = | .false. |
Use graph output |
|
| integer, | public, | allocatable, dimension(:,:) | :: | weight_shape |
Shape of weights |
| private module function layer_setup (hidden_size, input_size, use_bias, activation, kernel_initialiser, bias_initialiser, verbose) |
| procedure, public :: add_t_t => add_learnable | Add two layers |
| procedure, public, pass(this) :: build_from_onnx => build_from_onnx_base | Build layer from ONNX node and initialiser |
| procedure, public, pass(this) :: emit_onnx_graph_inputs => emit_onnx_graph_inputs_base | Emit graph input tensor declarations for this layer |
| procedure, public, pass(this) :: emit_onnx_nodes => emit_onnx_nodes_base | Emit ONNX JSON nodes for this layer (format-aware and polymorphic) |
| procedure, public, pass(this) :: extract_output => extract_output_base | Extract the output of the layer as a standard real array |
| procedure, public, pass(this) :: forward => forward_recurrent | |
| procedure, public, pass(this) :: forward_eval => forward_eval_base | Forward pass of layer and return output for evaluation |
| procedure, public, pass(this) :: get_attributes => get_attributes_base | Get the attributes of the layer (for ONNX export) |
| procedure, public, pass(this) :: get_gradients | Get parameter gradients of layer |
| procedure, public, pass(this) :: get_num_params => get_num_params_recurrent | |
| procedure, public, pass(this) :: get_params | Get learnable parameters of layer |
| procedure, public, pass(this) :: init => init_recurrent | |
| procedure, public, pass(this) :: nullify_graph => nullify_graph_base | Nullify the forward pass data of the layer to free memory |
| generic, public :: operator(+) => add_t_t | Operator overloading for addition |
| procedure, public, pass(this) :: print => print_base | Print the layer to a file with additional information |
| procedure, public, pass(this) :: print_to_unit => print_to_unit_recurrent | |
| procedure, public, pass(this) :: read => read_recurrent | |
| procedure, public, pass(this) :: reduce => reduce_learnable | Merge another learnable layer into this one |
| procedure, public, pass(this) :: reset_state => reset_state_recurrent | |
| procedure, public, pass(this) :: set_gradients | Set learnable parameters of layer |
| procedure, public, pass(this) :: set_graph => set_graph_base | Set the graph structure of the input data !! this is adjacency and edge weighting |
| procedure, public, pass(this) :: set_hyperparams => set_hyperparams_recurrent | |
| procedure, public, pass(this) :: set_params | Set learnable parameters of layer |
| procedure, public, pass(this) :: set_rank => set_rank_base | Set the input and output ranks of the layer |
| procedure, public, pass(this) :: set_shape => set_shape_base | Set the input shape of the layer |
Read recurrent layer from file and return layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | unit |
Unit number |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(recurrent_layer_type), | intent(in) | :: | this |
Setup a recurrent layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | hidden_size |
Size of hidden state |
||
| integer, | intent(in), | optional | :: | input_size |
Size of input |
|
| logical, | intent(in), | optional | :: | use_bias |
Whether to use bias |
|
| class(*), | intent(in), | optional | :: | activation |
Activation function |
|
| class(*), | intent(in), | optional | :: | kernel_initialiser |
Activation function, kernel initialiser, and bias initialiser |
|
| class(*), | intent(in), | optional | :: | bias_initialiser |
Activation function, kernel initialiser, and bias initialiser |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the recurrent layer
Forward propagation
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(recurrent_layer_type), | intent(inout) | :: | this |
Instance of the recurrent layer |
||
| class(array_type), | intent(in), | dimension(:,:) | :: | input |
Input values |
Initialise the recurrent layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(recurrent_layer_type), | intent(inout) | :: | this |
Instance of the recurrent layer |
||
| integer, | intent(in), | dimension(:) | :: | input_shape |
Shape of the input |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Print recurrent layer to unit
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(recurrent_layer_type), | intent(in) | :: | this |
Instance of the fully connected layer |
||
| integer, | intent(in) | :: | unit |
File unit |
Read recurrent layer from file
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(recurrent_layer_type), | intent(inout) | :: | this |
Instance of the recurrent layer |
||
| integer, | intent(in) | :: | unit |
Unit number |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Reset the hidden state of the recurrent layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(recurrent_layer_type), | intent(inout) | :: | this |
Instance of the recurrent layer |
Set the hyperparameters for fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(recurrent_layer_type), | intent(inout) | :: | this |
Instance of the recurrent layer |
||
| integer, | intent(in) | :: | hidden_size |
Number of hidden units |
||
| logical, | intent(in) | :: | use_bias |
Whether to use bias |
||
| class(base_actv_type), | intent(in), | allocatable | :: | activation |
Activation function |
|
| class(base_init_type), | intent(in), | allocatable | :: | kernel_initialiser |
Kernel and bias initialisers |
|
| class(base_init_type), | intent(in), | allocatable | :: | bias_initialiser |
Kernel and bias initialisers |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |