mom_ann module reference
Implements the general purpose Artificial Neural Network (ANN).
Data Types
Type for a single Linear layer of ANN, i.e. |
|
Control structure/type for ANN. |
Functions/Subroutines
Initialization of ANN. |
|
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 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
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.
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, privateNumber of rows in matrix A.% input_width ::
integer, privateNumber of columns in matrix A.% activation ::
logical, privateIf true, apply the default activation function.% a ::
real, dimension(:,:), allocatable, privateMatrix in column-major order of size A(output_width, input_width) [nondim].% b ::
real, dimension(:), allocatable, privatebias vector of size output_width [nondim]
- type mom_ann/ann_cs
Control structure/type for ANN.
- Type fields:
% num_layers ::
integerNumber 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(:), allocatableArray of length num_layers, storing the number of neurons in.% layers ::
type(layer_type), dimension(:), allocatableArray of length num_layers-1, where each element is the Linear.% input_means ::
real, dimension(:), allocatableArray of length layer_sizes(1) containing the mean of each input feature.% input_norms ::
real, dimension(:), allocatableArray of length layer_sizes(1) containing the inverse of the standard.% output_means ::
real, dimension(:), allocatableArray of length layer_sizes(num_layers) containing the mean of each.% output_norms ::
real, dimension(:), allocatableArray of length layer_sizes(num_layers) containing the standard deviation.% parameters ::
integer, publicCount of number of parameters.
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:
- Called from:
- 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 layerlayer_sizes ::
layer_sizes[in] The number of neurons in each layer
- Call to:
- Called from:
- 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:
- Called from:
- subroutine mom_ann/ann_end(CS)
Deallocates memory of ANN.
- Parameters:
cs :: [inout] ANN control structure.
- Called from:
- 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_oilayer_apply_origlayer_apply_sio
- 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:
- 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:
- 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 dimensionx ::
x[in] input [arbitrary]y ::
y[inout] output [arbitrary]
- Called from:
ann_unit_testsmom_zanna_bolton::compute_stress_ann_collocated
- 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 adjustedweights ::
weights[in] The weights to assignbiases ::
biases[in] The biases to assignactivation ::
activation[in] Turn on the activation function
- Call to:
- Called from:
- 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 inputnorms ::
norms[in] The standard deviation of each input
- Call to:
- Called from:
- 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 outputnorms ::
norms[in] The standard deviation of each output
- Call to:
- Called from:
- subroutine mom_ann/ann_random(ANN, nlayers, widths)
Create a random ANN.
- Parameters:
ann :: [inout] ANN control structure
nlayers ::
nlayers[in] Number of layerswidths ::
widths[in] Width of each layer
- Call to:
- Called from:
- 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 layerslayer ::
layer[in] Layer number to randomizewidths ::
widths[in] Width of each layer
- Call to:
- Called from:
- 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_allocateann_apply_array_sioann_apply_vector_oiann_apply_vector_origann_endann_randomset_input_normalizationset_layerset_output_normalization