|
| 1 | +module nf_locally_connected_1d_layer |
| 2 | + !! This module provides a locally connected 1d layer 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 :: locally_connected_1d_layer |
| 10 | + |
| 11 | + type, extends(base_layer) :: locally_connected_1d_layer |
| 12 | + ! For a 1D layer, we assume an input shape of [channels, input_length] |
| 13 | + integer :: channels ! number of input channels |
| 14 | + integer :: input_length ! length of the 1D input |
| 15 | + integer :: output_length ! computed as input_length - kernel_size + 1 |
| 16 | + integer :: kernel_size ! size of the 1D window |
| 17 | + integer :: filters ! number of filters (output channels) |
| 18 | + |
| 19 | + ! Parameters (unshared weights) |
| 20 | + ! Kernel shape: (filters, output_length, channels, kernel_size) |
| 21 | + real, allocatable :: kernel(:,:,:,:) |
| 22 | + ! Biases shape: (filters, output_length) |
| 23 | + real, allocatable :: biases(:,:) |
| 24 | + |
| 25 | + ! Forward-pass arrays |
| 26 | + ! Pre-activation values: shape (filters, output_length) |
| 27 | + real, allocatable :: z(:,:) |
| 28 | + ! Activated output: shape (filters, output_length) |
| 29 | + real, allocatable :: output(:,:) |
| 30 | + |
| 31 | + ! Gradients for backpropagation |
| 32 | + ! Gradient for kernel, same shape as kernel |
| 33 | + real, allocatable :: dw(:,:,:,:) |
| 34 | + ! Gradient for biases, same shape as biases |
| 35 | + real, allocatable :: db(:,:) |
| 36 | + ! Gradient with respect to the input, shape (channels, input_length) |
| 37 | + real, allocatable :: gradient(:,:) |
| 38 | + |
| 39 | + ! Activation function |
| 40 | + class(activation_function), allocatable :: activation |
| 41 | + contains |
| 42 | + procedure :: forward |
| 43 | + procedure :: backward |
| 44 | + procedure :: get_gradients |
| 45 | + procedure :: get_num_params |
| 46 | + procedure :: get_params |
| 47 | + procedure :: init |
| 48 | + procedure :: set_params |
| 49 | + end type locally_connected_1d_layer |
| 50 | + |
| 51 | + interface locally_connected_1d_layer |
| 52 | + module function locally_connected_1d_layer_cons(filters, kernel_size, activation) result(res) |
| 53 | + !! Constructor for the locally connected 1d layer. |
| 54 | + integer, intent(in) :: filters |
| 55 | + integer, intent(in) :: kernel_size |
| 56 | + class(activation_function), intent(in):: activation |
| 57 | + type(locally_connected_1d_layer) :: res |
| 58 | + end function locally_connected_1d_layer_cons |
| 59 | + end interface locally_connected_1d_layer |
| 60 | + |
| 61 | + interface |
| 62 | + module subroutine init(self, input_shape) |
| 63 | + !! Initialize the layer data structures. |
| 64 | + !! input_shape: integer array of length 2, where |
| 65 | + !! input_shape(1) = number of channels |
| 66 | + !! input_shape(2) = input length |
| 67 | + class(locally_connected_1d_layer), intent(inout) :: self |
| 68 | + integer, intent(in) :: input_shape(:) |
| 69 | + end subroutine init |
| 70 | + |
| 71 | + pure module subroutine forward(self, input) |
| 72 | + !! Apply the forward pass. |
| 73 | + !! Input shape: (channels, input_length) |
| 74 | + class(locally_connected_1d_layer), intent(inout) :: self |
| 75 | + real, intent(in) :: input(:,:) |
| 76 | + end subroutine forward |
| 77 | + |
| 78 | + pure module subroutine backward(self, input, gradient) |
| 79 | + !! Apply the backward pass. |
| 80 | + !! input: shape (channels, input_length) |
| 81 | + !! gradient: gradient w.r.t. output, shape (filters, output_length) |
| 82 | + class(locally_connected_1d_layer), intent(inout) :: self |
| 83 | + real, intent(in) :: input(:,:) |
| 84 | + real, intent(in) :: gradient(:,:) |
| 85 | + end subroutine backward |
| 86 | + |
| 87 | + pure module function get_num_params(self) result(num_params) |
| 88 | + !! Get the total number of parameters (kernel + biases) |
| 89 | + class(locally_connected_1d_layer), intent(in) :: self |
| 90 | + integer :: num_params |
| 91 | + end function get_num_params |
| 92 | + |
| 93 | + module function get_params(self) result(params) |
| 94 | + !! Return a flattened vector of parameters (kernel then biases). |
| 95 | + class(locally_connected_1d_layer), intent(in), target :: self |
| 96 | + real, allocatable :: params(:) |
| 97 | + end function get_params |
| 98 | + |
| 99 | + module function get_gradients(self) result(gradients) |
| 100 | + !! Return a flattened vector of gradients (dw then db). |
| 101 | + class(locally_connected_1d_layer), intent(in), target :: self |
| 102 | + real, allocatable :: gradients(:) |
| 103 | + end function get_gradients |
| 104 | + |
| 105 | + module subroutine set_params(self, params) |
| 106 | + !! Set the parameters from a flattened vector. |
| 107 | + class(locally_connected_1d_layer), intent(inout) :: self |
| 108 | + real, intent(in) :: params(:) |
| 109 | + end subroutine set_params |
| 110 | + end interface |
| 111 | + |
| 112 | +end module nf_locally_connected_1d_layer |
0 commit comments