| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | module athena__dropout_layer | ||
| 2 | !! Module containing implementation of a dropout layer | ||
| 3 | !! | ||
| 4 | !! This module implements dropout regularisation, randomly zeroing elements | ||
| 5 | !! during training to prevent overfitting and co-adaptation of neurons. | ||
| 6 | !! | ||
| 7 | !! Mathematical operation (training): | ||
| 8 | !! \[ | ||
| 9 | !! y_i = { 0 if r_i < p | ||
| 10 | !! { x_i / (1-p) otherwise | ||
| 11 | !! \] | ||
| 12 | !! where: | ||
| 13 | !! - \( y_i \) is the output | ||
| 14 | !! - \( x_i \) is the input | ||
| 15 | !! - \( p \) is the dropout probability (rate) | ||
| 16 | !! - \( r_i \) is a random variable uniformly distributed in [0,1] | ||
| 17 | !! | ||
| 18 | !! Scaling by $$1/(1-p)$$ maintains expected value: $$E[y_i] = x_i$$ | ||
| 19 | !! | ||
| 20 | !! Inference: acts as identity (no dropout applied) | ||
| 21 | !! \[ | ||
| 22 | !! y_i = x_i | ||
| 23 | !! \] | ||
| 24 | !! | ||
| 25 | !! Benefits: Prevents overfitting, ensemble effect, forces redundancy | ||
| 26 | !! Typical p values: 0.2-0.5 (higher dropout for larger networks) | ||
| 27 | !! Reference: Srivastava et al. (2014), JMLR | ||
| 28 | use coreutils, only: real32, stop_program | ||
| 29 | use athena__base_layer, only: drop_layer_type, base_layer_type | ||
| 30 | use diffstruc, only: array_type, operator(*) | ||
| 31 | use athena__misc_types, only: & | ||
| 32 | onnx_node_type, onnx_initialiser_type, onnx_tensor_type | ||
| 33 | use athena__diffstruc_extd, only: merge_over_channels | ||
| 34 | implicit none | ||
| 35 | |||
| 36 | |||
| 37 | private | ||
| 38 | |||
| 39 | public :: dropout_layer_type | ||
| 40 | public :: read_dropout_layer, create_from_onnx_dropout_layer | ||
| 41 | |||
| 42 | |||
| 43 | type, extends(drop_layer_type) :: dropout_layer_type | ||
| 44 | !! Type for dropout layer with overloaded procedures | ||
| 45 | integer :: idx = 0 | ||
| 46 | !! Temporary index of sample (doesn't need to be accurate) | ||
| 47 | integer :: num_masks | ||
| 48 | !! Number of unique masks = number of samples in batch | ||
| 49 | logical, allocatable, dimension(:,:) :: mask | ||
| 50 | !! Mask for dropout | ||
| 51 | contains | ||
| 52 | procedure, pass(this) :: set_hyperparams => set_hyperparams_dropout | ||
| 53 | !! Set hyperparameters for dropout layer | ||
| 54 | procedure, pass(this) :: init => init_dropout | ||
| 55 | !! Initialise dropout layer | ||
| 56 | procedure, pass(this) :: print_to_unit => print_to_unit_dropout | ||
| 57 | !! Print dropout layer to unit | ||
| 58 | procedure, pass(this) :: read => read_dropout | ||
| 59 | !! Read dropout layer from file | ||
| 60 | |||
| 61 | procedure, pass(this) :: forward => forward_dropout | ||
| 62 | !! Forward propagation derived type handler | ||
| 63 | |||
| 64 | procedure, pass(this) :: generate_mask => generate_dropout_mask | ||
| 65 | !! Generate dropout mask | ||
| 66 | end type dropout_layer_type | ||
| 67 | |||
| 68 | interface dropout_layer_type | ||
| 69 | !! Interface for setting up the dropout layer | ||
| 70 | module function layer_setup( & | ||
| 71 | rate, num_masks, & | ||
| 72 | input_shape) result(layer) | ||
| 73 | !! Set up the dropout layer | ||
| 74 | integer, intent(in) :: num_masks | ||
| 75 | !! Number of unique masks | ||
| 76 | real(real32), intent(in) :: rate | ||
| 77 | !! Drop rate | ||
| 78 | integer, dimension(:), optional, intent(in) :: input_shape | ||
| 79 | !! Input shape | ||
| 80 | type(dropout_layer_type) :: layer | ||
| 81 | !! Instance of the dropout layer | ||
| 82 | end function layer_setup | ||
| 83 | end interface dropout_layer_type | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | contains | ||
| 88 | |||
| 89 | !############################################################################### | ||
| 90 | 3 | module function layer_setup( & | |
| 91 | rate, num_masks, & | ||
| 92 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
3 | input_shape) result(layer) |
| 93 | !! Set up the dropout layer | ||
| 94 | implicit none | ||
| 95 | |||
| 96 | ! Arguments | ||
| 97 | integer, intent(in) :: num_masks | ||
| 98 | !! Number of unique masks | ||
| 99 | real(real32), intent(in) :: rate | ||
| 100 | !! Drop rate | ||
| 101 | integer, dimension(:), optional, intent(in) :: input_shape | ||
| 102 | !! Input shape | ||
| 103 | |||
| 104 | type(dropout_layer_type) :: layer | ||
| 105 | !! Instance of the dropout layer | ||
| 106 | |||
| 107 | |||
| 108 | !--------------------------------------------------------------------------- | ||
| 109 | ! Initialise hyperparameters | ||
| 110 | !--------------------------------------------------------------------------- | ||
| 111 | 3 | call layer%set_hyperparams(rate, num_masks) | |
| 112 | |||
| 113 | |||
| 114 | !--------------------------------------------------------------------------- | ||
| 115 | ! Initialise layer shape | ||
| 116 | !--------------------------------------------------------------------------- | ||
| 117 |
6/10✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
|
3 | if(present(input_shape)) call layer%init(input_shape=input_shape) |
| 118 | |||
| 119 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
6 | end function layer_setup |
| 120 | !############################################################################### | ||
| 121 | |||
| 122 | |||
| 123 | !############################################################################### | ||
| 124 | 4 | pure subroutine set_hyperparams_dropout(this, rate, num_masks) | |
| 125 | !! Set hyperparameters for dropout layer | ||
| 126 | implicit none | ||
| 127 | |||
| 128 | ! Arguments | ||
| 129 | class(dropout_layer_type), intent(inout) :: this | ||
| 130 | !! Instance of the dropout layer | ||
| 131 | real(real32), intent(in) :: rate | ||
| 132 | !! Drop rate | ||
| 133 | integer, intent(in) :: num_masks | ||
| 134 | !! Number of unique masks | ||
| 135 | |||
| 136 |
5/8✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
|
4 | this%name = "dropout" |
| 137 | 4 | this%type = "drop" | |
| 138 | 4 | this%input_rank = 1 | |
| 139 | 4 | this%output_rank = 1 | |
| 140 | |||
| 141 | 4 | this%num_masks = num_masks | |
| 142 | 4 | this%rate = rate | |
| 143 | |||
| 144 | 4 | end subroutine set_hyperparams_dropout | |
| 145 | !############################################################################### | ||
| 146 | |||
| 147 | |||
| 148 | !############################################################################### | ||
| 149 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | subroutine init_dropout(this, input_shape, verbose) |
| 150 | !! Initialise dropout layer | ||
| 151 | implicit none | ||
| 152 | |||
| 153 | ! Arguments | ||
| 154 | class(dropout_layer_type), intent(inout) :: this | ||
| 155 | !! Instance of the dropout layer | ||
| 156 | integer, dimension(:), intent(in) :: input_shape | ||
| 157 | !! Input shape | ||
| 158 | integer, optional, intent(in) :: verbose | ||
| 159 | !! Verbosity level | ||
| 160 | |||
| 161 | ! Local variables | ||
| 162 | integer :: verbose_ = 0 | ||
| 163 | !! Verbosity level | ||
| 164 | |||
| 165 | |||
| 166 | !--------------------------------------------------------------------------- | ||
| 167 | ! Initialise optional arguments | ||
| 168 | !--------------------------------------------------------------------------- | ||
| 169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if(present(verbose)) verbose_ = verbose |
| 170 | |||
| 171 | |||
| 172 | !--------------------------------------------------------------------------- | ||
| 173 | ! Initialise input shape | ||
| 174 | !--------------------------------------------------------------------------- | ||
| 175 |
4/8✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
|
3 | if(.not.allocated(this%input_shape)) call this%set_shape(input_shape) |
| 176 | |||
| 177 | |||
| 178 | !--------------------------------------------------------------------------- | ||
| 179 | ! Set up number of channels, width, height | ||
| 180 | !--------------------------------------------------------------------------- | ||
| 181 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
|
3 | allocate(this%output_shape(2)) |
| 182 |
11/20✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✓ Branch 12 taken 3 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 3 times.
✓ Branch 16 taken 3 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 3 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 3 times.
✓ Branch 22 taken 3 times.
✓ Branch 23 taken 3 times.
|
6 | this%output_shape = this%input_shape |
| 183 | |||
| 184 | |||
| 185 | !--------------------------------------------------------------------------- | ||
| 186 | ! Allocate mask | ||
| 187 | !--------------------------------------------------------------------------- | ||
| 188 |
23/42✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 3 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 3 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 3 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 3 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 3 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 3 times.
✗ Branch 30 not taken.
✓ Branch 31 taken 3 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 3 times.
✗ Branch 36 not taken.
✓ Branch 37 taken 3 times.
✗ Branch 39 not taken.
✓ Branch 40 taken 3 times.
✗ Branch 42 not taken.
✓ Branch 43 taken 3 times.
✗ Branch 45 not taken.
✓ Branch 46 taken 3 times.
✗ Branch 48 not taken.
✓ Branch 49 taken 3 times.
✓ Branch 51 taken 3 times.
✓ Branch 52 taken 3 times.
✓ Branch 53 taken 18 times.
✓ Branch 54 taken 3 times.
|
24 | allocate(this%mask(this%input_shape(1), this%num_masks), source=.true.) |
| 189 | |||
| 190 | |||
| 191 | !--------------------------------------------------------------------------- | ||
| 192 | ! Generate mask | ||
| 193 | !--------------------------------------------------------------------------- | ||
| 194 | 3 | call this%generate_mask() | |
| 195 | |||
| 196 | |||
| 197 | !--------------------------------------------------------------------------- | ||
| 198 | ! Generate mask | ||
| 199 | !--------------------------------------------------------------------------- | ||
| 200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if(this%use_graph_input)then |
| 201 | call stop_program( & | ||
| 202 | "Graph input not supported for dropout layer" & | ||
| 203 | ✗ | ) | |
| 204 | ✗ | return | |
| 205 | end if | ||
| 206 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
3 | if(allocated(this%output)) deallocate(this%output) |
| 207 |
15/26✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 3 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 3 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 3 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 3 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 3 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 3 times.
✗ Branch 30 not taken.
✓ Branch 31 taken 3 times.
✓ Branch 33 taken 3 times.
✓ Branch 34 taken 3 times.
✓ Branch 35 taken 3 times.
✓ Branch 36 taken 3 times.
|
9 | allocate( this%output(1,1) ) |
| 208 | |||
| 209 | end subroutine init_dropout | ||
| 210 | !############################################################################### | ||
| 211 | |||
| 212 | |||
| 213 | !############################################################################### | ||
| 214 | 3 | subroutine generate_dropout_mask(this) | |
| 215 | !! Generate dropout mask | ||
| 216 | implicit none | ||
| 217 | |||
| 218 | ! Arguments | ||
| 219 | class(dropout_layer_type), intent(inout) :: this | ||
| 220 | !! Instance of the dropout layer | ||
| 221 | |||
| 222 | ! Local variables | ||
| 223 | 3 | real(real32), allocatable, dimension(:,:) :: mask_real | |
| 224 | !! Real mask | ||
| 225 | |||
| 226 | ! Generate masks | ||
| 227 | !--------------------------------------------------------------------------- | ||
| 228 |
9/18✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 3 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 3 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 3 times.
|
3 | allocate(mask_real(size(this%mask,1), size(this%mask,2))) |
| 229 | 3 | call random_number(mask_real) ! Generate random values in [0..1] | |
| 230 |
15/28✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 3 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 3 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 3 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 3 times.
✓ Branch 24 taken 3 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 3 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 3 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✓ Branch 32 taken 3 times.
✓ Branch 33 taken 3 times.
✓ Branch 34 taken 18 times.
✓ Branch 35 taken 3 times.
|
24 | this%mask = mask_real .gt. this%rate |
| 231 | |||
| 232 | 3 | this%idx = 0 | |
| 233 | |||
| 234 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | end subroutine generate_dropout_mask |
| 235 | !############################################################################### | ||
| 236 | |||
| 237 | |||
| 238 | !##############################################################################! | ||
| 239 | ! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ! | ||
| 240 | !##############################################################################! | ||
| 241 | |||
| 242 | |||
| 243 | !############################################################################### | ||
| 244 | 1 | subroutine print_to_unit_dropout(this, unit) | |
| 245 | !! Print dropout layer to unit | ||
| 246 | use coreutils, only: to_upper | ||
| 247 | implicit none | ||
| 248 | |||
| 249 | ! Arguments | ||
| 250 | class(dropout_layer_type), intent(in) :: this | ||
| 251 | !! Instance of the dropout layer | ||
| 252 | integer, intent(in) :: unit | ||
| 253 | !! File unit | ||
| 254 | |||
| 255 | |||
| 256 | ! Write initial parameters | ||
| 257 | !--------------------------------------------------------------------------- | ||
| 258 | 1 | write(unit,'(3X,"INPUT_SHAPE = ",3(1X,I0))') this%input_shape | |
| 259 | 1 | write(unit,'(3X,"RATE = ",F0.9)') this%rate | |
| 260 | 1 | write(unit,'(3X,"NUM_MASKS = ",I0)') this%num_masks | |
| 261 | |||
| 262 | 1 | end subroutine print_to_unit_dropout | |
| 263 | !############################################################################### | ||
| 264 | |||
| 265 | |||
| 266 | !############################################################################### | ||
| 267 | 1 | subroutine read_dropout(this, unit, verbose) | |
| 268 | !! Read dropout layer from file | ||
| 269 | use athena__tools_infile, only: assign_val, assign_vec | ||
| 270 | use coreutils, only: to_lower, to_upper, icount | ||
| 271 | implicit none | ||
| 272 | |||
| 273 | ! Arguments | ||
| 274 | class(dropout_layer_type), intent(inout) :: this | ||
| 275 | !! Instance of the dropout layer | ||
| 276 | integer, intent(in) :: unit | ||
| 277 | !! File unit | ||
| 278 | integer, optional, intent(in) :: verbose | ||
| 279 | !! Verbosity level | ||
| 280 | |||
| 281 | ! Local variables | ||
| 282 | integer :: verbose_ = 0 | ||
| 283 | !! Verbosity level | ||
| 284 | integer :: stat | ||
| 285 | !! File status | ||
| 286 | integer :: itmp1 | ||
| 287 | !! Temporary integer | ||
| 288 | integer :: num_masks | ||
| 289 | !! Number of unique masks | ||
| 290 | real(real32) :: rate | ||
| 291 | !! Drop rate | ||
| 292 | integer, dimension(1) :: input_shape | ||
| 293 | !! Input shape | ||
| 294 | character(256) :: buffer, tag, err_msg | ||
| 295 | !! Buffer, tag, and error message | ||
| 296 | |||
| 297 | |||
| 298 | ! Initialise optional arguments | ||
| 299 | !--------------------------------------------------------------------------- | ||
| 300 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if(present(verbose)) verbose_ = verbose |
| 301 | |||
| 302 | |||
| 303 | ! Loop over tags in layer card | ||
| 304 | !--------------------------------------------------------------------------- | ||
| 305 | 3 | tag_loop: do | |
| 306 | |||
| 307 | ! Check for end of file | ||
| 308 | !------------------------------------------------------------------------ | ||
| 309 | 4 | read(unit,'(A)',iostat=stat) buffer | |
| 310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if(stat.ne.0)then |
| 311 | write(err_msg,'("file encountered error (EoF?) before END ",A)') & | ||
| 312 | ✗ | to_upper(this%name) | |
| 313 | ✗ | call stop_program(err_msg) | |
| 314 | ✗ | return | |
| 315 | end if | ||
| 316 |
2/4✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
|
4 | if(trim(adjustl(buffer)).eq."") cycle tag_loop |
| 317 | |||
| 318 | ! Check for end of layer card | ||
| 319 | !------------------------------------------------------------------------ | ||
| 320 |
4/6✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 3 times.
|
8 | if(trim(adjustl(buffer)).eq."END "//to_upper(trim(this%name)))then |
| 321 | 1 | backspace(unit) | |
| 322 | 4 | exit tag_loop | |
| 323 | end if | ||
| 324 | |||
| 325 |
2/4✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | tag=trim(adjustl(buffer)) |
| 326 |
5/10✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
|
3 | if(scan(buffer,"=").ne.0) tag=trim(tag(:scan(tag,"=")-1)) |
| 327 | |||
| 328 | ! Read parameters from save file | ||
| 329 | !------------------------------------------------------------------------ | ||
| 330 | 6 | select case(trim(tag)) | |
| 331 | case("INPUT_SHAPE") | ||
| 332 | 1 | call assign_vec(buffer, input_shape, itmp1) | |
| 333 | case("RATE") | ||
| 334 | 2 | call assign_val(buffer, rate, itmp1) | |
| 335 | case("NUM_MASKS") | ||
| 336 | 1 | call assign_val(buffer, num_masks, itmp1) | |
| 337 | case default | ||
| 338 | ! Don't look for "e" due to scientific notation of numbers | ||
| 339 | ! ... i.e. exponent (E+00) | ||
| 340 | ✗ | if(scan(to_lower(trim(adjustl(buffer))),& | |
| 341 | 'abcdfghijklmnopqrstuvwxyz').eq.0)then | ||
| 342 | ✗ | cycle tag_loop | |
| 343 | ✗ | elseif(tag(:3).eq.'END')then | |
| 344 | ✗ | cycle tag_loop | |
| 345 | end if | ||
| 346 | write(err_msg,'("Unrecognised line in input file: ",A)') & | ||
| 347 | ✗ | trim(adjustl(buffer)) | |
| 348 | ✗ | call stop_program(err_msg) | |
| 349 |
4/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
6 | return |
| 350 | end select | ||
| 351 | end do tag_loop | ||
| 352 | |||
| 353 | |||
| 354 | ! Set hyperparameters and initialise layer | ||
| 355 | !--------------------------------------------------------------------------- | ||
| 356 | 1 | call this%set_hyperparams(rate = rate, num_masks = num_masks) | |
| 357 | 1 | call this%init(input_shape = input_shape) | |
| 358 | |||
| 359 | |||
| 360 | ! Check for end of layer card | ||
| 361 | !--------------------------------------------------------------------------- | ||
| 362 | 1 | read(unit,'(A)') buffer | |
| 363 |
3/6✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
|
2 | if(trim(adjustl(buffer)).ne."END "//to_upper(trim(this%name)))then |
| 364 | ✗ | write(0,*) trim(adjustl(buffer)) | |
| 365 | ✗ | write(err_msg,'("END ",A," not where expected")') to_upper(this%name) | |
| 366 | ✗ | call stop_program(err_msg) | |
| 367 | 1 | return | |
| 368 | end if | ||
| 369 | |||
| 370 | end subroutine read_dropout | ||
| 371 | !############################################################################### | ||
| 372 | |||
| 373 | |||
| 374 | !############################################################################### | ||
| 375 | 1 | function read_dropout_layer(unit, verbose) result(layer) | |
| 376 | !! Read dropout layer from file and return layer | ||
| 377 | implicit none | ||
| 378 | |||
| 379 | ! Arguments | ||
| 380 | integer, intent(in) :: unit | ||
| 381 | !! File unit | ||
| 382 | integer, optional, intent(in) :: verbose | ||
| 383 | !! Verbosity level | ||
| 384 | class(base_layer_type), allocatable :: layer | ||
| 385 | !! Instance of the base layer | ||
| 386 | |||
| 387 | ! Local variables | ||
| 388 | integer :: verbose_ = 0 | ||
| 389 | !! Verbosity level | ||
| 390 | |||
| 391 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(present(verbose)) verbose_ = verbose |
| 392 |
9/54✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✓ Branch 48 taken 1 times.
✗ Branch 50 not taken.
✓ Branch 51 taken 1 times.
✗ Branch 52 not taken.
✓ Branch 53 taken 1 times.
✗ Branch 54 not taken.
✓ Branch 55 taken 1 times.
✗ Branch 56 not taken.
✓ Branch 57 taken 1 times.
|
2 | allocate(layer, source=dropout_layer_type(rate=0._real32, num_masks=0)) |
| 393 | 1 | call layer%read(unit, verbose=verbose_) | |
| 394 | |||
| 395 | 2 | end function read_dropout_layer | |
| 396 | !############################################################################### | ||
| 397 | |||
| 398 | |||
| 399 | !############################################################################### | ||
| 400 | ✗ | subroutine build_from_onnx_dropout( & | |
| 401 | ✗ | this, node, initialisers, value_info, verbose & | |
| 402 | ) | ||
| 403 | !! Read ONNX attributes for dropout layer | ||
| 404 | implicit none | ||
| 405 | |||
| 406 | ! Arguments | ||
| 407 | class(dropout_layer_type), intent(inout) :: this | ||
| 408 | !! Instance of the dropout layer | ||
| 409 | type(onnx_node_type), intent(in) :: node | ||
| 410 | !! ONNX node information | ||
| 411 | type(onnx_initialiser_type), dimension(:), intent(in) :: initialisers | ||
| 412 | !! ONNX initialiser information | ||
| 413 | type(onnx_tensor_type), dimension(:), intent(in) :: value_info | ||
| 414 | !! ONNX value info | ||
| 415 | integer, intent(in) :: verbose | ||
| 416 | !! Verbosity level | ||
| 417 | |||
| 418 | ! Local variables | ||
| 419 | integer :: i | ||
| 420 | !! Loop index | ||
| 421 | real(real32) :: rate | ||
| 422 | !! Dropout rate | ||
| 423 | integer :: num_masks | ||
| 424 | !! Number of masks (batch size) | ||
| 425 | character(256) :: val | ||
| 426 | !! Attribute value | ||
| 427 | |||
| 428 | ! Set default values | ||
| 429 | ✗ | rate = 0.5_real32 | |
| 430 | ✗ | num_masks = 1 | |
| 431 | |||
| 432 | ! Parse ONNX attributes | ||
| 433 | ✗ | do i = 1, size(node%attributes) | |
| 434 | ✗ | val = node%attributes(i)%val | |
| 435 | ✗ | select case(trim(adjustl(node%attributes(i)%name))) | |
| 436 | case("ratio") | ||
| 437 | ✗ | read(val,*) rate | |
| 438 | case default | ||
| 439 | ! Do nothing | ||
| 440 | ✗ | write(0,*) "WARNING: Unrecognised attribute in ONNX DROPOUT layer: ", & | |
| 441 | ✗ | trim(adjustl(node%attributes(i)%name)) | |
| 442 | end select | ||
| 443 | end do | ||
| 444 | |||
| 445 | ! Check size of initialisers is zero | ||
| 446 | ✗ | if(size(initialisers).ne.0)then | |
| 447 | ✗ | write(0,*) "WARNING: initialisers not used for ONNX DROPOUT layer" | |
| 448 | end if | ||
| 449 | |||
| 450 | call this%set_hyperparams( & | ||
| 451 | rate = rate, & | ||
| 452 | num_masks = num_masks & | ||
| 453 | ✗ | ) | |
| 454 | |||
| 455 | ✗ | end subroutine build_from_onnx_dropout | |
| 456 | !############################################################################### | ||
| 457 | |||
| 458 | |||
| 459 | !############################################################################### | ||
| 460 | ✗ | function create_from_onnx_dropout_layer( & | |
| 461 | ✗ | node, initialisers, value_info, verbose & | |
| 462 | ✗ | ) result(layer) | |
| 463 | !! Build dropout layer from attributes and return layer | ||
| 464 | implicit none | ||
| 465 | |||
| 466 | ! Arguments | ||
| 467 | type(onnx_node_type), intent(in) :: node | ||
| 468 | !! ONNX node information | ||
| 469 | type(onnx_initialiser_type), dimension(:), intent(in) :: initialisers | ||
| 470 | !! ONNX initialiser information | ||
| 471 | type(onnx_tensor_type), dimension(:), intent(in) :: value_info | ||
| 472 | !! ONNX value info information | ||
| 473 | integer, optional, intent(in) :: verbose | ||
| 474 | !! Verbosity level | ||
| 475 | class(base_layer_type), allocatable :: layer | ||
| 476 | !! Instance of the dropout layer | ||
| 477 | |||
| 478 | ! Local variables | ||
| 479 | integer :: verbose_ = 0 | ||
| 480 | !! Verbosity level | ||
| 481 | |||
| 482 | ✗ | if(present(verbose)) verbose_ = verbose | |
| 483 | |||
| 484 | ✗ | allocate(layer, source=dropout_layer_type(rate=0._real32, num_masks=0)) | |
| 485 | ✗ | call layer%build_from_onnx(node, initialisers, value_info, verbose=verbose_) | |
| 486 | |||
| 487 | ✗ | end function create_from_onnx_dropout_layer | |
| 488 | !############################################################################### | ||
| 489 | |||
| 490 | |||
| 491 | !##############################################################################! | ||
| 492 | ! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ! | ||
| 493 | !##############################################################################! | ||
| 494 | |||
| 495 | |||
| 496 | !############################################################################### | ||
| 497 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | subroutine forward_dropout(this, input) |
| 498 | !! Forward propagation | ||
| 499 | implicit none | ||
| 500 | |||
| 501 | ! Arguments | ||
| 502 | class(dropout_layer_type), intent(inout) :: this | ||
| 503 | !! Instance of the dropout layer | ||
| 504 | class(array_type), dimension(:,:), intent(in) :: input | ||
| 505 | !! Input values | ||
| 506 | |||
| 507 | ! Local variables | ||
| 508 | real(real32) :: rtmp1 | ||
| 509 | !! Temporary variable | ||
| 510 | type(array_type), pointer :: ptr | ||
| 511 | !! Pointer array | ||
| 512 | |||
| 513 | |||
| 514 | 1 | rtmp1 = 1._real32 - this%rate | |
| 515 | 1 | select case(this%inference) | |
| 516 | case(.true.) | ||
| 517 | ! Do not perform the drop operation | ||
| 518 | ✗ | ptr => input(1,1) * rtmp1 | |
| 519 | case default | ||
| 520 | ! Perform the drop operation | ||
| 521 | 1 | this%idx = this%idx + 1 | |
| 522 | |||
| 523 | 1 | rtmp1 = 1._real32 / rtmp1 | |
| 524 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
|
1 | ptr => merge_over_channels( input(1,1), 0._real32, this%mask) * rtmp1 |
| 525 | end select | ||
| 526 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
|
1 | call this%output(1,1)%assign_and_deallocate_source(ptr) |
| 527 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
|
1 | this%output(1,1)%is_temporary = .false. |
| 528 | |||
| 529 | 1 | end subroutine forward_dropout | |
| 530 | !############################################################################### | ||
| 531 | |||
| 532 |
34/124✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✓ Branch 36 taken 3 times.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✓ Branch 39 taken 3 times.
✓ Branch 40 taken 2 times.
✗ Branch 41 not taken.
✓ Branch 42 taken 3 times.
✓ Branch 43 taken 2 times.
✓ Branch 44 taken 3 times.
✓ Branch 45 taken 5 times.
✓ Branch 46 taken 2 times.
✓ Branch 47 taken 3 times.
✓ Branch 48 taken 3 times.
✓ Branch 49 taken 3 times.
✓ Branch 50 taken 1 times.
✓ Branch 51 taken 3 times.
✓ Branch 52 taken 1 times.
✗ Branch 53 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 61 not taken.
✗ Branch 62 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✓ Branch 67 taken 2 times.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✓ Branch 70 taken 2 times.
✓ Branch 71 taken 2 times.
✗ Branch 72 not taken.
✗ Branch 73 not taken.
✓ Branch 74 taken 2 times.
✗ Branch 75 not taken.
✗ Branch 76 not taken.
✗ Branch 77 not taken.
✗ Branch 78 not taken.
✗ Branch 79 not taken.
✗ Branch 80 not taken.
✗ Branch 81 not taken.
✗ Branch 82 not taken.
✗ Branch 83 not taken.
✗ Branch 84 not taken.
✗ Branch 85 not taken.
✗ Branch 86 not taken.
✗ Branch 87 not taken.
✗ Branch 88 not taken.
✗ Branch 89 not taken.
✗ Branch 90 not taken.
✗ Branch 91 not taken.
✗ Branch 92 not taken.
✗ Branch 93 not taken.
✗ Branch 94 not taken.
✗ Branch 95 not taken.
✗ Branch 96 not taken.
✗ Branch 97 not taken.
✗ Branch 98 not taken.
✗ Branch 99 not taken.
✗ Branch 100 not taken.
✗ Branch 101 not taken.
✗ Branch 102 not taken.
✗ Branch 103 not taken.
✗ Branch 104 not taken.
✗ Branch 105 not taken.
✗ Branch 106 not taken.
✗ Branch 107 not taken.
✗ Branch 108 not taken.
✓ Branch 109 taken 2 times.
✗ Branch 110 not taken.
✓ Branch 111 taken 2 times.
✗ Branch 112 not taken.
✗ Branch 113 not taken.
✓ Branch 114 taken 2 times.
✓ Branch 115 taken 2 times.
✗ Branch 116 not taken.
✓ Branch 118 taken 2 times.
✗ Branch 119 not taken.
✗ Branch 120 not taken.
✓ Branch 121 taken 2 times.
✓ Branch 122 taken 2 times.
✗ Branch 123 not taken.
✗ Branch 124 not taken.
✓ Branch 125 taken 2 times.
✓ Branch 126 taken 2 times.
✗ Branch 127 not taken.
✗ Branch 128 not taken.
✓ Branch 129 taken 2 times.
|
28 | end module athena__dropout_layer |
| 533 |