|
| 1 | +module nf_conv1d_layer |
| 2 | + !! This modules provides a 1-d convolutional `conv1d` type. |
| 3 | + |
| 4 | + use nf_activation, only: activation_function |
| 5 | + use nf_base_layer, only: base_layer |
| 6 | + implicit none |
| 7 | + |
| 8 | + private |
| 9 | + public :: conv1d_layer |
| 10 | + |
| 11 | + type, extends(base_layer) :: conv1d_layer |
| 12 | + |
| 13 | + integer :: width |
| 14 | + integer :: height |
| 15 | + integer :: channels |
| 16 | + integer :: kernel_size |
| 17 | + integer :: filters |
| 18 | + |
| 19 | + real, allocatable :: biases(:) ! size(filters) |
| 20 | + real, allocatable :: kernel(:,:,:) ! filters x channels x window |
| 21 | + real, allocatable :: output(:,:) ! filters x output_width |
| 22 | + real, allocatable :: z(:,:) ! kernel .dot. input + bias |
| 23 | + |
| 24 | + real, allocatable :: dw(:,:,:) ! weight (kernel) gradients |
| 25 | + real, allocatable :: db(:) ! bias gradients |
| 26 | + real, allocatable :: gradient(:,:) |
| 27 | + |
| 28 | + class(activation_function), allocatable :: activation |
| 29 | + |
| 30 | + contains |
| 31 | + |
| 32 | + procedure :: forward |
| 33 | + procedure :: backward |
| 34 | + procedure :: get_gradients |
| 35 | + procedure :: get_num_params |
| 36 | + procedure :: get_params |
| 37 | + procedure :: init |
| 38 | + procedure :: set_params |
| 39 | + |
| 40 | + end type conv1d_layer |
| 41 | + |
| 42 | + interface conv1d_layer |
| 43 | + module function conv1d_layer_cons(filters, kernel_size, activation) & |
| 44 | + result(res) |
| 45 | + !! `conv1d_layer` constructor function |
| 46 | + integer, intent(in) :: filters |
| 47 | + integer, intent(in) :: kernel_size |
| 48 | + class(activation_function), intent(in) :: activation |
| 49 | + type(conv1d_layer) :: res |
| 50 | + end function conv1d_layer_cons |
| 51 | + end interface conv1d_layer |
| 52 | + |
| 53 | + interface |
| 54 | + |
| 55 | + module subroutine init(self, input_shape) |
| 56 | + !! Initialize the layer data structures. |
| 57 | + !! |
| 58 | + !! This is a deferred procedure from the `base_layer` abstract type. |
| 59 | + class(conv1d_layer), intent(in out) :: self |
| 60 | + !! A `conv1d_layer` instance |
| 61 | + integer, intent(in) :: input_shape(:) |
| 62 | + !! Input layer dimensions |
| 63 | + end subroutine init |
| 64 | + |
| 65 | + pure module subroutine forward(self, input) |
| 66 | + !! Apply a forward pass on the `conv1d` layer. |
| 67 | + class(conv1d_layer), intent(in out) :: self |
| 68 | + !! A `conv1d_layer` instance |
| 69 | + real, intent(in) :: input(:,:) |
| 70 | + !! Input data |
| 71 | + end subroutine forward |
| 72 | + |
| 73 | + pure module subroutine backward(self, input, gradient) |
| 74 | + !! Apply a backward pass on the `conv1d` layer. |
| 75 | + class(conv1d_layer), intent(in out) :: self |
| 76 | + !! A `conv1d_layer` instance |
| 77 | + real, intent(in) :: input(:,:) |
| 78 | + !! Input data (previous layer) |
| 79 | + real, intent(in) :: gradient(:,:) |
| 80 | + !! Gradient (next layer) |
| 81 | + end subroutine backward |
| 82 | + |
| 83 | + pure module function get_num_params(self) result(num_params) |
| 84 | + !! Get the number of parameters in the layer. |
| 85 | + class(conv1d_layer), intent(in) :: self |
| 86 | + !! A `conv1d_layer` instance |
| 87 | + integer :: num_params |
| 88 | + !! Number of parameters |
| 89 | + end function get_num_params |
| 90 | + |
| 91 | + module function get_params(self) result(params) |
| 92 | + !! Return the parameters (weights and biases) of this layer. |
| 93 | + !! The parameters are ordered as weights first, biases second. |
| 94 | + class(conv1d_layer), intent(in), target :: self |
| 95 | + !! A `conv1d_layer` instance |
| 96 | + real, allocatable :: params(:) |
| 97 | + !! Parameters to get |
| 98 | + end function get_params |
| 99 | + |
| 100 | + module function get_gradients(self) result(gradients) |
| 101 | + !! Return the gradients of this layer. |
| 102 | + !! The gradients are ordered as weights first, biases second. |
| 103 | + class(conv1d_layer), intent(in), target :: self |
| 104 | + !! A `conv1d_layer` instance |
| 105 | + real, allocatable :: gradients(:) |
| 106 | + !! Gradients to get |
| 107 | + end function get_gradients |
| 108 | + |
| 109 | + module subroutine set_params(self, params) |
| 110 | + !! Set the parameters of the layer. |
| 111 | + class(conv1d_layer), intent(in out) :: self |
| 112 | + !! A `conv1d_layer` instance |
| 113 | + real, intent(in) :: params(:) |
| 114 | + !! Parameters to set |
| 115 | + end subroutine set_params |
| 116 | + |
| 117 | + end interface |
| 118 | + |
| 119 | +end module nf_conv1d_layer |
0 commit comments