Optimizer.optimizer(优化器) 模块¶
ppsci.optimizer.optimizer
¶
Adam
¶
Adam: A Method for Stochastic Optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
Union[float, LRScheduler]
|
The learning rate used to update parameter(s). Defaults to 0.001. |
0.001
|
beta1
|
float
|
The exponential decay rate for the 1st moment estimates. Defaults to 0.9. |
0.9
|
beta2
|
float
|
The exponential decay rate for the 2nd moment estimates. Defaults to 0.999. |
0.999
|
epsilon
|
float
|
A small float value for numerical stability. Defaults to 1e-08. |
1e-08
|
weight_decay
|
Optional[Union[float, L1Decay, L2Decay]]
|
Regularization strategy. Defaults to None. |
None
|
grad_clip
|
Optional[Union[ClipGradByNorm, ClipGradByValue, ClipGradByGlobalNorm]]
|
Gradient clipping strategy. Defaults to None. |
None
|
lazy_mode
|
bool
|
Whether to enable lazy mode for moving-average. Defaults to False. |
False
|
amsgrad
|
bool
|
Whether to use the AMSGrad variant of this algorithm from the paper
|
False
|
Examples:
>>> import ppsci
>>> model = ppsci.arch.MLP(("x",), ("u",), 5, 20)
>>> opt = ppsci.optimizer.Adam(1e-3)(model)
Source code in ppsci/optimizer/optimizer.py
AdamW
¶
AdamW is implemented based on DECOUPLED WEIGHT DECAY REGULARIZATION.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
Union[float, LRScheduler]
|
The learning rate used to update parameter(s). Defaults to 0.001. |
0.001
|
beta1
|
float
|
The exponential decay rate for the 1st moment estimates. Defaults to 0.9. |
0.9
|
beta2
|
float
|
The exponential decay rate for the 2nd moment estimates. Defaults to 0.999. |
0.999
|
epsilon
|
float
|
A small float value for numerical stability. Defaults to 1e-8. |
1e-08
|
weight_decay
|
float
|
Regularization coefficient. Defaults to 0.01. |
0.001
|
grad_clip
|
Optional[Union[ClipGradByNorm, ClipGradByValue, ClipGradByGlobalNorm]]
|
Gradient clipping strategy. Defaults to None. |
None
|
no_weight_decay_name
|
Optional[str]
|
List of names of no weight decay parameters split by white space. Defaults to None. |
None
|
one_dim_param_no_weight_decay
|
bool
|
Apply no weight decay on 1-D parameter(s). Defaults to False. |
False
|
amsgrad
|
bool
|
Whether to use the AMSGrad variant of this algorithm from the paper
|
False
|
Examples:
>>> import ppsci
>>> model = ppsci.arch.MLP(("x",), ("u",), 5, 20)
>>> opt = ppsci.optimizer.AdamW(1e-3)(model)
Source code in ppsci/optimizer/optimizer.py
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 | |
LBFGS
¶
The L-BFGS is a quasi-Newton method for solving an unconstrained optimization problem over a differentiable function. Closely related is the Newton method for minimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
The learning rate used to update parameter(s). Defaults to 1.0. |
1.0
|
max_iter
|
int
|
Maximal number of iterations per optimization step. Defaults to 1. |
1
|
max_eval
|
Optional[int]
|
Maximal number of function evaluations per optimization step. Defaults to None. |
None
|
tolerance_grad
|
float
|
Termination tolerance on first order optimality. Defaults to 1e-07. |
1e-07
|
tolerance_change
|
float
|
Termination tolerance on function value/parameter changes. Defaults to 1e-09. |
1e-09
|
history_size
|
int
|
Update history size. Defaults to 100. |
100
|
line_search_fn
|
Optional[Literal['strong_wolfe']]
|
Either 'strong_wolfe' or None. Defaults to "strong_wolfe". |
'strong_wolfe'
|
Examples:
>>> import ppsci
>>> model = ppsci.arch.MLP(("x",), ("u",), 5, 20)
>>> opt = ppsci.optimizer.LBFGS(1e-3)(model)
Source code in ppsci/optimizer/optimizer.py
Momentum
¶
Simple Momentum optimizer with velocity state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
Union[float, LRScheduler]
|
The learning rate used to update parameter(s). |
required |
momentum
|
float
|
Momentum factor. |
required |
weight_decay
|
Optional[Union[float, L1Decay, L2Decay]]
|
Regularization strategy. Defaults to None. |
None
|
grad_clip
|
Optional[Union[ClipGradByNorm, ClipGradByValue, ClipGradByGlobalNorm]]
|
Gradient clipping strategy. Defaults to None. |
None
|
use_nesterov
|
bool
|
Whether to use nesterov momentum. Defaults to False. |
False
|
no_weight_decay_name
|
Optional[str]
|
List of names of no weight decay parameters split by white space. Defaults to None. |
None
|
Examples:
>>> import ppsci
>>> model = ppsci.arch.MLP(("x",), ("u",), 5, 20)
>>> opt = ppsci.optimizer.Momentum(1e-3, 0.9)(model)
Source code in ppsci/optimizer/optimizer.py
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 | |
OptimizerList
¶
OptimizerList which wrap more than one optimizer. NOTE: LBFGS is not supported yet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer_list
|
Tuple[Optimizer, ...]
|
Optimizers listed in a tuple. |
required |
Examples:
>>> import ppsci
>>> model1 = ppsci.arch.MLP(("x",), ("u",), 5, 20)
>>> opt1 = ppsci.optimizer.Adam(1e-3)(model1)
>>> model2 = ppsci.arch.MLP(("y",), ("v",), 5, 20)
>>> opt2 = ppsci.optimizer.Adam(1e-3)(model2)
>>> opt = ppsci.optimizer.OptimizerList((opt1, opt2))
Source code in ppsci/optimizer/optimizer.py
RMSProp
¶
Root Mean Squared Propagation (RMSProp) is an unpublished, adaptive learning rate method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
Union[float, LRScheduler]
|
The learning rate used to update parameter(s) |
required |
rho
|
float
|
Factor ρ in equation. Defaults to 0.95. |
0.95
|
epsilon
|
float
|
Factor ϵ in equation as a smoothing term. Defaults to 1e-6. |
1e-06
|
momentum
|
float
|
β in equation is the momentum term. Defaults to 0.0. |
0.0
|
weight_decay
|
Optional[Union[float, L1Decay, L2Decay]]
|
Regularization strategy. Defaults to None. |
None
|
grad_clip
|
Optional[Union[ClipGradByNorm, ClipGradByValue, ClipGradByGlobalNorm]]
|
Gradient clipping strategy. Defaults to None. |
None
|
Examples:
>>> import ppsci
>>> model = ppsci.arch.MLP(("x",), ("u",), 5, 20)
>>> opt = ppsci.optimizer.RMSProp(1e-3)(model)
Source code in ppsci/optimizer/optimizer.py
SGD
¶
Stochastic Gradient Descent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
Union[float, LRScheduler]
|
The learning rate used to update parameter(s). Defaults to 0.001. |
0.001
|
weight_decay
|
Optional[Union[float, L1Decay, L2Decay]]
|
Regularization strategy. Defaults to None. |
None
|
grad_clip
|
Optional[Union[ClipGradByNorm, ClipGradByValue, ClipGradByGlobalNorm]]
|
Gradient clipping strategy. Defaults to None. |
None
|
Examples:
>>> import ppsci
>>> model = ppsci.arch.MLP(("x",), ("u",), 5, 20)
>>> opt = ppsci.optimizer.SGD(1e-3)(model)
Source code in ppsci/optimizer/optimizer.py
SOAP
¶
Improving and Stabilizing Shampoo using Adam. Implements SOAP algorithm (https://arxiv.org/abs/2409.11321).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
The learning rate to use. defaults to 0.003. |
0.003
|
beta1
|
float
|
Adam's betas parameters beta1. defaults to 0.95. |
0.95
|
beta2
|
float
|
Adam's betas parameters beta2. defaults to 0.95. |
0.95
|
shampoo_beta
|
float
|
If >= 0, use this beta for the preconditioner (L and R in paper, state['GG'] below) moving average instead of betas[1]. defaults to -1. |
-1
|
epsilon
|
float
|
Adam's epsilon for numerical stability. defaults to 1e-08. |
1e-08
|
weight_decay
|
float
|
weight decay coefficient. defaults to 0.01. |
0.01
|
precondition_frequency
|
int
|
How often to update the preconditioner. defaults to 10. |
10
|
max_precond_dim
|
int
|
Maximum dimension of the preconditioner. Set to 10000, so that we exclude most common vocab sizes while including layers. defaults to 10000. |
10000
|
merge_dims
|
bool
|
Whether or not to merge dimensions of the preconditioner. defaults to |
False
|
precondition_1d
|
bool
|
Whether or not to precondition 1D gradients. defaults to |
False
|
normalize_grads
|
bool
|
Whether or not to normalize gradients per layer.
Helps at large precondition_frequency (~100 in our experiments),
but hurts performance at small precondition_frequency (~10 in our experiments). defaults to |
False
|
data_format
|
str
|
Data format of the input for convolutional layers.
Should be "channels_last" for data_format of NHWC and "channels_first" for NCHW. defaults to |
'channels_first'
|
correct_bias
|
bool
|
Whether or not to use bias correction in Adam. defaults to |
True
|
Examples:
>>> import ppsci
>>> model = ppsci.arch.MLP(("x",), ("u",), 5, 20)
>>> opt = ppsci.optimizer.SOAP(1e-3)(model)
Source code in ppsci/optimizer/optimizer.py
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 | |