GCC Code Coverage Report


Directory: src/lib/
File: src/lib/mod_lr_decay.f90
Date: 2024-06-28 12:51:18
Exec Total Coverage
Lines: 37 37 100.0%
Functions: 0 0 -%
Branches: 10 10 100.0%

Line Branch Exec Source
1 !!!#############################################################################
2 !!! Code written by Ned Thaddeus Taylor
3 !!! Code part of the ATHENA library - a feedforward neural network library
4 !!!#############################################################################
5 !!! module contains learning rate decay types and procedures
6 !!! module contains the following derived types:
7 !!! - base_lr_decay_type - base learning rate decay type
8 !!! - exp_lr_decay_type - exponential learning rate decay type
9 !!! - step_lr_decay_type - step learning rate decay type
10 !!! - inv_lr_decay_type - inverse learning rate decay type
11 !!!##################
12 !!! <NAME>_lr_decay_type contains the following procedures:
13 !!! - get_lr - returns the updated learning rate
14 !!!##################
15 !!! module contains the following procedures:
16 !!! - setup_lr_decay_<NAME> - sets up the <NAME> learning rate decay type
17 !!!#############################################################################
18 module learning_rate_decay
19 use constants, only: real12
20 implicit none
21
22
23 type base_lr_decay_type
24 real(real12) :: initial_learning_rate
25 real(real12) :: decay_rate
26 contains
27 procedure :: get_lr => lr_decay_none
28 end type base_lr_decay_type
29
30 interface base_lr_decay_type
31 module function setup_lr_decay_base() result(lr_decay)
32 type(base_lr_decay_type) :: lr_decay
33 end function setup_lr_decay_base
34 end interface base_lr_decay_type
35
36 !!!-----------------------------------------------------------------------------
37
38 type, extends(base_lr_decay_type) :: exp_lr_decay_type
39 contains
40 procedure :: get_lr => lr_decay_exp
41 end type exp_lr_decay_type
42
43 interface exp_lr_decay_type
44 module function setup_lr_decay_exp(decay_rate) result(lr_decay)
45 real(real12), optional, intent(in) :: decay_rate
46 type(exp_lr_decay_type) :: lr_decay
47 end function setup_lr_decay_exp
48 end interface exp_lr_decay_type
49
50 !!!-----------------------------------------------------------------------------
51
52 type, extends(base_lr_decay_type) :: step_lr_decay_type
53 integer :: decay_steps
54 contains
55 procedure :: get_lr => lr_decay_step
56 end type step_lr_decay_type
57
58 interface step_lr_decay_type
59 module function setup_lr_decay_step(decay_rate, decay_steps) &
60 result(lr_decay)
61 real(real12), optional, intent(in) :: decay_rate
62 integer, optional, intent(in) :: decay_steps
63 type(step_lr_decay_type) :: lr_decay
64 end function setup_lr_decay_step
65 end interface step_lr_decay_type
66
67 !!!-----------------------------------------------------------------------------
68
69 type, extends(base_lr_decay_type) :: inv_lr_decay_type
70 real(real12) :: decay_power
71 contains
72 procedure :: get_lr => lr_decay_inv
73 end type inv_lr_decay_type
74
75 interface inv_lr_decay_type
76 module function setup_lr_decay_inv(decay_rate, decay_power) &
77 result(lr_decay)
78 real(real12), optional, intent(in) :: decay_rate, decay_power
79 type(inv_lr_decay_type) :: lr_decay
80 end function setup_lr_decay_inv
81 end interface inv_lr_decay_type
82
83 !!!-----------------------------------------------------------------------------
84
85
86 private
87
88 public :: base_lr_decay_type
89 public :: exp_lr_decay_type
90 public :: step_lr_decay_type
91 public :: inv_lr_decay_type
92
93
94 contains
95
96 !!!#############################################################################
97 !!! set up learning rate types
98 !!!#############################################################################
99 16 module function setup_lr_decay_base() result(lr_decay)
100 type(base_lr_decay_type) :: lr_decay
101
102 16 lr_decay%decay_rate = 0._real12
103
104 16 end function setup_lr_decay_base
105 !!!-----------------------------------------------------------------------------
106 2 module function setup_lr_decay_exp(decay_rate) result(lr_decay)
107 real(real12), optional, intent(in) :: decay_rate
108 type(exp_lr_decay_type) :: lr_decay
109
110
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(present(decay_rate))then
111 1 lr_decay%decay_rate = decay_rate
112 else
113 1 lr_decay%decay_rate = 0.9_real12
114 end if
115
116 2 end function setup_lr_decay_exp
117 !!!-----------------------------------------------------------------------------
118 2 module function setup_lr_decay_step(decay_rate, decay_steps) result(lr_decay)
119 real(real12), optional, intent(in) :: decay_rate
120 integer, optional, intent(in) :: decay_steps
121 type(step_lr_decay_type) :: lr_decay
122
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(present(decay_rate))then
124 1 lr_decay%decay_rate = decay_rate
125 else
126 1 lr_decay%decay_rate = 0.1_real12
127 end if
128
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(present(decay_steps))then
129 1 lr_decay%decay_steps = decay_steps
130 else
131 1 lr_decay%decay_steps = 100
132 end if
133
134 2 end function setup_lr_decay_step
135 !!!-----------------------------------------------------------------------------
136 2 module function setup_lr_decay_inv(decay_rate, decay_power) result(lr_decay)
137 real(real12), optional, intent(in) :: decay_rate, decay_power
138 type(inv_lr_decay_type) :: lr_decay
139
140
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(present(decay_rate))then
141 1 lr_decay%decay_rate = decay_rate
142 else
143 1 lr_decay%decay_rate = 0.001_real12
144 end if
145
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(present(decay_power))then
146 1 lr_decay%decay_power = decay_power
147 else
148 1 lr_decay%decay_power = 1._real12
149 end if
150
151 2 end function setup_lr_decay_inv
152 !!!#############################################################################
153
154
155 !!!#############################################################################
156 !!! learning rate decay procedures
157 !!!#############################################################################
158 515 pure function lr_decay_none(this, learning_rate, iteration) result(output)
159 implicit none
160 class(base_lr_decay_type), intent(in) :: this
161 real(real12), intent(in) :: learning_rate
162 integer, intent(in) :: iteration
163
164 real(real12) :: output
165
166 515 output = learning_rate
167
168 515 end function lr_decay_none
169 !!!-----------------------------------------------------------------------------
170 1 pure function lr_decay_exp(this, learning_rate, iteration) result(output)
171 implicit none
172 class(exp_lr_decay_type), intent(in) :: this
173 real(real12), intent(in) :: learning_rate
174 integer, intent(in) :: iteration
175
176 real(real12) :: output
177
178 1 output = learning_rate * exp(- iteration * this%decay_rate)
179
180 1 end function lr_decay_exp
181 !!!-----------------------------------------------------------------------------
182 1 pure function lr_decay_step(this, learning_rate, iteration) result(output)
183 implicit none
184 class(step_lr_decay_type), intent(in) :: this
185 real(real12), intent(in) :: learning_rate
186 integer, intent(in) :: iteration
187
188 real(real12) :: output
189
190 1 output = learning_rate * this%decay_rate ** (iteration / this%decay_steps)
191
192 1 end function lr_decay_step
193 !!!-----------------------------------------------------------------------------
194 1 pure function lr_decay_inv(this, learning_rate, iteration) result(output)
195 implicit none
196 class(inv_lr_decay_type), intent(in) :: this
197 real(real12), intent(in) :: learning_rate
198 integer, intent(in) :: iteration
199
200 real(real12) :: output
201
202 output = learning_rate * &
203 1 (1._real12 + this%decay_rate * iteration) ** (- this%decay_power)
204
205 1 end function lr_decay_inv
206 !!!#############################################################################
207
208 74 end module learning_rate_decay
209