abs
Author: Heli Qi Affiliation: NAIST Date: 2022.07
OptimScheduler
Bases: ABC
OptimScheduler is the base class of all OptimScheduler objects that combine the roles of traditional optimizers and schedulers together. Its main job is optimizing the target model parameters and scheduling the learning rate during training.
In this toolkit, we combine traditional optimizers and schedulers into a single class: OptimScheduler. Each
OptimScheduler object has one built-in member optimizer (torch.optim.Optimizer) which is initialized automatically
by optim_type
and optim_conf
given in your configuration.
Source code in speechain/optim_sche/abs.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
__init__(optim_type, optim_conf, model, distributed=False, optim_loss=None, updated_modules=None, step_per_update=1, use_amp=True, accum_grad=1, ft_factor=1.0, grad_clip=1.0, grad_norm_type=2.0, **sche_conf)
This initialization function initializes the general part shared by all
OptimScheduler subclasses. At the end of this function, an interface function
sche_init()
is called to initialize the customized part of each OptimScheduler
subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Model
|
speechain.model.abs.Model
The pointer to the model whose parameters will be optimized by the built-in |
required |
distributed
|
bool
|
bool = False Whether the model to be optimized is distributed to multiple GPUs. If True, gradient accumulation will be done asynchronously in the DDP mode to speed up training. |
False
|
use_amp
|
bool
|
bool = True
Whether the Automatic Mixed Precision (AMP) technique is used during back-propagation.
If True, a built-in |
True
|
accum_grad
|
int
|
int = 1 The number of steps to accumulate gradients before optimization. The larger this argument is, the larger your virtual batches will be. |
1
|
ft_factor
|
float
|
float = 1.0 The finetuning factor used to scale down the learning rates during training. |
1.0
|
optim_type
|
str
|
str
The optimizer query used to pick up the target Optimizer subclass from |
required |
optim_conf
|
Dict[str, Any]
|
Dict The optimizer configuration used to initialize the optimizer |
required |
optim_loss
|
str
|
str = None
The name of the target loss used in this OptimScheduler object to calculate the gradients.
If not given, the loss named |
None
|
updated_modules
|
List[str]
|
str or List[str] = None
This argument allows you to update only a part of parameters of the built-in model pointer.
|
None
|
step_per_update
|
int
|
int = 1
The optimization interval for the built-in optimizer.
It means that the parameter optimization will be done once every |
1
|
**sche_conf
|
The arguments used to initialize the customized part of this OptimScheduler. Mainly used to decide the learning rate scheduling strategy. |
{}
|
Source code in speechain/optim_sche/abs.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
__repr__()
This function returns the description string of the OptimScheduler object. There is a general description part shared by all the OptimScheduler subclasses.
In this function, an interface hook function extra_repr_fn()
will be called to generate the specific
description part of each OptimScheduler subclass.
str
Type | Description |
---|---|
The description string for the OptimScheduler object. |
Source code in speechain/optim_sche/abs.py
extra_repr_fn()
This interface hook function returns the specific part of the description string of the OptimScheduler object. The original implementation in the base class returns an empty string.
In principle, this interface hook function must be overridden by each OptimScheduler subclass. But there won't be any errors if you don't override it in your implementation.
str
Type | Description |
---|---|
str
|
The specific part of the description string of the OptimScheduler object. |
Source code in speechain/optim_sche/abs.py
get_lr()
This function returns the current learning rate of the built-in
torch.optim.Optimizer
member.
float
Type | Description |
---|---|
The value of the learning rates obtained from |
Source code in speechain/optim_sche/abs.py
load_state_dict(state_dict)
This function loads the existing checkpoint information into the OptimScheduler object as the starting status.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_dict
|
Dict[str, Any]
|
Dict The status information loaded from the existing checkpoint. |
required |
Source code in speechain/optim_sche/abs.py
sche_init(**sche_conf)
abstractmethod
This abstract interface function is the customized initialization function which decides how the learning rate is scheduled as the training goes. This interface is mandatory to be overridden.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**sche_conf
|
Dict
The arguments used to initialize the customized part of this OptimScheduler.
For more details about the learning rate scheduling strategy, please refer to the docstring of
|
{}
|
Source code in speechain/optim_sche/abs.py
state_dict()
This function returns the current status of the OptimScheduler object for checkpoint storage.
Dict
Type | Description |
---|---|
Dict
|
The status Dict containing the current status of the built-in |
Dict
|
|
Source code in speechain/optim_sche/abs.py
step(losses, time_func, optim_name, step_num, epoch_num, logger=None)
This function optimizes the target parameters of the built-in model pointer with the input training losses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
losses
|
Dict[str, Tensor]
|
Dict[str, torch.Tensor]
The training loss Dict received from the |
required |
time_func
|
The context function used to record the consumed time during gradient back-propagation and parameter optimization. |
required | |
optim_name
|
str
|
str The name of the OptimScheduler object. This argument is used to identify the recorded consumed time information. |
required |
step_num
|
int
|
int
The number of the current training step.
This argument is used to update the learning rate for the current step by |
required |
logger
|
Lazily passed logger object. Used to record logging information during optimization. |
None
|
Source code in speechain/optim_sche/abs.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
update_lr(real_step, epoch_num)
abstractmethod
This abstract interface function generates the learning rate by the input step number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
real_step
|
int
|
int
The number of the real step for parameter optimization. Due to the existence of |
required |
float
Type | Description |
---|---|
float
|
The learning rate used for parameter optimization in the current training step. |