Module containing implementation of a fully connected layer
This module implements a fully connected (dense) layer, the fundamental building block of neural networks that connects every input to every output.
Mathematical operation:
where: - is the input vector - is the weight matrix - is the bias vector - is the activation function - is the output vector
Number of parameters: (if bias used)
Properties: Universal function approximator (with sufficient width/depth) Learns arbitrary non-linear mappings between input and output spaces
Attribution statement: The get_num_params procedure is based on code from the neural-fortran library https://github.com/modern-fortran/neural-fortran
Interface for setting up the fully connected layer
Setup a fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | num_outputs |
Number of outputs |
||
| integer, | intent(in), | optional | :: | num_inputs |
Number of inputs |
|
| logical, | intent(in), | optional | :: | use_bias |
Whether to use bias |
|
| class(*), | intent(in), | optional | :: | activation |
Activation function |
|
| class(*), | intent(in), | optional | :: | kernel_initialiser |
Kernel and bias initialisers |
|
| class(*), | intent(in), | optional | :: | bias_initialiser |
Kernel and bias initialisers |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the fully connected layer
Type for fully connected (aka dense) layer with overloaded procedures
| 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 | :: | 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 |
||
| 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_inputs |
Number of inputs |
|||
| integer, | public | :: | num_outputs |
Number of outputs |
|||
| 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) | ||
| 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 |
||
| type(array_type), | public, | dimension(1) | :: | z |
Temporary arrays for forward propagation |
Interface for setting up the fully connected layer
| private module function layer_setup (num_outputs, num_inputs, use_bias, activation, kernel_initialiser, bias_initialiser, verbose) | Setup a fully connected layer |
| final :: finalise_full | Finalise fully connected layer |
| procedure, public :: add_t_t => add_learnable | Add two layers |
| procedure, public, pass(this) :: build_from_onnx => build_from_onnx_full | Build fully connected 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_full | Forward propagation derived type handler |
| 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_full | Get the number of parameters for fully connected layer |
| procedure, public, pass(this) :: get_params | Get learnable parameters of layer |
| procedure, public, pass(this) :: init => init_full | Initialise fully connected layer |
| 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_full | Print the layer to a file |
| procedure, public, pass(this) :: read => read_full | Read the layer from a file |
| procedure, public, pass(this) :: reduce => reduce_learnable | Merge another learnable layer into this one |
| 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_full | Set the hyperparameters for fully connected layer |
| 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 |
Build fully connected layer from attributes and return layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(onnx_node_type), | intent(in) | :: | node |
Instance of ONNX node information |
||
| type(onnx_initialiser_type), | intent(in), | dimension(:) | :: | initialisers |
Instance of ONNX initialiser information |
|
| type(onnx_tensor_type), | intent(in), | dimension(:) | :: | value_info |
Instance of ONNX value info information |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the 2D convolutional layer
Read fully connected 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
Get the number of parameters for fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(full_layer_type), | intent(in) | :: | this |
Instance of the fully connected layer |
Number of parameters
Setup a fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | num_outputs |
Number of outputs |
||
| integer, | intent(in), | optional | :: | num_inputs |
Number of inputs |
|
| 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 fully connected layer
Read ONNX attributes for fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(full_layer_type), | intent(inout) | :: | this |
Instance of the fully connected layer |
||
| type(onnx_node_type), | intent(in) | :: | node |
Instance of ONNX node information |
||
| type(onnx_initialiser_type), | intent(in), | dimension(:) | :: | initialisers |
Instance of ONNX initialiser information |
|
| type(onnx_tensor_type), | intent(in), | dimension(:) | :: | value_info |
Instance of ONNX value info information |
|
| integer, | intent(in) | :: | verbose |
Verbosity level |
Finalise fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(full_layer_type), | intent(inout) | :: | this |
Instance of the fully connected layer |
Forward propagation
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(full_layer_type), | intent(inout) | :: | this |
Instance of the fully connected layer |
||
| class(array_type), | intent(in), | dimension(:,:) | :: | input |
Input values |
Initialise fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(full_layer_type), | intent(inout) | :: | this |
Instance of the fully connected layer |
||
| integer, | intent(in), | dimension(:) | :: | input_shape |
Input shape |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Print fully connected layer to unit
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(full_layer_type), | intent(in) | :: | this |
Instance of the fully connected layer |
||
| integer, | intent(in) | :: | unit |
File unit |
Read fully connected layer from file
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(full_layer_type), | intent(inout) | :: | this |
Instance of the fully connected layer |
||
| integer, | intent(in) | :: | unit |
Unit number |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Set the hyperparameters for fully connected layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(full_layer_type), | intent(inout) | :: | this |
Instance of the fully connected layer |
||
| integer, | intent(in) | :: | num_outputs |
Number of outputs |
||
| 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 |