network_type Derived Type

type, public :: network_type


Inherits

type~~network_type~~InheritsGraph type~network_type network_type type~layer_type layer_type type~network_type->type~layer_type layers

Contents

Source Code


Components

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

Constructor

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)


Type-Bound Procedures

procedure, public, pass(self) :: accuracy

  • 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)

procedure, public, pass(self) :: backprop

  • 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(:)

procedure, public, pass(self) :: fwdprop

  • 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(:)

procedure, public, pass(self) :: init

  • 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(:)

procedure, public, pass(self) :: load

  • 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

procedure, public, pass(self) :: loss

  • 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)

generic, public :: output => output_batch, output_single

  • 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, (:)

procedure, public, pass(self) :: output_batch

  • 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, (:,:)

procedure, public, pass(self) :: output_single

  • 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, (:)

procedure, public, pass(self) :: save

  • 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

generic, public :: set_activation => set_activation_equal, set_activation_layers

  • 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))

procedure, public, pass(self) :: set_activation_equal

  • 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

procedure, public, pass(self) :: set_activation_layers

  • 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))

procedure, public, pass(self) :: sync

  • 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

generic, public :: train => train_batch, train_epochs, train_single

  • 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

procedure, public, pass(self) :: train_batch

  • 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

procedure, public, pass(self) :: train_epochs

  • 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

procedure, public, pass(self) :: train_single

  • 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

procedure, public, pass(self) :: update

  • 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

Source Code

  type :: network_type

    type(layer_type), allocatable :: layers(:)
    integer(ik), allocatable :: dims(:)

  contains

    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
    procedure, public, pass(self) :: output_batch
    procedure, public, pass(self) :: output_single
    procedure, public, pass(self) :: save
    procedure, public, pass(self) :: set_activation_equal
    procedure, public, pass(self) :: set_activation_layers
    procedure, public, pass(self) :: sync
    procedure, public, pass(self) :: train_batch
    procedure, public, pass(self) :: train_epochs
    procedure, public, pass(self) :: train_single
    procedure, public, pass(self) :: update

    generic, public :: output => output_batch, output_single
    generic, public :: set_activation => set_activation_equal, set_activation_layers
    generic, public :: train => train_batch, train_epochs, train_single

  end type network_type