|
| 1 | +module nf_activation_3d |
| 2 | + |
| 3 | + ! A collection of activation functions and their derivatives. |
| 4 | + |
| 5 | + implicit none |
| 6 | + |
| 7 | + private |
| 8 | + |
| 9 | + public :: activation_function |
| 10 | + public :: elu, elu_prime |
| 11 | + public :: exponential |
| 12 | + public :: gaussian, gaussian_prime |
| 13 | + public :: relu, relu_prime |
| 14 | + public :: sigmoid, sigmoid_prime |
| 15 | + public :: softmax, softmax_prime |
| 16 | + public :: softplus, softplus_prime |
| 17 | + public :: step, step_prime |
| 18 | + public :: tanhf, tanh_prime |
| 19 | + |
| 20 | + interface |
| 21 | + pure function activation_function(x) result(res) |
| 22 | + real, intent(in) :: x(:,:,:) |
| 23 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 24 | + end function activation_function |
| 25 | + end interface |
| 26 | + |
| 27 | +contains |
| 28 | + |
| 29 | + pure function elu(x, alpha) result(res) |
| 30 | + ! Exponential Linear Unit (ELU) activation function. |
| 31 | + real, intent(in) :: x(:,:,:) |
| 32 | + real, intent(in) :: alpha |
| 33 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 34 | + where (x >= 0) |
| 35 | + res = x |
| 36 | + elsewhere |
| 37 | + res = alpha * (exp(x) - 1) |
| 38 | + end where |
| 39 | + end function elu |
| 40 | + |
| 41 | + pure function elu_prime(x, alpha) result(res) |
| 42 | + ! First derivative of the Exponential Linear Unit (ELU) |
| 43 | + ! activation function. |
| 44 | + real, intent(in) :: x(:,:,:) |
| 45 | + real, intent(in) :: alpha |
| 46 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 47 | + where (x >= 0) |
| 48 | + res = 1 |
| 49 | + elsewhere |
| 50 | + res = alpha * exp(x) |
| 51 | + end where |
| 52 | + end function elu_prime |
| 53 | + |
| 54 | + pure function exponential(x) result(res) |
| 55 | + ! Exponential activation function. |
| 56 | + real, intent(in) :: x(:,:,:) |
| 57 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 58 | + res = exp(x) |
| 59 | + end function exponential |
| 60 | + |
| 61 | + pure function gaussian(x) result(res) |
| 62 | + ! Gaussian activation function. |
| 63 | + real, intent(in) :: x(:,:,:) |
| 64 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 65 | + res = exp(-x**2) |
| 66 | + end function gaussian |
| 67 | + |
| 68 | + pure function gaussian_prime(x) result(res) |
| 69 | + ! First derivative of the Gaussian activation function. |
| 70 | + real, intent(in) :: x(:,:,:) |
| 71 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 72 | + res = -2 * x * gaussian(x) |
| 73 | + end function gaussian_prime |
| 74 | + |
| 75 | + pure function relu(x) result(res) |
| 76 | + !! Rectified Linear Unit (ReLU) activation function. |
| 77 | + real, intent(in) :: x(:,:,:) |
| 78 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 79 | + res = max(0., x) |
| 80 | + end function relu |
| 81 | + |
| 82 | + pure function relu_prime(x) result(res) |
| 83 | + ! First derivative of the Rectified Linear Unit (ReLU) activation function. |
| 84 | + real, intent(in) :: x(:,:,:) |
| 85 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 86 | + where (x > 0) |
| 87 | + res = 1 |
| 88 | + elsewhere |
| 89 | + res = 0 |
| 90 | + end where |
| 91 | + end function relu_prime |
| 92 | + |
| 93 | + pure function sigmoid(x) result(res) |
| 94 | + ! Sigmoid activation function. |
| 95 | + real, intent(in) :: x(:,:,:) |
| 96 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 97 | + res = 1 / (1 + exp(-x)) |
| 98 | + endfunction sigmoid |
| 99 | + |
| 100 | + pure function sigmoid_prime(x) result(res) |
| 101 | + ! First derivative of the sigmoid activation function. |
| 102 | + real, intent(in) :: x(:,:,:) |
| 103 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 104 | + res = sigmoid(x) * (1 - sigmoid(x)) |
| 105 | + end function sigmoid_prime |
| 106 | + |
| 107 | + pure function softmax(x) result(res) |
| 108 | + !! Softmax activation function |
| 109 | + real, intent(in) :: x(:,:,:) |
| 110 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 111 | + res = exp(x - maxval(x)) |
| 112 | + res = res / sum(res) |
| 113 | + end function softmax |
| 114 | + |
| 115 | + pure function softmax_prime(x) result(res) |
| 116 | + !! Derivative of the softmax activation function. |
| 117 | + real, intent(in) :: x(:,:,:) |
| 118 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 119 | + res = softmax(x) * (1 - softmax(x)) |
| 120 | + end function softmax_prime |
| 121 | + |
| 122 | + pure function softplus(x) result(res) |
| 123 | + ! Softplus activation function. |
| 124 | + real, intent(in) :: x(:,:,:) |
| 125 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 126 | + res = log(exp(x) + 1) |
| 127 | + end function softplus |
| 128 | + |
| 129 | + pure function softplus_prime(x) result(res) |
| 130 | + ! First derivative of the softplus activation function. |
| 131 | + real, intent(in) :: x(:,:,:) |
| 132 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 133 | + res = exp(x) / (exp(x) + 1) |
| 134 | + end function softplus_prime |
| 135 | + |
| 136 | + pure function step(x) result(res) |
| 137 | + ! Step activation function. |
| 138 | + real, intent(in) :: x(:,:,:) |
| 139 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 140 | + where (x > 0) |
| 141 | + res = 1 |
| 142 | + elsewhere |
| 143 | + res = 0 |
| 144 | + end where |
| 145 | + end function step |
| 146 | + |
| 147 | + pure function step_prime(x) result(res) |
| 148 | + ! First derivative of the step activation function. |
| 149 | + real, intent(in) :: x(:,:,:) |
| 150 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 151 | + res = 0 |
| 152 | + end function step_prime |
| 153 | + |
| 154 | + pure function tanhf(x) result(res) |
| 155 | + ! Tangent hyperbolic activation function. |
| 156 | + ! Same as the intrinsic tanh, but must be |
| 157 | + ! defined here so that we can use procedure |
| 158 | + ! pointer with it. |
| 159 | + real, intent(in) :: x(:,:,:) |
| 160 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 161 | + res = tanh(x) |
| 162 | + end function tanhf |
| 163 | + |
| 164 | + pure function tanh_prime(x) result(res) |
| 165 | + ! First derivative of the tanh activation function. |
| 166 | + real, intent(in) :: x(:,:,:) |
| 167 | + real :: res(size(x,1),size(x,2),size(x,3)) |
| 168 | + res = 1 - tanh(x)**2 |
| 169 | + end function tanh_prime |
| 170 | + |
| 171 | +end module nf_activation_3d |
0 commit comments