Optimizers
create_optimizer(model, optimizer_name, lr=0.001, weight_decay=0.0, wd_ban_list=('bias', 'LayerNorm.bias', 'LayerNorm.weight'), use_lookahead=False, use_orthograd=False, **kwargs)
Build optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
nn.Module. model. |
required |
optimizer_name
|
str
|
str. name of optimizer. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
weight_decay
|
float
|
float. weight decay. |
0.0
|
wd_ban_list
|
List[str]
|
List[str]. weight decay ban list by layer. |
('bias', 'LayerNorm.bias', 'LayerNorm.weight')
|
use_lookahead
|
bool
|
bool. use Lookahead. |
False
|
use_orthograd
|
bool
|
bool. use OrthoGrad. |
False
|
Source code in pytorch_optimizer/optimizer/__init__.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
get_optimizer_parameters(model_or_parameter, weight_decay, wd_ban_list=('bias', 'LayerNorm.bias', 'LayerNorm.weight'))
Get optimizer parameters while filtering specified modules.
Notice that, You can also ban by a module name level (e.g. LayerNorm) if you pass nn.Module instance. You just only
need to input LayerNorm to exclude weight decay from the layer norm layer(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_or_parameter
|
Union[Module, List]
|
Union[nn.Module, List]. model or parameters. |
required |
weight_decay
|
float
|
float. weight_decay. |
required |
wd_ban_list
|
List[str]
|
List[str]. ban list not to set weight decay. |
('bias', 'LayerNorm.bias', 'LayerNorm.weight')
|
Returns:
| Type | Description |
|---|---|
PARAMETERS
|
PARAMETERS. new parameter list. |
Source code in pytorch_optimizer/optimizer/__init__.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | |
A2Grad
Bases: BaseOptimizer
Optimal Adaptive and Accelerated Stochastic Gradient Descent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
Optional[float]
|
Optional[float]. learning rate. no needed. |
None
|
beta
|
float
|
float. beta. |
10.0
|
lips
|
float
|
float. Lipschitz constant. |
10.0
|
rho
|
float
|
float. represents the degree of weighting decrease, a constant smoothing factor between 0 and 1. |
0.5
|
variant
|
VARIANTS
|
str. variant of A2Grad optimizer. 'uni', 'inc', 'exp'. |
'uni'
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/a2grad.py
13 14 15 16 17 18 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 | |
AdaBelief
Bases: BaseOptimizer
Adapting Step-sizes by the Belief in Observed Gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
rectify
|
bool
|
bool. perform the rectified update similar to RAdam. |
False
|
n_sma_threshold
|
int
|
number of SMA threshold (recommended is 5). |
5
|
degenerated_to_sgd
|
bool
|
bool. perform SGD update when variance of gradient is high. |
True
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-16
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adabelief.py
10 11 12 13 14 15 16 17 18 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 | |
AdaBound
Bases: BaseOptimizer
Adaptive Gradient Methods with Dynamic Bound of Learning Rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
final_lr
|
float
|
float. final learning rate. |
0.1
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
gamma
|
float
|
float. convergence speed of the bound functions. |
0.001
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adabound.py
11 12 13 14 15 16 17 18 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 | |
AdaDelta
Bases: BaseOptimizer
An Adaptive Learning Rate Method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
rho
|
float
|
float. coefficient used for computing a running average of squared gradients. |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adadelta.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
AdaFactor
Bases: BaseOptimizer
Adaptive Learning Rates with Sublinear Memory Cost with some tweaks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
Optional[float]
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. if beta1 is None, first momentum will be skipped. |
(0.9, 0.999)
|
decay_rate
|
float
|
float. coefficient used to compute running averages of square gradient. |
-0.8
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
clip_threshold
|
float
|
float. threshold of root-mean-square of final gradient update. |
1.0
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
scale_parameter
|
bool
|
bool. if true, learning rate is scaled by root-mean-square of parameter. |
True
|
relative_step
|
bool
|
bool. if true, time-dependent learning rate is computed instead of external learning rate. |
True
|
warmup_init
|
bool
|
bool. time-dependent learning rate computation depends on whether warm-up initialization is being used. |
False
|
eps1
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-30
|
eps2
|
float
|
float. term added to the denominator to improve numerical stability. |
0.001
|
momentum_dtype
|
dtype
|
torch.dtype. type of momentum variable. In VIT paper observed that storing momentum in half-precision (bfloat16 type) does not affect training dynamics and has no effect on the outcome while reducing optimize overhead from 2-fold to 1.5-fold. |
bfloat16
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adafactor.py
11 12 13 14 15 16 17 18 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 | |
get_lr(lr, step, rms, relative_step, warmup_init, scale_parameter)
Get AdaFactor learning rate.
Source code in pytorch_optimizer/optimizer/adafactor.py
125 126 127 128 129 130 131 132 133 134 135 136 | |
get_options(shape)
staticmethod
Get factored.
Source code in pytorch_optimizer/optimizer/adafactor.py
138 139 140 141 | |
AdaGC
Bases: BaseOptimizer
Improving Training Stability for Large Language Model Pretraining.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
beta
|
float
|
float. smoothing coefficient for EMA. |
0.98
|
lambda_abs
|
float
|
float. absolute clipping threshold to prevent unstable updates from gradient explosions. |
1.0
|
lambda_rel
|
float
|
float. relative clipping threshold to prevent unstable updates from gradient explosions. |
1.05
|
warmup_steps
|
int
|
int. warmup steps. |
100
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.1
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adagc.py
11 12 13 14 15 16 17 18 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 | |
AdaHessian
Bases: BaseOptimizer
An Adaptive Second Order Optimizer for Machine Learning.
Requires `loss.backward(create_graph=True)` in order to calculate hessians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.1
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
hessian_power
|
float
|
float. exponent of the hessian trace. |
1.0
|
update_period
|
int
|
int. number of steps after which to apply hessian approximation. |
1
|
num_samples
|
int
|
int. times to sample |
1
|
hessian_distribution
|
HUTCHINSON_G
|
HUTCHINSON_G. type of distribution to initialize hessian. |
'rademacher'
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-16
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adahessian.py
10 11 12 13 14 15 16 17 18 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 | |
AdaLOMO
Bases: BaseOptimizer
Low-memory Optimization with Adaptive Learning Rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
nn.Module. pytorch model. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
loss_scale
|
float
|
float. loss scale. |
2.0 ** 10
|
clip_threshold
|
float
|
float. threshold of root-mean-square of final gradient update. |
1.0
|
decay_rate
|
float
|
float. coefficient used to compute running averages of square gradient. |
-0.8
|
clip_grad_norm
|
Optional[float]
|
Optional[float]. clip grad norm. |
None
|
clip_grad_value
|
Optional[float]
|
Optional[float]. clip grad value. |
None
|
eps1
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-30
|
eps2
|
float
|
float. term added to the denominator to improve numerical stability. |
0.001
|
Source code in pytorch_optimizer/optimizer/lomo.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
Adai
Bases: BaseOptimizer
Disentangling the Effects of Adaptive Learning Rate and Momentum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.1, 0.99)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
stable_weight_decay
|
bool
|
bool. perform stable weight decay. |
False
|
dampening
|
float
|
float. dampening for momentum. where dampening < 1, it will show some adaptive-moment behavior. |
1.0
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
0.001
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adai.py
11 12 13 14 15 16 17 18 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 | |
Adalite
Bases: BaseOptimizer
Adalite optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
g_norm_min
|
float
|
float. |
1e-10
|
ratio_min
|
float
|
float. |
0.0001
|
tau
|
float
|
float. |
1.0
|
eps1
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
eps2
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-10
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adalite.py
9 10 11 12 13 14 15 16 17 18 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 | |
AdamMini
Bases: BaseOptimizer
Use Fewer Learning Rates To Gain More.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
nn.Module. model instance. |
required |
model_sharding
|
bool
|
bool. set to True if you are using model parallelism with more than 1 GPU, including FSDP and zero_1, 2, 3 in Deepspeed. Set to False if otherwise. |
False
|
lr
|
float
|
float. learning rate. |
1.0
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.1
|
num_embeds
|
int
|
int. number of embedding dimensions. could be unspecified if you are training non-transformer models. |
2048
|
num_heads
|
int
|
int. number of attention heads. could be unspecified if you are training non-transformer models. |
32
|
num_query_groups
|
Optional[int]
|
Optional[int]. number of query groups in Group Query Attention (GQA). if not specified, it will be equal to num_heads. could be unspecified if you are training non-transformer models. |
None
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adam_mini.py
13 14 15 16 17 18 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 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
AdaMax
Bases: BaseOptimizer
An Adaptive and Momental Bound Method for Stochastic Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamax.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
AdamC
Bases: BaseOptimizer
Why Gradients Rapidly Increase Near the End of Training.
Set normalized=True for LayerNorm and BatchNorm layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamc.py
10 11 12 13 14 15 16 17 18 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 | |
AdamG
Bases: BaseOptimizer
Towards Stability of Parameter-free Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.95, 0.999, 0.95)
|
p
|
float
|
float. p for a numerator function |
0.2
|
q
|
float
|
float. q for a numerator function |
0.24
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamg.py
10 11 12 13 14 15 16 17 18 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 | |
s(p)
Numerator function f(x) = p * x^q.
Source code in pytorch_optimizer/optimizer/adamg.py
81 82 83 | |
AdaMod
Bases: BaseOptimizer
An Adaptive and Momental Bound Method for Stochastic Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. beta3 is for smoothing coefficient for adaptive learning rates. |
(0.9, 0.99, 0.9999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamod.py
10 11 12 13 14 15 16 17 18 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 | |
AdamP
Bases: BaseOptimizer
Slowing Down the Slowdown for Momentum Optimizers on Scale-invariant Weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
delta
|
float
|
float. threshold that determines whether a set of parameters is scale invariant or not. |
0.1
|
wd_ratio
|
float
|
float. relative weight decay applied on scale-invariant parameters compared to that applied on scale-variant parameters. |
0.1
|
nesterov
|
bool
|
bool. enables Nesterov momentum. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamp.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
AdamS
Bases: BaseOptimizer
Adam with stable weight decay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0001
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adams.py
10 11 12 13 14 15 16 17 18 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 | |
AdamWSN
Bases: BaseOptimizer
Lean and Mean Adaptive Optimization via Subset-Norm and Subspace-Momentum with Convergence Guarantees.
.. code-block:: python
sn_params = [module.weight for module in model.modules() if isinstance(module, nn.Linear)]
sn_param_ids = [id(p) for p in sn_params]
regular_params = [p for p in model.parameters() if id(p) not in sn_param_ids]
param_groups = [{'params': regular_params, 'sn': False}, {'params': sn_params, 'sn': True}]
optimizer = AdamWSN(param_groups, lr=args.lr, weight_decay=args.weight_decay, subset_size=args.subset_size)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
subset_size
|
int
|
int. If you do not know what subset_size to set, a good rule of thumb is to set it as d/2 where d is the hidden dimension of your transformer model. For example, the hidden dimension is 4096 for Llama 7B and so a good subset_size could be 2048. You can leave the subset_size argument to its default value of -1 to use the recommended subset size as stated above. |
-1
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/snsm.py
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 | |
Adan
Bases: BaseOptimizer
Adaptive Nesterov Momentum Algorithm for Faster Optimizing Deep Models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.98, 0.92, 0.99)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. decoupled weight decay. |
False
|
max_grad_norm
|
float
|
float. max gradient norm to clip. |
0.0
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adan.py
13 14 15 16 17 18 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 | |
AdaNorm
Bases: BaseOptimizer
Symbolic Discovery of Optimization Algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.99)
|
r
|
float
|
float. EMA factor. between 0.9 ~ 0.99 is preferred. |
0.95
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adanorm.py
10 11 12 13 14 15 16 17 18 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 | |
AdaPNM
Bases: BaseOptimizer
Adam + Positive-Negative Momentum Optimizers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999, 1.0)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. use weight_decouple. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
ams_bound
|
bool
|
bool. whether to use the ams_bound variant. |
True
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adapnm.py
10 11 12 13 14 15 16 17 18 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 | |
AdaShift
Bases: BaseOptimizer
Decorrelation and Convergence of Adaptive Learning Rate Methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
keep_num
|
int
|
int. number of gradients used to compute first moment estimation. |
10
|
reduce_func
|
Optional[Callable]
|
Optional[Callable]. function applied to squared gradients to further reduce the correlation. If None, no function is applied. |
max
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-10
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adashift.py
11 12 13 14 15 16 17 18 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 | |
AdaSmooth
Bases: BaseOptimizer
An Adaptive Learning Rate Method based on Effective Ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.5, 0.99)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adasmooth.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
AdEMAMix
Bases: BaseOptimizer
Better, Faster, Older.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999, 0.9999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
alpha
|
float
|
float. usually between 4 and 10 would work well. |
5.0
|
t_alpha_beta3
|
Optional[float]
|
Optional[float]. total number of iterations is preferred when needed. |
None
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/ademamix.py
11 12 13 14 15 16 17 18 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 | |
SimplifiedAdEMAMix
Bases: BaseOptimizer
Connections between Schedule-Free Optimizers, AdEMAMix, and Accelerated SGD Variants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.0001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.99, 0.95)
|
alpha
|
float
|
float. coefficient for mixing the current gradient and EMA. |
0.0
|
beta1_warmup
|
Optional[int]
|
Optional[int]. number of warmup steps used to increase beta1. |
None
|
min_beta1
|
float
|
float. minimum value of beta1 to start from. |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/ademamix.py
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 | |
ADOPT
Bases: BaseOptimizer
Modified Adam Can Converge with Any β2 with the Optimal Rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.9999)
|
clip_lambda
|
Callable[[float], float]
|
Callable[[float], float]. function to clip gradient. default is |
lambda step: pow(step, 0.25)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adopt.py
11 12 13 14 15 16 17 18 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 | |
agc
agc(p, grad, agc_eps=0.001, agc_clip_val=0.01, eps=1e-06)
Clip gradient values in excess of the unit wise norm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Tensor
|
torch.Tensor. parameter. |
required |
grad
|
Tensor
|
torch.Tensor, gradient. |
required |
agc_eps
|
float
|
float. agc epsilon to clip the norm of parameter. |
0.001
|
agc_clip_val
|
float
|
float. norm clip. |
0.01
|
eps
|
float
|
float. simple stop from div by zero and no relation to standard optimizer eps. |
1e-06
|
Source code in pytorch_optimizer/optimizer/agc.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
AggMo
Bases: BaseOptimizer
Aggregated Momentum: Stability Through Passive Damping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.0, 0.9, 0.99)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/aggmo.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
Aida
Bases: BaseOptimizer
A DNN Optimizer that Improves over AdaBelief by Suppression of the Adaptive Stepsize Range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
k
|
int
|
int. number of vector projected per iteration. |
2
|
xi
|
float
|
float. term used in vector projections to avoid division by zero. |
1e-20
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
rectify
|
bool
|
bool. perform the rectified update similar to RAdam. |
False
|
n_sma_threshold
|
int
|
number of SMA threshold (recommended is 5). |
5
|
degenerated_to_sgd
|
bool
|
bool. perform SGD update when variance of gradient is high. |
True
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/aida.py
10 11 12 13 14 15 16 17 18 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 | |
Alice
Bases: BaseOptimizer
Adaptive low-dimensional subspace estimation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.02
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. beta3=0 for Alice-0 optimizer. |
(0.9, 0.9, 0.999)
|
alpha
|
float
|
float. scaler. |
0.3
|
alpha_c
|
float
|
float. compensation scaler. |
0.4
|
update_interval
|
int
|
int. update interval. |
200
|
rank
|
int
|
int. rank. |
256
|
gamma
|
float
|
limiter threshold. |
1.01
|
leading_basis
|
int
|
int. leading basis. |
40
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/racs.py
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 340 341 342 343 344 345 | |
subspace_iteration(a, mat, num_steps=1)
staticmethod
Perform subspace iteration.
Source code in pytorch_optimizer/optimizer/racs.py
215 216 217 218 219 220 221 222 223 224 | |
AliG
Bases: BaseOptimizer
Adaptive Learning Rates for Interpolation with Gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
max_lr
|
Optional[float]
|
Optional[float]. max learning rate. |
None
|
projection_fn
|
Optional[Callable]
|
Callable. projection function to enforce constraints. |
None
|
momentum
|
float
|
float. momentum. |
0.0
|
adjusted_momentum
|
bool
|
bool. if True, use pytorch-like momentum, instead of standard Nesterov momentum. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/alig.py
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 | |
compute_step_size(loss)
Compute step_size.
Source code in pytorch_optimizer/optimizer/alig.py
74 75 76 77 78 79 80 | |
Amos
Bases: BaseOptimizer
An Adam-style Optimizer with Adaptive Weight Decay towards Model-Oriented Scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
beta
|
float
|
float. A float slightly < 1. We recommend setting |
0.999
|
momentum
|
float
|
float. Exponential decay rate for optional moving average of updates. |
0.0
|
extra_l2
|
float
|
float. Additional L2 regularization. |
0.0
|
c_coef
|
float
|
float. Coefficient for decay_factor_c. |
0.25
|
d_coef
|
float
|
float. Coefficient for decay_factor_d. |
0.25
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-18
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/amos.py
10 11 12 13 14 15 16 17 18 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 | |
get_scale(p)
staticmethod
Get expected scale for model weights.
Source code in pytorch_optimizer/optimizer/amos.py
78 79 80 81 82 83 84 85 | |
APOLLO
Bases: BaseOptimizer
SGD-like Memory, AdamW-level Performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
correct_bias
|
bool
|
bool. Whether to correct bias in Adam. |
True
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/apollo.py
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 | |
ApolloDQN
Bases: BaseOptimizer
An Adaptive Parameter-wise Diagonal Quasi-Newton Method for Nonconvex Stochastic Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
init_lr
|
Optional[float]
|
Optional[float]. initial learning rate (default lr / 1000). |
1e-05
|
beta
|
float
|
float. coefficient used for computing running averages of gradient. |
0.9
|
rebound
|
str
|
str. rectified bound for diagonal hessian. (constant, belief). |
'constant'
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decay_type
|
str
|
str. type of weight decay. (l2, decoupled, stable). |
'l2'
|
warmup_steps
|
int
|
int. number of warmup steps. |
500
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
0.0001
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/apollo.py
15 16 17 18 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 | |
AvaGrad
Bases: BaseOptimizer
Domain-independent Dominance of Adaptive Methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.1
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
0.1
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/avagrad.py
10 11 12 13 14 15 16 17 18 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 | |
BSAM
Bases: BaseOptimizer
SAM as an Optimal Relaxation of Bayes.
Example:
Here's an example::
model = YourModel()
optimizer = BSAM(model.parameters(), ...)
def closure():
loss = loss_function(output, model(input))
loss.backward()
return loss
for input, output in data:
loss = loss_function(output, model(input))
loss.backward()
optimizer.step(closure)
optimizer.zero_grad()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
num_data
|
int
|
int. number of training data. |
required |
lr
|
float
|
float. learning rate. |
0.5
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0001
|
rho
|
float
|
float. size of the neighborhood for computing the max loss. |
0.05
|
adaptive
|
bool
|
bool. element-wise Adaptive SAM. |
False
|
damping
|
float
|
float. damping to stabilize the method. |
0.1
|
kwargs
|
Dict. parameters for optimizer. |
{}
|
Source code in pytorch_optimizer/optimizer/sam.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 | |
CAME
Bases: BaseOptimizer
Confidence-guided Adaptive Memory Efficient Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.0002
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999, 0.9999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
clip_threshold
|
float
|
float. threshold of root-mean-square of final gradient update. |
1.0
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps1
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-30
|
eps2
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-16
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/came.py
11 12 13 14 15 16 17 18 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 | |
approximate_sq_grad(exp_avg_sq_row, exp_avg_sq_col, output)
staticmethod
Get approximation of EMA of squared gradient.
Source code in pytorch_optimizer/optimizer/came.py
116 117 118 119 120 121 122 123 124 125 | |
get_options(shape)
staticmethod
Get factored.
Source code in pytorch_optimizer/optimizer/came.py
106 107 108 109 | |
get_rms(x)
staticmethod
Get RMS.
Source code in pytorch_optimizer/optimizer/came.py
111 112 113 114 | |
DAdaptAdaGrad
Bases: BaseOptimizer
AdaGrad with D-Adaptation. Leave LR set to 1 unless you encounter instability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
momentum
|
float
|
float. momentum. |
0.0
|
d0
|
float
|
float. initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
growth_rate
|
float
|
float. prevent the D estimate from growing faster than this multiplicative rate. |
float('inf')
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
0.0
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
17 18 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 | |
DAdaptAdam
Bases: BaseOptimizer
Adam with D-Adaptation. Leave LR set to 1 unless you encounter instability. This implementation is based on V3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
betas
|
BETAS
|
BETAS. betas. |
(0.9, 0.999)
|
d0
|
float
|
float. initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
growth_rate
|
float
|
float. prevent the D estimate from growing faster than this multiplicative rate. |
float('inf')
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. use AdamW style weight decay. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
bias_correction
|
bool
|
bool. Turn on Adam's bias correction. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
DAdaptSGD
Bases: BaseOptimizer
SGD with D-Adaptation. Leave LR set to 1 unless you encounter instability. This implementation is based on V3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
momentum
|
float
|
float. momentum. |
0.9
|
d0
|
float
|
float. initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
growth_rate
|
float
|
float. prevent the D estimate from growing faster than this multiplicative rate. |
float('inf')
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
DAdaptAdan
Bases: BaseOptimizer
Adan with D-Adaptation. Leave LR set to 1 unless you encounter instability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.98, 0.92, 0.99)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. decoupled weight decay. |
False
|
d0
|
float
|
float. initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
growth_rate
|
float
|
float. prevent the D estimate from growing faster than this multiplicative rate. Default is inf, for unrestricted. |
float('inf')
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | |
DAdaptLion
Bases: BaseOptimizer
Lion with D-Adaptation. Leave LR set to 1 unless you encounter instability. This implementation is based on V3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
d0
|
float
|
float. initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
DeMo
Bases: SGD, BaseOptimizer
Decoupled Momentum Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
compression_decay
|
float
|
float. compression_decay. |
0.999
|
compression_top_k
|
int
|
int. compression_top_k. |
32
|
compression_chunk
|
int
|
int. compression_chunk. |
64
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/demo.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
find_dtype()
Return dtype of the parameter.
Source code in pytorch_optimizer/optimizer/demo.py
348 349 350 351 352 353 354 | |
DiffGrad
Bases: BaseOptimizer
An Optimization Method for Convolutional Neural Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
rectify
|
bool
|
bool. perform the rectified update similar to RAdam. |
False
|
n_sma_threshold
|
int
|
int. (recommended is 5). |
5
|
degenerated_to_sgd
|
bool
|
bool. degenerated to SGD. |
True
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/diffgrad.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
EXAdam
Bases: BaseOptimizer
The Power of Adaptive Cross-Moments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/exadam.py
9 10 11 12 13 14 15 16 17 18 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 | |
DynamicLossScaler
Dynamically adjusts the loss scaling factor.
Dynamic loss scalers are important in mixed-precision training.
They help us avoid underflows and overflows in low-precision gradients.
See here for information:
<https://docs.nvidia.com/deeplearning/performance/mixed-precision-training/index.html#lossscaling>
Shamelessly stolen and adapted from FairSeq.
<https://github.com/pytorch/fairseq/blob/main/fairseq/optim/fp16_optimizer.py>
Reference : 'https://github.com/facebookresearch/ParlAI/blob/main/parlai/utils/fp16.py'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
init_scale
|
float
|
Initial loss scale. |
2.0 ** 15
|
scale_factor
|
float
|
Factor by which to increase or decrease loss scale. |
2.0
|
scale_window
|
int
|
If we do not experience overflow in scale_window iterations, loss scale will increase by scale_factor. |
2000
|
tolerance
|
float
|
Pct of iterations that have overflowed after which we must decrease the loss scale. |
0.0
|
threshold
|
Optional[float]
|
If not None, loss scale will decrease below this threshold. |
None
|
Source code in pytorch_optimizer/optimizer/fp16.py
11 12 13 14 15 16 17 18 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 | |
decrease_loss_scale()
Decrease the loss scale by self.scale_factor.
NOTE: the loss_scale will not go below self.threshold.
Source code in pytorch_optimizer/optimizer/fp16.py
83 84 85 86 87 88 89 90 | |
update_scale(overflow)
Update the loss scale.
If overflow exceeds our tolerance, we decrease the loss scale.
If the number of iterations since the last overflow exceeds the scale window, we increase the loss scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overflow
|
bool
|
bool. adjust scales to prevent overflow. |
required |
Source code in pytorch_optimizer/optimizer/fp16.py
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 | |
SafeFP16Optimizer
Bases: Optimizer
Safe FP16 Optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Optimizer
|
OPTIMIZER. |
required |
aggregate_g_norms
|
bool
|
bool. aggregate_g_norms. |
False
|
min_loss_scale
|
float
|
float. min_loss_scale. |
2 ** -5
|
Source code in pytorch_optimizer/optimizer/fp16.py
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 | |
loss_scale: float
property
Convenience function which TorchAgent calls to get current scale value.
backward(loss, update_main_grads=False)
Compute the sum of gradients of the given tensor w.r.t. graph leaves.
Compared to :func:`fairseq.optim.FairseqOptimizer.backward`, this function
additionally dynamically scales the loss to avoid gradient underflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss
|
float. loss. |
required | |
update_main_grads
|
bool
|
bool. update main gradient. |
False
|
Source code in pytorch_optimizer/optimizer/fp16.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
clip_main_grads(max_norm)
Clip gradient norm and updates dynamic loss scaler.
Source code in pytorch_optimizer/optimizer/fp16.py
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 | |
get_lr()
Get learning rate.
Source code in pytorch_optimizer/optimizer/fp16.py
273 274 275 | |
load_state_dict(state_dict)
Load an optimizer state dict.
In general, we should prefer the configuration of the existing optimizer instance (e.g., learning rate)
over that found in the state_dict. This allows us to resume training from a checkpoint using a new set of
optimizer args.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
Dict
|
Dict. state_dict. |
required |
Source code in pytorch_optimizer/optimizer/fp16.py
166 167 168 169 170 171 172 173 174 175 176 177 | |
multiply_grads(c)
Multiply grads by a constant c.
Source code in pytorch_optimizer/optimizer/fp16.py
215 216 217 218 219 220 221 222 | |
set_lr(lr)
Set learning rate.
Source code in pytorch_optimizer/optimizer/fp16.py
277 278 279 | |
state_dict()
Return the optimizer state dict.
Source code in pytorch_optimizer/optimizer/fp16.py
159 160 161 162 163 164 | |
step(closure=None)
Perform a single optimization step.
Source code in pytorch_optimizer/optimizer/fp16.py
255 256 257 258 259 260 261 262 263 | |
sync_fp16_grads_to_fp32(multiply_grads=1.0)
Sync fp16 to fp32 gradients.
Source code in pytorch_optimizer/optimizer/fp16.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
zero_grad()
Clear the gradients of all optimized parameters.
Source code in pytorch_optimizer/optimizer/fp16.py
265 266 267 268 269 270 271 | |
FAdam
Bases: BaseOptimizer
Adam is a natural gradient optimizer using diagonal empirical Fisher information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.1
|
clip
|
float
|
float. maximum norm of the gradient. |
1.0
|
p
|
float
|
float. momentum factor. |
0.5
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
momentum_dtype
|
dtype
|
torch.dtype. dtype of momentum. |
float32
|
fim_dtype
|
dtype
|
torch.dtype. dtype of fim. |
float32
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/fadam.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
Fira
Bases: BaseOptimizer
Can We Achieve Full-rank Training of LLMs Under Low-rank Constraint? Fira with AdamW optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/fira.py
11 12 13 14 15 16 17 18 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 | |
FOCUS
Bases: BaseOptimizer
First Order Concentrated Updating Scheme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
gamma
|
float
|
float. control the strength of the attraction. |
0.1
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/focus.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
Fromage
Bases: BaseOptimizer
On the distance between two neural networks and the stability of learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
p_bound
|
Optional[float]
|
Optional[float]. Restricts the optimisation to a bounded set. A value of 2.0 restricts parameter norms to lie within 2x their initial norms. This regularises the model class. |
None
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/fromage.py
16 17 18 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 | |
FTRL
Bases: BaseOptimizer
Follow The Regularized Leader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
lr_power
|
float
|
float. controls how the learning rate decreases during training. use zero for a fixed learning rate. |
-0.5
|
beta
|
float
|
float. beta value in the paper. |
0.0
|
lambda_1
|
float
|
float. L1 regularization parameter. |
0.0
|
lambda_2
|
float
|
float. L2 regularization parameter. |
0.0
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/ftrl.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
centralize_gradient(grad, gc_conv_only=False)
Gradient Centralization (GC).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grad
|
Tensor
|
torch.Tensor. gradient. |
required |
gc_conv_only
|
bool
|
bool. 'False' for both conv & fc layers. |
False
|
Source code in pytorch_optimizer/optimizer/gradient_centralization.py
4 5 6 7 8 9 10 11 12 | |
Grams
Bases: BaseOptimizer
Gradient Descent with Adaptive Momentum Scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. decoupled weight decay. |
True
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/grams.py
10 11 12 13 14 15 16 17 18 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 | |
Gravity
Bases: BaseOptimizer
a Kinematic Approach on Optimization in Deep Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
alpha
|
float
|
float. alpha controls the V initialization. |
0.01
|
beta
|
float
|
float. beta will be used to compute running average of V. |
0.9
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/gravity.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
GrokFastAdamW
Bases: BaseOptimizer
Accelerated Grokking by Amplifying Slow Gradients with AdamW.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.0001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.99)
|
grokfast
|
bool
|
bool. whether to use grokfast. |
True
|
grokfast_alpha
|
float
|
float. momentum hyperparameter of the EMA. |
0.98
|
grokfast_lamb
|
float
|
float. amplifying factor hyperparameter of the filter. |
2.0
|
grokfast_after_step
|
int
|
int. warmup step for grokfast. |
0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/grokfast.py
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 | |
GSAM
Bases: BaseOptimizer
Surrogate Gap Guided Sharpness-Aware Minimization.
Example:
Here's an example::
model = YourModel()
base_optimizer = AdamP(model.parameters())
lr_scheduler = LinearScheduler(base_optimizer, t_max=num_total_steps)
rho_scheduler = ProportionScheduler(lr_scheduler, max_lr=max_lr)
optimizer = GSAM(model.parameters(), base_optimizer, model, rho_scheduler)
def loss_fn(predictions, targets):
return F.cross_entropy(predictions, targets)
for inputs, targets in data:
optimizer.set_closure(loss_fn, inputs, targets)
predictions, loss = optimizer.step()
lr_scheduler.step()
optimizer.update_rho_t()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
Optimizer
|
Optimizer. base optimizer. |
required |
model
|
Module
|
nn.Module. model. |
required |
alpha
|
float
|
float. rho alpha. |
0.4
|
rho_scheduler
|
rho scheduler. |
required | |
adaptive
|
bool
|
bool. element-wise Adaptive SAM. |
False
|
perturb_eps
|
float
|
float. epsilon for perturbation. |
1e-12
|
kwargs
|
Dict. parameters for optimizer. |
{}
|
Source code in pytorch_optimizer/optimizer/sam.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
set_closure(loss_fn, inputs, targets, **kwargs)
Set closure.
Create `self.forward_backward_func`, which is a function such that `self.forward_backward_func()`
automatically performs forward and backward passes. This function does not take any arguments,
and the inputs and targets data should be pre-set in the definition of partial-function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loss_fn
|
Module
|
nn.Module. loss function. |
required |
inputs
|
Tensor
|
torch.Tensor. inputs. |
required |
targets
|
Tensor
|
torch.Tensor. targets. |
required |
Source code in pytorch_optimizer/optimizer/sam.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | |
Kate
Bases: BaseOptimizer
Remove that Square Root: A New Efficient Scale-Invariant Version of AdaGrad.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
delta
|
float
|
float. delta. 0.0 or 1e-8. |
0.0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. epsilon value. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/kate.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
Lamb
Bases: BaseOptimizer
Large Batch Optimization for Deep Learning.
This Lamb implementation is based on the paper v3, which does not use de-biasing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
rectify
|
bool
|
bool. perform the rectified update similar to RAdam. |
False
|
degenerated_to_sgd
|
bool
|
bool. degenerated to SGD. |
False
|
n_sma_threshold
|
int
|
int. (recommended is 5). |
5
|
grad_averaging
|
bool
|
bool. whether apply (1 - beta2) to gradient when calculating running averages of gradient. |
True
|
max_grad_norm
|
float
|
float. max gradient norm to clip. |
1.0
|
adam
|
bool
|
bool. always use trust ratio = 1, which turns this into Adam. Useful for comparison purposes. |
False
|
pre_norm
|
bool
|
bool. perform pre-normalization of all gradients. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/lamb.py
11 12 13 14 15 16 17 18 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 | |
LaProp
Bases: BaseOptimizer
Separating Momentum and Adaptivity in Adam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.0004
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
centered
|
bool
|
bool. |
False
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. epsilon value. |
1e-15
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/laprop.py
10 11 12 13 14 15 16 17 18 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 | |
LARS
Bases: BaseOptimizer
Layer-wise Adaptive Rate Scaling (no rate scaling or weight decay for parameters <= 1D).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
momentum
|
float
|
float. momentum. |
0.9
|
dampening
|
float
|
float. dampening for momentum. |
0.0
|
trust_coefficient
|
float
|
float. trust_coefficient. |
0.001
|
nesterov
|
bool
|
bool. enables nesterov momentum. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/lars.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
Lion
Bases: BaseOptimizer
Symbolic Discovery of Optimization Algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.0001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.99)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/lion.py
9 10 11 12 13 14 15 16 17 18 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 | |
LOMO
Bases: BaseOptimizer
Full Parameter Fine-tuning for Large Language Models with Limited Resources.
Reference : https://github.com/OpenLMLab/LOMO/blob/main/src/lomo.py Check the usage from here : https://github.com/OpenLMLab/LOMO/blob/main/lomo/src/lomo_trainer.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
nn.Module. pytorch model. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
clip_grad_norm
|
Optional[float]
|
Optional[float]. clip grad norm. |
None
|
clip_grad_value
|
Optional[float]
|
Optional[float]. clip grad value. |
None
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/lomo.py
15 16 17 18 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 | |
Lookahead
Bases: BaseOptimizer
k steps forward, 1 step back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
OPTIMIZER_INSTANCE_OR_CLASS
|
OPTIMIZER_INSTANCE_OR_CLASS. base optimizer. |
required |
k
|
int
|
int. number of lookahead steps. |
5
|
alpha
|
float
|
float. linear interpolation factor. |
0.5
|
pullback_momentum
|
str
|
str. change to inner optimizer momentum on interpolation update. |
'none'
|
Source code in pytorch_optimizer/optimizer/lookahead.py
11 12 13 14 15 16 17 18 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 | |
backup_and_load_cache()
Backup cache parameters.
Source code in pytorch_optimizer/optimizer/lookahead.py
81 82 83 84 85 86 87 88 | |
clear_and_load_backup()
Load backup parameters.
Source code in pytorch_optimizer/optimizer/lookahead.py
90 91 92 93 94 95 96 | |
load_state_dict(state)
Load state.
Source code in pytorch_optimizer/optimizer/lookahead.py
101 102 103 104 | |
LookSAM
Bases: BaseOptimizer
Towards Efficient and Scalable Sharpness-Aware Minimization.
Example:
Here's an example::
model = YourModel()
base_optimizer = Ranger21
optimizer = LookSAM(model.parameters(), base_optimizer)
for input, output in data:
# first forward-backward pass
loss = loss_function(output, model(input))
loss.backward()
optimizer.first_step(zero_grad=True)
# second forward-backward pass
# make sure to do a full forward pass
loss_function(output, model(input)).backward()
optimizer.second_step(zero_grad=True)
Alternative example with a single closure-based step function::
model = YourModel()
base_optimizer = Ranger21
optimizer = LookSAM(model.parameters(), base_optimizer)
def closure():
loss = loss_function(output, model(input))
loss.backward()
return loss
for input, output in data:
loss = loss_function(output, model(input))
loss.backward()
optimizer.step(closure)
optimizer.zero_grad()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
OPTIMIZER
|
OPTIMIZER. base optimizer. |
required |
rho
|
float
|
float. size of the neighborhood for computing the max loss. |
0.1
|
k
|
int
|
int. lookahead step. |
10
|
alpha
|
float
|
float. lookahead blending alpha. |
0.7
|
adaptive
|
bool
|
bool. element-wise Adaptive SAM. |
False
|
use_gc
|
bool
|
bool. perform gradient centralization, GCSAM variant. |
False
|
perturb_eps
|
float
|
float. eps for perturbation. |
1e-12
|
kwargs
|
Dict. parameters for optimizer. |
{}
|
Source code in pytorch_optimizer/optimizer/sam.py
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 | |
MADGRAD
Bases: BaseOptimizer
A Momentumized, Adaptive, Dual Averaged Gradient Method for Stochastic (slightly modified).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
weight_decay
|
float
|
float. weight decay (L2 penalty). MADGRAD optimizer requires less weight decay than other methods, often as little as zero. On sparse problems both weight_decay and momentum should be set to 0. |
0.0
|
weight_decouple
|
bool
|
float. Apply AdamW style decoupled weight decay. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/madgrad.py
15 16 17 18 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 | |
MARS
Bases: BaseOptimizer
Unleashing the Power of Variance Reduction for Training Large Models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.003
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.95, 0.99)
|
gamma
|
float
|
float. the scaling parameter that controls the strength of gradient correction. |
0.025
|
mars_type
|
MARS_TYPE
|
MARS TYPE. type of MARS. |
'adamw'
|
optimize_1d
|
bool
|
bool. whether MARS should optimize 1D parameters. |
False
|
lr_1d
|
bool
|
float. learning rate for AdamW when optimize_1d is set to False. |
0.003
|
betas_1d
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace for 1d. |
(0.9, 0.95)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decay_1d
|
float
|
float. weight decay for 1d. |
0.1
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/mars.py
14 15 16 17 18 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 | |
MSVAG
Bases: BaseOptimizer
Dissecting Adam: The Sign, Magnitude and Variance of Stochastic Gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
beta
|
float
|
float. Moving average (momentum) constant (scalar tensor or float value). |
0.9
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/msvag.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
get_rho(beta_power, beta)
staticmethod
Get rho.
Source code in pytorch_optimizer/optimizer/msvag.py
53 54 55 56 57 58 | |
Muon
Bases: BaseOptimizer
Momentum Orthogonalized by Newton-schulz.
Muon internally runs standard SGD-momentum, and then performs an orthogonalization post-processing step, in which each 2D parameter's update is replaced with the nearest orthogonal matrix. To efficiently orthogonalize each update, we use a Newton-Schulz iteration, which has the advantage that it can be stably run in bfloat16 on the GPU.
Muon is intended to optimize only the internal ≥2D parameters of a network. Embeddings, classifier heads, and scalar or vector parameters should be optimized using AdamW.
Some warnings: - We believe this optimizer is unlikely to work well for training with small batch size. - We believe it may not work well for fine-tuning pretrained models, but we haven't tested this.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. the parameters to be optimized by Muon. |
required |
lr
|
float
|
float. learning rate. |
0.02
|
momentum
|
float
|
float. the momentum used by the internal SGD. |
0.95
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
betas
|
BETAS
|
The betas for the internal AdamW. |
(0.9, 0.95)
|
nesterov
|
bool
|
bool. whether to use nesterov momentum. |
True
|
ns_steps
|
int
|
int. the number of Newton-Schulz iterations to run. (5 is probably always enough) |
5
|
use_adjusted_lr
|
bool
|
bool. whether to use adjusted learning rate, which is from the Moonlight. reference: https://github.com/MoonshotAI/Moonlight/blob/master/examples/toy_train.py |
False
|
adamw_params
|
Optional[PARAMETERS]
|
Optional[PARAMETERS] The parameters to be optimized by AdamW. Any parameters in |
None
|
adamw_lr
|
float
|
float. The learning rate for the internal AdamW. |
0.0003
|
adamw_wd
|
float
|
float. The weight decay for the internal AdamW. |
0.0
|
adamw_eps
|
float
|
float. The epsilon for the internal AdamW. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/muon.py
14 15 16 17 18 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 | |
get_adjusted_lr(lr, param_shape, use_adjusted_lr=False)
staticmethod
Get the adjust learning rate.
Source code in pytorch_optimizer/optimizer/muon.py
129 130 131 132 133 134 135 136 137 138 139 140 141 | |
set_muon_state(params, adamw_params)
Set use_muon flag.
Source code in pytorch_optimizer/optimizer/muon.py
118 119 120 121 122 123 124 | |
Nero
Bases: BaseOptimizer
Learning by Turning: Neural Architecture Aware Optimisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
beta
|
float
|
float. coefficients used for computing running averages of gradient and the squared hessian trace. |
0.999
|
constraints
|
bool
|
bool. |
True
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/nero.py
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 | |
NovoGrad
Bases: BaseOptimizer
Stochastic Gradient Methods with Layer-wise Adaptive Moments for Training of Deep Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.95, 0.98)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
grad_averaging
|
bool
|
bool. multiply ck (1 - momentum). |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/novograd.py
10 11 12 13 14 15 16 17 18 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 | |
OrthoGrad
Bases: BaseOptimizer
Grokking at the Edge of Numerical Stability.
A wrapper optimizer that projects gradients to be orthogonal to the current parameters before performing an update.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
OPTIMIZER_INSTANCE_OR_CLASS
|
OPTIMIZER_INSTANCE_OR_CLASS. base optimizer. |
required |
Source code in pytorch_optimizer/optimizer/orthograd.py
10 11 12 13 14 15 16 17 18 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 | |
PAdam
Bases: BaseOptimizer
Closing the Generalization Gap of Adaptive Gradient Methods in Training Deep Neural Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.1
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
partial
|
float
|
float. partially adaptive parameter. |
0.25
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/padam.py
10 11 12 13 14 15 16 17 18 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 | |
PCGrad
Bases: BaseOptimizer
Gradient Surgery for Multi-Task Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Optimizer
|
Optimizer: optimizer instance. |
required |
reduction
|
str
|
str. reduction method. |
'mean'
|
Source code in pytorch_optimizer/optimizer/pcgrad.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 | |
pack_grad(objectives)
Pack the gradient of the parameters of the network for each objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectives
|
Iterable
|
Iterable[nn.Module]. a list of objectives. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[List[Tensor], List[List[int]], List[Tensor]]
|
torch.Tensor. packed gradients. |
Source code in pytorch_optimizer/optimizer/pcgrad.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
pc_backward(objectives)
Calculate the gradient of the parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectives
|
Iterable[Module]
|
Iterable[nn.Module]. a list of objectives. |
required |
Source code in pytorch_optimizer/optimizer/pcgrad.py
124 125 126 127 128 129 130 131 132 133 134 | |
project_conflicting(grads, has_grads)
Project conflicting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grads
|
List[Tensor]
|
a list of the gradient of the parameters. |
required |
has_grads
|
List[Tensor]
|
a list of mask represent whether the parameter has gradient. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor. merged gradients. |
Source code in pytorch_optimizer/optimizer/pcgrad.py
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 | |
retrieve_grad()
Get the gradient of the parameters of the network with specific objective.
Source code in pytorch_optimizer/optimizer/pcgrad.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
PID
Bases: BaseOptimizer
A PID Controller Approach for Stochastic Optimization of Deep Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
momentum
|
float
|
float. momentum factor. |
0.0
|
dampening
|
float
|
float. dampening for momentum. |
0.0
|
derivative
|
float
|
float. D part of the PID. |
10.0
|
integral
|
float
|
float. I part of the PID. |
5.0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/pid.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
PNM
Bases: BaseOptimizer
Positive-Negative Momentum Optimizers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 1.0)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. use weight_decouple. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/pnm.py
10 11 12 13 14 15 16 17 18 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 | |
Prodigy
Bases: BaseOptimizer
An Expeditiously Adaptive Parameter-Free Learner.
Leave LR set to 1 unless you encounter instability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
betas
|
BETAS
|
BETAS. betas. |
(0.9, 0.999)
|
beta3
|
Optional[float]
|
float. coefficients for computing the Prodigy step-size using running averages. If set to None, uses the value of square root of beta2. |
None
|
d0
|
float
|
float. initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
d_coef
|
float
|
float. Coefficient in the expression for the estimate of d. |
1.0
|
growth_rate
|
float
|
float. prevent the D estimate from growing faster than this multiplicative rate. |
float('inf')
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. use AdamW style weight decay. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
bias_correction
|
bool
|
bool. turn on Adam's bias correction. |
False
|
safeguard_warmup
|
bool
|
bool. remove lr from the denominator of D estimate to avoid issues during warm-up stage. |
False
|
eps
|
Optional[float]
|
float. term added to the denominator to improve numerical stability. when eps is None, use atan2 rather than epsilon and division for parameter updates. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/prodigy.py
11 12 13 14 15 16 17 18 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 | |
Kron
Bases: BaseOptimizer
PSGD with the Kronecker product pre-conditioner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
momentum
|
float
|
float. momentum factor. |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
pre_conditioner_update_probability
|
Optional[Tuple[Callable, float]]
|
Optional[Tuple[Callable, float]]. Probability of updating the pre-conditioner. If None, defaults to a schedule that anneals from 1.0 to 0.03 by 4000 steps. |
None
|
max_size_triangular
|
int
|
int. max size for dim's pre-conditioner to be triangular. |
8192
|
min_ndim_triangular
|
int
|
int. minimum number of dimensions a layer needs to have triangular pre-conditioners. |
2
|
memory_save_mode
|
Optional[MEMORY_SAVE_MODE_TYPE]
|
Optional[str]. None, 'one_diag', or 'all_diag', None is default to set all pre-conditioners to be triangular, 'one_diag' sets the largest or last dim to be diagonal per layer, and 'all_diag' sets all pre-conditioners to be diagonal. |
None
|
momentum_into_precondition_update
|
bool
|
bool. whether to send momentum into pre-conditioner update instead of raw gradients. |
True
|
mu_dtype
|
Optional[dtype]
|
Optional[torch.dtype]. dtype of the momentum accumulator. |
None
|
precondition_dtype
|
Optional[dtype]
|
torch.dtype. dtype of the pre-conditioner. |
float32
|
balance_prob
|
float
|
float. probability of performing balancing. |
0.01
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/psgd.py
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 | |
QHAdam
Bases: BaseOptimizer
Quasi-hyperbolic momentum and Adam for deep learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
nus
|
Tuple[float, float]
|
Tuple[float, float]. immediate discount factors used to estimate the gradient and its square. |
(1.0, 1.0)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/qhadam.py
10 11 12 13 14 15 16 17 18 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 | |
QHM
Bases: BaseOptimizer
Quasi-hyperbolic momentum (QHM) optimization algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
momentum
|
float
|
float. momentum factor. |
0.0
|
nu
|
float
|
float. immediate discount factor used to estimate the gradient and its square. |
1.0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/qhm.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
RACS
Bases: BaseOptimizer
Row and Column Scaled SGD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
beta
|
float
|
float. momentum factor. |
0.9
|
alpha
|
float
|
float. scaler. |
0.05
|
gamma
|
float
|
float. limiter threshold. |
1.01
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/racs.py
11 12 13 14 15 16 17 18 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 | |
RAdam
Bases: BaseOptimizer
Rectified Adam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
n_sma_threshold
|
int
|
int. (recommended is 5). |
5
|
degenerated_to_sgd
|
bool
|
float. degenerated to SGD. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/radam.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
Ranger
Bases: BaseOptimizer
a synergistic optimizer combining RAdam and LookAhead, and now GC in one optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.95, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
n_sma_threshold
|
int
|
int. (recommended is 5). |
5
|
degenerated_to_sgd
|
bool
|
bool. perform SGD update when variance of gradient is high. |
False
|
use_gc
|
bool
|
bool. use Gradient Centralization (both convolution & fc layers). |
True
|
gc_conv_only
|
bool
|
bool. use Gradient Centralization (only convolution layer). |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-05
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/ranger.py
9 10 11 12 13 14 15 16 17 18 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 | |
Ranger21
Bases: BaseOptimizer
Integrating the latest deep learning components into a single optimizer.
Here's the components
* uses the AdamW optimizer as its core (or, optionally, MadGrad)
* Adaptive gradient clipping
* Gradient centralization
* Positive-Negative momentum
* Norm loss
* Stable weight decay
* Linear learning rate warm-up
* Explore-exploit learning rate schedule
* Lookahead
* Softplus transformation
* Gradient Normalization
* Corrects the denominator (AdamD).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
num_iterations
|
int
|
int. number of the total training steps. Ranger21 optimizer schedules the learning rate with its own recipes. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
beta0
|
float
|
float. Manages the amplitude of the noise introduced by positive negative momentum While 0.9 is a recommended default value, you can use -0.5 to minimize the noise. |
0.9
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
use_softplus
|
bool
|
bool. use softplus to smooth. |
True
|
beta_softplus
|
float
|
float. beta. |
50.0
|
disable_lr_scheduler
|
bool
|
bool. whether to disable learning rate schedule. |
False
|
num_warm_up_iterations
|
Optional[int]
|
Optional[int]. number of warm-up iterations. Ranger21 performs linear learning rate warmup. |
None
|
num_warm_down_iterations
|
Optional[int]
|
Optional[int]. number of warm-down iterations. Ranger21 performs Explore-exploit learning rate scheduling. |
None
|
agc_clipping_value
|
float
|
float. |
0.01
|
agc_eps
|
float
|
float. eps for AGC |
0.001
|
centralize_gradients
|
bool
|
bool. use GC both convolution & fc layers. |
True
|
normalize_gradients
|
bool
|
bool. use gradient normalization. |
True
|
lookahead_merge_time
|
int
|
int. merge time. |
5
|
lookahead_blending_alpha
|
float
|
float. blending alpha. |
0.5
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0001
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
norm_loss_factor
|
float
|
float. norm loss factor. |
0.0001
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/ranger21.py
15 16 17 18 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 | |
RotoGrad
Bases: RotateOnly
Implementation of RotoGrad as described in the original paper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone
|
Module
|
nn.Module. shared module. |
required |
heads
|
Sequence[Module]
|
List[nn.Module]. task-specific modules. |
required |
latent_size
|
int
|
int. size of the shared representation, size of the output of the backbone.z. |
required |
burn_in_period
|
int
|
int. When back-propagating towards the shared parameters, each task loss is normalized dividing by its initial value, :math: |
20
|
normalize_losses
|
bool
|
bool. Whether to use this normalized losses to back-propagate through the task-specific parameters as well. |
False
|
Source code in pytorch_optimizer/optimizer/rotograd.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
SAM
Bases: BaseOptimizer
Sharpness-Aware Minimization for Efficiently Improving Generalization.
Example:
Here's an example::
model = YourModel()
base_optimizer = Ranger21
optimizer = SAM(model.parameters(), base_optimizer)
for input, output in data:
# first forward-backward pass
loss = loss_function(output, model(input))
loss.backward()
optimizer.first_step(zero_grad=True)
# second forward-backward pass
# make sure to do a full forward pass
loss_function(output, model(input)).backward()
optimizer.second_step(zero_grad=True)
Alternative example with a single closure-based step function::
model = YourModel()
base_optimizer = Ranger21
optimizer = SAM(model.parameters(), base_optimizer)
def closure():
loss = loss_function(output, model(input))
loss.backward()
return loss
for input, output in data:
loss = loss_function(output, model(input))
loss.backward()
optimizer.step(closure)
optimizer.zero_grad()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
OPTIMIZER
|
OPTIMIZER. base optimizer. |
required |
rho
|
float
|
float. size of the neighborhood for computing the max loss. |
0.05
|
adaptive
|
bool
|
bool. element-wise Adaptive SAM. |
False
|
use_gc
|
bool
|
bool. perform gradient centralization, GCSAM variant. |
False
|
perturb_eps
|
float
|
float. eps for perturbation. |
1e-12
|
kwargs
|
Dict. parameters for optimizer. |
{}
|
Source code in pytorch_optimizer/optimizer/sam.py
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 | |
ScheduleFreeSGD
Bases: BaseOptimizer
Schedule-Free SGD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
1.0
|
momentum
|
float
|
float. momentum factor, must be between 0 and 1 exclusive. |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
r
|
float
|
float. use polynomial weighting in the average with power r. |
0.0
|
weight_lr_power
|
float
|
float. during warmup, the weights in the average will be equal to lr raised to this power. set to 0 for no weighting. |
2.0
|
warmup_steps
|
int
|
int. enables a linear learning rate warmup. |
0
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/schedulefree.py
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 | |
ScheduleFreeAdamW
Bases: BaseOptimizer
Schedule-Free AdamW.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.0025
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
r
|
float
|
float. use polynomial weighting in the average with power r. |
0.0
|
weight_lr_power
|
float
|
float. during warmup, the weights in the average will be equal to lr raised to this power. set to 0 for no weighting. |
2.0
|
warmup_steps
|
int
|
int. enables a linear learning rate warmup. |
0
|
ams_bound
|
bool
|
bool. whether to use the AMSBound variant. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/schedulefree.py
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 | |
ScheduleFreeRAdam
Bases: BaseOptimizer
Schedule-Free RAdam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.0025
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
r
|
float
|
float. use polynomial weighting in the average with power r. |
0.0
|
weight_lr_power
|
float
|
float. during warmup, the weights in the average will be equal to lr raised to this power. set to 0 for no weighting. |
2.0
|
silent_sgd_phase
|
bool
|
bool. the optimizer will not use the first SGD phase of RAdam. This means that the optimizer will not update model parameters during the early training steps (e.g., < 5 when β_2 = 0.999), but just update the momentum values of the optimizer. This helps stabilize training by ensuring smoother warmup behavior and more reliable calculation of the moving average coefficient ( |
True
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/schedulefree.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
ScheduleFreeWrapper
Bases: BaseOptimizer
Wrap any optimizer to make it Schedule-Free.
This version uses a memory-efficient swap operation but may be slower than the reference version. In most cases
the performance difference is negligible. For the best possible performance and memory-usage, Schedule-Free
needs to be directly integrated with the base optimizer.
When using this version, you can disable the base optimizer's momentum, as it's no longer necessary when using
our wrapper's momentum (although you can use both types of momentum if you want).
If you set weight decay on the base optimizer, it computes weight decay at $z$. We offer the option to compute
weight decay at $y$, via the `weight_decay_at_y` parameter, which seems to give better results in our
experiments. This approach to decay only works correctly if the base optimizer uses group['lr'] as the current
learning rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
OPTIMIZER_INSTANCE_OR_CLASS
|
OPTIMIZER_INSTANCE_OR_CLASS. base optimizer. |
required |
momentum
|
float
|
float. momentum. |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
r
|
float
|
float. use polynomial weighting in the average with power r. |
0.0
|
weight_lr_power
|
float
|
float. during warmup, the weights in the average will be equal to lr raised to this power. set to 0 for no weighting. |
2.0
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/schedulefree.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | |
load_state_dict(state)
Load state.
Source code in pytorch_optimizer/optimizer/schedulefree.py
568 569 570 571 | |
SCION
Bases: BaseOptimizer
Training Deep Learning Models with Norm-Constrained LMOs.
Example: >>> radius = 50.0 >>> parameter_groups = [{ ... 'params': model.transformer.h.parameters(), ... 'norm_type': 'spectral', ... 'norm_kwargs': {}, ... 'scale': radius, ... }, { ... 'params': model.lm_head.parameters(), ... 'norm_type': 'sign', ... 'norm_kwargs': {}, ... 'scale': radius * 60.0, ... }] >>> optimizer = SCION(parameter_groups)
For more details, checkout here https://github.com/LIONS-EPFL/scion/tree/main?tab=readme-ov-file#examples
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
momentum
|
float
|
float. momentum factor. 1.0 - usual momentum. |
0.1
|
constraint
|
bool
|
bool. whether to use a constraint SCG or not. |
False
|
norm_type
|
int
|
int. supported LMO norm types. 0 stands for no normalization and 1 stands for AUTO. 0 to 7. please check LMONorm Enum class for the details. |
AUTO
|
norm_kwargs
|
Optional[Dict]
|
Optional[Dict]. arguments for the Norm. |
None
|
scale
|
float
|
float. based on the usage of the original intend, 50.0 is used for Transformer block, and 3000.0 is used for others (e.g. Embedding, LM head) |
1.0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/scion.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | |
StableAdamW
Bases: BaseOptimizer
Stable and low-precision training for large-scale vision-language models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.99)
|
kahan_sum
|
bool
|
bool. Enables Kahan summation for more accurate parameter updates when training in low precision (float16 or bfloat16). |
True
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
bool. decoupled weight decay. |
True
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamw.py
10 11 12 13 14 15 16 17 18 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 | |
AccSGD
Bases: BaseOptimizer
Accelerating Stochastic Gradient Descent For Least Squares Regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
kappa
|
float
|
float. ratio of long to short step. |
1000.0
|
xi
|
float
|
float. statistical advantage parameter. |
10.0
|
constant
|
float
|
float. any small constant under 1. |
0.7
|
weight_decay
|
float
|
float. weight decay. |
0.0
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
11 12 13 14 15 16 17 18 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 | |
SGDW
Bases: BaseOptimizer
Decoupled Weight Decay Regularization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.0001
|
momentum
|
float
|
float. momentum factor. |
0.0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
dampening
|
float
|
float. dampening for momentum. |
0.0
|
nesterov
|
bool
|
bool. enables Nesterov momentum. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
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 | |
ASGD
Bases: BaseOptimizer
Adaptive SGD with estimation of the local smoothness (curvature).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
amplifier
|
float
|
float. amplifier. |
0.02
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
theta
|
float
|
float. theta. |
1.0
|
dampening
|
float
|
float. dampening for momentum. |
1.0
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-05
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
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 340 341 342 343 | |
get_norms_by_group(group, device)
staticmethod
Get parameter & gradient norm by group.
Source code in pytorch_optimizer/optimizer/sgd.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
SignSGD
Bases: BaseOptimizer
Compressed Optimisation for Non-Convex Problems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
momentum
|
float
|
float. momentum factor (0.0 = SignSGD, >0 = Signum). |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
SGDSaI
Bases: BaseOptimizer
No More Adam: Learning Rate Scaling at Initialization is All You Need.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
momentum
|
float
|
float. coefficients used for computing running averages of gradient. |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | |
SGDP
Bases: BaseOptimizer
SGD + Slowing Down the Slowdown for Momentum Optimizers on Scale-invariant Weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
momentum
|
float
|
float. momentum factor. |
0.0
|
dampening
|
float
|
float. dampening for momentum. |
0.0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
delta
|
float
|
float. threshold that determines whether a set of parameters is scale invariant or not. |
0.1
|
wd_ratio
|
float
|
float. relative weight decay applied on scale-invariant parameters compared to that applied on scale-variant parameters. |
0.1
|
nesterov
|
bool
|
bool. enables nesterov momentum. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamp.py
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 | |
Shampoo
Bases: BaseOptimizer
Preconditioned Stochastic Tensor Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
momentum
|
float
|
float. momentum. |
0.0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
preconditioning_compute_steps
|
int
|
int. performance tuning params for controlling memory and compute requirements. How often to compute pre-conditioner. |
1
|
matrix_eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/shampoo.py
15 16 17 18 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 | |
ScalableShampoo
Bases: BaseOptimizer
Scalable Preconditioned Stochastic Tensor Optimization.
This version of Scalable Shampoo Optimizer aims for a single GPU environment, not for a distributed environment
or XLA devices. So, the original intention is to compute pre-conditioners asynchronously on the distributed
CPUs, but this implementation calculates them which takes 99% of the optimization time on a GPU synchronously.
Still, it is much faster than the previous Shampoo Optimizer because using coupled Newton iteration when
computing G^{-1/p} matrices while the previous one uses SVD which is really slow.
Also, this implementation offers
1. lots of plug-ins (e.g. gradient grafting, type of pre-conditioning, etc)
2. not-yet implemented features in the official Pytorch code.
3. readable, organized, clean code.
Reference : https://github.com/google-research/google-research/blob/master/scalable_shampoo/pytorch/shampoo.py.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. beta1, beta2. |
(0.9, 0.999)
|
moving_average_for_momentum
|
bool
|
bool. perform moving_average for momentum (beta1). |
False
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
decoupled_weight_decay
|
bool
|
bool. use decoupled weight_decay. |
False
|
decoupled_learning_rate
|
bool
|
bool. use decoupled lr, otherwise couple it w/ preconditioned gradient. |
True
|
inverse_exponent_override
|
int
|
int. fixed exponent for pre-conditioner, if > 0. |
0
|
start_preconditioning_step
|
int
|
int. |
25
|
preconditioning_compute_steps
|
int
|
int. performance tuning params for controlling memory and compute requirements. How often to compute pre-conditioner. Ideally, 1 is the best. However, the current implementation doesn't work on the distributed environment (there are no statistics & pre-conditioners sync among replicas), compute on the GPU (not CPU) and the precision is fp32 (not fp64). Also, followed by the paper, |
1000
|
statistics_compute_steps
|
int
|
int. How often to compute statistics. usually set to 1 (or 10). |
1
|
block_size
|
int
|
int. Block size for large layers (if > 0). Block size = 1 ==> AdaGrad (Don't do this, extremely inefficient!) Block size should be as large as feasible under memory/time constraints. |
512
|
skip_preconditioning_rank_lt
|
int
|
int. Skips preconditioning for parameters with rank less than this value. |
1
|
no_preconditioning_for_layers_with_dim_gt
|
int
|
int. avoid preconditioning large layers to reduce overall memory. |
8192
|
shape_interpretation
|
bool
|
bool. Automatic shape interpretation (for eg: [4, 3, 1024, 512] would result in 12 x [1024, 512] L and R statistics. Disabled by default which results in Shampoo constructing statistics [4, 4], [3, 3], [1024, 1024], [512, 512]. |
True
|
graft_type
|
int
|
int. type of grafting (SGD or AdaGrad or RMSProp or SQRT_N or None). |
SGD
|
pre_conditioner_type
|
int
|
int. type of pre-conditioner. |
ALL
|
nesterov
|
bool
|
bool. Nesterov momentum. |
True
|
diagonal_eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-10
|
matrix_eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
use_svd
|
bool
|
bool. use SVD instead of Schur-Newton method to calculate M^{-1/p}. Theoretically, Schur-Newton method is faster than SVD method. However, the inefficiency of the loop code and proper svd kernel, SVD is much faster in some cases (usually in case of small models). see https://github.com/kozistr/pytorch_optimizer/pull/103 |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/shampoo.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 | |
SM3
Bases: BaseOptimizer
Memory-Efficient Adaptive Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.1
|
momentum
|
float
|
float. coefficient used to scale prior updates before adding. This drastically increases memory usage if |
0.0
|
beta
|
float
|
float. coefficient used for exponential moving averages. |
0.0
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-30
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sm3.py
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 | |
SOAP
Bases: BaseOptimizer
Improving and Stabilizing Shampoo using Adam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.003
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace |
(0.95, 0.95)
|
shampoo_beta
|
Optional[float]
|
Optional[float]. if not None, use this beta for the pre-conditioner (L and R in paper, state['GG'] below) moving average instead of betas[1]. |
None
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.01
|
precondition_frequency
|
int
|
int. how often to update the pre-conditioner. |
10
|
max_precondition_dim
|
int
|
int. maximum dimension of the pre-conditioner. Set to 10000, so that we exclude most common vocab sizes while including layers. |
10000
|
merge_dims
|
bool
|
bool. whether to merge dimensions of the pre-conditioner |
False
|
precondition_1d
|
bool
|
bool. whether to precondition 1D gradients. |
False
|
correct_bias
|
bool
|
bool. whether to correct bias in Adam. |
True
|
normalize_gradient
|
bool
|
bool. whether to normalize the gradients. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/soap.py
13 14 15 16 17 18 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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
get_orthogonal_matrix_qr(state, max_precondition_dim=10000, merge_dims=False)
Compute the eigen-bases of the pre-conditioner using one round of power iteration.
Source code in pytorch_optimizer/optimizer/soap.py
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 | |
SophiaH
Bases: BaseOptimizer
Second-order Clipped Stochastic Optimization.
Requires `loss.backward(create_graph=True)` in order to calculate hessians.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.06
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.96, 0.99)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
p
|
float
|
float. clip effective (applied) gradient (p). |
0.01
|
update_period
|
int
|
int. number of steps after which to apply hessian approximation. |
10
|
num_samples
|
int
|
int. times to sample |
1
|
hessian_distribution
|
HUTCHINSON_G
|
HUTCHINSON_G. type of distribution to initialize hessian. |
'gaussian'
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-12
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sophia.py
10 11 12 13 14 15 16 17 18 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 | |
SPAM
Bases: BaseOptimizer
Spike-Aware Adam with Momentum Reset for Stable LLM Training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
density
|
float
|
float. density parameter. only used for 2d parameters (e.g. Linear). |
1.0
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
warmup_epoch
|
int
|
int: number of epochs to warm up. defaults to 50. |
50
|
threshold
|
int
|
int. threshold for gradient masking. defaults to 5000. |
5000
|
grad_accu_steps
|
int
|
int. gradient accumulation steps before threshold-based masking applies. defaults to 20. |
20
|
update_proj_gap
|
int
|
int. update projection gap. |
500
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/spam.py
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 | |
init_masks()
Initialize random masks for each parameter group that has 'density'.
Source code in pytorch_optimizer/optimizer/spam.py
167 168 169 170 171 172 173 174 175 176 177 178 | |
initialize_random_rank_boolean_tensor(m, n, density, device)
staticmethod
Create an (m x n) boolean tensor with density fraction of True entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
m
|
int
|
int. number of rows. |
required |
n
|
int
|
int. number of columns. |
required |
density
|
float
|
float. fraction of True entries. 1.0 means all True. |
required |
device
|
device
|
torch.device. device. |
required |
Source code in pytorch_optimizer/optimizer/spam.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
update_mask_random(p, old_mask)
Update a random mask.
Create a new random mask with the same density, compute overlap ratio with old_mask, and update the EMA for the overlap region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Tensor
|
torch.Tensor. parameter to which the mask is applied. |
required |
old_mask
|
Tensor
|
torch.Tensor. previous binary mask. |
required |
Source code in pytorch_optimizer/optimizer/spam.py
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 | |
update_masks()
Update masks in each parameter group that has 'density'.
The new mask is selected randomly, and the overlap ratio with the old mask is printed.
Source code in pytorch_optimizer/optimizer/spam.py
155 156 157 158 159 160 161 162 163 164 165 | |
StableSPAM
Bases: BaseOptimizer
How to Train in 4-Bit More Stably than 16-Bit Adam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
gamma1
|
float
|
float. gamma1 parameter. |
0.7
|
gamma2
|
float
|
float. gamma2 parameter. |
0.9
|
theta
|
float
|
float. theta parameter. |
0.999
|
t_max
|
Optional[int]
|
Optional[int]. total number of steps. |
None
|
eta_min
|
float
|
float. eta_min of CosineDecay. |
0.5
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
update_proj_gap
|
int
|
int. update projection gap. |
1000
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/spam.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
SRMM
Bases: BaseOptimizer
Stochastic regularized majorization-minimization with weakly convex and multi-convex surrogates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
beta
|
float
|
float. adaptivity weight. |
0.5
|
memory_length
|
Optional[int]
|
Optional[int]. internal memory length for moving average. None for no refreshing. |
100
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/srmm.py
10 11 12 13 14 15 16 17 18 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 | |
SWATS
Bases: BaseOptimizer
Improving Generalization Performance by Switching from Adam to SGD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
ams_bound
|
bool
|
bool. whether to use the ams_bound variant of this algorithm from the paper. |
False
|
nesterov
|
bool
|
bool. enables Nesterov momentum. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/swats.py
10 11 12 13 14 15 16 17 18 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 | |
TAM
Bases: BaseOptimizer
Torque-Aware Momentum.
:parma decay_rate: float. smoothing decay rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
momentum
|
float
|
float. coefficients used for computing running averages of gradient. |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/tam.py
9 10 11 12 13 14 15 16 17 18 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 | |
AdaTAM
Bases: BaseOptimizer
Adaptive Torque-Aware Momentum.
:parma decay_rate: float. smoothing decay rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/tam.py
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 | |
Tiger
Bases: BaseOptimizer
A Tight-fisted Optimizer, an optimizer that is extremely budget-conscious.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.001
|
beta
|
float
|
float. coefficients used for computing running averages of gradient and the squared hessian trace. |
0.965
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/tiger.py
8 9 10 11 12 13 14 15 16 17 18 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 | |
TRAC
Bases: BaseOptimizer
A Parameter-Free Optimizer for Lifelong Reinforcement Learning.
Example:
Here's an example::
model = YourModel()
optimizer = TRAC(AdamW(model.parameters()))
for input, output in data:
optimizer.zero_grad()
loss = loss_fn(model(input), output)
loss.backward()
optimizer.step()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
OPTIMIZER_INSTANCE_OR_CLASS
|
OPTIMIZER_INSTANCE_OR_CLASS. base optimizer. |
required |
betas
|
List[float]
|
List[float]. list of beta values. |
(0.9, 0.99, 0.999, 0.9999, 0.99999, 0.999999)
|
num_coefs
|
int
|
int. the number of polynomial coefficients to use in the approximation. |
128
|
s_prev
|
float
|
float. initial scale value. |
1e-08
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
Source code in pytorch_optimizer/optimizer/trac.py
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 | |
VSGD
Bases: BaseOptimizer
Variational Stochastic Gradient Descent for Deep Neural Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.1
|
ghattg
|
float
|
float. prior variance ratio between ghat and g, Var(ghat_t-g_t)/Var(g_t-g_{t-1}). |
30.0
|
ps
|
float
|
float. prior strength. |
1e-08
|
tau1
|
float
|
float. remember rate for the gamma parameters of g. |
0.81
|
tau2
|
float
|
float. remember rate for the gamma parameter of ghat. |
0.9
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | |
WSAM
Bases: BaseOptimizer
Sharpness-Aware Minimization Revisited: Weighted Sharpness as a Regularization Term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Union[Module, DistributedDataParallel]
|
Union[torch.nn.Module, torch.nn.DataParallel]. the model instance. DDP model is recommended to make |
required |
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
OPTIMIZER
|
Optimizer. base optimizer. |
required |
rho
|
float
|
float. size of the neighborhood for computing the max loss. |
0.05
|
gamma
|
float
|
float. weighted factor gamma / (1 - gamma) of the sharpness term. 0.8 ~ 0.95 is the optimal. |
0.9
|
adaptive
|
bool
|
bool. element-wise adaptive SAM. |
False
|
decouple
|
bool
|
bool. whether to perform a decoupled sharpness regularization. |
True
|
max_norm
|
Optional[float]
|
Optional[float]. max norm of the gradients. |
None
|
eps
|
float
|
float. term added to the denominator of WSAM to improve numerical stability. |
1e-12
|
kwargs
|
Dict. parameters for optimizer. |
{}
|
Source code in pytorch_optimizer/optimizer/sam.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | |
Yogi
Bases: BaseOptimizer
Decoupled Weight Decay Regularization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
PARAMETERS
|
PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
float. learning rate. |
0.01
|
betas
|
BETAS
|
BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
initial_accumulator
|
float
|
float. initial values for first and second moments. |
1e-06
|
weight_decay
|
float
|
float. weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
bool. the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
bool. fix weight decay. |
False
|
eps
|
float
|
float. term added to the denominator to improve numerical stability. |
0.001
|
maximize
|
bool
|
bool. maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/yogi.py
10 11 12 13 14 15 16 17 18 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 | |