Commit 296713ea authored by Martin Beseda's avatar Martin Beseda
Browse files

ENH: Added classes for neurons with tanh and arctan activation functions.

parent 9479e6a1
#define SINGLE 4
#define DOUBLE 8
# define PREC DOUBLE
module data_kinds_4neuro_m
integer, parameter :: integer_4neuro = 8
integer, parameter :: real_4neuro = 8
integer, parameter :: integer_4neuro = PREC
integer, parameter :: real_4neuro = PREC
end module data_kinds_4neuro_m
......@@ -99,7 +99,6 @@ module neuron_m
real(kind=real_4neuro) :: threshold !< When neuron potential exceeds this value, neuron becomes excited
contains
!> Activation function - transforms potential into the output state value
......@@ -132,8 +131,8 @@ module neuron_m
private
!! Coefficients for the linear activation function in format 'f(x)=a*x + b'
real(kind=real_4neuro) :: a_coef !> The coefficient 'a' in the activation function f(x)=a*x + b
real(kind=real_4neuro) :: b_coef !> The coefficient 'b' in the activation function f(x)=a*x + b
real(kind=real_4neuro) :: a_coef !< The coefficient 'a' in the activation function f(x)=a*x + b
real(kind=real_4neuro) :: b_coef !< The coefficient 'b' in the activation function f(x)=a*x + b
contains
......@@ -163,7 +162,7 @@ module neuron_m
type, extends(neuron_t) :: logistic_neuron_t
private
real(kind=real_4neuro) :: alpha_coef
real(kind=real_4neuro) :: alpha_coef !< The alpha coefficient used in the activation function
contains
......@@ -183,6 +182,38 @@ module neuron_m
module procedure :: new_logistic_neuron_1
end interface logistic_neuron_t
!---------------------!------------------------------------------------------------------------
! class tanh_neuron_t !
!---------------------!
!> Hyperbolic tangent neuron class - uses f(x) = (e^x - e^(-x))/(e^x + e^(-x)) as
!! an activation function, 'x' being the neuron potential.
type, extends(neuron_t) :: tanh_neuron_t
contains
!> Activation function - hyperbolic tangent
procedure, private :: activate => hyperbolic_tangent_activate_impl
end type tanh_neuron_t
interface tanh_neuron_t
!> Constructor for an instance of the class tanh_neuron_t
module procedure :: new_tanh_neuron
end interface tanh_neuron_t
!> Arcus tangents neuron class - uses f(x)=tan^(-1)(x) as an
!! activation function, 'x' being the neuron potential
type, extends(neuron_t) :: arctan_neuron_t
contains
!> Activation function - arcus tangens
procedure, private :: activate => arcus_tangens_activate_impl
end type arctan_neuron_t
interface arctan_neuron_t
!> Constructor for an instance of the class arctan_neuron_t
module procedure :: new_arctan_neuron
end interface arctan_neuron_t
contains
!------------------------!---------------------------------------------------------------------
! Method implementations !
......@@ -567,5 +598,98 @@ module neuron_m
#endif
end subroutine logistic_activate_impl
!---------------------!
! class tanh_neuron_t !
!---------------------!
!--------------!------------------------------------------------------------------------------
! Constructors !
!--------------!
!> Constructor for an instance of the class tanh_neuron_t
function new_tanh_neuron() result(new_obj)
class(tanh_neuron_t), pointer :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'new_tanh_neuron')
#endif
end function new_tanh_neuron
!----------------!----------------------------------------------------------------------------
! Common methods !
!----------------!
!> Activation function - hyperbolic tangent
subroutine hyperbolic_tangent_activate_impl(this)
class(tanh_neuron_t), intent(inout) :: this
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
#if PREC == DOUBLE
this%state = dtanh(this%potential)
#else
this%state = tanh(this%potential)
#endif
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'hyperbolic_tangent_activate_impl')
#endif
end subroutine hyperbolic_tangent_activate_impl
!-----------------------!
! class arctan_neuron_t !
!-----------------------!
!--------------!------------------------------------------------------------------------------
! Constructors !
!--------------!
!> Constructor for an instance of the class arctan_neuron_t
function new_arctan_neuron() result(new_obj)
class(arctan_neuron_t), pointer :: new_obj
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
allocate(new_obj)
call new_obj%init_components()
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'new_arctan_neuron')
#endif
end function new_arctan_neuron
!----------------!----------------------------------------------------------------------------
! Common methods !
!----------------!
!> Activation function - arcus tangens
subroutine arcus_tangens_activate_impl(this)
class(arctan_neuron_t), intent(inout) :: this
#ifdef TIME_PROFILING
real :: start_time
call time_profiling_start(start_time)
#endif
#if PREC == DOUBLE
this%state = datan(this%potential)
#else
this%state = atan(this%potential)
#endif
#ifdef TIME_PROFILING
call time_profiling_stop(start_time, 'arcus_tangens_activate_impl')
#endif
end subroutine arcus_tangens_activate_impl
end module neuron_m
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment