-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathnf_maxpool2d_layer.f90
76 lines (59 loc) · 2.21 KB
/
nf_maxpool2d_layer.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module nf_maxpool2d_layer
!! This module provides the 2-d maxpooling layer.
use nf_base_layer, only: base_layer
implicit none
private
public :: maxpool2d_layer
type, extends(base_layer) :: maxpool2d_layer
integer :: channels
integer :: width
integer :: height
integer :: pool_size
integer :: stride
! Locations (as input matrix indices) of the maximum values
! in the width (x) and height (y) dimensions
integer, allocatable :: maxloc_x(:,:,:)
integer, allocatable :: maxloc_y(:,:,:)
real, allocatable :: gradient(:,:,:)
real, allocatable :: output(:,:,:)
contains
procedure :: init
procedure :: forward
procedure :: backward
end type maxpool2d_layer
interface maxpool2d_layer
pure module function maxpool2d_layer_cons(pool_size, stride) result(res)
!! `maxpool2d` constructor function
integer, intent(in) :: pool_size
!! Width and height of the pooling window
integer, intent(in) :: stride
!! Stride of the pooling window
type(maxpool2d_layer) :: res
end function maxpool2d_layer_cons
end interface maxpool2d_layer
interface
module subroutine init(self, input_shape)
!! Initialize the `maxpool2d` layer instance with an input shape.
class(maxpool2d_layer), intent(in out) :: self
!! `maxpool2d_layer` instance
integer, intent(in) :: input_shape(:)
!! Array shape of the input layer
end subroutine init
pure module subroutine forward(self, input)
!! Run a forward pass of the `maxpool2d` layer.
class(maxpool2d_layer), intent(in out) :: self
!! `maxpool2d_layer` instance
real, intent(in) :: input(:,:,:)
!! Input data (output of the previous layer)
end subroutine forward
pure module subroutine backward(self, input, gradient)
!! Run a backward pass of the `maxpool2d` layer.
class(maxpool2d_layer), intent(in out) :: self
!! `maxpool2d_layer` instance
real, intent(in) :: input(:,:,:)
!! Input data (output of the previous layer)
real, intent(in) :: gradient(:,:,:)
!! Gradient from the downstream layer
end subroutine backward
end interface
end module nf_maxpool2d_layer