mod_network Module


Uses

  • module~~mod_network~~UsesGraph module~mod_network mod_network module~mod_layer mod_layer module~mod_network->module~mod_layer module~mod_parallel mod_parallel module~mod_network->module~mod_parallel module~mod_kinds mod_kinds module~mod_network->module~mod_kinds module~mod_layer->module~mod_kinds module~mod_random mod_random module~mod_layer->module~mod_random module~mod_activation mod_activation module~mod_layer->module~mod_activation module~mod_parallel->module~mod_kinds iso_fortran_env iso_fortran_env module~mod_kinds->iso_fortran_env module~mod_random->module~mod_kinds module~mod_activation->module~mod_kinds

Contents


Interfaces

public interface network_type

  • private function net_constructor(dims, activation) result(net)

    Network class constructor. Size of input array dims indicates the total number of layers (input + hidden + output), and the value of its elements corresponds the size of each layer.

    Arguments

    Type IntentOptional AttributesName
    integer(kind=ik), intent(in) :: dims(:)
    character(len=*), intent(in), optional :: activation

    Return Value type(network_type)


Derived Types

type, public :: network_type

Components

TypeVisibility AttributesNameInitial
integer(kind=ik), public, allocatable:: dims(:)
type(layer_type), public, allocatable:: layers(:)

Constructor

private function net_constructor(dims, activation)

Network class constructor. Size of input array dims indicates the total number of layers (input + hidden + output), and the value of its elements corresponds the size of each layer.

Type-Bound Procedures

procedure, public, pass(self) :: accuracy
procedure, public, pass(self) :: backprop
procedure, public, pass(self) :: fwdprop
procedure, public, pass(self) :: init
procedure, public, pass(self) :: load
procedure, public, pass(self) :: loss
generic, public :: output => output_batch, output_single
procedure, public, pass(self) :: output_batch
procedure, public, pass(self) :: output_single
procedure, public, pass(self) :: save
generic, public :: set_activation => set_activation_equal, set_activation_layers
procedure, public, pass(self) :: set_activation_equal
procedure, public, pass(self) :: set_activation_layers
procedure, public, pass(self) :: sync
generic, public :: train => train_batch, train_epochs, train_single
procedure, public, pass(self) :: train_batch
procedure, public, pass(self) :: train_epochs
procedure, public, pass(self) :: train_single
procedure, public, pass(self) :: update

Functions

private pure function accuracy(self, x, y)

Given input x and output y, evaluates the position of the maximum value of the output and returns the number of matches relative to the size of the dataset.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(in) :: self
real(kind=rk), intent(in) :: x(:,:)
real(kind=rk), intent(in) :: y(:,:)

Return Value real(kind=rk)

private pure function loss(self, x, y)

Given input x and expected output y, returns the loss of the network.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(in) :: self
real(kind=rk), intent(in) :: x(:)
real(kind=rk), intent(in) :: y(:)

Return Value real(kind=rk)

private function net_constructor(dims, activation) result(net)

Network class constructor. Size of input array dims indicates the total number of layers (input + hidden + output), and the value of its elements corresponds the size of each layer.

Arguments

Type IntentOptional AttributesName
integer(kind=ik), intent(in) :: dims(:)
character(len=*), intent(in), optional :: activation

Return Value type(network_type)

private pure function output_batch(self, x) result(a)

Use forward propagation to compute the output of the network. This specific procedure is for a batch of 1-d input data.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(in) :: self
real(kind=rk), intent(in) :: x(:,:)

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

private pure function output_single(self, x) result(a)

Use forward propagation to compute the output of the network. This specific procedure is for a single sample of 1-d input data.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(in) :: self
real(kind=rk), intent(in) :: x(:)

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


Subroutines

private pure subroutine backprop(self, y, dw, db)

Applies a backward propagation through the network and returns the weight and bias gradients.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
real(kind=rk), intent(in) :: y(:)
type(array2d), intent(out), allocatable:: dw(:)
type(array1d), intent(out), allocatable:: db(:)

private pure subroutine fwdprop(self, x)

Performs the forward propagation and stores arguments to activation functions and activations themselves for use in backprop.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
real(kind=rk), intent(in) :: x(:)

private subroutine init(self, dims)

Allocates and initializes the layers with given dimensions dims.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
integer(kind=ik), intent(in) :: dims(:)

private subroutine load(self, filename)

Loads the network from file.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
character(len=*), intent(in) :: filename

private subroutine save(self, filename)

Saves the network to a file.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
character(len=*), intent(in) :: filename

private pure subroutine set_activation_equal(self, activation)

A thin wrapper around layer % set_activation(). This method can be used to set an activation function for all layers at once.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
character(len=*), intent(in) :: activation

private pure subroutine set_activation_layers(self, activation)

A thin wrapper around layer % set_activation(). This method can be used to set different activation functions for each layer separately.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
character(len=*), intent(in) :: activation(size(self%layers))

private subroutine sync(self, image)

Broadcasts network weights and biases from specified image to all others.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
integer(kind=ik), intent(in) :: image

private subroutine train_batch(self, x, y, eta)

Trains a network using input data x and output data y, and learning rate eta. The learning rate is normalized with the size of the data batch. mini-batch size number of layers

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
real(kind=rk), intent(in) :: x(:,:)
real(kind=rk), intent(in) :: y(:,:)
real(kind=rk), intent(in) :: eta

private subroutine train_epochs(self, x, y, eta, num_epochs, batch_size)

Trains for num_epochs epochs with mini-bachtes of size equal to batch_size.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
real(kind=rk), intent(in) :: x(:,:)
real(kind=rk), intent(in) :: y(:,:)
real(kind=rk), intent(in) :: eta
integer(kind=ik), intent(in) :: num_epochs
integer(kind=ik), intent(in) :: batch_size

private pure subroutine train_single(self, x, y, eta)

Trains a network using a single set of input data x and output data y, and learning rate eta.

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
real(kind=rk), intent(in) :: x(:)
real(kind=rk), intent(in) :: y(:)
real(kind=rk), intent(in) :: eta

private pure subroutine update(self, dw, db, eta)

Updates network weights and biases with gradients dw and db, scaled by learning rate eta. update biases update weights

Arguments

Type IntentOptional AttributesName
class(network_type), intent(inout) :: self
class(array2d), intent(in) :: dw(:)
class(array1d), intent(in) :: db(:)
real(kind=rk), intent(in) :: eta