Submodule containing implementations for the network module
Format a training metric with a configurable number of decimal places.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=real32), | intent(in) | :: | value |
Value to format |
||
| integer, | intent(in) | :: | decimals |
Number of decimal places |
||
| logical, | intent(in) | :: | scientific |
Whether to use scientific notation |
Formatted string
Get the loss for the output
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
||
| class(*), | intent(in), | dimension(:,:) | :: | output |
Output |
|
| integer, | intent(in) | :: | start_index |
Start and end batch indices |
||
| integer, | intent(in) | :: | end_index |
Start and end batch indices |
Loss value
Forward pass for evaluation
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout), | target | :: | this |
Instance of network |
|
| class(*), | intent(in), | dimension(:,:) | :: | input |
Input |
Output
Forward pass for evaluation
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout), | target | :: | this |
Instance of network |
|
| class(*), | intent(in), | dimension(:,:) | :: | input |
Input |
Output
Get gradients
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
Gradients
Get the number of learnable parameters in the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
Number of parameters
Get the output of the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
Output
Get the output of the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
Output shape
Get learnable parameters
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
Parameters
Get samples of batch size from a derived type array
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(array_type), | intent(in), | dimension(:,:) | :: | input |
Input array |
|
| integer, | intent(in) | :: | start_index |
Start and end indices |
||
| integer, | intent(in) | :: | end_index |
Start and end indices |
||
| integer, | intent(in) | :: | batch_size |
Batch size |
||
| logical, | intent(in) | :: | as_graph |
Boolean whether to treat the input as a graph |
Sample array
Get samples of batch size from a real array
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=real32), | intent(in), | dimension(..) | :: | input |
Input array |
|
| integer, | intent(in) | :: | start_index |
Start and end indices |
||
| integer, | intent(in) | :: | end_index |
Start and end indices |
||
| integer, | intent(in) | :: | batch_size |
Batch size |
Sample array
Get samples of batch size from a graph
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(graph_type), | intent(in), | dimension(:) | :: | input |
Input array |
|
| integer, | intent(in) | :: | start_index |
Start and end indices |
||
| integer, | intent(in) | :: | end_index |
Start and end indices |
||
| integer, | intent(in) | :: | batch_size |
Batch size |
Sample array
Get samples of batch size from a graph
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(graph_type), | intent(in), | dimension(:,:) | :: | input |
Input array |
|
| integer, | intent(in) | :: | start_index |
Start and end indices |
||
| integer, | intent(in) | :: | end_index |
Start and end indices |
||
| integer, | intent(in) | :: | batch_size |
Batch size |
Sample array
Get samples of batch size from a real array
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=real32), | intent(in), | dimension(..), target | :: | input |
Input array |
|
| integer, | intent(in) | :: | start_index |
Start and end indices |
||
| integer, | intent(in) | :: | end_index |
Start and end indices |
||
| integer, | intent(in) | :: | batch_size |
Batch size |
Pointer to sample
Optimise the input so the network output matches a target. Wraps the array_type implementation after converting to 2D array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout), | target | :: | this |
Instance of the network |
|
| type(array_type), | intent(in) | :: | target |
Target output values |
||
| type(array_type), | intent(in) | :: | x_init |
Initial input values |
||
| class(base_optimiser_type), | intent(in), | optional | :: | optimiser |
Optimiser for input updates (defaults to network optimiser) |
|
| integer, | intent(in) | :: | steps |
Number of optimisation iterations |
Optimised input
Optimise the input so the network output matches a target. Wraps the array_type implementation after converting to 2D array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout), | target | :: | this |
Instance of the network |
|
| type(array_type), | intent(in), | dimension(:,:) | :: | target |
Target output values |
|
| type(array_type), | intent(in), | dimension(:,:) | :: | x_init |
Initial input values |
|
| class(base_optimiser_type), | intent(in), | optional | :: | optimiser |
Optimiser for input updates (defaults to network optimiser) |
|
| integer, | intent(in) | :: | steps |
Number of optimisation iterations |
Optimised input
Optimise input to match a target output (real inputs). Wraps the array_type implementation after converting real arrays.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout), | target | :: | this |
Instance of the network |
|
| real(kind=real32), | intent(in), | dimension(:,:) | :: | target |
Target output values |
|
| real(kind=real32), | intent(in), | dimension(:,:) | :: | x_init |
Initial input values |
|
| class(base_optimiser_type), | intent(in), | optional | :: | optimiser |
Optimiser for input updates (defaults to network optimiser) |
|
| integer, | intent(in) | :: | steps |
Number of optimisation iterations |
Optimised input
Get layer from its ID
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in), | target | :: | this |
Instance of network |
|
| integer, | intent(in) | :: | id |
Layer ID |
Layer
Get the loss for the output
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout), | target | :: | this |
Instance of network |
|
| integer, | intent(in) | :: | start_index |
Start and end batch indices |
||
| integer, | intent(in) | :: | end_index |
Start and end batch indices |
Loss value
Setup the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(container_layer_type), | intent(in), | dimension(:) | :: | layers |
Layers to add to the network |
|
| class(base_optimiser_type), | intent(in), | optional | :: | optimiser |
Optimiser to use for training |
|
| class(*), | intent(in), | optional | :: | loss_method |
Loss method |
|
| character(len=*), | intent(in), | optional | :: | accuracy_method |
Accuracy method |
|
| class(*), | intent(in), | optional, | dimension(..) | :: | metrics |
Metrics |
| integer, | intent(in), | optional | :: | batch_size |
Batch size |
Network to setup
Predict the output for a generic input
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(array_type), | intent(in), | dimension(..) | :: | input |
Input graph |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Predicted output
Predict the output for a generic input
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(*), | intent(in), | dimension(..) | :: | input |
Input graph |
|
| logical, | intent(in) | :: | output_as_array |
Whether to output as array |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Predicted output
Predict the output for a generic input
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(*), | intent(in), | dimension(:,:) | :: | input |
Input graph |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
|
| logical, | intent(in), | optional | :: | output_as_graph |
Boolean whether to output as graph |
Predicted output
Predict the output for a graph input
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| type(graph_type), | intent(in), | dimension(:) | :: | input |
Input graph |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Output graph
Predict the output for a graph input
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| type(graph_type), | intent(in), | dimension(:,:) | :: | input |
Input graph |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Output graph
Predict the output for a 1D input
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| real(kind=real32), | intent(in), | dimension(..) | :: | input |
Input |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Output
Save input to network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(*), | intent(in), | dimension(..) | :: | input |
Input |
Number of samples
Add a layer to the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(base_layer_type), | intent(in) | :: | layer |
Layer to add to the network |
||
| integer, | intent(in), | optional, | dimension(:) | :: | input_list |
List of input layers |
| integer, | intent(in), | optional, | dimension(:) | :: | output_list |
List of output layers |
| class(*), | intent(in), | optional | :: | operator |
Operator to use to connect the layers |
Build network from ONNX nodes and initialisers
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| type(onnx_node_type), | intent(in), | dimension(:) | :: | nodes |
Array of ONNX nodes |
|
| type(onnx_initialiser_type), | intent(in), | dimension(:) | :: | initialisers |
Array of ONNX initialisers |
|
| type(onnx_tensor_type), | intent(in), | dimension(:) | :: | inputs |
Array of ONNX inputs |
|
| type(onnx_tensor_type), | intent(in), | dimension(:) | :: | value_info |
Array of ONNX value infos |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Calculate the output vertices of the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
Calculate the root vertices of the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
Generate the order of the layers in the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
Compile the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(base_optimiser_type), | intent(in), | optional | :: | optimiser |
Optimiser to use for training |
|
| class(*), | intent(in), | optional | :: | loss_method |
Loss method |
|
| character(len=*), | intent(in), | optional | :: | accuracy_method |
Accuracy method |
|
| class(*), | intent(in), | optional, | dimension(..) | :: | metrics |
Metrics |
| integer, | intent(in), | optional | :: | batch_size |
Batch size |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Depth first search algorithm
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
||
| integer, | intent(in) | :: | vertex_index |
Index of the vertex to start the search from |
||
| logical, | intent(inout), | dimension(this%auto_graph%num_vertices) | :: | visited |
Array to store whether a vertex has been visited |
|
| integer, | intent(inout), | dimension(this%auto_graph%num_vertices) | :: | order |
Array to store the order of the vertices |
|
| integer, | intent(inout) | :: | order_index |
Index of the current vertex in the order array |
Get the output of the network as real array
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this | |||
| real(kind=real32), | intent(out), | dimension(..), allocatable | :: | output |
Output |
Forward pass for array derived type input
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout), | target | :: | this |
Instance of network |
|
| class(*), | intent(in), | dimension(:,:) | :: | input |
Input |
Procedure to copy a network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| type(network_type), | intent(in), | target | :: | source |
Instance of network to be copied |
Procedure to add two networks together
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| type(network_type), | intent(in) | :: | source |
Instance of network to be added to this |
Nullify the input graph
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
Default epoch hook — no-op. Override in a derived type to add custom per-epoch behaviour.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| integer, | intent(in) | :: | epoch |
Current epoch number |
||
| real(kind=real32), | intent(in) | :: | loss |
Mean loss over the epoch |
||
| real(kind=real32), | intent(in) | :: | accuracy |
Mean accuracy over the epoch |
Print the network to a file
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
||
| character(len=*), | intent(in) | :: | file |
File to print the network to |
Print a summary of the network architecture
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(in) | :: | this |
Instance of network |
Read the network from a file check if a tag line check for card
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| character(len=*), | intent(in) | :: | file |
File to read the network from |
Read the network settings from a file
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| integer, | intent(in) | :: | unit |
File unit |
Read the optimiser settings from a file
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| integer, | intent(in) | :: | unit |
File unit |
Reset the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
Reset gradients
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
Reset the hidden state of all layers in the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
Restore the training/inference mode of each layer from a stored array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| logical, | intent(in), | dimension(:) | :: | mode_store |
Array storing the mode of each layer .true. = inference, .false. = training |
Save output to network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(*), | intent(in), | dimension(:,:) | :: | output |
Output |
Set the accuracy method for the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| character(len=*), | intent(in) | :: | accuracy_method |
Accuracy method |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Set the batch size for the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| integer, | intent(in) | :: | batch_size |
Batch size |
Set gradients
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| real(kind=real32), | intent(in), | dimension(..) | :: | gradients |
Gradients |
Put the network in inference mode.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| logical, | intent(out), | optional, | dimension(:), allocatable | :: | mode_store |
Optional array to store the training mode of each layer |
| integer, | intent(in), | optional, | dimension(:) | :: | layer_indices |
Optional array of layer indices to set to inference mode. If not provided, all layers will be set to inference mode. |
Set the loss method for the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(*), | intent(in) | :: | loss_method |
Loss method |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Set the metrics for the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(*), | intent(in), | dimension(..) | :: | metrics |
Metrics |
Set learnable parameters
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| real(kind=real32), | intent(in), | dimension(this%num_params) | :: | params |
Parameters |
Put the network in training mode.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| logical, | intent(out), | optional, | dimension(:), allocatable | :: | mode_store |
Optional array to store the training mode of each layer |
| integer, | intent(in), | optional, | dimension(:) | :: | layer_indices |
Optional array of layer indices to set to training mode. If not provided, all layers will be set to training mode. |
Test the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(*), | intent(in), | dimension(..) | :: | input |
Input data |
|
| class(*), | intent(in), | dimension(:,:) | :: | output |
Output data |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Train the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |
||
| class(*), | intent(in), | dimension(..) | :: | input |
Input data |
|
| class(*), | intent(in), | dimension(:,:) | :: | output |
Output data |
|
| integer, | intent(in) | :: | num_epochs |
Number of epochs |
||
| integer, | intent(in), | optional | :: | batch_size |
Batch size |
|
| real(kind=real32), | intent(in), | optional | :: | plateau_threshold |
Plateau threshold |
|
| logical, | intent(in), | optional | :: | shuffle_batches |
Shuffle batches |
|
| integer, | intent(in), | optional | :: | batch_print_step |
Batch print step |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
|
| integer, | intent(in), | optional | :: | print_precision |
Number of decimal places to print for training metrics |
|
| logical, | intent(in), | optional | :: | scientific_print |
Whether to print training metrics in scientific notation |
|
| logical, | intent(in), | optional | :: | early_stopping |
Whether to stop training early if convergence is detected |
|
| class(*), | intent(in), | optional, | dimension(..) | :: | val_input |
Validation input data |
| class(*), | intent(in), | optional, | dimension(:,:) | :: | val_output |
Validation expected output data |
Update the network
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | this |
Instance of network |