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
|
model. |
required |
optimizer_name
|
str
|
optimizer name. |
required |
lr
|
float
|
learning rate. |
0.001
|
weight_decay
|
float
|
weight decay. |
0.0
|
wd_ban_list
|
List[str]
|
weight decay ban list by layer. |
('bias', 'LayerNorm.bias', 'LayerNorm.weight')
|
use_lookahead
|
bool
|
use Lookahead. |
False
|
use_orthograd
|
bool
|
use OrthoGrad. |
False
|
**kwargs
|
dict
|
optimizer parameters. |
{}
|
Source code in pytorch_optimizer/optimizer/__init__.py
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 | |
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]
|
model or parameters. |
required |
weight_decay
|
float
|
weight decay. |
required |
wd_ban_list
|
List[str]
|
weight decay ban list. |
('bias', 'LayerNorm.bias', 'LayerNorm.weight')
|
Returns:
| Name | Type | Description |
|---|---|---|
ParamsT |
ParamsT
|
optimizer parameters. |
Source code in pytorch_optimizer/optimizer/__init__.py
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 | |
A2Grad
Bases: BaseOptimizer
Optimal Adaptive and Accelerated Stochastic Gradient Descent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
Optional[float]
|
Learning rate. No needed. |
None
|
beta
|
float
|
Beta. |
10.0
|
lips
|
float
|
Lipschitz constant. |
10.0
|
rho
|
float
|
Represents the degree of weighting decrease, a constant smoothing factor between 0 and 1. |
0.5
|
variant
|
str
|
Variant of A2Grad optimizer. One of 'uni', 'inc', or 'exp'. |
'uni'
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 134 135 | |
AccSGD
Bases: BaseOptimizer
Accelerating Stochastic Gradient Descent For Least Squares Regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
kappa
|
float
|
ratio of long to short step. |
1000.0
|
xi
|
float
|
statistical advantage parameter. |
10.0
|
constant
|
float
|
any small constant under 1. |
0.7
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
maximize
|
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 113 114 | |
AdaBelief
Bases: BaseOptimizer
Adapting Step-sizes by the Belief in Observed Gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
rectify
|
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
|
Perform SGD update when variance of gradient is high. |
True
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-16
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adabelief.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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
AdaBound
Bases: BaseOptimizer
Adaptive Gradient Methods with Dynamic Bound of Learning Rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
final_lr
|
float
|
Final learning rate. |
0.1
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
gamma
|
float
|
Convergence speed of the bound functions. |
0.001
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 152 153 | |
AdaDelta
Bases: BaseOptimizer
An Adaptive Learning Rate Method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
1.0
|
rho
|
float
|
Coefficient used for computing a running average of squared gradients. |
0.9
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 119 120 | |
AdaFactor
Bases: BaseOptimizer
Adaptive Learning Rates with Sublinear Memory Cost with some tweaks.
PyTorch implementation of BigVision's AdaFactor variant
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Union[Tuple[None, float], Tuple[float, float], Tuple[float, float, float]]
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. If beta1 is None, first momentum will be skipped. beta2 is an upper bound cap. |
(0.9, 0.999)
|
decay_rate
|
float
|
Coefficient used to compute running averages of squared gradient. |
-0.8
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
clip_threshold
|
float
|
Threshold of root-mean-square of final gradient update. |
1.0
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
scale_parameter
|
bool
|
If True, the learning rate is scaled by root-mean-square of parameter. |
True
|
relative_step
|
bool
|
If True, time-dependent learning rate is computed instead of external learning rate. |
True
|
warmup_init
|
bool
|
Time-dependent learning rate computation depends on whether warm-up initialization is being used. |
False
|
eps1
|
float
|
Term added to the denominator to improve numerical stability. |
1e-30
|
eps2
|
float
|
Term added to the denominator to improve numerical stability. |
0.001
|
momentum_dtype
|
dtype
|
Type of momentum variable. In the ViT paper, it was observed that storing momentum in half-precision (bfloat16 type) does not affect training dynamics and reduces optimizer overhead from 2-fold to 1.5-fold. |
bfloat16
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adafactor.py
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 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 | |
get_lr(relative_step_size, rms, scale_parameter)
Get the learning rate(s).
Source code in pytorch_optimizer/optimizer/adafactor.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
get_options(shape)
staticmethod
Get factored.
Source code in pytorch_optimizer/optimizer/adafactor.py
165 166 167 168 | |
AdaGC
Bases: BaseOptimizer
Improving Training Stability for Large Language Model Pretraining.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
beta
|
float
|
Smoothing coefficient for the exponential moving average (EMA). |
0.98
|
lambda_abs
|
float
|
Absolute clipping threshold to prevent unstable updates from gradient explosions. |
1.0
|
lambda_rel
|
float
|
Relative clipping threshold to prevent unstable updates from gradient explosions. |
1.05
|
warmup_steps
|
int
|
Number of warmup steps. |
100
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.1
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 156 157 | |
AdaGO
Bases: BaseOptimizer
AdaGrad Meets Muon: Adaptive Stepsizes for Orthogonal Updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
The parameters to be optimized by Muon. |
required |
lr
|
float
|
Learning rate. |
0.05
|
momentum
|
float
|
The momentum used by the internal SGD. |
0.95
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
nesterov
|
bool
|
Whether to use nesterov momentum. |
False
|
gamma
|
float
|
Gamma factor. Empirically, AdaGO performs robustly across a wide range of gamma values. |
10.0
|
eps
|
float
|
Epsilon value. Lower bound eps > 0 on the stepsizes. |
0.0005
|
ns_steps
|
int
|
The number of Newton-Schulz iterations to run. (5 is probably always enough) |
5
|
ns_coeffs
|
NewtonSchulzWeights
|
Newton-Schulz coefficients or preset name. |
'original'
|
use_adjusted_lr
|
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_lr
|
float
|
The learning rate for the internal AdamW. |
0.0003
|
adamw_betas
|
tuple
|
The betas for the internal AdamW. |
(0.9, 0.95)
|
adamw_wd
|
float
|
The weight decay for the internal AdamW. |
0.0
|
adamw_eps
|
float
|
The epsilon for the internal AdamW. |
1e-10
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Example
from pytorch_optimizer import AdaGO
hidden_weights = [p for p in model.body.parameters() if p.ndim >= 2] hidden_gains_biases = [p for p in model.body.parameters() if p.ndim < 2] non_hidden_params = [*model.head.parameters(), *model.embed.parameters()]
param_groups = [ dict(params=hidden_weights, lr=0.02, weight_decay=0.01, use_muon=True), dict( params=hidden_gains_biases + non_hidden_params, lr=3e-4, betas=(0.9, 0.95), weight_decay=0.01, use_muon=False, ), ]
optimizer = AdaGO(param_groups)
Source code in pytorch_optimizer/optimizer/muon.py
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 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 | |
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
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.1
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
hessian_power
|
float
|
Exponent applied to the Hessian trace for scaling updates. |
1.0
|
update_period
|
int
|
Number of steps after which to apply the Hessian approximation. |
1
|
num_samples
|
int
|
Number of times to sample |
1
|
hessian_distribution
|
HutchinsonG
|
Type of distribution used to initialize the Hutchinson trace estimator. |
'rademacher'
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-16
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 154 155 | |
Adai
Bases: BaseOptimizer
Disentangling the Effects of Adaptive Learning Rate and Momentum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
(ParamsT). Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.1, 0.99)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
stable_weight_decay
|
bool
|
Perform stable weight decay. |
False
|
dampening
|
float
|
Dampening for momentum. When dampening < 1, it exhibits adaptive-moment behavior. |
1.0
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
0.001
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 183 184 | |
Adalite
Bases: BaseOptimizer
Adalite optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
g_norm_min
|
float
|
Minimum gradient norm threshold. |
1e-10
|
ratio_min
|
float
|
Minimum ratio value for adaptive adjustment. |
0.0001
|
tau
|
float
|
Time constant controlling parameter smoothing or decay behavior. |
1.0
|
eps1
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
eps2
|
float
|
Additional term added to the denominator for extra numerical stability. |
1e-10
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 172 173 | |
AdaLOMO
Bases: BaseOptimizer
Low-memory Optimization with Adaptive Learning Rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PyTorch model. |
required |
lr
|
float
|
Learning rate. |
0.001
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
loss_scale
|
float
|
Loss scale. |
2.0 ** 10
|
clip_threshold
|
float
|
Threshold of root-mean-square of final gradient update. |
1.0
|
decay_rate
|
float
|
Coefficient used to compute running averages of square gradient. |
-0.8
|
clip_grad_norm
|
Optional[float]
|
Clip gradient norm. |
None
|
clip_grad_value
|
Optional[float]
|
Clip gradient value. |
None
|
eps1
|
float
|
Term added to the denominator to improve numerical stability. |
1e-30
|
eps2
|
float
|
Term added to the denominator to improve numerical stability. |
0.001
|
Source code in pytorch_optimizer/optimizer/lomo.py
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 469 470 471 472 473 474 475 476 477 478 | |
AdaMax
Bases: BaseOptimizer
An Adaptive and Momental Bound Method for Stochastic Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 138 139 | |
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
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 140 141 | |
AdamG
Bases: BaseOptimizer
Towards Stability of Parameter-free Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
1.0
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.95, 0.999, 0.95)
|
p
|
float
|
The p value in the numerator function |
0.2
|
q
|
float
|
The q value in the numerator function |
0.24
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 138 139 | |
s(p)
Numerator function f(x) = p * x^q.
Source code in pytorch_optimizer/optimizer/adamg.py
86 87 88 | |
AdamMini
Bases: BaseOptimizer
Use Fewer Learning Rates To Gain More.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
Model instance. |
required |
model_sharding
|
bool
|
Set to True if you are using model parallelism with more than 1 GPU, including FSDP and zero_1, zero_2, zero_3 in DeepSpeed. Set to False otherwise. |
False
|
lr
|
float
|
Learning rate. |
1.0
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.1
|
num_embeds
|
int
|
Number of embedding dimensions. Could be unspecified if training non-transformer models. |
2048
|
num_heads
|
int
|
Number of attention heads. Could be unspecified if training non-transformer models. |
32
|
num_query_groups
|
Optional[int]
|
Number of query groups in Group Query Attention (GQA). If not specified, defaults to num_heads. Could be unspecified for non-transformer models. |
None
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 | |
AdaMod
Bases: BaseOptimizer
An Adaptive and Momental Bound Method for Stochastic Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. beta3 is the smoothing coefficient for adaptive learning rates. |
(0.9, 0.99, 0.9999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 137 138 | |
AdamP
Bases: BaseOptimizer
Slowing Down the Slowdown for Momentum Optimizers on Scale-invariant Weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
delta
|
float
|
Threshold that determines whether a set of parameters is scale-invariant or not. |
0.1
|
wd_ratio
|
float
|
Relative weight decay applied on scale-invariant parameters compared to that applied on scale-variant parameters. |
0.1
|
nesterov
|
bool
|
Enables Nesterov momentum. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamp.py
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 | |
AdamS
Bases: BaseOptimizer
Adam with stable weight decay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of the gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0001
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
ams_bound
|
bool
|
Whether to use the AMSBound variant of Adam. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 182 183 | |
AdaMuon
Bases: BaseOptimizer
Adaptive Muon optimizer.
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
|
ParamsT
|
The parameters to be optimized by Muon. |
required |
lr
|
float
|
Learning rate. |
0.02
|
betas
|
tuple
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.95)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
ns_steps
|
int
|
The number of Newton-Schulz iterations to run. (5 is probably always enough) |
5
|
ns_coeffs
|
NewtonSchulzWeights
|
Newton-Schulz coefficients or preset name. |
'original'
|
use_adjusted_lr
|
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_lr
|
float
|
The learning rate for the internal AdamW. |
0.0003
|
adamw_betas
|
tuple
|
The betas for the internal AdamW. |
(0.9, 0.999)
|
adamw_wd
|
float
|
The weight decay for the internal AdamW. |
0.0
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-10
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Example
from pytorch_optimizer import AdaMuon
hidden_weights = [p for p in model.body.parameters() if p.ndim >= 2] hidden_gains_biases = [p for p in model.body.parameters() if p.ndim < 2] non_hidden_params = [*model.head.parameters(), *model.embed.parameters()]
param_groups = [ dict(params=hidden_weights, lr=0.02, weight_decay=0.01, use_muon=True), dict( params=hidden_gains_biases + non_hidden_params, lr=3e-4, betas=(0.9, 0.95), weight_decay=0.01, use_muon=False, ), ]
optimizer = AdaMuon(param_groups)
Source code in pytorch_optimizer/optimizer/muon.py
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 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 | |
AdamWSN
Bases: BaseOptimizer
Lean and Mean Adaptive Optimization via Subset-Norm and Subspace-Momentum with Convergence Guarantees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
subset_size
|
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
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Example
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)
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 183 | |
Adan
Bases: BaseOptimizer
Adaptive Nesterov Momentum Algorithm for Faster Optimizing Deep Models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.98, 0.92, 0.99)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Decoupled weight decay. |
False
|
max_grad_norm
|
float
|
Maximum gradient norm to clip. |
0.0
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 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 | |
AdaNorm
Bases: BaseOptimizer
Symbolic Discovery of Optimization Algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.99)
|
r
|
float
|
EMA factor. Preferred values are between 0.9 and 0.99. |
0.95
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 152 153 | |
AdaPNM
Bases: BaseOptimizer
Adam + Positive-Negative Momentum Optimizers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999, 1.0)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Use decoupled weight decay. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
True
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 161 162 | |
AdaShift
Bases: BaseOptimizer
Decorrelation and Convergence of Adaptive Learning Rate Methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
keep_num
|
int
|
Number of gradients used to compute first moment estimation. |
10
|
reduce_func
|
Optional[Callable]
|
Function applied to squared gradients to reduce correlation. If None, no function is applied. |
max
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-10
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 126 127 | |
AdaSmooth
Bases: BaseOptimizer
An Adaptive Learning Rate Method based on Effective Ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.5, 0.99)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 131 132 | |
AdaTAM
Bases: BaseOptimizer
Adaptive Torque-Aware Momentum.
:param params: PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. :param lr: float. learning rate. :param betas: BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. :parma decay_rate: float. smoothing decay rate. :param weight_decay: float. weight decay (L2 penalty). :param weight_decouple: bool. the optimizer uses decoupled weight decay as in AdamW. :param fixed_decay: bool. fix weight decay. :param eps: float. term added to the denominator to improve numerical stability. :param maximize: bool. maximize the objective with respect to the params, instead of minimizing.
Source code in pytorch_optimizer/optimizer/tam.py
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 | |
AdEMAMix
Bases: BaseOptimizer
Better, Faster, Older.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999, 0.9999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
alpha
|
float
|
Usually between 4 and 10 would work well. |
5.0
|
t_alpha_beta3
|
Optional[float]
|
Total number of iterations preferred when needed. |
None
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 166 167 | |
ADOPT
Bases: BaseOptimizer
Modified Adam Can Converge with Any β2 with the Optimal Rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.9999)
|
clip_lambda
|
Callable[[float], float]
|
Function to clip gradient. Default is |
lambda step: pow(step, 0.25)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 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 | |
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
|
Parameter tensor. |
required |
grad
|
Tensor
|
Gradient tensor. |
required |
agc_eps
|
float
|
AGC epsilon to clip the norm of the parameter. |
0.001
|
agc_clip_val
|
float
|
Norm clip value. |
0.01
|
eps
|
float
|
Small term to prevent division by zero, unrelated 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 23 24 | |
AggMo
Bases: BaseOptimizer
Aggregated Momentum: Stability Through Passive Damping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.0, 0.9, 0.99)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 106 107 | |
Aida
Bases: BaseOptimizer
A DNN Optimizer that Improves over AdaBelief by Suppression of the Adaptive Stepsize Range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
k
|
int
|
Number of vectors projected per iteration. |
2
|
xi
|
float
|
Term used in vector projections to avoid division by zero. |
1e-20
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
rectify
|
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
|
Perform SGD update when variance of gradient is high. |
True
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 197 198 | |
Alice
Bases: BaseOptimizer
Adaptive low-dimensional subspace estimation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.02
|
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
|
scaler. |
0.3
|
alpha_c
|
float
|
compensation scaler. |
0.4
|
update_interval
|
int
|
update interval. |
200
|
rank
|
int
|
rank. |
256
|
gamma
|
float
|
limiter threshold. |
1.01
|
leading_basis
|
int
|
leading basis. |
40
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
eps
|
float
|
term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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
218 219 220 221 222 223 224 225 226 227 | |
AliG
Bases: BaseOptimizer
Adaptive Learning Rates for Interpolation with Gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
max_lr
|
Optional[float]
|
Maximum learning rate. |
None
|
projection_fn
|
Callable
|
Projection function to enforce constraints. |
None
|
momentum
|
float
|
Momentum factor. |
0.0
|
adjusted_momentum
|
bool
|
If True, use PyTorch-like momentum instead of standard Nesterov momentum. |
False
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 130 131 | |
compute_step_size(loss)
Compute step_size.
Source code in pytorch_optimizer/optimizer/alig.py
79 80 81 82 83 84 85 | |
Amos
Bases: BaseOptimizer
An Adam-style Optimizer with Adaptive Weight Decay towards Model-Oriented Scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
beta
|
float
|
A float slightly less than 1. Recommended to set |
0.999
|
momentum
|
float
|
Exponential decay rate for optional moving average of updates. |
0.0
|
extra_l2
|
float
|
Additional L2 regularization. |
0.0
|
c_coef
|
float
|
Coefficient for decay_factor_c. |
0.25
|
d_coef
|
float
|
Coefficient for decay_factor_d. |
0.25
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-18
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/amos.py
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 230 231 232 233 234 235 236 | |
get_scale(p)
staticmethod
Get expected scale for model weights.
Source code in pytorch_optimizer/optimizer/amos.py
96 97 98 99 100 101 102 103 | |
Ano
Bases: BaseOptimizer
Ano optimizer with adaptive momentum and sign-based updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.0001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared gradient. |
(0.92, 0.99)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
logarithmic_schedule
|
bool
|
Enable adaptive beta1 scheduling based on step count. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/ano.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 | |
APOLLO
Bases: BaseOptimizer
SGD-like Memory, AdamW-level Performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
correct_bias
|
bool
|
Whether to correct bias in Adam. |
True
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/apollo.py
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 | |
ApolloDQN
Bases: BaseOptimizer
An Adaptive Parameter-wise Diagonal Quasi-Newton Method for Nonconvex Stochastic Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
init_lr
|
Optional[float]
|
Initial learning rate (default lr / 1000). |
1e-05
|
beta
|
float
|
Coefficient used for computing running averages of gradient. |
0.9
|
rebound
|
str
|
Rectified bound for diagonal Hessian. Options: 'constant', 'belief'. |
'constant'
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decay_type
|
str
|
Type of weight decay. Options: 'l2', 'decoupled', 'stable'. |
'l2'
|
warmup_steps
|
int
|
Number of warmup steps. |
500
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
0.0001
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 161 162 | |
ASGD
Bases: BaseOptimizer
Adaptive SGD with estimation of the local smoothness (curvature).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.01
|
amplifier
|
float
|
amplifier. |
0.02
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
theta
|
float
|
theta. |
1.0
|
dampening
|
float
|
dampening for momentum. |
1.0
|
eps
|
float
|
term added to denominator to improve numerical stability. |
1e-05
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
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 | |
get_norms_by_group(group, device)
staticmethod
Get parameter & gradient norm by group.
Source code in pytorch_optimizer/optimizer/sgd.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
AvaGrad
Bases: BaseOptimizer
Domain-independent Dominance of Adaptive Methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.1
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
0.1
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 143 144 145 | |
BCOS
Bases: BaseOptimizer
Stochastic Approximation with Block Coordinate Optimal Stepsizes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
beta
|
float
|
smoothing factor in computing the momentum and EMA estimators. |
0.9
|
beta2
|
Optional[float]
|
|
None
|
mode
|
Mode
|
algorithmic mode of BCOS, must be one of the three choices. 'g': use gradient as search direction and EMA estimator for its 2nd moment (equivalent to RMSprop). 'm': use momentum as search direction and EMA estimator for its 2nd moment (using same beta). 'c': use momentum as search direction and conditional estimator for its 2nd moment. |
'c'
|
simple_cond
|
bool
|
whether use simple alternative in BCOS-c variant. |
False
|
weight_decay
|
float
|
weight decay regularization strength. |
0.1
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/bcos.py
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 | |
BSAM
Bases: BaseOptimizer
SAM as an Optimal Relaxation of Bayes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
num_data
|
int
|
number of training data. |
required |
lr
|
float
|
learning rate. |
0.5
|
betas
|
Betas
|
coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0001
|
rho
|
float
|
size of the neighborhood for computing the max loss. |
0.05
|
adaptive
|
bool
|
element-wise Adaptive SAM. |
False
|
damping
|
float
|
damping to stabilize the method. |
0.1
|
kwargs
|
Dict
|
parameters for optimizer. |
{}
|
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()
Source code in pytorch_optimizer/optimizer/sam.py
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 | |
CAME
Bases: BaseOptimizer
Confidence-guided Adaptive Memory Efficient Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.0002
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999, 0.9999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
clip_threshold
|
float
|
Threshold of root-mean-square of final gradient update. |
1.0
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
eps1
|
float
|
Term added to the denominator to improve numerical stability. |
1e-30
|
eps2
|
float
|
Term added to the denominator to improve numerical stability. |
1e-16
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 212 213 | |
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
121 122 123 124 125 126 127 128 129 130 | |
get_options(shape)
staticmethod
Get factored.
Source code in pytorch_optimizer/optimizer/came.py
111 112 113 114 | |
get_rms(x)
staticmethod
Get RMS.
Source code in pytorch_optimizer/optimizer/came.py
116 117 118 119 | |
centralize_gradient(grad, gc_conv_only=False)
Gradient Centralization (GC).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grad
|
Tensor
|
Gradient tensor. |
required |
gc_conv_only
|
bool
|
If False, apply GC to both convolutional and fully connected layers; if True, apply only to convolutional layers. |
False
|
Source code in pytorch_optimizer/optimizer/gradient_centralization.py
4 5 6 7 8 9 10 11 12 13 14 15 | |
Conda
Bases: BaseOptimizer
Column-Normalized Adam for Training Large Language Models Faster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
update_proj_gap
|
int
|
Update projection gap. |
2000
|
scale
|
float
|
Galore projection scaling factor. |
1.0
|
projection_type
|
PROJECTION_TYPE
|
The type of the projection. |
'std'
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/conda.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 | |
DAdaptAdaGrad
Bases: BaseOptimizer
AdaGrad with D-Adaptation. Leave LR set to 1 unless you encounter instability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
1.0
|
momentum
|
float
|
Momentum factor. |
0.0
|
d0
|
float
|
Initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
growth_rate
|
float
|
Prevent the D estimate from growing faster than this multiplicative rate. |
float('inf')
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
0.0
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 251 252 | |
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
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
1.0
|
betas
|
Betas
|
Betas. |
(0.9, 0.999)
|
d0
|
float
|
Initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
growth_rate
|
float
|
Prevent the D estimate from growing faster than this multiplicative rate. |
float('inf')
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Use AdamW style weight decay. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
bias_correction
|
bool
|
Turn on Adam's bias correction. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
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 | |
DAdaptAdan
Bases: BaseOptimizer
Adan with D-Adaptation. Leave LR set to 1 unless you encounter instability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
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
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Decoupled weight decay. |
False
|
d0
|
float
|
Initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
growth_rate
|
float
|
Prevent the D estimate from growing faster than this multiplicative rate. Default is inf, for unrestricted. |
float('inf')
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 | |
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
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
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
|
Initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
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 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | |
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
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
1.0
|
momentum
|
float
|
Momentum. |
0.9
|
d0
|
float
|
Initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
growth_rate
|
float
|
Prevent the D estimate from growing faster than this multiplicative rate. |
float('inf')
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dadapt.py
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 569 570 571 572 573 574 575 576 577 578 579 580 | |
DeMo
Bases: SGD, BaseOptimizer
Decoupled Momentum Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
compression_decay
|
float
|
Compression decay. |
0.999
|
compression_top_k
|
int
|
Compression top-k. |
32
|
compression_chunk
|
int
|
Compression chunk size. |
64
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/demo.py
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 | |
find_dtype()
Return dtype of the parameter.
Source code in pytorch_optimizer/optimizer/demo.py
358 359 360 361 362 363 364 | |
DiffGrad
Bases: BaseOptimizer
An Optimization Method for Convolutional Neural Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
rectify
|
bool
|
Perform the rectified update similar to RAdam. |
False
|
n_sma_threshold
|
int
|
Recommended is 5. |
5
|
degenerated_to_sgd
|
bool
|
Degenerated to SGD. |
True
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 180 181 | |
DistributedMuon
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
|
ParamsT
|
The parameters to be optimized by Muon. |
required |
lr
|
float
|
Learning rate. |
0.02
|
momentum
|
float
|
The momentum used by the internal SGD. |
0.95
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
nesterov
|
bool
|
Whether to use nesterov momentum. |
True
|
ns_steps
|
int
|
The number of Newton-Schulz iterations to run. (5 is probably always enough) |
5
|
ns_coeffs
|
NewtonSchulzWeights
|
Newton-Schulz coefficients or preset name. |
'original'
|
use_adjusted_lr
|
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_lr
|
float
|
The learning rate for the internal AdamW. |
0.0003
|
adamw_betas
|
tuple
|
The betas for the internal AdamW. |
(0.9, 0.95)
|
adamw_wd
|
float
|
The weight decay for the internal AdamW. |
0.0
|
adamw_eps
|
float
|
The epsilon for the internal AdamW. |
1e-10
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Example
from pytorch_optimizer import DistributedMuon
hidden_weights = [p for p in model.body.parameters() if p.ndim >= 2] hidden_gains_biases = [p for p in model.body.parameters() if p.ndim < 2] non_hidden_params = [*model.head.parameters(), *model.embed.parameters()]
param_groups = [ dict(params=hidden_weights, lr=0.02, weight_decay=0.01, use_muon=True), dict( params=hidden_gains_biases + non_hidden_params, lr=3e-4, betas=(0.9, 0.95), weight_decay=0.01, use_muon=False, ), ]
optimizer = DistributedMuon(param_groups)
Source code in pytorch_optimizer/optimizer/muon.py
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 | |
DualAdam
Bases: BaseOptimizer
Combining Adam and its inverse counterpart to enhance generalization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and squared gradient. |
(0.9, 0.999)
|
switch_rate
|
float
|
Linear decay rate for the inverse Adam update contribution. |
0.01
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Whether to fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/dual_adam.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 | |
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 no overflow occurs within scale_window iterations, the loss scale will increase by scale_factor. |
2000
|
tolerance
|
float
|
Percentage of iterations that may overflow before decreasing the loss scale. |
0.0
|
threshold
|
float
|
Minimum threshold below which the loss scale will not decrease. |
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 91 92 93 | |
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
86 87 88 89 90 91 92 93 | |
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.
:param overflow: bool. adjust scales to prevent overflow.
Source code in pytorch_optimizer/optimizer/fp16.py
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 | |
EmoFact
Bases: BaseOptimizer
EmoFact optimizer.
EmoFact is inspired by AdaFactor and its VRAM-friendly design is something everyone loves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
use_shadow
|
bool
|
Whether to use shadow weights or not. |
False
|
shadow_weight
|
float
|
The weight of the shadow. |
0.05
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/emonavi.py
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 498 499 500 501 502 503 504 505 506 | |
EmoLynx
Bases: BaseOptimizer
EmoLynx optimizer.
Lynx was developed with inspiration from Lion and Tiger, which we deeply respect for their lightweight and intelligent design. It also integrates EmoNAVI to enhance its capabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize, or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.99)
|
use_shadow
|
bool
|
Whether to use shadow feature. |
False
|
shadow_weight
|
float
|
The weight of the shadow. |
0.05
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/emonavi.py
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 | |
EmoNavi
Bases: BaseOptimizer
An emotion-driven optimizer that feels loss and navigates accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
use_shadow
|
bool
|
Whether to use shadowing or not. |
False
|
shadow_weight
|
float
|
The weight of the shadow. |
0.05
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/emonavi.py
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 | |
EXAdam
Bases: BaseOptimizer
The Power of Adaptive Cross-Moments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/exadam.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 | |
FAdam
Bases: BaseOptimizer
Adam is a natural gradient optimizer using diagonal empirical Fisher information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
ParamsT to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.1
|
clip
|
float
|
Maximum norm of the gradient. |
1.0
|
p
|
float
|
Momentum factor. |
0.5
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
momentum_dtype
|
dtype
|
Dtype of momentum. |
float32
|
fim_dtype
|
dtype
|
Dtype of Fisher information matrix. |
float32
|
maximize
|
bool
|
Maximize the objective with respect to the parameters 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 135 136 | |
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
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
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 153 154 | |
FlashAdamW
Bases: BaseOptimizer
FlashOptim-style AdamW with compressed optimizer states.
The optimizer mirrors FlashOptim's AdamW semantics while keeping the implementation portable for environments where Triton kernels are not available. It supports grouped 8-bit optimizer-state compression, compressed state dicts, optional low-precision master-weight error correction, and fully LR-decoupled weight decay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and squared gradient. |
(0.9, 0.999)
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
weight_decay
|
float
|
Decoupled weight decay coefficient. |
0.01
|
decouple_lr
|
bool
|
Scale weight decay by |
False
|
quantize
|
bool
|
Store Adam moments as grouped 8-bit values plus fp16 scales. |
True
|
compress_state_dict
|
bool
|
Save quantized states in checkpoints when |
True
|
master_weight_bits
|
Optional[int]
|
Effective master-weight precision for bf16/fp16 parameters. Supports
|
None
|
check_numerics
|
bool
|
Raise if low-precision parameter updates are unlikely to alter the master weight. |
False
|
fused
|
bool
|
Placeholder for FlashOptim's Triton fused path. Currently unsupported in this portable backend. |
False
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/flash_adamw.py
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 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 | |
FOCUS
Bases: BaseOptimizer
First Order Concentrated Updating Scheme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
gamma
|
float
|
Controls the strength of the attraction. |
0.1
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
maximize
|
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 105 106 | |
FriendlySAM
Bases: BaseOptimizer
Friendly Sharpness-Aware Minimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
Optimizer
|
base optimizer. |
required |
rho
|
float
|
size of the neighborhood for computing the max loss. |
0.05
|
sigma
|
float
|
sigma of FriendlySAM. |
1.0
|
lmbda
|
float
|
lambda for FriendlySAM. |
0.9
|
adaptive
|
bool
|
element-wise Adaptive SAM. |
False
|
perturb_eps
|
float
|
eps for perturbation. |
1e-12
|
kwargs
|
Dict
|
parameters for optimizer. |
{}
|
Example
model = YourModel()
base_optimizer = Ranger21
optimizer = FriendlySAM(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 = FriendlySAM(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()
Source code in pytorch_optimizer/optimizer/sam.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 | |
Fromage
Bases: BaseOptimizer
On the distance between two neural networks and the stability of learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
p_bound
|
Optional[float]
|
Restricts the optimization to a bounded set. For example, a value of 2.0 restricts parameter norms to lie within 2x their initial norms, which helps regularize the model class. |
None
|
maximize
|
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 98 99 | |
FTRL
Bases: BaseOptimizer
Follow The Regularized Leader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
lr_power
|
float
|
Controls how the learning rate decreases during training. Use zero for a fixed learning rate. |
-0.5
|
beta
|
float
|
Beta value as described in the paper. |
0.0
|
lambda_1
|
float
|
L1 regularization parameter. |
0.0
|
lambda_2
|
float
|
L2 regularization parameter. |
0.0
|
maximize
|
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 104 | |
GaLore
Bases: BaseOptimizer
AdamW optimizer with GaLore projector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/galore.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 | |
get_supported_optimizers(filters=None)
Return list of available optimizer names, sorted alphabetically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
Optional[Union[str, List[str]]]
|
wildcard filter string that works with fmatch. if None, it will return the whole list. |
None
|
Source code in pytorch_optimizer/optimizer/__init__.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 | |
Grams
Bases: BaseOptimizer
Gradient Descent with Adaptive Momentum Scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay. |
True
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
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 119 120 | |
Gravity
Bases: BaseOptimizer
a Kinematic Approach on Optimization in Deep Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
alpha
|
float
|
Alpha controls the V initialization. |
0.01
|
beta
|
float
|
Beta will be used to compute running average of V. |
0.9
|
maximize
|
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 92 93 | |
GrokFastAdamW
Bases: BaseOptimizer
Accelerated Grokking by Amplifying Slow Gradients with AdamW.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.0001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.99)
|
grokfast
|
bool
|
Whether to use grokfast. |
True
|
grokfast_alpha
|
float
|
Momentum hyperparameter of the EMA. |
0.98
|
grokfast_lamb
|
float
|
Amplifying factor hyperparameter of the filter. |
2.0
|
grokfast_after_step
|
int
|
Warmup step for grokfast. |
0
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/grokfast.py
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 | |
GSAM
Bases: BaseOptimizer
Surrogate Gap Guided Sharpness-Aware Minimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
Optimizer
|
base optimizer. |
required |
model
|
Module
|
model. |
required |
alpha
|
float
|
rho alpha. |
0.4
|
rho_scheduler
|
Scheduler
|
rho scheduler. |
required |
adaptive
|
bool
|
element-wise Adaptive SAM. |
False
|
perturb_eps
|
float
|
epsilon for perturbation. |
1e-12
|
kwargs
|
Dict
|
parameters for optimizer. |
{}
|
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()
Source code in pytorch_optimizer/optimizer/sam.py
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 382 383 384 385 386 387 | |
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
|
loss function. |
required |
inputs
|
Tensor
|
inputs. |
required |
targets
|
Tensor
|
targets. |
required |
kwargs
|
Dict
|
keyword arguments. |
{}
|
Source code in pytorch_optimizer/optimizer/sam.py
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 | |
Kate
Bases: BaseOptimizer
Remove that Square Root: A New Efficient Scale-Invariant Version of AdaGrad.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
delta
|
float
|
Delta parameter, typically 0.0 or 1e-8. |
0.0
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Whether to fix weight decay. |
False
|
eps
|
float
|
Epsilon value for numerical stability. |
1e-08
|
maximize
|
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 118 119 | |
Kron
Bases: BaseOptimizer
PSGD with the Kronecker product pre-conditioner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
momentum
|
float
|
momentum factor. |
0.9
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
pre_conditioner_update_probability
|
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
|
max size for dim's pre-conditioner to be triangular. |
8192
|
min_ndim_triangular
|
int
|
minimum number of dimensions a layer needs to have triangular pre-conditioners. |
2
|
memory_save_mode
|
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
|
whether to send momentum into pre-conditioner update instead of raw gradients. |
True
|
mu_dtype
|
Optional[dtype]
|
dtype of the momentum accumulator. |
None
|
precondition_dtype
|
dtype
|
dtype of the pre-conditioner. |
float32
|
balance_prob
|
float
|
probability of performing balancing. |
0.01
|
maximize
|
bool
|
maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/psgd.py
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 | |
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
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
rectify
|
bool
|
Perform the rectified update similar to RAdam. |
False
|
degenerated_to_sgd
|
bool
|
Degenerate to SGD. |
False
|
n_sma_threshold
|
int
|
Recommended is 5. |
5
|
grad_averaging
|
bool
|
Whether to apply (1 - beta2) to gradient when calculating running averages of gradient. |
True
|
max_grad_norm
|
float
|
Max gradient norm to clip. |
1.0
|
adam
|
bool
|
Always use trust ratio = 1, which turns this into Adam. Useful for comparison purposes. |
False
|
pre_norm
|
bool
|
Perform pre-normalization of all gradients. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
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 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 | |
LaProp
Bases: BaseOptimizer
Separating Momentum and Adaptivity in Adam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.0004
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
centered
|
bool
|
If True, use the centered variant of Adam. |
False
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
eps
|
float
|
Epsilon value for numerical stability. |
1e-15
|
maximize
|
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 168 169 | |
LARS
Bases: BaseOptimizer
Layer-wise Adaptive Rate Scaling (no rate scaling or weight decay for parameters <= 1D).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
momentum
|
float
|
Momentum. |
0.9
|
dampening
|
float
|
Dampening for momentum. |
0.0
|
trust_coefficient
|
float
|
Trust coefficient. |
0.001
|
nesterov
|
bool
|
Enables Nesterov momentum. |
False
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/lars.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 | |
Lion
Bases: BaseOptimizer
Symbolic Discovery of Optimization Algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.0001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.99)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/lion.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 | |
load_ao_optimizer(optimizer)
Load TorchAO optimizer instance.
Source code in pytorch_optimizer/optimizer/__init__.py
299 300 301 302 303 304 305 306 307 308 309 310 | |
load_bnb_optimizer(optimizer)
Load bnb optimizer instance.
Source code in pytorch_optimizer/optimizer/__init__.py
278 279 280 281 282 283 284 285 286 | |
load_optimizer(optimizer)
Load optimizers.
Source code in pytorch_optimizer/optimizer/__init__.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
load_q_galore_optimizer(optimizer)
Load Q-GaLore optimizer instance.
Source code in pytorch_optimizer/optimizer/__init__.py
289 290 291 292 293 294 295 296 | |
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 usage: https://github.com/OpenLMLab/LOMO/blob/main/lomo/src/lomo_trainer.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PyTorch model. |
required |
lr
|
float
|
Learning rate. |
0.001
|
clip_grad_norm
|
Optional[float]
|
Gradient norm clipping value. |
None
|
clip_grad_value
|
Optional[float]
|
Gradient value clipping threshold. |
None
|
maximize
|
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 204 205 206 | |
Lookahead
Bases: BaseOptimizer
k steps forward, 1 step back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
OptimizerInstanceOrClass
|
Base optimizer. |
required |
k
|
int
|
Number of lookahead steps. |
5
|
alpha
|
float
|
Linear interpolation factor. |
0.5
|
pullback_momentum
|
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 139 140 141 142 143 | |
backup_and_load_cache()
Backup cache parameters.
Source code in pytorch_optimizer/optimizer/lookahead.py
84 85 86 87 88 89 90 91 | |
clear_and_load_backup()
Load backup parameters.
Source code in pytorch_optimizer/optimizer/lookahead.py
93 94 95 96 97 98 99 | |
load_state_dict(state)
Load state.
Source code in pytorch_optimizer/optimizer/lookahead.py
105 106 107 108 109 | |
LookSAM
Bases: BaseOptimizer
An Expeditiously Adaptive Parameter-Free Learner.
Leave LR set to 1 unless you encounter instability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
Optimizer
|
Base optimizer. |
required |
rho
|
float
|
Size of the neighborhood for computing the max loss. |
0.1
|
k
|
int
|
Lookahead step. |
10
|
alpha
|
float
|
Lookahead blending alpha. |
0.7
|
adaptive
|
bool
|
Element-wise Adaptive SAM. |
False
|
use_gc
|
bool
|
Perform gradient centralization, GCSAM variant. |
False
|
perturb_eps
|
float
|
Epsilon for perturbation. |
1e-12
|
kwargs
|
Dict
|
Additional parameters for optimizer. |
{}
|
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()
Source code in pytorch_optimizer/optimizer/sam.py
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 853 854 855 856 857 858 859 860 861 862 863 864 865 | |
LoRARite
Bases: BaseOptimizer
Robust Invariant Transformation Equilibration for LoRA optimization.
This optimizer expects LoRA factors in alternating order, such as lora_a_1, lora_b_1, lora_a_2, lora_b_2.
Unpaired parameters and pairs with missing gradients are skipped, matching common fine-tuning workflows where only
part of the model may receive gradients on a given step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of LoRA parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for first-moment and matrix second-moment estimates. |
(0.9, 0.999)
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
relative_epsilon
|
bool
|
Scale the root epsilon by the largest matrix second-moment eigenvalue. |
False
|
clip_unmagnified_grad
|
float
|
Global clipping threshold for unmagnified LoRA gradients. Disabled when 0. |
1.0
|
update_capping
|
float
|
Per-update RMS capping threshold after preconditioning. Disabled when 0. |
0.0
|
update_skipping
|
float
|
Skip unmagnified updates whose RMS is above this threshold. Disabled when 0. |
1.0
|
weight_decay
|
float
|
Coupled weight decay coefficient. |
0.0
|
apply_escape
|
bool
|
Apply the RITE escape correction when rotating second-moment bases. |
False
|
lora_l_dim
|
int
|
LoRA rank dimension for left factors. |
0
|
lora_r_dim
|
int
|
LoRA rank dimension for right factors. |
-1
|
maybe_inf_to_nan
|
bool
|
Convert infinite update statistics to NaN before threshold checks. |
True
|
balance_param
|
bool
|
Balance the norms of each LoRA factor pair after applying the update. |
False
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/lora_rite.py
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 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 | |
MADGRAD
Bases: BaseOptimizer
A Momentumized, Adaptive, Dual Averaged Gradient Method for Stochastic (slightly modified).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
weight_decay
|
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
|
float
|
Apply AdamW style decoupled weight decay. |
False
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/madgrad.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 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 | |
MARS
Bases: BaseOptimizer
Unleashing the Power of Variance Reduction for Training Large Models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.003
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.95, 0.99)
|
gamma
|
float
|
The scaling parameter that controls the strength of gradient correction. |
0.025
|
mars_type
|
MARS_TYPE
|
Type of MARS. Supported types are |
'adamw'
|
optimize_1d
|
bool
|
Whether MARS should optimize 1D parameters. |
False
|
lr_1d
|
float
|
Learning rate for AdamW when optimize_1d is set to False. |
0.003
|
betas_1d
|
Betas
|
Coefficients for running averages of gradient and squared Hessian for 1D. |
(0.9, 0.95)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decay_1d
|
float
|
Weight decay for 1D parameters. |
0.1
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
ams_bound
|
bool
|
Whether to use the AMSBound variant. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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 264 | |
MSVAG
Bases: BaseOptimizer
Dissecting Adam: The Sign, Magnitude and Variance of Stochastic Gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
beta
|
float
|
Moving average (momentum) constant (scalar tensor or float value). |
0.9
|
maximize
|
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 109 110 | |
get_rho(beta_power, beta)
staticmethod
Get rho.
Source code in pytorch_optimizer/optimizer/msvag.py
58 59 60 61 62 63 | |
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
|
ParamsT
|
The parameters to be optimized by Muon. |
required |
lr
|
float
|
Learning rate. |
0.02
|
momentum
|
float
|
The momentum used by the internal SGD. |
0.95
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
nesterov
|
bool
|
Whether to use nesterov momentum. |
True
|
ns_steps
|
int
|
The number of Newton-Schulz iterations to run. (5 is probably always enough) |
5
|
ns_coeffs
|
NewtonSchulzWeights
|
Newton-Schulz coefficients or preset name. |
'original'
|
use_adjusted_lr
|
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_lr
|
float
|
The learning rate for the internal AdamW. |
0.0003
|
adamw_betas
|
tuple
|
The betas for the internal AdamW. |
(0.9, 0.95)
|
adamw_wd
|
float
|
The weight decay for the internal AdamW. |
0.0
|
adamw_eps
|
float
|
The epsilon for the internal AdamW. |
1e-10
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Example
from pytorch_optimizer import Muon
hidden_weights = [p for p in model.body.parameters() if p.ndim >= 2] hidden_gains_biases = [p for p in model.body.parameters() if p.ndim < 2] non_hidden_params = [*model.head.parameters(), *model.embed.parameters()]
param_groups = [ dict(params=hidden_weights, lr=0.02, weight_decay=0.01, use_muon=True), dict( params=hidden_gains_biases + non_hidden_params, lr=3e-4, betas=(0.9, 0.95), weight_decay=0.01, use_muon=False, ), ]
optimizer = Muon(param_groups)
Source code in pytorch_optimizer/optimizer/muon.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 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 | |
Nero
Bases: BaseOptimizer
Learning by Turning: Neural Architecture Aware Optimisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
beta
|
float
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
0.999
|
constraints
|
bool
|
Boolean flag indicating usage of constraints. |
True
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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 135 136 | |
NovoGrad
Bases: BaseOptimizer
Stochastic Gradient Methods with Layer-wise Adaptive Moments for Training of Deep Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.95, 0.98)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
grad_averaging
|
bool
|
Multiply ck (1 - momentum). |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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 139 140 | |
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
|
OptimizerInstanceOrClass
|
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 71 72 73 | |
PAdam
Bases: BaseOptimizer
Closing the Generalization Gap of Adaptive Gradient Methods in Training Deep Neural Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.1
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
partial
|
float
|
Partially adaptive parameter. |
0.25
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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 125 126 | |
PCGrad
Bases: BaseOptimizer
Gradient Surgery for Multi-Task Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Optimizer
|
Optimizer instance. |
required |
reduction
|
str
|
Reduction method for gradients. |
'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 135 136 137 138 139 140 | |
pack_grad(objectives)
Pack the gradient of the parameters of the network for each objective.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectives
|
Iterable[Module]
|
A list of objectives. |
required |
Source code in pytorch_optimizer/optimizer/pcgrad.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
pc_backward(objectives)
Calculate the gradient of the parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objectives
|
Iterable[Module]
|
A list of objectives. |
required |
Source code in pytorch_optimizer/optimizer/pcgrad.py
128 129 130 131 132 133 134 135 136 137 138 139 140 | |
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 masks representing whether the parameter has gradient. |
required |
Source code in pytorch_optimizer/optimizer/pcgrad.py
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 | |
retrieve_grad()
Get the gradient of the parameters of the network with specific objective.
Source code in pytorch_optimizer/optimizer/pcgrad.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
PID
Bases: BaseOptimizer
A PID Controller Approach for Stochastic Optimization of Deep Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
momentum
|
float
|
Momentum factor. |
0.0
|
dampening
|
float
|
Dampening for momentum. |
0.0
|
derivative
|
float
|
D part of the PID. |
10.0
|
integral
|
float
|
I part of the PID. |
5.0
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
maximize
|
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 131 132 | |
PNM
Bases: BaseOptimizer
Positive-Negative Momentum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of the parameters to optimize. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 1.0)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Use weight decoupling, as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective 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 122 123 | |
Prodigy
Bases: BaseOptimizer
An Expeditiously Adaptive Parameter-Free Learner.
Leave LR set to 1 unless you encounter instability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
1.0
|
betas
|
Betas
|
betas. |
(0.9, 0.999)
|
beta3
|
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
|
initial D estimate for D-adaptation (default 1e-6). Rarely needs changing. |
1e-06
|
d_coef
|
float
|
Coefficient in the expression for the estimate of d. |
1.0
|
growth_rate
|
float
|
prevent the D estimate from growing faster than this multiplicative rate. |
float('inf')
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
use AdamW style weight decay. |
True
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
bias_correction
|
bool
|
turn on Adam's bias correction. |
False
|
safeguard_warmup
|
bool
|
remove lr from the denominator of D estimate to avoid issues during warm-up stage. |
False
|
eps
|
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
|
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 206 207 208 209 210 | |
QHAdam
Bases: BaseOptimizer
Quasi-hyperbolic momentum and Adam for deep learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
betas
|
Betas
|
coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
nus
|
Tuple[float, float]
|
immediate discount factors used to estimate the gradient and its square. |
(1.0, 1.0)
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
eps
|
float
|
term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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 142 143 | |
QHM
Bases: BaseOptimizer
Quasi-hyperbolic momentum (QHM) optimization algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
momentum
|
float
|
momentum factor. |
0.0
|
nu
|
float
|
immediate discount factor used to estimate the gradient and its square. |
1.0
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
maximize
|
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 111 112 | |
RACS
Bases: BaseOptimizer
Row and Column Scaled SGD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
beta
|
float
|
momentum factor. |
0.9
|
alpha
|
float
|
scaler. |
0.05
|
gamma
|
float
|
limiter threshold. |
1.01
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
eps
|
float
|
term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
betas
|
Betas
|
coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
n_sma_threshold
|
int
|
recommended is 5. |
5
|
degenerated_to_sgd
|
float
|
degenerated to SGD. |
False
|
eps
|
float
|
term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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 153 154 | |
Ranger
Bases: BaseOptimizer
A synergistic optimizer combining RAdam and LookAhead, and now GC in one optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
betas
|
Betas
|
coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.95, 0.999)
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
n_sma_threshold
|
int
|
recommended is 5. |
5
|
degenerated_to_sgd
|
bool
|
perform SGD update when variance of gradient is high. |
False
|
use_gc
|
bool
|
use Gradient Centralization (both convolution & fc layers). |
True
|
gc_conv_only
|
bool
|
use Gradient Centralization (only convolution layer). |
False
|
eps
|
float
|
term added to the denominator to improve numerical stability. |
1e-05
|
maximize
|
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 176 177 | |
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
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
num_iterations
|
int
|
number of the total training steps. Ranger21 optimizer schedules the learning rate with its own recipes. |
required |
lr
|
float
|
learning rate. |
0.001
|
beta0
|
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
|
coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
use_softplus
|
bool
|
use softplus to smooth. |
True
|
beta_softplus
|
float
|
beta. |
50.0
|
disable_lr_scheduler
|
bool
|
whether to disable learning rate schedule. |
False
|
num_warm_up_iterations
|
Optional[int]
|
number of warm-up iterations. Ranger21 performs linear learning rate warmup. |
None
|
num_warm_down_iterations
|
Optional[int]
|
number of warm-down iterations. Ranger21 performs Explore-exploit learning rate scheduling. |
None
|
agc_clipping_value
|
float
|
|
0.01
|
agc_eps
|
float
|
eps for AGC |
0.001
|
centralize_gradients
|
bool
|
use GC both convolution & fc layers. |
True
|
normalize_gradients
|
bool
|
use gradient normalization. |
True
|
lookahead_merge_time
|
int
|
merge time. |
5
|
lookahead_blending_alpha
|
float
|
blending alpha. |
0.5
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0001
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
norm_loss_factor
|
float
|
norm loss factor. |
0.0001
|
eps
|
float
|
term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
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 318 319 | |
Ranger25
Bases: BaseOptimizer
Mixin' every fancy optimizer hacks.
Here's the components: * ADOPT * AdEMAMix * Cautious * StableAdamW or Adam-atan2 * OrthoGrad * Adaptive gradient clipping * Lookahead * Cautious Weight Decay
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.98, 0.9999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.001
|
alpha
|
float
|
Usually between 4 and 10 works well. |
5.0
|
t_alpha_beta3
|
Optional[float]
|
Total number of iterations is preferred when needed. |
None
|
cautious
|
bool
|
Whether to use the Cautious variant. |
True
|
stable_adamw
|
bool
|
Whether to use stable AdamW variant. |
True
|
orthograd
|
bool
|
Whether to use OrthoGrad variant. |
True
|
eps
|
Optional[float]
|
Term added to the denominator to improve numerical stability. When eps is None and stable_adamw is False, adam-atan2 feature will be used. |
1e-08
|
maximize
|
bool
|
Maximize the objective w.r.t the parameters instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/experimental/ranger25.py
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 | |
ROSE
Bases: BaseOptimizer
Range-Of-Slice Equilibration optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0001
|
wd_schedule
|
Union[float, bool]
|
Schedule-Coupled Weight Decay. If |
False
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
centralize
|
bool
|
Gradient Centralization. Removes shared offsets from gradient slices before the range computation. This can improve generalization and training stability. Biases and other 1D parameters are not centralized. |
True
|
stabilize
|
bool
|
Coefficient-of-Variation Trust Gating. Computes a trust factor from the coefficient of variation of the per-slice range tensor, and then interpolates between the local range and a smoother global mean denominator. This can smooth noisy gradients. |
True
|
bf16_sr
|
bool
|
Stochastic Rounding for BFloat16. |
True
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/rose.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 | |
RotoGrad
Bases: RotateOnly
Implementation of RotoGrad as described in the original paper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone
|
Module
|
shared module. |
required |
heads
|
Sequence[Module]
|
task-specific modules. |
required |
latent_size
|
int
|
size of the shared representation, size of the output of the backbone.z. |
required |
burn_in_period
|
int
|
When back-propagating towards the shared parameters, each task loss is normalized dividing by its initial value, L_k(t) / L_k(t_0=0). This parameter sets a number of iterations after which the denominator will be replaced by the value of the loss at that iteration, that is, t_0 = burn\_in\_period. This is done to overcome problems with losses quickly changing in the first iterations. |
20
|
normalize_losses
|
bool
|
Whether to use these normalized losses to back-propagate through the task-specific parameters as well. |
False
|
Source code in pytorch_optimizer/optimizer/rotograd.py
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 | |
SafeFP16Optimizer
Bases: Optimizer
Safe FP16 Optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Optimizer
|
Optimizer instance. |
required |
aggregate_g_norms
|
bool
|
Aggregate gradient norms. |
False
|
min_loss_scale
|
float
|
Minimum loss scale. |
2 ** -5
|
Source code in pytorch_optimizer/optimizer/fp16.py
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 | |
loss_scale
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
|
The loss tensor to backpropagate. |
required |
update_main_grads
|
bool
|
Whether to update the main gradient during backpropagation. |
False
|
Source code in pytorch_optimizer/optimizer/fp16.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
clip_main_grads(max_norm)
Clip gradient norm and updates dynamic loss scaler.
Source code in pytorch_optimizer/optimizer/fp16.py
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 | |
get_lr()
Get learning rate.
Source code in pytorch_optimizer/optimizer/fp16.py
282 283 284 | |
load_state_dict(state_dict)
Load an optimizer state dict.
In general, prefer using the existing optimizer instance's configuration (e.g., learning rate) over the values found in the state_dict. This approach allows resuming training from a checkpoint while applying new optimizer arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dict
|
dict
|
The state dictionary to load into the optimizer. |
required |
Source code in pytorch_optimizer/optimizer/fp16.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
multiply_grads(c)
Multiply grads by a constant c.
Source code in pytorch_optimizer/optimizer/fp16.py
224 225 226 227 228 229 230 231 | |
set_lr(lr)
Set learning rate.
Source code in pytorch_optimizer/optimizer/fp16.py
286 287 288 | |
state_dict()
Return the optimizer state dict.
Source code in pytorch_optimizer/optimizer/fp16.py
164 165 166 167 168 169 | |
step(closure=None)
Perform a single optimization step.
Source code in pytorch_optimizer/optimizer/fp16.py
264 265 266 267 268 269 270 271 272 | |
sync_fp16_grads_to_fp32(multiply_grads=1.0)
Sync fp16 to fp32 gradients.
Source code in pytorch_optimizer/optimizer/fp16.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
zero_grad()
Clear the gradients of all optimized parameters.
Source code in pytorch_optimizer/optimizer/fp16.py
274 275 276 277 278 279 280 | |
SAM
Bases: BaseOptimizer
Sharpness-Aware Minimization for Efficiently Improving Generalization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
Optimizer
|
base optimizer. |
required |
rho
|
float
|
size of the neighborhood for computing the max loss. |
0.05
|
adaptive
|
bool
|
element-wise Adaptive SAM. |
False
|
use_gc
|
bool
|
perform gradient centralization, GCSAM variant. |
False
|
perturb_eps
|
float
|
eps for perturbation. |
1e-12
|
kwargs
|
Dict
|
parameters for optimizer. |
{}
|
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()
Source code in pytorch_optimizer/optimizer/sam.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 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 | |
ScalableShampoo
Bases: BaseOptimizer
Scalable Preconditioned Stochastic Tensor Optimization.
This version of the Scalable Shampoo Optimizer targets single GPU environments, computing pre-conditioners synchronously on GPU (which takes most of the optimization time). It is faster than previous Shampoo implementations by using coupled Newton iteration for matrix inverse powers instead of slow SVD calculations.
Features include: 1. Various plug-ins (e.g., gradient grafting, preconditioning types), 2. Additional features beyond official PyTorch code, 3. Readable and well-organized implementation.
Reference: https://github.com/google-research/google-research/blob/master/scalable_shampoo/pytorch/shampoo.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
betas
|
tuple
|
beta1 and beta2 for momentum. |
(0.9, 0.999)
|
moving_average_for_momentum
|
bool
|
whether to perform moving average for momentum (beta1). |
False
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
decoupled_weight_decay
|
bool
|
use decoupled weight decay. |
False
|
decoupled_learning_rate
|
bool
|
use decoupled learning rate, otherwise coupled with preconditioned gradient. |
True
|
inverse_exponent_override
|
int
|
fixed exponent for preconditioner if > 0. |
0
|
start_preconditioning_step
|
int
|
step to start preconditioning. |
25
|
preconditioning_compute_steps
|
int
|
frequency of preconditioner computation. |
1000
|
statistics_compute_steps
|
int
|
frequency of statistics computation. |
1
|
block_size
|
int
|
block size for large layers; 1 means AdaGrad (inefficient). |
512
|
skip_preconditioning_rank_lt
|
int
|
skip preconditioning for layers with rank below this. |
1
|
no_preconditioning_for_layers_with_dim_gt
|
int
|
avoid preconditioning large layers. |
8192
|
shape_interpretation
|
bool
|
automatic shape interpretation for tensor dims. |
True
|
graft_type
|
int
|
type of grafting (SGD, AdaGrad, RMSProp, etc.). |
SGD
|
pre_conditioner_type
|
int
|
type of preconditioner. |
ALL
|
nesterov
|
bool
|
enable Nesterov momentum. |
True
|
diagonal_eps
|
float
|
epsilon for numerical stability in diagonal. |
1e-10
|
matrix_eps
|
float
|
epsilon for numerical stability in matrix. |
1e-06
|
use_svd
|
bool
|
whether to use SVD for matrix inverse powers (alternative is Schur-Newton). |
False
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/shampoo.py
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 | |
ScheduleFreeAdamW
Bases: BaseOptimizer
Schedule-Free AdamW.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.0025
|
betas
|
Betas
|
coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
r
|
float
|
use polynomial weighting in the average with power r. |
0.0
|
weight_lr_power
|
float
|
during warmup, weights in the average equal to lr raised to this power; 0 disables weighting. |
2.0
|
warmup_steps
|
int
|
enables a linear learning rate warmup. |
0
|
decoupling_c
|
int
|
proposed coefficient in Refined Schedule-Free AdamW optimizer; default around 200. |
0
|
ams_bound
|
bool
|
whether to use the AMSBound variant. |
False
|
eps
|
float
|
term added to denominator for numerical stability. |
1e-08
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/schedulefree.py
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 | |
ScheduleFreeRAdam
Bases: BaseOptimizer
Schedule-Free RAdam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.0025
|
betas
|
Betas
|
coefficients used for computing running averages of gradient and the squared hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
r
|
float
|
use polynomial weighting in the average with power r. |
0.0
|
weight_lr_power
|
float
|
during warmup, weights in the average equal to lr raised to this power; 0 disables weighting. |
2.0
|
silent_sgd_phase
|
bool
|
if True, disables updates in the early SGD phase, only updates momentum to stabilize training. |
True
|
eps
|
float
|
term added to denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/schedulefree.py
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 498 499 500 501 502 503 504 505 506 507 508 | |
ScheduleFreeSGD
Bases: BaseOptimizer
Schedule-Free SGD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
1.0
|
momentum
|
float
|
momentum factor, must be between 0 and 1 exclusive. |
0.9
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
r
|
float
|
use polynomial weighting in the average with power r. |
0.0
|
weight_lr_power
|
float
|
during warmup, weights in the average equal to lr raised to this power; 0 disables weighting. |
2.0
|
warmup_steps
|
int
|
enables a linear learning rate warmup. |
0
|
eps
|
float
|
term added to denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
maximize the objective 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 166 167 | |
ScheduleFreeWrapper
Bases: BaseOptimizer
Schedule-Free Wrapper for any base optimizer.
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
|
base optimizer instance or class to wrap. |
required |
momentum
|
float
|
momentum factor. |
0.9
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
r
|
float
|
use polynomial weighting in the average with power r. |
0.0
|
weight_lr_power
|
float
|
during warmup, weights in average equal lr raised to this power; 0 disables weighting. |
2.0
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/schedulefree.py
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 700 701 702 703 704 705 706 707 708 709 710 711 | |
load_state_dict(state)
Load state.
Source code in pytorch_optimizer/optimizer/schedulefree.py
580 581 582 583 | |
SCION
Bases: BaseOptimizer
Training Deep Learning Models with Norm-Constrained LMOs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
momentum
|
float
|
momentum factor. 1.0 - usual momentum. |
0.1
|
constraint
|
bool
|
whether to use a constraint SCG or not. |
False
|
norm_type
|
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]
|
arguments for the Norm. |
None
|
scale
|
float
|
scale factor. For Transformer block typical value is 50.0, and 3000.0 for others (e.g., Embeddings, LM head). |
1.0
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
maximize the objective with respect to the params, instead of minimizing. |
False
|
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
Source code in pytorch_optimizer/optimizer/scion.py
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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
SCIONLight
Bases: BaseOptimizer
Memory-efficient variant of the Scion optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
momentum
|
float
|
momentum factor. 1.0 - usual momentum. |
0.1
|
constraint
|
bool
|
whether to use a constraint SCG or not. |
False
|
norm_type
|
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]
|
arguments for the Norm. |
None
|
scale
|
float
|
scale factor. For Transformer block typical value is 50.0, and 3000.0 for others (e.g., Embeddings, LM head). |
1.0
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
the optimizer uses decoupled weight decay as in AdamW. |
True
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
maximize the objective with respect to the params, instead of minimizing. |
False
|
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 = SCIONLight(parameter_groups)
For more details, checkout here https://github.com/LIONS-EPFL/scion/tree/main?tab=readme-ov-file#examples
Source code in pytorch_optimizer/optimizer/scion.py
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 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 | |
SGDP
Bases: BaseOptimizer
SGD + Slowing Down the Slowdown for Momentum Optimizers on Scale-invariant Weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
momentum
|
float
|
Momentum factor. |
0.0
|
dampening
|
float
|
Dampening for momentum. |
0.0
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
delta
|
float
|
Threshold that determines whether a set of parameters is scale-invariant or not. |
0.1
|
wd_ratio
|
float
|
Relative weight decay applied on scale-invariant parameters compared to that applied on scale-variant parameters. |
0.1
|
nesterov
|
bool
|
Enables Nesterov momentum. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamp.py
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 | |
SGDSaI
Bases: BaseOptimizer
No More Adam: Learning Rate Scaling at Initialization is All You Need.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.01
|
momentum
|
float
|
coefficients used for computing running averages of gradient. |
0.9
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
optimizer uses decoupled weight decay as in AdamW. |
True
|
eps
|
float
|
term added to denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
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 | |
SGDW
Bases: BaseOptimizer
Decoupled Weight Decay Regularization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.0001
|
momentum
|
float
|
momentum factor. |
0.0
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
optimizer uses decoupled weight decay as in AdamW. |
True
|
dampening
|
float
|
dampening for momentum. |
0.0
|
nesterov
|
bool
|
enables Nesterov momentum. |
False
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
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 | |
Shampoo
Bases: BaseOptimizer
Preconditioned Stochastic Tensor Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
momentum
|
float
|
momentum factor. |
0.0
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
fix weight decay. |
False
|
preconditioning_compute_steps
|
int
|
how often to compute the preconditioner, tuning memory and compute requirements. |
1
|
matrix_eps
|
float
|
term added to denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
maximize the objective 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 153 154 | |
SignSGD
Bases: BaseOptimizer
Compressed Optimisation for Non-Convex Problems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.001
|
momentum
|
float
|
momentum factor (0.0 = SignSGD, >0 = Signum). |
0.9
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
optimizer uses decoupled weight decay as in AdamW. |
True
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
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 526 527 528 | |
SimplifiedAdEMAMix
Bases: BaseOptimizer
Connections between Schedule-Free Optimizers, AdEMAMix, and Accelerated SGD Variants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.0001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.99, 0.95)
|
alpha
|
float
|
Coefficient for mixing the current gradient and EMA. |
0.0
|
beta1_warmup
|
Optional[int]
|
Number of warmup steps used to increase beta1. |
None
|
min_beta1
|
float
|
Minimum value of beta1 to start from. |
0.9
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether to use decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Apply fixed weight decay instead of adaptive. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/ademamix.py
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 | |
SM3
Bases: BaseOptimizer
Memory-Efficient Adaptive Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.1
|
momentum
|
float
|
Coefficient used to scale prior updates before adding. This drastically increases memory usage if momentum > 0.0. This is ignored if the parameter's gradient is sparse. |
0.0
|
beta
|
float
|
Coefficient used for exponential moving averages. |
0.0
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-30
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sm3.py
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 | |
SOAP
Bases: BaseOptimizer
Improving and Stabilizing Shampoo using Adam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.003
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.95, 0.95)
|
shampoo_beta
|
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. |
None
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
precondition_frequency
|
int
|
How often to update the pre-conditioner. |
10
|
max_precondition_dim
|
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
|
Whether to merge dimensions of the pre-conditioner. |
False
|
precondition_1d
|
bool
|
Whether to precondition 1D gradients. |
False
|
correct_bias
|
bool
|
Whether to correct bias in Adam. |
True
|
normalize_gradient
|
bool
|
Whether to normalize the gradients. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 362 363 364 365 366 | |
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
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 | |
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
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.06
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.96, 0.99)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Fix weight decay. |
False
|
p
|
float
|
Clip effective (applied) gradient (p). |
0.01
|
update_period
|
int
|
Number of steps after which to apply Hessian approximation. |
10
|
num_samples
|
int
|
Times to sample z for the approximation of the Hessian trace. |
1
|
hessian_distribution
|
HutchinsonG
|
HutchinsonG. Type of distribution to initialize Hessian. |
'gaussian'
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-12
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, 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 151 152 | |
SPAM
Bases: BaseOptimizer
Spike-Aware Adam with Momentum Reset for Stable LLM Training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
density
|
float
|
Density parameter. Only used for 2D parameters (e.g., Linear). |
1.0
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
warmup_epoch
|
int
|
Number of epochs to warm up. Defaults to 50. |
50
|
threshold
|
int
|
Threshold for gradient masking. Defaults to 5000. |
5000
|
grad_accu_steps
|
int
|
Gradient accumulation steps before threshold-based masking applies. Defaults to 20. |
20
|
update_proj_gap
|
int
|
Update projection gap. |
500
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
Maximize the objective with respect to the parameters instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/spam.py
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 | |
init_masks()
Initialize random masks for each parameter group that has 'density'.
Source code in pytorch_optimizer/optimizer/spam.py
175 176 177 178 179 180 181 182 183 184 185 186 | |
initialize_random_rank_boolean_tensor(m, n, density, device)
staticmethod
Create an (m x n) boolean tensor with density fraction of True entries.
:param m: int. number of rows. :param n: int. number of columns. :param density: float. fraction of True entries. 1.0 means all True. :param device: torch.device. device.
Source code in pytorch_optimizer/optimizer/spam.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
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.
:param p: torch.Tensor. parameter to which the mask is applied. :param old_mask: torch.Tensor. previous binary mask.
Source code in pytorch_optimizer/optimizer/spam.py
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 | |
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
163 164 165 166 167 168 169 170 171 172 173 | |
SpectralSphere
Bases: BaseOptimizer
Controlled LLM Training on Spectral Sphere.
This optimizer constrains weight matrices to lie on a spectral sphere of fixed radius R, where ||W||_2 = R. The optimization proceeds by:
- Power iteration to compute spectral norm sigma and top singular vectors (u, v)
- Retraction to spectral sphere: W ← (R / sigma) * W
- Form Θ = u @ v^T
- Solve for Lagrange multiplier lambda: <Θ, msign(M + lambdaΘ)> = 0
- Compute update direction: Φ = msign(M + lambdaΘ)
- Update: W ← W - lr * Φ
The key insight is that the retraction step at the end of iteration t is equivalent to the retraction at the beginning of iteration t+1. This allows us to unify the power iteration for both retraction and Theta computation in a single efficient step.
References
- Spectral MuP: Spectral Control of Feature Learning
- Modular Duality in Deep Learning. arXiv:2410.21265 (2024).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
The parameters to be optimized by Muon. |
required |
lr
|
float
|
Learning rate. |
0.0003
|
momentum
|
float
|
The momentum used by the internal SGD. |
0.9
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
The optimizer uses decoupled weight decay as in AdamW. |
True
|
nesterov
|
bool
|
Whether to use nesterov momentum. |
True
|
power_iteration_steps
|
int
|
Number of power iteration steps for spectral norm computation. |
10
|
msign_steps
|
int
|
Number of Newton-Schulz iterations for msign (uses Polar-Express). |
5
|
solver_tolerance_f
|
float
|
Function value tolerance for solver. |
1e-08
|
solver_max_iterations
|
int
|
Maximum iterations for solver. |
100
|
maximize
|
bool
|
Maximize the objective with respect to the params, instead of minimizing. |
False
|
Example
from pytorch_optimizer import SpectralSphere
hidden_weights = [p for p in model.body.parameters() if p.ndim >= 2]
param_groups = [ dict(params=hidden_weights, lr=0.02, weight_decay=0.01), ]
optimizer = SpectralSphere(param_groups) ...
Source code in pytorch_optimizer/optimizer/sso.py
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 | |
SPlus
Bases: BaseOptimizer
A Stable Whitening Optimizer for Efficient Neural Network Training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.1
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
Whether the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Whether to fix weight decay. |
False
|
ema_rate
|
float
|
Exponential moving average decay rate. |
0.999
|
inverse_steps
|
int
|
Number of steps to perform inverse. |
100
|
nonstandard_constant
|
float
|
Scale factor for the learning rate in case of a non-linear layer. |
0.001
|
max_dim
|
int
|
Maximum number of dimensions to perform the operation on. |
10000
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-30
|
maximize
|
bool
|
Maximize the objective with respect to the parameters instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/splus.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 | |
SRMM
Bases: BaseOptimizer
Stochastic regularized majorization-minimization with weakly convex and multi-convex surrogates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
beta
|
float
|
Adaptivity weight. |
0.5
|
memory_length
|
Optional[int]
|
Internal memory length for moving average. None for no refreshing. |
100
|
maximize
|
bool
|
Maximize the objective with respect to the parameters 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 99 100 | |
StableAdamW
Bases: BaseOptimizer
Stable and low-precision training for large-scale vision-language models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.99)
|
kahan_sum
|
bool
|
Enables Kahan summation for more accurate parameter updates when training in low precision (float16 or bfloat16). |
True
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
Decoupled weight decay. |
True
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
Maximize the objective with respect to the parameters, instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/adamw.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 | |
StableSPAM
Bases: BaseOptimizer
How to Train in 4-Bit More Stably than 16-Bit Adam.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
gamma1
|
float
|
Gamma1 parameter. |
0.7
|
gamma2
|
float
|
Gamma2 parameter. |
0.9
|
theta
|
float
|
Theta parameter. |
0.999
|
t_max
|
Optional[int]
|
Total number of steps. |
None
|
eta_min
|
float
|
Eta_min of CosineDecay. |
0.5
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
update_proj_gap
|
int
|
Update projection gap. |
1000
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/spam.py
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 | |
SWATS
Bases: BaseOptimizer
Improving Generalization Performance by Switching from Adam to SGD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether the optimizer uses decoupled weight decay as in AdamW. |
False
|
fixed_decay
|
bool
|
Whether to fix weight decay. |
False
|
ams_bound
|
bool
|
Whether to use the AMSBound variant of this algorithm from the paper. |
False
|
nesterov
|
bool
|
Enables Nesterov momentum. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-06
|
maximize
|
bool
|
Maximize the objective with respect to the parameters 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 182 183 | |
TAM
Bases: BaseOptimizer
Torque-Aware Momentum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
momentum
|
float
|
Coefficient used for computing running averages of gradient. |
0.9
|
decay_rate
|
float
|
Smoothing decay rate. |
0.9
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Whether to fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
Maximize the objective with respect to the parameters 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 122 123 | |
Tiger
Bases: BaseOptimizer
A Tight-fisted Optimizer, an optimizer that is extremely budget-conscious.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.001
|
beta
|
float
|
Coefficient used for computing running averages of gradient and the squared Hessian trace. |
0.965
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.01
|
weight_decouple
|
bool
|
Whether the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Whether to fix weight decay. |
False
|
foreach
|
Optional[bool]
|
Whether to use foreach (multi-tensor) operations for speed. None means auto-detect based on device (True for CUDA, False otherwise). |
None
|
maximize
|
bool
|
Maximize the objective with respect to the parameters instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/tiger.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 | |
TRAC
Bases: BaseOptimizer
A Parameter-Free Optimizer for Lifelong Reinforcement Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
OptimizerInstanceOrClass
|
Base optimizer. |
required |
betas
|
List[float]
|
List of beta values. |
(0.9, 0.99, 0.999, 0.9999, 0.99999, 0.999999)
|
num_coefs
|
int
|
Number of polynomial coefficients to use in the approximation. |
128
|
s_prev
|
float
|
Initial scale value. |
1e-08
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
1e-08
|
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()
Source code in pytorch_optimizer/optimizer/trac.py
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 | |
VSGD
Bases: BaseOptimizer
Variational Stochastic Gradient Descent for Deep Neural Networks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
learning rate. |
0.1
|
ghattg
|
float
|
prior variance ratio between ghat and g, Var(ghat_t-g_t)/Var(g_t-g_{t-1}). |
30.0
|
ps
|
float
|
prior strength. |
1e-08
|
tau1
|
float
|
remember rate for the gamma parameters of g. |
0.81
|
tau2
|
float
|
remember rate for the gamma parameter of ghat. |
0.9
|
weight_decay
|
float
|
weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
optimizer uses decoupled weight decay as in AdamW. |
True
|
eps
|
float
|
term added to denominator to improve numerical stability. |
1e-08
|
maximize
|
bool
|
maximize the objective instead of minimizing. |
False
|
Source code in pytorch_optimizer/optimizer/sgd.py
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 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 | |
WSAM
Bases: BaseOptimizer
Sharpness-Aware Minimization Revisited: Weighted Sharpness as a Regularization Term.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Union[Module, DataParallel]
|
the model instance. DDP model is recommended to make
|
required |
params
|
ParamsT
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
base_optimizer
|
Optimizer
|
base optimizer. |
required |
rho
|
float
|
size of the neighborhood for computing the max loss. |
0.05
|
gamma
|
float
|
weighted factor gamma / (1 - gamma) of the sharpness term. 0.8 ~ 0.95 is the optimal. |
0.9
|
adaptive
|
bool
|
element-wise adaptive SAM. |
False
|
decouple
|
bool
|
whether to perform a decoupled sharpness regularization. |
True
|
max_norm
|
Optional[float]
|
max norm of the gradients. |
None
|
eps
|
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
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 526 527 528 529 530 531 532 533 534 | |
Yogi
Bases: BaseOptimizer
Decoupled Weight Decay Regularization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ParamsT
|
Iterable of parameters to optimize or dicts defining parameter groups. |
required |
lr
|
float
|
Learning rate. |
0.01
|
betas
|
Betas
|
Coefficients used for computing running averages of gradient and the squared Hessian trace. |
(0.9, 0.999)
|
initial_accumulator
|
float
|
Initial values for first and second moments. |
1e-06
|
weight_decay
|
float
|
Weight decay (L2 penalty). |
0.0
|
weight_decouple
|
bool
|
Whether the optimizer uses decoupled weight decay as in AdamW. |
True
|
fixed_decay
|
bool
|
Whether to fix weight decay. |
False
|
eps
|
float
|
Term added to the denominator to improve numerical stability. |
0.001
|
maximize
|
bool
|
Maximize the objective with respect to the parameters 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 136 137 | |