Skip to content

Commit 820b081

Browse files
committed
Test uninitialized dropout layer
1 parent 37aa7a5 commit 820b081

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/nf/nf_dropout_layer.f90

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ module nf_dropout_layer
1212
public :: dropout_layer
1313

1414
type, extends(base_layer) :: dropout_layer
15-
1615
!! Concrete implementation of a dropout layer type
1716

18-
integer :: input_size
17+
integer :: input_size = 0
1918

2019
real, allocatable :: output(:)
2120
real, allocatable :: gradient(:)

src/nf/nf_layer_submodule.f90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use iso_fortran_env, only: stderr => error_unit
44
use nf_conv2d_layer, only: conv2d_layer
55
use nf_dense_layer, only: dense_layer
6+
use nf_dropout_layer, only: dropout_layer
67
use nf_flatten_layer, only: flatten_layer
78
use nf_input1d_layer, only: input1d_layer
89
use nf_input3d_layer, only: input3d_layer
@@ -240,15 +241,17 @@ impure elemental module subroutine init(self, input)
240241
call this_layer % init(input % layer_shape)
241242
end select
242243

243-
! The shape of conv2d, maxpool2d, or flatten layers is not known
244+
! The shape of conv2d, dropout, flatten, or maxpool2d layers is not known
244245
! until we receive an input layer.
245246
select type(this_layer => self % p)
246247
type is(conv2d_layer)
247248
self % layer_shape = shape(this_layer % output)
248-
type is(maxpool2d_layer)
249+
type is(dropout_layer)
249250
self % layer_shape = shape(this_layer % output)
250251
type is(flatten_layer)
251252
self % layer_shape = shape(this_layer % output)
253+
type is(maxpool2d_layer)
254+
self % layer_shape = shape(this_layer % output)
252255
end select
253256

254257
self % input_layer_shape = input % layer_shape

test/test_dropout_layer.f90

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
program test_dropout_layer
22
use iso_fortran_env, only: stderr => error_unit
33
use nf, only: dropout, layer
4+
use nf_dropout_layer, only: dropout_layer
45
type(layer) :: layer1
56
logical :: ok = .true.
67

@@ -11,6 +12,23 @@ program test_dropout_layer
1112
write(stderr, '(a)') 'dropout layer has its name set correctly.. failed'
1213
end if
1314

15+
! Dropout on its own is not initialized and its arrays not allocated.
16+
select type(layer1_p => layer1 % p)
17+
type is(dropout_layer)
18+
19+
if (layer1_p % input_size /= 0) then
20+
print *, 'input_size: ', layer1_p % input_size
21+
ok = .false.
22+
write(stderr, '(a)') 'dropout layer size should be zero.. failed'
23+
end if
24+
25+
if (allocated(layer1_p % output)) then
26+
ok = .false.
27+
write(stderr, '(a)') 'dropout layer output array should not be allocated.. failed'
28+
end if
29+
30+
end select
31+
1432
if (ok) then
1533
print '(a)', 'test_dropout_layer: All tests passed.'
1634
else

0 commit comments

Comments
 (0)