1+ program test_maxpool1d_layer
2+
3+ use iso_fortran_env, only: stderr = > error_unit
4+ use nf, only: maxpool1d, input, layer
5+ use nf_input2d_layer, only: input2d_layer
6+ use nf_maxpool1d_layer, only: maxpool1d_layer
7+
8+ implicit none
9+
10+ type (layer) :: maxpool_layer, input_layer
11+ integer , parameter :: pool_size = 2 , stride = 2
12+ integer , parameter :: channels = 3 , length = 32
13+ integer , parameter :: input_shape(2 ) = [channels, length]
14+ integer , parameter :: output_shape(2 ) = [channels, length / 2 ]
15+ real , allocatable :: sample_input(:,:), output(:,:), gradient(:,:)
16+ integer :: i
17+ logical :: ok = .true. , gradient_ok = .true.
18+
19+ maxpool_layer = maxpool1d(pool_size)
20+
21+ if (.not. maxpool_layer % name == ' maxpool1d' ) then
22+ ok = .false.
23+ write (stderr, ' (a)' ) ' maxpool1d layer has its name set correctly.. failed'
24+ end if
25+
26+ if (maxpool_layer % initialized) then
27+ ok = .false.
28+ write (stderr, ' (a)' ) ' maxpool1d layer should not be marked as initialized yet.. failed'
29+ end if
30+
31+ input_layer = input(channels, length)
32+ call maxpool_layer % init(input_layer)
33+
34+ if (.not. maxpool_layer % initialized) then
35+ ok = .false.
36+ write (stderr, ' (a)' ) ' maxpool1d layer should now be marked as initialized.. failed'
37+ end if
38+
39+ if (.not. all (maxpool_layer % input_layer_shape == input_shape)) then
40+ ok = .false.
41+ write (stderr, ' (a)' ) ' maxpool1d layer input layer shape should be correct.. failed'
42+ end if
43+
44+ if (.not. all (maxpool_layer % layer_shape == output_shape)) then
45+ ok = .false.
46+ write (stderr, ' (a)' ) ' maxpool1d layer output layer shape should be correct.. failed'
47+ end if
48+
49+ ! Allocate and initialize sample input data
50+ allocate (sample_input(channels, length))
51+ do concurrent(i = 1 :length)
52+ sample_input(:,i) = i
53+ end do
54+
55+ select type (this_layer = > input_layer % p); type is(input2d_layer)
56+ call this_layer % set(sample_input)
57+ end select
58+
59+ call maxpool_layer % forward(input_layer)
60+ call maxpool_layer % get_output(output)
61+
62+ do i = 1 , length / 2
63+ ! Since input is i, maxpool1d output must be stride*i
64+ if (.not. all (output(:,i) == stride * i)) then
65+ ok = .false.
66+ write (stderr, ' (a)' ) ' maxpool1d layer forward pass correctly propagates the max value.. failed'
67+ end if
68+ end do
69+
70+ ! Test the backward pass
71+ ! Allocate and initialize the downstream gradient field
72+ allocate (gradient, source= output)
73+
74+ ! Make a backward pass
75+ call maxpool_layer % backward(input_layer, gradient)
76+
77+ select type (this_layer = > maxpool_layer % p); type is(maxpool1d_layer)
78+ do i = 1 , length
79+ if (mod (i,2 ) == 0 ) then
80+ if (.not. all (sample_input(:,i) == this_layer % gradient(:,i))) gradient_ok = .false.
81+ else
82+ if (.not. all (this_layer % gradient(:,i) == 0 )) gradient_ok = .false.
83+ end if
84+ end do
85+ end select
86+
87+ if (.not. gradient_ok) then
88+ ok = .false.
89+ write (stderr, ' (a)' ) ' maxpool1d layer backward pass produces the correct dL/dx.. failed'
90+ end if
91+
92+ if (ok) then
93+ print ' (a)' , ' test_maxpool1d_layer: All tests passed.'
94+ else
95+ write (stderr, ' (a)' ) ' test_maxpool1d_layer: One or more tests failed.'
96+ stop 1
97+ end if
98+
99+ end program test_maxpool1d_layer
100+
0 commit comments