|
| 1 | +program test_flatten2d_layer |
| 2 | + |
| 3 | + use iso_fortran_env, only: stderr => error_unit |
| 4 | + use nf, only: dense, flatten2d, input, layer, network |
| 5 | + use nf_flatten2d_layer, only: flatten2d_layer |
| 6 | + use nf_input2d_layer, only: input2d_layer |
| 7 | + |
| 8 | + implicit none |
| 9 | + |
| 10 | + type(layer) :: test_layer, input_layer |
| 11 | + type(network) :: net |
| 12 | + real, allocatable :: gradient(:,:) |
| 13 | + real, allocatable :: output(:) |
| 14 | + logical :: ok = .true. |
| 15 | + |
| 16 | + test_layer = flatten2d() |
| 17 | + |
| 18 | + if (.not. test_layer % name == 'flatten2d') then |
| 19 | + ok = .false. |
| 20 | + write(stderr, '(a)') 'flatten2d layer has its name set correctly.. failed' |
| 21 | + end if |
| 22 | + |
| 23 | + if (test_layer % initialized) then |
| 24 | + ok = .false. |
| 25 | + write(stderr, '(a)') 'flatten2d layer is not initialized yet.. failed' |
| 26 | + end if |
| 27 | + |
| 28 | + input_layer = input(1, 2) |
| 29 | + call test_layer % init(input_layer) |
| 30 | + |
| 31 | + if (.not. test_layer % initialized) then |
| 32 | + ok = .false. |
| 33 | + write(stderr, '(a)') 'flatten2d layer is now initialized.. failed' |
| 34 | + end if |
| 35 | + |
| 36 | + if (.not. all(test_layer % layer_shape == [2])) then |
| 37 | + ok = .false. |
| 38 | + write(stderr, '(a)') 'flatten2d layer has an incorrect output shape.. failed' |
| 39 | + end if |
| 40 | + |
| 41 | + ! Test forward pass - reshaping from 2-d to 1-d |
| 42 | + |
| 43 | + select type(this_layer => input_layer % p); type is(input2d_layer) |
| 44 | + call this_layer % set(reshape(real([1, 2, 3, 4]), [2, 2])) |
| 45 | + end select |
| 46 | + |
| 47 | + call test_layer % forward(input_layer) |
| 48 | + call test_layer % get_output(output) |
| 49 | + |
| 50 | + if (.not. all(output == [1, 2, 3, 4])) then |
| 51 | + ok = .false. |
| 52 | + write(stderr, '(a)') 'flatten2d layer correctly propagates forward.. failed' |
| 53 | + end if |
| 54 | + |
| 55 | + ! Test backward pass - reshaping from 1-d to 2-d |
| 56 | + |
| 57 | + ! Calling backward() will set the values on the gradient component |
| 58 | + ! input_layer is used only to determine shape |
| 59 | + call test_layer % backward(input_layer, real([1, 2, 3, 4])) |
| 60 | + |
| 61 | + select type(this_layer => test_layer % p); type is(flatten2d_layer) |
| 62 | + gradient = this_layer % gradient |
| 63 | + end select |
| 64 | + |
| 65 | + if (.not. all(gradient == reshape(real([1, 2, 3, 4]), [2, 2]))) then |
| 66 | + ok = .false. |
| 67 | + write(stderr, '(a)') 'flatten2d layer correctly propagates backward.. failed' |
| 68 | + end if |
| 69 | + |
| 70 | + net = network([ & |
| 71 | + input(28, 28), & |
| 72 | + flatten2d(), & |
| 73 | + dense(10) & |
| 74 | + ]) |
| 75 | + |
| 76 | + ! Test that the output layer receives 784 elements in the input |
| 77 | + if (.not. all(net % layers(3) % input_layer_shape == [784])) then |
| 78 | + ok = .false. |
| 79 | + write(stderr, '(a)') 'flatten2d layer correctly chains input2d to dense.. failed' |
| 80 | + end if |
| 81 | + |
| 82 | + if (ok) then |
| 83 | + print '(a)', 'test_flatten2d_layer: All tests passed.' |
| 84 | + else |
| 85 | + write(stderr, '(a)') 'test_flatten2d_layer: One or more tests failed.' |
| 86 | + stop 1 |
| 87 | + end if |
| 88 | + |
| 89 | +end program test_flatten2d_layer |
0 commit comments