Module containing implementation of a dropout layer
This module implements dropout regularisation, randomly zeroing elements during training to prevent overfitting and co-adaptation of neurons.
Mathematical operation (training): where: - is the output - is the input - is the dropout probability (rate) - is a random variable uniformly distributed in [0,1]
Scaling by maintains expected value:
Inference: acts as identity (no dropout applied)
Benefits: Prevents overfitting, ensemble effect, forces redundancy Typical p values: 0.2-0.5 (higher dropout for larger networks) Reference: Srivastava et al. (2014), JMLR
Interface for setting up the dropout layer
Set up the dropout layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=real32), | intent(in) | :: | rate |
Drop rate |
||
| integer, | intent(in) | :: | num_masks |
Number of unique masks |
||
| integer, | intent(in), | optional, | dimension(:) | :: | input_shape |
Input shape |
Instance of the dropout layer
Type for dropout layer with overloaded procedures
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| type(graph_type), | public, | allocatable, dimension(:) | :: | graph |
Graph structure of input data |
||
| integer, | public | :: | id |
Unique identifier |
|||
| integer, | public | :: | idx | = | 0 |
Temporary index of sample (doesn't need to be accurate) |
|
| logical, | public | :: | inference | = | .false. |
Inference mode |
|
| integer, | public | :: | input_rank | = | 0 |
Rank of input data |
|
| integer, | public, | allocatable, dimension(:) | :: | input_shape |
Input shape |
||
| logical, | public, | allocatable, dimension(:,:) | :: | mask |
Mask for dropout |
||
| character(len=:), | public, | allocatable | :: | name |
Layer name |
||
| integer, | public | :: | num_masks |
Number of unique masks = number of samples in batch |
|||
| 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 |
||
| real(kind=real32), | public | :: | rate | = | 0.1_real32 |
Dropout rate, rate = 1 - keep_prob -- typical = 0.05-0.25 |
|
| character(len=20), | public | :: | subtype | = | repeat(" ", 20) | ||
| character(len=4), | public | :: | type | = | 'base' |
Layer type |
|
| logical, | public | :: | use_graph_input | = | .false. |
Use graph input |
|
| logical, | public | :: | use_graph_output | = | .false. |
Use graph output |
Interface for setting up the dropout layer
| private module function layer_setup (rate, num_masks, input_shape) | Set up the dropout layer |
| 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_dropout | 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) :: generate_mask => generate_dropout_mask | Generate dropout mask |
| procedure, public, pass(this) :: get_attributes => get_attributes_base | Get the attributes of the layer (for ONNX export) |
| procedure, public, pass(this) :: get_num_params => get_num_params_base | Get the number of parameters in the layer |
| procedure, public, pass(this) :: init => init_dropout | Initialise dropout layer |
| procedure, public, pass(this) :: nullify_graph => nullify_graph_base | Nullify the forward pass data of the layer to free memory |
| 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_dropout | Print dropout layer to unit |
| procedure, public, pass(this) :: read => read_dropout | Read dropout layer from file |
| 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_dropout | Set hyperparameters for dropout 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 dropout layer from attributes and return layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(onnx_node_type), | intent(in) | :: | node |
ONNX node information |
||
| type(onnx_initialiser_type), | intent(in), | dimension(:) | :: | initialisers |
ONNX initialiser information |
|
| type(onnx_tensor_type), | intent(in), | dimension(:) | :: | value_info |
ONNX value info information |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the dropout layer
Read dropout layer from file and return layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | unit |
File unit |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the base layer
Set up the dropout layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=real32), | intent(in) | :: | rate |
Drop rate |
||
| integer, | intent(in) | :: | num_masks |
Number of unique masks |
||
| integer, | intent(in), | optional, | dimension(:) | :: | input_shape |
Input shape |
Instance of the dropout layer
Read ONNX attributes for dropout layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(dropout_layer_type), | intent(inout) | :: | this |
Instance of the dropout layer |
||
| type(onnx_node_type), | intent(in) | :: | node |
ONNX node information |
||
| type(onnx_initialiser_type), | intent(in), | dimension(:) | :: | initialisers |
ONNX initialiser information |
|
| type(onnx_tensor_type), | intent(in), | dimension(:) | :: | value_info |
ONNX value info |
|
| integer, | intent(in) | :: | verbose |
Verbosity level |
Forward propagation
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(dropout_layer_type), | intent(inout) | :: | this |
Instance of the dropout layer |
||
| class(array_type), | intent(in), | dimension(:,:) | :: | input |
Input values |
Generate dropout mask
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(dropout_layer_type), | intent(inout) | :: | this |
Instance of the dropout layer |
Initialise dropout layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(dropout_layer_type), | intent(inout) | :: | this |
Instance of the dropout layer |
||
| integer, | intent(in), | dimension(:) | :: | input_shape |
Input shape |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Print dropout layer to unit
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(dropout_layer_type), | intent(in) | :: | this |
Instance of the dropout layer |
||
| integer, | intent(in) | :: | unit |
File unit |
Read dropout layer from file
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(dropout_layer_type), | intent(inout) | :: | this |
Instance of the dropout layer |
||
| integer, | intent(in) | :: | unit |
File unit |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Set hyperparameters for dropout layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(dropout_layer_type), | intent(inout) | :: | this |
Instance of the dropout layer |
||
| real(kind=real32), | intent(in) | :: | rate |
Drop rate |
||
| integer, | intent(in) | :: | num_masks |
Number of unique masks |