|
| 1 | +submodule(nf_conv1d_layer) nf_conv1d_layer_submodule |
| 2 | + |
| 3 | + use nf_activation, only: activation_function |
| 4 | + use nf_random, only: random_normal |
| 5 | + |
| 6 | + implicit none |
| 7 | + |
| 8 | +contains |
| 9 | + |
| 10 | + module function conv1d_layer_cons(filters, kernel_size, activation) result(res) |
| 11 | + implicit none |
| 12 | + integer, intent(in) :: filters |
| 13 | + integer, intent(in) :: kernel_size |
| 14 | + class(activation_function), intent(in) :: activation |
| 15 | + type(conv1d_layer) :: res |
| 16 | + |
| 17 | + res % kernel_size = kernel_size |
| 18 | + res % filters = filters |
| 19 | + res % activation_name = activation % get_name() |
| 20 | + allocate( res % activation, source = activation ) |
| 21 | + end function conv1d_layer_cons |
| 22 | + |
| 23 | + module subroutine init(self, input_shape) |
| 24 | + implicit none |
| 25 | + class(conv1d_layer), intent(in out) :: self |
| 26 | + integer, intent(in) :: input_shape(:) |
| 27 | + |
| 28 | + self % channels = input_shape(1) |
| 29 | + self % width = input_shape(2) - self % kernel_size + 1 |
| 30 | + |
| 31 | + ! Output of shape: filters x width |
| 32 | + allocate(self % output(self % filters, self % width)) |
| 33 | + self % output = 0 |
| 34 | + |
| 35 | + ! Kernel of shape: filters x channels x kernel_size |
| 36 | + allocate(self % kernel(self % filters, self % channels, self % kernel_size)) |
| 37 | + call random_normal(self % kernel) |
| 38 | + self % kernel = self % kernel / real(self % kernel_size**2) |
| 39 | + |
| 40 | + allocate(self % biases(self % filters)) |
| 41 | + self % biases = 0 |
| 42 | + |
| 43 | + allocate(self % z, mold=self % output) |
| 44 | + self % z = 0 |
| 45 | + |
| 46 | + allocate(self % gradient(input_shape(1), input_shape(2))) |
| 47 | + self % gradient = 0 |
| 48 | + |
| 49 | + allocate(self % dw, mold=self % kernel) |
| 50 | + self % dw = 0 |
| 51 | + |
| 52 | + allocate(self % db, mold=self % biases) |
| 53 | + self % db = 0 |
| 54 | + |
| 55 | + end subroutine init |
| 56 | + |
| 57 | + pure module subroutine forward(self, input) |
| 58 | + implicit none |
| 59 | + class(conv1d_layer), intent(in out) :: self |
| 60 | + real, intent(in) :: input(:,:) |
| 61 | + integer :: input_channels, input_width |
| 62 | + integer :: j, n |
| 63 | + integer :: iws, iwe, half_window |
| 64 | + |
| 65 | + input_channels = size(input, dim=1) |
| 66 | + input_width = size(input, dim=2) |
| 67 | + half_window = self % kernel_size / 2 |
| 68 | + |
| 69 | + ! Loop over output positions. |
| 70 | + do j = 1, self % width |
| 71 | + ! Compute the input window corresponding to output index j. |
| 72 | + ! In forward: center index = j + half_window, so window = indices j to j+kernel_size-1. |
| 73 | + iws = j |
| 74 | + iwe = j + self % kernel_size - 1 |
| 75 | + |
| 76 | + ! For each filter, compute the convolution (inner product over channels and kernel width). |
| 77 | + do concurrent (n = 1:self % filters) |
| 78 | + self % z(n, j) = sum(self % kernel(n, :, :) * input(:, iws:iwe)) |
| 79 | + end do |
| 80 | + |
| 81 | + ! Add the bias for each filter. |
| 82 | + self % z(:, j) = self % z(:, j) + self % biases |
| 83 | + end do |
| 84 | + |
| 85 | + ! Apply the activation function. |
| 86 | + self % output = self % activation % eval(self % z) |
| 87 | + end subroutine forward |
| 88 | + |
| 89 | + pure module subroutine backward(self, input, gradient) |
| 90 | + implicit none |
| 91 | + class(conv1d_layer), intent(in out) :: self |
| 92 | + ! 'input' has shape: (channels, input_width) |
| 93 | + ! 'gradient' (dL/dy) has shape: (filters, output_width) |
| 94 | + real, intent(in) :: input(:,:) |
| 95 | + real, intent(in) :: gradient(:,:) |
| 96 | + |
| 97 | + integer :: input_channels, input_width, output_width |
| 98 | + integer :: j, n, k |
| 99 | + integer :: iws, iwe, half_window |
| 100 | + real :: gdz_val |
| 101 | + |
| 102 | + ! Local arrays to accumulate gradients. |
| 103 | + real :: gdz(self % filters, self % width) ! local gradient (dL/dz) |
| 104 | + real :: db_local(self % filters) |
| 105 | + real :: dw_local(self % filters, self % channels, self % kernel_size) |
| 106 | + |
| 107 | + ! Determine dimensions. |
| 108 | + input_channels = size(input, dim=1) |
| 109 | + input_width = size(input, dim=2) |
| 110 | + output_width = self % width ! Note: output_width = input_width - kernel_size + 1 |
| 111 | + |
| 112 | + half_window = self % kernel_size / 2 |
| 113 | + |
| 114 | + !--- Compute the local gradient gdz = (dL/dy) * sigma'(z) for each output. |
| 115 | + do j = 1, output_width |
| 116 | + gdz(:, j) = gradient(:, j) * self % activation % eval_prime(self % z(:, j)) |
| 117 | + end do |
| 118 | + |
| 119 | + !--- Compute bias gradients: db(n) = sum_j gdz(n, j) |
| 120 | + do n = 1, self % filters |
| 121 | + db_local(n) = sum(gdz(n, :)) |
| 122 | + end do |
| 123 | + |
| 124 | + !--- Initialize weight gradient and input gradient accumulators. |
| 125 | + dw_local = 0.0 |
| 126 | + self % gradient = 0.0 |
| 127 | + |
| 128 | + !--- Accumulate gradients over each output position. |
| 129 | + ! In the forward pass the window for output index j was: |
| 130 | + ! iws = j, iwe = j + kernel_size - 1. |
| 131 | + do n = 1, self % filters |
| 132 | + do j = 1, output_width |
| 133 | + iws = j |
| 134 | + iwe = j + self % kernel_size - 1 |
| 135 | + do k = 1, self % channels |
| 136 | + ! Weight gradient: accumulate contribution from the input window. |
| 137 | + dw_local(n, k, :) = dw_local(n, k, :) + input(k, iws:iwe) * gdz(n, j) |
| 138 | + ! Input gradient: propagate gradient back to the input window. |
| 139 | + self % gradient(k, iws:iwe) = self % gradient(k, iws:iwe) + self % kernel(n, k, :) * gdz(n, j) |
| 140 | + end do |
| 141 | + end do |
| 142 | + end do |
| 143 | + |
| 144 | + !--- Update stored gradients. |
| 145 | + self % dw = self % dw + dw_local |
| 146 | + self % db = self % db + db_local |
| 147 | + |
| 148 | + end subroutine backward |
| 149 | + |
| 150 | + pure module function get_num_params(self) result(num_params) |
| 151 | + class(conv1d_layer), intent(in) :: self |
| 152 | + integer :: num_params |
| 153 | + num_params = product(shape(self % kernel)) + size(self % biases) |
| 154 | + end function get_num_params |
| 155 | + |
| 156 | + module function get_params(self) result(params) |
| 157 | + class(conv1d_layer), intent(in), target :: self |
| 158 | + real, allocatable :: params(:) |
| 159 | + real, pointer :: w_(:) => null() |
| 160 | + w_(1:size(self % kernel)) => self % kernel |
| 161 | + params = [ w_, self % biases ] |
| 162 | + end function get_params |
| 163 | + |
| 164 | + module function get_gradients(self) result(gradients) |
| 165 | + class(conv1d_layer), intent(in), target :: self |
| 166 | + real, allocatable :: gradients(:) |
| 167 | + real, pointer :: dw_(:) => null() |
| 168 | + dw_(1:size(self % dw)) => self % dw |
| 169 | + gradients = [ dw_, self % db ] |
| 170 | + end function get_gradients |
| 171 | + |
| 172 | + module subroutine set_params(self, params) |
| 173 | + class(conv1d_layer), intent(in out) :: self |
| 174 | + real, intent(in) :: params(:) |
| 175 | + |
| 176 | + if (size(params) /= self % get_num_params()) then |
| 177 | + error stop 'conv1d_layer % set_params: Number of parameters does not match' |
| 178 | + end if |
| 179 | + |
| 180 | + self % kernel = reshape(params(:product(shape(self % kernel))), shape(self % kernel)) |
| 181 | + associate(n => product(shape(self % kernel))) |
| 182 | + self % biases = params(n + 1 : n + self % filters) |
| 183 | + end associate |
| 184 | + |
| 185 | + end subroutine set_params |
| 186 | + |
| 187 | +end submodule nf_conv1d_layer_submodule |
0 commit comments