Skip to content

Commit a1959f9

Browse files
committed
Implement forward pass of a maxpool2d layer
1 parent 673cb26 commit a1959f9

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/nf_maxpool2d_layer_submodule.f90

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
contains
66

77
pure module function maxpool2d_layer_cons(pool_size, stride) result(res)
8+
implicit none
89
integer, intent(in) :: pool_size
910
integer, intent(in) :: stride
1011
type(maxpool2d_layer) :: res
@@ -14,6 +15,7 @@ end function maxpool2d_layer_cons
1415

1516

1617
module subroutine init(self, input_shape)
18+
implicit none
1719
class(maxpool2d_layer), intent(in out) :: self
1820
integer, intent(in) :: input_shape(:)
1921

@@ -34,13 +36,42 @@ end subroutine init
3436

3537

3638
module subroutine forward(self, input)
39+
implicit none
3740
class(maxpool2d_layer), intent(in out) :: self
3841
real, intent(in) :: input(:,:,:)
39-
print *, 'Warning: maxpool2d forward pass not implemented'
42+
integer :: input_width, input_height
43+
integer :: i, j, n
44+
integer :: ii, jj
45+
integer :: iend, jend
46+
47+
input_width = size(input, dim=2)
48+
input_height = size(input, dim=2)
49+
50+
! Stride along the width and height of the input image
51+
do concurrent( &
52+
i = 1:input_width:self % stride, &
53+
j = 1:input_height:self % stride &
54+
)
55+
56+
! Indices of the pooling layer
57+
ii = i / self % stride + 1
58+
jj = j / self % stride + 1
59+
60+
iend = i + self % pool_size - 1
61+
jend = j + self % pool_size - 1
62+
63+
do concurrent(n = 1:self % channels)
64+
!TODO find and store maxloc
65+
self % output(n,ii,jj) = maxval(input(n,i:iend,j:jend))
66+
end do
67+
68+
end do
69+
4070
end subroutine forward
4171

4272

4373
module subroutine backward(self, input, gradient)
74+
implicit none
4475
class(maxpool2d_layer), intent(in out) :: self
4576
real, intent(in) :: input(:,:,:)
4677
real, intent(in) :: gradient(:,:,:)

0 commit comments

Comments
 (0)