Skip to content

Commit 21851d0

Browse files
committed
add selu activation
1 parent 5d0419e commit 21851d0

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/stdlib_specialfunctions.fypp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,32 @@ module stdlib_specialfunctions
163163
end interface
164164
public :: gelu_approx_grad
165165

166+
interface selu
167+
!! Version: experimental
168+
!!
169+
!! Scaled Exponential Linear Unit
170+
#:for rk, rt in REAL_KINDS_TYPES
171+
elemental module function selu_${rk}$( x ) result( y )
172+
${rt}$, intent(in) :: x
173+
${rt}$ :: y
174+
end function
175+
#:endfor
176+
end interface
177+
public :: selu
178+
179+
interface selu_grad
180+
!! Version: experimental
181+
!!
182+
!! Scaled Exponential Linear Unit
183+
#:for rk, rt in REAL_KINDS_TYPES
184+
elemental module function selu_grad_${rk}$( x ) result( y )
185+
${rt}$, intent(in) :: x
186+
${rt}$ :: y
187+
end function
188+
#:endfor
189+
end interface
190+
public :: selu_grad
191+
166192
interface sigmoid
167193
!! Version: experimental
168194
!!

src/stdlib_specialfunctions_activations.fypp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ end function
112112

113113
#:endfor
114114

115+
!==================================================
116+
! Scaled Exponential Linear Unit (SELU)
117+
!==================================================
118+
#:for rk, rt in REAL_KINDS_TYPES
119+
elemental module function selu_${rk}$( x ) result( y )
120+
${rt}$, intent(in) :: x
121+
${rt}$ :: y
122+
${rt}$, parameter :: scale = 1.0507009873554804934193349852946_${rk}$
123+
${rt}$, parameter :: alpha = 1.6732632423543772848170429916717_${rk}$
124+
if(x > 0._${rk}$)then
125+
y = scale * x
126+
else
127+
y = scale * (alpha * exp(x) - alpha)
128+
end if
129+
end function
130+
131+
elemental module function selu_grad_${rk}$( x ) result( y )
132+
${rt}$, intent(in) :: x
133+
${rt}$ :: y
134+
${rt}$, parameter :: scale = 1.0507009873554804934193349852946_${rk}$
135+
${rt}$, parameter :: alpha = 1.6732632423543772848170429916717_${rk}$
136+
if(x > 0._${rk}$)then
137+
y = scale
138+
else
139+
y = scale * alpha * exp(x)
140+
end if
141+
end function
142+
143+
#:endfor
144+
115145
!==================================================
116146
! Sigmoid
117147
!==================================================

0 commit comments

Comments
 (0)