Closed
Description
Some activation functions like leaky ReLU (#123) require one or more additional parameters.
To allow passing activation functions as procedure pointers, all functions must have the same interface. A proposed general solution (thank @jvdp1) is to:
- Define a derived type
activation_params
or similar that defines any possible extra parameters that may be needed by activation functions; set default values of activation parameters in the type definition.
type :: activation_params
real :: alpha = 0.3
end type activation_params
- Make each activation function have a
type(activation_params), intent(in) optional :: params
dummy argument (instead of the currentalpha
). Inside activation function definitions, function that use one or more activation parameters access them directly; those that don't simply ignore it.
pure function leaky_relu(x, params) result(res)
!! Leaky Rectified Linear Unit (Leaky ReLU) activation function.
real, intent(in) :: x(:)
type(activation_params), intent(in), optional :: params
real :: res(size(x))
res = max(params % alpha * x, x)
end function leaky_relu
- Make
activation_params
an attribute ofdense
andconv2d
layers (and later any other layers that activate). - Pass the attributes to the activation procedure associated with the layer.