Skip to content

Commit 9fb9b7d

Browse files
committed
Generalize the input window slice to work with any odd window_size
1 parent 33972b6 commit 9fb9b7d

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/nf_conv2d_layer_submodule.f90

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,46 @@ pure module subroutine forward(self, input)
5555
integer :: jstart, jend
5656
integer :: i, j, n
5757
integer :: ii, jj
58+
integer :: iws, iwe, jws, jwe
59+
integer :: half_window
5860

5961
! Input dimensions are channels x width x height
6062
input_channels = size(input, dim=1)
6163
input_width = size(input, dim=2)
6264
input_height = size(input, dim=3)
6365

66+
! Half-window is 1 for window size 3; 2 for window size 5; etc.
67+
half_window = self % window_size / 2
68+
6469
! Determine the start and end indices for the width and height dimensions
65-
! of the input that correspond to the center of each filter (window).
66-
istart = self % window_size / 2 + 1 ! TODO window_width
67-
jstart = self % window_size / 2 + 1 ! TODO window_height
70+
! of the input that correspond to the center of each window.
71+
istart = half_window + 1 ! TODO window_width
72+
jstart = half_window + 1 ! TODO window_height
6873
iend = input_width - istart + 1
6974
jend = input_height - jstart + 1
7075

7176
convolution: do concurrent(i = istart:iend, j = jstart:jend)
7277

73-
! Indices of the output matrix
74-
ii = i - self % window_size / 2 ! TODO window_width
75-
jj = j - self % window_size / 2 ! TODO window_height
78+
! Start and end indices of the input data on the filter window
79+
! iws and jws are also coincidentally the indices of the output matrix
80+
iws = i - half_window ! TODO window_width
81+
iwe = i + half_window ! TODO window_width
82+
jws = j - half_window ! TODO window_height
83+
jwe = j + half_window ! TODO window_height
7684

85+
! This computes the inner tensor product, sum(w_ij * x_ij), for each filter,
86+
! and we add bias b_n to it.
7787
inner_product: do concurrent(n = 1:self % filters)
78-
self % output(n,ii,jj) = &
79-
sum(self % kernel(n,:,:,:) * input(:,i-1:i+1,j-1:j+1)) &
88+
self % output(n,iws,jws) = &
89+
sum(self % kernel(n,:,:,:) * input(:,iws:iwe,jws:jwe)) &
8090
+ self % biases(n)
8191
end do inner_product
8292

93+
! TODO We may need to store self % output before we activate it for the backward
94+
! TODO pass, just like we do for the dense layer.
95+
8396
! Activate
84-
self % output(:,ii,jj) = self % activation(self % output(:,ii,jj))
97+
self % output(:,iws,jws) = self % activation(self % output(:,iws,jws))
8598

8699
end do convolution
87100

0 commit comments

Comments
 (0)