|
| 1 | +module nf_dropout_layer |
| 2 | + |
| 3 | + !! Dropout layer by Srivastava et al. (2014). |
| 4 | + !! |
| 5 | + !! Srivastava, N., Hinton, G., Krizhevsky, A., Sutskever, I. and |
| 6 | + !! Salakhutdinov, R., 2014. Dropout: a simple way to prevent neural networks |
| 7 | + !! from overfitting. The Journal of Machine Learning Research, 16(1), |
| 8 | + !! pp.1929-1958. |
| 9 | + |
| 10 | + use nf_base_layer, only: base_layer |
| 11 | + |
| 12 | + implicit none |
| 13 | + |
| 14 | + private |
| 15 | + public :: dropout_layer |
| 16 | + |
| 17 | + type, extends(base_layer) :: dropout_layer |
| 18 | + !! Concrete implementation of a dropout layer type |
| 19 | + |
| 20 | + integer :: input_size = 0 |
| 21 | + |
| 22 | + real, allocatable :: output(:) |
| 23 | + real, allocatable :: gradient(:) |
| 24 | + real, allocatable :: mask(:) ! binary mask for dropout |
| 25 | + |
| 26 | + real :: dropout_rate ! probability of dropping a neuron |
| 27 | + real :: scale ! scale factor to preserve the input sum |
| 28 | + logical :: training = .true. ! set to .false. for inference |
| 29 | + |
| 30 | + contains |
| 31 | + |
| 32 | + procedure :: backward |
| 33 | + procedure :: forward |
| 34 | + procedure :: init |
| 35 | + |
| 36 | + end type dropout_layer |
| 37 | + |
| 38 | + interface dropout_layer |
| 39 | + module function dropout_layer_cons(rate) & |
| 40 | + result(res) |
| 41 | + !! This function returns the `dropout_layer` instance. |
| 42 | + real, intent(in) :: rate |
| 43 | + !! Dropout rate |
| 44 | + type(dropout_layer) :: res |
| 45 | + !! dropout_layer instance |
| 46 | + end function dropout_layer_cons |
| 47 | + end interface dropout_layer |
| 48 | + |
| 49 | + interface |
| 50 | + |
| 51 | + pure module subroutine backward(self, gradient) |
| 52 | + !! Apply the backward gradient descent pass. |
| 53 | + !! Only weight and bias gradients are updated in this subroutine, |
| 54 | + !! while the weights and biases themselves are untouched. |
| 55 | + class(dropout_layer), intent(in out) :: self |
| 56 | + !! Dropout layer instance |
| 57 | + real, intent(in) :: gradient(:) |
| 58 | + !! Gradient from the next layer |
| 59 | + end subroutine backward |
| 60 | + |
| 61 | + module subroutine forward(self, input) |
| 62 | + !! Propagate forward the layer. |
| 63 | + !! Calling this subroutine updates the values of a few data components |
| 64 | + !! of `dropout_layer` that are needed for the backward pass. |
| 65 | + class(dropout_layer), intent(in out) :: self |
| 66 | + !! Dense layer instance |
| 67 | + real, intent(in) :: input(:) |
| 68 | + !! Input from the previous layer |
| 69 | + end subroutine forward |
| 70 | + |
| 71 | + module subroutine init(self, input_shape) |
| 72 | + !! Initialize the layer data structures. |
| 73 | + !! |
| 74 | + !! This is a deferred procedure from the `base_layer` abstract type. |
| 75 | + class(dropout_layer), intent(in out) :: self |
| 76 | + !! Dropout layer instance |
| 77 | + integer, intent(in) :: input_shape(:) |
| 78 | + !! Shape of the input layer |
| 79 | + end subroutine init |
| 80 | + |
| 81 | + end interface |
| 82 | + |
| 83 | +end module nf_dropout_layer |
0 commit comments