|
| 1 | +module nf_rnn_layer |
| 2 | + |
| 3 | + !! This module provides the concrete dense layer type. |
| 4 | + !! It is used internally by the layer type. |
| 5 | + !! It is not intended to be used directly by the user. |
| 6 | + |
| 7 | + use nf_activation, only: activation_function |
| 8 | + use nf_base_layer, only: base_layer |
| 9 | + |
| 10 | + implicit none |
| 11 | + |
| 12 | + private |
| 13 | + public :: rnn_layer |
| 14 | + |
| 15 | + type, extends(base_layer) :: rnn_layer |
| 16 | + |
| 17 | + !! Concrete implementation of a dense (fully-connected) layer type |
| 18 | + |
| 19 | + integer :: input_size |
| 20 | + integer :: output_size |
| 21 | + |
| 22 | + real, allocatable :: weights(:,:) |
| 23 | + real, allocatable :: recurrent(:,:) |
| 24 | + real, allocatable :: biases(:) |
| 25 | + real, allocatable :: state(:) |
| 26 | + real, allocatable :: z(:) ! matmul(x, w) + b |
| 27 | + real, allocatable :: output(:) ! activation(z) |
| 28 | + real, allocatable :: gradient(:) ! matmul(w, db) |
| 29 | + real, allocatable :: dw(:,:) ! weight gradients |
| 30 | + real, allocatable :: db(:) ! bias gradients |
| 31 | + |
| 32 | + class(activation_function), allocatable :: activation |
| 33 | + |
| 34 | + contains |
| 35 | + |
| 36 | + !procedure :: backward |
| 37 | + !procedure :: forward |
| 38 | + !procedure :: get_gradients |
| 39 | + procedure :: get_num_params |
| 40 | + !procedure :: get_params |
| 41 | + procedure :: init |
| 42 | + !procedure :: set_params |
| 43 | + |
| 44 | + end type rnn_layer |
| 45 | + |
| 46 | + interface rnn_layer |
| 47 | + elemental module function rnn_layer_cons(output_size, activation) & |
| 48 | + result(res) |
| 49 | + !! This function returns the `dense_layer` instance. |
| 50 | + integer, intent(in) :: output_size |
| 51 | + !! Number of neurons in this layer |
| 52 | + class(activation_function), intent(in) :: activation |
| 53 | + !! Instance of the activation_function to use; |
| 54 | + !! See nf_activation.f90 for available functions. |
| 55 | + type(rnn_layer) :: res |
| 56 | + !! dense_layer instance |
| 57 | + end function rnn_layer_cons |
| 58 | + end interface rnn_layer |
| 59 | + |
| 60 | + interface |
| 61 | + |
| 62 | + pure module subroutine backward(self, input, gradient) |
| 63 | + !! Apply the backward gradient descent pass. |
| 64 | + !! Only weight and bias gradients are updated in this subroutine, |
| 65 | + !! while the weights and biases themselves are untouched. |
| 66 | + class(rnn_layer), intent(in out) :: self |
| 67 | + !! Dense layer instance |
| 68 | + real, intent(in) :: input(:) |
| 69 | + !! Input from the previous layer |
| 70 | + real, intent(in) :: gradient(:) |
| 71 | + !! Gradient from the next layer |
| 72 | + end subroutine backward |
| 73 | + |
| 74 | + pure module subroutine forward(self, input) |
| 75 | + !! Propagate forward the layer. |
| 76 | + !! Calling this subroutine updates the values of a few data components |
| 77 | + !! of `dense_layer` that are needed for the backward pass. |
| 78 | + class(rnn_layer), intent(in out) :: self |
| 79 | + !! Dense layer instance |
| 80 | + real, intent(in) :: input(:) |
| 81 | + !! Input from the previous layer |
| 82 | + end subroutine forward |
| 83 | + |
| 84 | + pure module function get_num_params(self) result(num_params) |
| 85 | + !! Return the number of parameters in this layer. |
| 86 | + class(rnn_layer), intent(in) :: self |
| 87 | + !! Dense layer instance |
| 88 | + integer :: num_params |
| 89 | + !! Number of parameters in this layer |
| 90 | + end function get_num_params |
| 91 | + |
| 92 | + pure module function get_params(self) result(params) |
| 93 | + !! Return the parameters (weights and biases) of this layer. |
| 94 | + !! The parameters are ordered as weights first, biases second. |
| 95 | + class(rnn_layer), intent(in) :: self |
| 96 | + !! Dense layer instance |
| 97 | + real, allocatable :: params(:) |
| 98 | + !! Parameters of this layer |
| 99 | + end function get_params |
| 100 | + |
| 101 | + pure module function get_gradients(self) result(gradients) |
| 102 | + !! Return the gradients of this layer. |
| 103 | + !! The gradients are ordered as weights first, biases second. |
| 104 | + class(rnn_layer), intent(in) :: self |
| 105 | + !! Dense layer instance |
| 106 | + real, allocatable :: gradients(:) |
| 107 | + !! Gradients of this layer |
| 108 | + end function get_gradients |
| 109 | + |
| 110 | + module subroutine set_params(self, params) |
| 111 | + !! Set the parameters of this layer. |
| 112 | + !! The parameters are ordered as weights first, biases second. |
| 113 | + class(rnn_layer), intent(in out) :: self |
| 114 | + !! Dense layer instance |
| 115 | + real, intent(in) :: params(:) |
| 116 | + !! Parameters of this layer |
| 117 | + end subroutine set_params |
| 118 | + |
| 119 | + module subroutine init(self, input_shape) |
| 120 | + !! Initialize the layer data structures. |
| 121 | + !! |
| 122 | + !! This is a deferred procedure from the `base_layer` abstract type. |
| 123 | + class(rnn_layer), intent(in out) :: self |
| 124 | + !! Dense layer instance |
| 125 | + integer, intent(in) :: input_shape(:) |
| 126 | + !! Shape of the input layer |
| 127 | + end subroutine init |
| 128 | + |
| 129 | + end interface |
| 130 | + |
| 131 | +end module nf_rnn_layer |
0 commit comments