|
1 | 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 |
| 2 | + !! This modules provides a 1-d convolutional `locally_connected_1d` 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 | + |
| 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 x window |
| 21 | + real, allocatable :: output(:,:) ! filters x output_width * output_height |
| 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 locally_connected_1d_layer |
| 41 | + |
| 42 | + interface locally_connected_1d_layer |
| 43 | + module function locally_connected_1d_layer_cons(filters, kernel_size, activation) & |
| 44 | + result(res) |
| 45 | + !! `locally_connected_1d_layer` constructor function |
| 46 | + integer, intent(in) :: filters |
| 47 | + integer, intent(in) :: kernel_size |
| 48 | + class(activation_function), intent(in) :: activation |
| 49 | + type(locally_connected_1d_layer) :: res |
| 50 | + end function locally_connected_1d_layer_cons |
| 51 | + end interface locally_connected_1d_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(locally_connected_1d_layer), intent(in out) :: self |
| 60 | + !! A `locally_connected_1d_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 `locally_connected_1d` layer. |
| 67 | + class(locally_connected_1d_layer), intent(in out) :: self |
| 68 | + !! A `locally_connected_1d_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 `locally_connected_1d` layer. |
| 75 | + class(locally_connected_1d_layer), intent(in out) :: self |
| 76 | + !! A `locally_connected_1d_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(locally_connected_1d_layer), intent(in) :: self |
| 86 | + !! A `locally_connected_1d_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(locally_connected_1d_layer), intent(in), target :: self |
| 95 | + !! A `locally_connected_1d_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(locally_connected_1d_layer), intent(in), target :: self |
| 104 | + !! A `locally_connected_1d_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(locally_connected_1d_layer), intent(in out) :: self |
| 112 | + !! A `locally_connected_1d_layer` instance |
| 113 | + real, intent(in) :: params(:) |
| 114 | + !! Parameters to set |
| 115 | + end subroutine set_params |
| 116 | + |
| 117 | + end interface |
111 | 118 |
|
112 | 119 | end module nf_locally_connected_1d_layer |
0 commit comments