mom_ann module reference

Implements the general purpose Artificial Neural Network (ANN).

More…

Data Types

layer_type

Type for a single Linear layer of ANN, i.e.

ann_cs

Control structure/type for ANN.

Functions/Subroutines

ann_init()

Initialization of ANN.

ann_allocate()

Allocate an ANN.

ann_test()

Test ANN by comparing the prediction with the test data.

ann_end()

Deallocates memory of ANN.

activation_fn()

The default activation function.

ann_apply_vector_orig()

Single application of ANN inference using vector input and output.

ann_apply_vector_oi()

Single application of ANN inference using vector input and output.

ann_apply_array_sio()

Single application of ANN inference using array input and output with (space,feature) indexing.

set_layer()

Sets weights and bias for a single layer.

set_input_normalization()

Sets input normalization.

set_output_normalization()

Sets output normalization.

ann_random()

Create a random ANN.

randomize_layer()

Fill a layer with random numbers.

ann_unit_tests()

Runs unit tests on ANN functions.

Detailed Description

The mom_ann() module is a pure fortran implementation of fully-connected feed-forward networks to facilitate easy evaluation of data-driven functions in MOM6. For performant implementations or for novel architectires, using machine-learning libraries (e.g. via module is a pure fortran implementation of fully-connected feed-forward networks to facilitate easy evaluation of data-driven functions in MOM6. For performant implementations or for novel architectires, using machine-learning libraries (e.g. via mom_database_comms()) are necessary, or at least likely to be more efficient.) are necessary, or at least likely to be more efficient.

The artificial neural network (ANN) understood by this MOM6 module has \(N\) layers, including the input-layer and output-layer, thus requireing \(N \geq 2\).

The output values (neurons or nodes) of any layer other than the input layer (i.e. \(l>1\)) are

\[y_{l,j} = f_l( b_{l,j} + A_{l,j,i} x_{l-1,i} )\]

where \(f(x) = max(0, x)\) is the ReLU activation function, \(b_{l,j}\) is a bias for each neuron, \(A_{l,j,i}\) are a rectangular matrix of weights for each layer, and \(x_{l-1,i}\) are the outputs of the previous layer, \(l-1\). The subscript on \(f_l()\) indicates the activation function is optional for each layer.

Currently, the performance of various implementations is dependent on the shape/size of the network and the size of input data. For this reason we provide several versions that all yield the same result but for differently shaped inputs.

/images/https://upload.wikimedia.org/wikipedia/commons/4/46/Colored_neural_network.svgFig:Athreelayernetworkwith3inputs,2outputs,and1hiddenlayer.Therearetworectanglarmatricesofweights(blackarrows).Thebiasforeachneuronisimplied."

Type Documentation

type  mom_ann/layer_type

Type for a single Linear layer of ANN, i.e. stores the matrix A and bias b for matrix-vector multiplication y = A*x + b.

Type fields:
  • % output_width :: integer, private Number of rows in matrix A.

  • % input_width :: integer, private Number of columns in matrix A.

  • % activation :: logical, private If true, apply the default activation function.

  • % a :: real, dimension(:,:), allocatable, private Matrix in column-major order of size A(output_width, input_width) [nondim].

  • % b :: real, dimension(:), allocatable, private bias vector of size output_width [nondim]

[source]

type  mom_ann/ann_cs

Control structure/type for ANN.

Type fields:
  • % num_layers :: integer Number of layers in the ANN, including the input and output. For example, for ANN with one hidden layer, num_layers = 3.

  • % layer_sizes :: integer, dimension(:), allocatable Array of length num_layers, storing the number of neurons in.

  • % layers :: type(layer_type), dimension(:), allocatable Array of length num_layers-1, where each element is the Linear.

  • % input_means :: real, dimension(:), allocatable Array of length layer_sizes(1) containing the mean of each input feature.

  • % input_norms :: real, dimension(:), allocatable Array of length layer_sizes(1) containing the inverse of the standard.

  • % output_means :: real, dimension(:), allocatable Array of length layer_sizes(num_layers) containing the mean of each.

  • % output_norms :: real, dimension(:), allocatable Array of length layer_sizes(num_layers) containing the standard deviation.

  • % parameters :: integer, public Count of number of parameters.

[source]

Function/Subroutine Documentation

subroutine mom_ann/ann_init(CS, NNfile)

Initialization of ANN. Allocates memory and reads ANN parameters from NetCDF file. The NetCDF file must contain: Integer num_layers. Integer arrays: layer_sizes, input_norms, output_norms Matrices and biases for Linear layers can be Real(4) or Real(8) and are named as: A0, b0 for the first layer; A1, b1 for the second layer and so on.

Parameters:
  • cs :: [inout] ANN control structure.

  • nnfile :: [in] The name of NetCDF file having neural network parameters

Call to:

ann_allocate ann_test mom_error_handler::mom_mesg

Called from:

mom_zanna_bolton::zb2020_init

[source]

subroutine mom_ann/ann_allocate(CS, num_layers, layer_sizes)

Allocate an ANN.

This creates the memory for storing weights and intermediate work arrays, but does not set the values of weights or biases (not even initializing with zeros).

Parameters:
  • cs :: [inout] ANN control structure

  • num_layers :: num_layers [in] The number of layers, including the input and output layer

  • layer_sizes :: layer_sizes [in] The number of neurons in each layer

Call to:

mom_error_handler::mom_error

Called from:

ann_init ann_random ann_unit_tests

[source]

subroutine mom_ann/ann_test(CS, NNfile)

Test ANN by comparing the prediction with the test data.

Parameters:
  • cs :: [inout] ANN control structure.

  • nnfile :: [in] The name of NetCDF file having neural network parameters

Call to:

ann_apply_vector_oi mom_error_handler::mom_error

Called from:

ann_init

[source]

subroutine mom_ann/ann_end(CS)

Deallocates memory of ANN.

Parameters:

cs :: [inout] ANN control structure.

Called from:

ann_unit_tests mom_zanna_bolton::zb2020_end

[source]

function  mom_ann/activation_fn(x)

The default activation function.

Parameters:

x :: x [in] Scalar input value [nondim]

Return:

undefined :: Scalar output value [nondim]

Called from:

layer_apply_oi layer_apply_orig layer_apply_sio

[source]

subroutine mom_ann/ann_apply_vector_orig(x, y, CS)

Single application of ANN inference using vector input and output.

This implementation is the simplest using allocation and de-allocation of temporary arrays

Parameters:
  • cs :: [in] ANN instance

  • x :: x [in] Inputs [arbitrary]

  • y :: y [inout] Outputs [arbitrary]

Call to:

layer_apply_orig

Called from:

ann_unit_tests

[source]

subroutine mom_ann/ann_apply_vector_oi(x, y, CS)

Single application of ANN inference using vector input and output.

This implementation avoids repeated reallocation of work arrays and uses the output index for the fastest (inner-most) loop in the layer matrix multiply.

Parameters:
  • cs :: [in] ANN instance

  • x :: x [in] Inputs [arbitrary]

  • y :: y [inout] Outputs [arbitrary]

Called from:

ann_test ann_unit_tests

[source]

subroutine mom_ann/ann_apply_array_sio(nij, x, y, CS)

Single application of ANN inference using array input and output with (space,feature) indexing.

This implementation uses the space index for the fastest (inner-most) loop in the layer matrix multiply, with the input index as the next fastest loop, and uses the weights matrix A(output,index). It also applies the activation function within the outer loop of the matrix multiply.

Parameters:
  • cs :: [in] ANN control structure

  • nij :: nij [in] Size of spatial dimension

  • x :: x [in] input [arbitrary]

  • y :: y [inout] output [arbitrary]

Called from:

ann_unit_tests mom_zanna_bolton::compute_stress_ann_collocated

[source]

subroutine mom_ann/set_layer(ANN, layer, weights, biases, activation)

Sets weights and bias for a single layer.

Parameters:
  • ann :: [inout] ANN control structure

  • layer :: layer [in] The number of the layer being adjusted

  • weights :: weights [in] The weights to assign

  • biases :: biases [in] The biases to assign

  • activation :: activation [in] Turn on the activation function

Call to:

mom_error_handler::mom_error

Called from:

ann_unit_tests randomize_layer

[source]

subroutine mom_ann/set_input_normalization(ANN, means, norms)

Sets input normalization.

Parameters:
  • ann :: [inout] ANN control structure

  • means :: means [in] The mean of each input

  • norms :: norms [in] The standard deviation of each input

Call to:

mom_error_handler::mom_error

Called from:

ann_unit_tests

[source]

subroutine mom_ann/set_output_normalization(ANN, means, norms)

Sets output normalization.

Parameters:
  • ann :: [inout] ANN control structure

  • means :: means [in] The mean of each output

  • norms :: norms [in] The standard deviation of each output

Call to:

mom_error_handler::mom_error

Called from:

ann_unit_tests

[source]

subroutine mom_ann/ann_random(ANN, nlayers, widths)

Create a random ANN.

Parameters:
  • ann :: [inout] ANN control structure

  • nlayers :: nlayers [in] Number of layers

  • widths :: widths [in] Width of each layer

Call to:

ann_allocate randomize_layer

Called from:

ann_unit_tests

[source]

subroutine mom_ann/randomize_layer(ANN, nlayers, layer, widths)

Fill a layer with random numbers.

Parameters:
  • ann :: [inout] ANN control structure

  • nlayers :: nlayers [in] Number of layers

  • layer :: layer [in] Layer number to randomize

  • widths :: widths [in] Width of each layer

Call to:

set_layer

Called from:

ann_random

[source]

function  mom_ann/ann_unit_tests(verbose)

Runs unit tests on ANN functions.

Should only be called from a single/root thread. Returns True if a test fails, otherwise False.

Parameters:

verbose :: verbose [in] If true, write results to stdout

Call to:

ann_allocate ann_apply_array_sio ann_apply_vector_oi ann_apply_vector_orig ann_end ann_random set_input_normalization set_layer set_output_normalization

[source]

[source]