MOM_ANN module reference
Implements the general purpose Artificial Neural Network (ANN).
Data Types
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. |
|
Control structure/type for ANN |
Generic Interfaces
Applies ANN to x, returning results in y |
Functions/Subroutines
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. |
|
Allocate an ANN |
|
Test ANN by comparing the prediction with the test data. |
|
Deallocates memory of ANN |
|
The default activation function |
|
Single application of ANN inference using vector input and output |
|
Single application of ANN inference using vector input and output |
|
Single application of ANN inference using array input and output with (space,feature) indexing |
|
Sets weights and bias for a single layer |
|
Sets input normalization |
|
Sets output normalization |
|
Create a random ANN |
|
Fill a layer with random numbers |
|
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 mom_database_comms) 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
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.
Fig: A three layer network with 3 inputs, 2 outputs, and 1 hidden layer. There are two rectanglar matrices of weights (black arrows). The bias for each neuron is implied."
Type Documentation
- type, private :: 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.
- Parameters
output_width
Number of rows in matrix A
input_width
Number of columns in matrix A
activation
If true, apply the default activation function
A
Matrix in column-major order of size A(output_width, input_width) [nondim]
b
bias vector of size output_width [nondim]
- type, public :: ANN_CS
Control structure/type for ANN
- Parameters
num_layers
Number of layers in the ANN, including the input and output. For example, for ANN with one hidden layer, num_layers = 3.
layer_sizes
Array of length num_layers, storing the number of neurons in each layer.
layers
Array of length num_layers-1, where each element is the Linear transformation between layers defined by Matrix A and vias b.
input_means
Array of length layer_sizes(1) containing the mean of each input feature prior to normalization by input_norms [arbitrary].
input_norms
Array of length layer_sizes(1) containing the inverse of the standard deviation for each input feature used to normalize (multiply) before feeding into the ANN [arbitrary]
output_means
Array of length layer_sizes(num_layers) containing the mean of each output prior to normalization by output_norms [arbitrary].
output_norms
Array of length layer_sizes(num_layers) containing the standard deviation each output of the ANN will be multiplied [arbitrary]
parameters
Count of number of parameters
Generic Interface Documentation
- interface ANN_apply
Applies ANN to x, returning results in y
- Implementations
Functions/Subroutine Documentation
- subroutine 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
Callees call graph
Callers call graph
- subroutine 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 [in]
The number of layers, including the input and output layer
layer_sizes [in]
The number of neurons in each layer
Callers call graph
- subroutine 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
Callees call graph
- subroutine ANN_end(CS)
Deallocates memory of ANN
- Parameters
CS [inout]
ANN control structure.
Callers call graph
- pure elemental function activation_fn(x) result(y)
The default activation function
- Parameters
x [in]
Scalar input value [nondim]
y
Scalar output value [nondim]
- subroutine 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 [in]
Inputs [arbitrary]
y [inout]
Outputs [arbitrary]
Callees call graph
- subroutine layer_apply_orig(x, y, layer)
Applies linear layer to input data x and stores the result in y with y = A*x + b with optional application of the activation function so the overall operations is ReLU(A*x + b)
- Parameters
layer [in]
Linear layer
x [in]
Input vector [nondim]
y [inout]
Output vector [nondim]
Callees call graph
Callers call graph
- subroutine 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 [in]
Inputs [arbitrary]
y [inout]
Outputs [arbitrary]
Callees call graph
Callers call graph
- subroutine layer_apply_oi(x, y, layer)
Applies linear layer to input data x and stores the result in y with y = A*x + b with optional application of the activation function so the overall operations is ReLU(A*x + b)
- Parameters
layer [in]
Linear layer
x [in]
Input vector [nondim]
y [inout]
Output vector [nondim]
Callers call graph
- subroutine 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 [in]
Size of spatial dimension
x [in]
input [arbitrary]
y [inout]
output [arbitrary]
Callees call graph
Callers call graph
- subroutine layer_apply_sio(nij, x, y, layer)
Applies linear layer to input data x and stores the result in y with y = A*x + b with optional application of the activation function so the overall operations is ReLU(A*x + b)
- Parameters
layer [in]
Linear layer
nij [in]
Size of spatial dimension
x [in]
Input vector [nondim]
y [inout]
Output vector [nondim]
Callers call graph
- subroutine set_layer(ANN, layer, weights, biases, activation)
Sets weights and bias for a single layer
- Parameters
ANN [inout]
ANN control structure
layer [in]
The number of the layer being adjusted
weights [in]
The weights to assign
biases [in]
The biases to assign
activation [in]
Turn on the activation function
Callers call graph
- subroutine set_input_normalization(ANN, means, norms)
Sets input normalization
- Parameters
ANN [inout]
ANN control structure
means [in]
The mean of each input
norms [in]
The standard deviation of each input
- subroutine set_output_normalization(ANN, means, norms)
Sets output normalization
- Parameters
ANN [inout]
ANN control structure
means [in]
The mean of each output
norms [in]
The standard deviation of each output
- subroutine ANN_random(ANN, nlayers, widths)
Create a random ANN
- Parameters
ANN [inout]
ANN control structure
nlayers [in]
Number of layers
widths [in]
Width of each layer
Callees call graph
- subroutine randomize_layer(ANN, nlayers, layer, widths)
Fill a layer with random numbers
- Parameters
ANN [inout]
ANN control structure
nlayers [in]
Number of layers
layer [in]
Layer number to randomize
widths [in]
Width of each layer
- logical function 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 [in]
If true, write results to stdout