athena__network_submodule Submodule

Submodule containing implementations for the network module



Functions

function format_training_real(value, decimals, scientific) result(formatted)

Format a training metric with a configurable number of decimal places.

Arguments

Type IntentOptional 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

Return Value character(len=64)

Formatted string


Module Functions

module function accuracy_eval(this, output, start_index, end_index) result(accuracy)

Get the loss for the output

Arguments

Type IntentOptional 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

Return Value real(kind=real32)

Loss value

module function forward_eval(this, input) result(output)

Forward pass for evaluation

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout), target :: this

Instance of network

class(*), intent(in), dimension(:,:) :: input

Input

Return Value type(array_type), pointer, (:,:)

Output

module function forward_eval_multi(this, input) result(output)

Forward pass for evaluation

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout), target :: this

Instance of network

class(*), intent(in), dimension(:,:) :: input

Input

Return Value type(array_ptr_type), pointer, (:)

Output

pure module function get_gradients(this) result(gradients)

Get gradients

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this

Instance of network

Return Value real(kind=real32), dimension(this%num_params)

Gradients

pure module function get_num_params(this) result(num_params)

Get the number of learnable parameters in the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this

Instance of network

Return Value integer

Number of parameters

module function get_output(this) result(output)

Get the output of the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this

Instance of network

Return Value type(array_type), dimension(:,:), allocatable

Output

module function get_output_shape(this) result(output_shape)

Get the output of the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this

Instance of network

Return Value integer, dimension(2)

Output shape

pure module function get_params(this) result(params)

Get learnable parameters

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this

Instance of network

Return Value real(kind=real32), dimension(this%num_params)

Parameters

module function get_sample_array(input, start_index, end_index, batch_size, as_graph) result(sample)

Get samples of batch size from a derived type array

Arguments

Type IntentOptional 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

Return Value type(array_type), dimension(:,:), allocatable

Sample array

module function get_sample_flang(input, start_index, end_index, batch_size) result(sample)

Get samples of batch size from a real array

Arguments

Type IntentOptional 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

Return Value real(kind=real32), allocatable, (:,:)

Sample array

module function get_sample_graph1d(input, start_index, end_index, batch_size) result(sample)

Get samples of batch size from a graph

Arguments

Type IntentOptional 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

Return Value type(graph_type), dimension(1, batch_size)

Sample array

module function get_sample_graph2d(input, start_index, end_index, batch_size) result(sample)

Get samples of batch size from a graph

Arguments

Type IntentOptional 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

Return Value type(graph_type), dimension(size(input,1), batch_size)

Sample array

module function get_sample_ptr(input, start_index, end_index, batch_size) result(sample_ptr)

Get samples of batch size from a real array

Arguments

Type IntentOptional 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

Return Value real(kind=real32), pointer, (:,:)

Pointer to sample

module function inverse_design_array_0d(this, target, x_init, optimiser, steps) result(x_opt)

Optimise the input so the network output matches a target. Wraps the array_type implementation after converting to 2D array.

Arguments

Type IntentOptional 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

Return Value type(array_type)

Optimised input

module function inverse_design_array_2d(this, target, x_init, optimiser, steps) result(x_opt)

Optimise the input so the network output matches a target. Wraps the array_type implementation after converting to 2D array.

Arguments

Type IntentOptional 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

Return Value type(array_type), dimension(size(x_init,1), size(x_init,2))

Optimised input

module function inverse_design_real(this, target, x_init, optimiser, steps) result(x_opt)

Optimise input to match a target output (real inputs). Wraps the array_type implementation after converting real arrays.

Arguments

Type IntentOptional 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

Return Value real(kind=real32), dimension(size(x_init,1), size(x_init,2))

Optimised input

module function layer_from_id(this, id) result(layer)

Get layer from its ID

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in), target :: this

Instance of network

integer, intent(in) :: id

Layer ID

Return Value class(base_layer_type), pointer

Layer

module function loss_eval(this, start_index, end_index) result(loss)

Get the loss for the output

Arguments

Type IntentOptional 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

Return Value type(array_type), pointer

Loss value

module function network_setup(layers, optimiser, loss_method, accuracy_method, metrics, batch_size) result(network)

Setup the network

Arguments

Type IntentOptional 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

Return Value type(network_type)

Network to setup

module function predict_array(this, input, verbose) result(output)

Predict the output for a generic input

Arguments

Type IntentOptional 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

Return Value type(array_type), dimension(:,:), allocatable

Predicted output

module function predict_array_from_real(this, input, output_as_array, verbose) result(output)

Predict the output for a generic input

Arguments

Type IntentOptional 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

Return Value type(array_type), dimension(:,:), allocatable

Predicted output

module function predict_generic(this, input, verbose, output_as_graph) result(output)

Predict the output for a generic input

Arguments

Type IntentOptional 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

Return Value class(*), dimension(:,:), allocatable

Predicted output

module function predict_graph1d(this, input, verbose) result(output)

Predict the output for a graph input

Arguments

Type IntentOptional 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

Return Value type(graph_type), dimension(size(this%leaf_vertices),size(input))

Output graph

module function predict_graph2d(this, input, verbose) result(output)

Predict the output for a graph input

Arguments

Type IntentOptional 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

Return Value type(graph_type), dimension(size(this%leaf_vertices),size(input,dim=2))

Output graph

module function predict_real(this, input, verbose) result(output)

Predict the output for a 1D input

Arguments

Type IntentOptional 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

Return Value real(kind=real32), dimension(:,:), allocatable

Output

module function save_input_to_network(this, input) result(num_samples)

Save input to network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

class(*), intent(in), dimension(..) :: input

Input

Return Value integer

Number of samples


Module Subroutines

module subroutine add(this, layer, input_list, output_list, operator)

Add a layer to the network

Arguments

Type IntentOptional 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

module subroutine build_from_onnx(this, nodes, initialisers, inputs, value_info, verbose)

Build network from ONNX nodes and initialisers

Arguments

Type IntentOptional 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

module subroutine build_leaf_vertices(this)

Calculate the output vertices of the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

module subroutine build_root_vertices(this)

Calculate the root vertices of the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

module subroutine build_vertex_order(this)

Generate the order of the layers in the network

Read more…

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

module subroutine compile(this, optimiser, loss_method, accuracy_method, metrics, batch_size, verbose)

Compile the network

Arguments

Type IntentOptional 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

recursive module subroutine dfs(this, vertex_index, visited, order, order_index)

Depth first search algorithm

Arguments

Type IntentOptional 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

module subroutine extract_output_real(this, output)

Get the output of the network as real array

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this
real(kind=real32), intent(out), dimension(..), allocatable :: output

Output

module subroutine forward_generic2d(this, input)

Forward pass for array derived type input

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout), target :: this

Instance of network

class(*), intent(in), dimension(:,:) :: input

Input

module subroutine network_copy(this, source)

Procedure to copy a network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

type(network_type), intent(in), target :: source

Instance of network to be copied

module subroutine network_reduction(this, source)

Procedure to add two networks together

Arguments

Type IntentOptional 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

module subroutine nullify_graph(this)

Nullify the input graph

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

module subroutine post_epoch_hook(this, epoch, loss, accuracy)

Default epoch hook — no-op. Override in a derived type to add custom per-epoch behaviour.

Arguments

Type IntentOptional 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

module subroutine print(this, file)

Print the network to a file

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this

Instance of network

character(len=*), intent(in) :: file

File to print the network to

module subroutine print_summary(this)

Print a summary of the network architecture

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(in) :: this

Instance of network

module subroutine read(this, file)

Read the network from a file check if a tag line check for card

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

character(len=*), intent(in) :: file

File to read the network from

module subroutine read_network_settings(this, unit)

Read the network settings from a file

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

integer, intent(in) :: unit

File unit

module subroutine read_optimiser_settings(this, unit)

Read the optimiser settings from a file

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

integer, intent(in) :: unit

File unit

module subroutine reset(this)

Reset the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

module subroutine reset_gradients(this)

Reset gradients

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

module subroutine reset_state(this)

Reset the hidden state of all layers in the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

module subroutine restore_mode(this, mode_store)

Restore the training/inference mode of each layer from a stored array.

Arguments

Type IntentOptional 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

module subroutine save_output_to_network(this, output)

Save output to network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

class(*), intent(in), dimension(:,:) :: output

Output

module subroutine set_accuracy(this, accuracy_method, verbose)

Set the accuracy method for the network

Arguments

Type IntentOptional 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

module subroutine set_batch_size(this, batch_size)

Set the batch size for the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

integer, intent(in) :: batch_size

Batch size

module subroutine set_gradients(this, gradients)

Set gradients

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

real(kind=real32), intent(in), dimension(..) :: gradients

Gradients

module subroutine set_inference_mode(this, mode_store, layer_indices)

Put the network in inference mode.

Arguments

Type IntentOptional 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.

module subroutine set_loss(this, loss_method, verbose)

Set the loss method for the network

Arguments

Type IntentOptional 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

module subroutine set_metrics(this, metrics)

Set the metrics for the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

class(*), intent(in), dimension(..) :: metrics

Metrics

module subroutine set_params(this, params)

Set learnable parameters

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network

real(kind=real32), intent(in), dimension(this%num_params) :: params

Parameters

module subroutine set_training_mode(this, mode_store, layer_indices)

Put the network in training mode.

Arguments

Type IntentOptional 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.

module subroutine test(this, input, output, verbose)

Test the network

Arguments

Type IntentOptional 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

module subroutine train(this, input, output, num_epochs, batch_size, plateau_threshold, shuffle_batches, batch_print_step, verbose, print_precision, scientific_print, early_stopping, val_input, val_output)

Train the network

Read more…

Arguments

Type IntentOptional 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

module subroutine update(this)

Update the network

Arguments

Type IntentOptional Attributes Name
class(network_type), intent(inout) :: this

Instance of network