GCC Code Coverage Report


Directory: src/athena/
File: athena_activation_gaussian.f90
Date: 2025-12-10 07:37:07
Exec Total Coverage
Lines: 0 0 100.0%
Functions: 0 0 -%
Branches: 0 0 -%

Line Branch Exec Source
1 module athena__activation_gaussian
2 !! Module containing implementation of the Gaussian activation function
3 !!
4 !! This module implements the Gaussian (bell curve) activation,
5 !! which produces maximum activation at the origin.
6 !!
7 !! Mathematical operation:
8 !! \[ f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right) \]
9 !!
10 !! Derivative:
11 !! \[ f'(x) = -\frac{x-\mu}{\sigma^3\sqrt{2\pi}} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right) \]
12 !!
13 !! Properties: Smooth, symmetric, max at \(x=\mu\)
14 !! Used in Radial Basis Function (RBF) networks
15 !! Responds strongly to inputs near \(\mu\)
16 use coreutils, only: real32, print_warning
17 use diffstruc, only: array_type, gaussian, operator(*)
18 use athena__misc_types, only: base_actv_type
19 use athena__misc_types, only: onnx_attribute_type
20 implicit none
21
22
23 private
24
25 public :: gaussian_actv_type, create_from_onnx_gaussian_activation
26
27
28 type, extends(base_actv_type) :: gaussian_actv_type
29 !! Type for Gaussian activation function with overloaded procedures
30 real(real32) :: sigma
31 !! Standard deviation parameter for Gaussian function
32 real(real32) :: mu
33 !! Mean parameter for Gaussian function
34 contains
35 procedure, pass(this) :: apply => apply_gaussian
36 procedure, pass(this) :: reset => reset_gaussian
37 procedure, pass(this) :: apply_attributes => apply_attributes_gaussian
38 procedure, pass(this) :: export_attributes => export_attributes_gaussian
39 end type gaussian_actv_type
40
41 interface gaussian_actv_type
42 procedure initialise
43 end interface gaussian_actv_type
44
45
46
47 contains
48
49 !###############################################################################
50 function initialise(scale, sigma, mu, attributes) result(activation)
51 !! Initialise a Gaussian activation function
52 implicit none
53
54 ! Arguments
55 real(real32), intent(in), optional :: scale
56 !! Optional scale factor for activation output
57 real(real32), intent(in), optional :: sigma
58 !! Optional standard deviation parameter
59 real(real32), intent(in), optional :: mu
60 !! Optional mean parameter
61 type(onnx_attribute_type), dimension(:), intent(in), optional :: attributes
62 !! Optional array of ONNX attributes
63 type(gaussian_actv_type) :: activation
64 !! Gaussian activation type
65
66
67 call activation%reset()
68
69 if(present(scale)) activation%scale = scale
70 if(abs(activation%scale-1._real32) .gt. 1.e-6_real32)then
71 activation%apply_scaling = .true.
72 end if
73
74 if(present(sigma)) activation%sigma = sigma
75 if(present(mu)) activation%mu = mu
76
77 if(present(attributes)) then
78 call activation%apply_attributes(attributes)
79 end if
80
81 end function initialise
82 !-------------------------------------------------------------------------------
83 pure subroutine reset_gaussian(this)
84 !! Reset Gaussian activation function attributes and variables
85 implicit none
86
87 ! Arguments
88 class(gaussian_actv_type), intent(inout) :: this
89 !! Gaussian activation type
90
91 this%name = "gaussian"
92 this%scale = 1._real32
93 this%threshold = 0._real32
94 this%apply_scaling = .false.
95 this%sigma = 1.5_real32
96 this%mu = 0._real32
97
98 end subroutine reset_gaussian
99 !-------------------------------------------------------------------------------
100 function create_from_onnx_gaussian_activation(attributes) result(activation)
101 !! Create Gaussian activation function from ONNX attributes
102 implicit none
103
104 ! Arguments
105 type(onnx_attribute_type), dimension(:), intent(in) :: attributes
106 !! Array of ONNX attributes
107
108 class(base_actv_type), allocatable :: activation
109 !! Instance of activation type
110
111 allocate(activation, source = gaussian_actv_type(attributes = attributes))
112
113 end function create_from_onnx_gaussian_activation
114 !###############################################################################
115
116
117 !###############################################################################
118 subroutine apply_attributes_gaussian(this, attributes)
119 !! Load ONNX attributes into Gaussian activation function
120 implicit none
121
122 ! Arguments
123 class(gaussian_actv_type), intent(inout) :: this
124 !! Gaussian activation type
125 type(onnx_attribute_type), dimension(:), intent(in) :: attributes
126 !! Array of ONNX attributes
127
128 ! Local variables
129 integer :: i
130 !! Loop variable
131
132 ! Load provided attributes
133 do i=1, size(attributes,dim=1)
134 select case(trim(attributes(i)%name))
135 case("scale")
136 read(attributes(i)%val,*) this%scale
137 if(abs(this%scale-1._real32) .gt. 1.e-6_real32)then
138 this%apply_scaling = .true.
139 else
140 this%apply_scaling = .false.
141 end if
142 case("sigma")
143 read(attributes(i)%val,*) this%sigma
144 case("mu")
145 read(attributes(i)%val,*) this%mu
146 case("name")
147 if(trim(attributes(i)%val) .ne. trim(this%name)) then
148 call print_warning( &
149 'Gaussian activation: name attribute "' // &
150 trim(attributes(i)%val) // &
151 '"" does not match expected "' // trim(this%name)//'"' &
152 )
153
154 end if
155 case default
156 call print_warning( &
157 'Gaussian activation: unknown attribute '// &
158 trim(attributes(i)%name) &
159 )
160 end select
161 end do
162
163 end subroutine apply_attributes_gaussian
164 !###############################################################################
165
166
167 !###############################################################################
168 pure function export_attributes_gaussian(this) result(attributes)
169 !! Export Gaussian activation function attributes as ONNX attributes
170 implicit none
171
172 ! Arguments
173 class(gaussian_actv_type), intent(in) :: this
174 !! Gaussian activation type
175 type(onnx_attribute_type), allocatable, dimension(:) :: attributes
176 !! Array of ONNX attributes
177
178 ! Local variables
179 character(50) :: buffer
180 !! Temporary string buffer
181
182 allocate(attributes(4))
183
184 write(buffer, '(A)') this%name
185 attributes(1) = onnx_attribute_type( &
186 "name", "string", trim(adjustl(buffer)) )
187
188 write(buffer, '(F10.6)') this%scale
189 attributes(2) = onnx_attribute_type( &
190 "scale", "float", trim(adjustl(buffer)) )
191
192 write(buffer, '(F10.6)') this%sigma
193 attributes(3) = onnx_attribute_type( &
194 "sigma", "float", trim(adjustl(buffer)) )
195
196 write(buffer, '(F10.6)') this%mu
197 attributes(4) = onnx_attribute_type( &
198 "mu", "float", trim(adjustl(buffer)) )
199
200 end function export_attributes_gaussian
201 !###############################################################################
202
203
204 !###############################################################################
205 function apply_gaussian(this, val) result(output)
206 !! Apply Gaussian activation to array
207 !!
208 !! Applies the Gaussian function element-wise to input array:
209 !! f = exp(-x^2/(2σ^2))/(σ√(2π))
210 implicit none
211
212 ! Arguments
213 class(gaussian_actv_type), intent(in) :: this
214 !! Gaussian activation type containing sigma parameter
215 type(array_type), intent(in) :: val
216 !! Input values
217 type(array_type), pointer :: output
218 !! Gaussian activated output values
219
220 if(this%apply_scaling)then
221 output => gaussian(val, this%mu, this%sigma) * this%scale
222 else
223 output => gaussian(val, this%mu, this%sigma)
224 end if
225
226 end function apply_gaussian
227 !###############################################################################
228
229 end module athena__activation_gaussian
230