Utils.save_load(参数加载与保存) 模块¶
ppsci.utils.save_load
¶
load_checkpoint(path, model, optimizer, grad_scaler=None, equation=None, ema_model=None)
¶
Load from checkpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Path for checkpoint. |
required |
model |
Layer
|
Model with parameters. |
required |
optimizer |
Optimizer
|
Optimizer for model. |
required |
grad_scaler |
Optional[GradScaler]
|
GradScaler for AMP. Defaults to None. |
None
|
equation |
Optional[Dict[str, PDE]]
|
Equations. Defaults to None. |
None
|
ema_model |
Optional[AveragedModel]
|
Optional[ema.AveragedModel]: Average model. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Loaded metric information. |
Source code in ppsci/utils/save_load.py
save_checkpoint(model, optimizer, metric, grad_scaler=None, output_dir=None, prefix='model', equation=None, print_log=True, ema_model=None)
¶
Save checkpoint, including model params, optimizer params, metric information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Layer
|
Model with parameters. |
required |
optimizer |
Optional[Optimizer]
|
Optimizer for model. |
required |
metric |
Dict[str, float]
|
Metric information, such as {"RMSE": 0.1, "MAE": 0.2}. |
required |
grad_scaler |
Optional[GradScaler]
|
GradScaler for AMP. Defaults to None. |
None
|
output_dir |
Optional[str]
|
Directory for checkpoint storage. |
None
|
prefix |
str
|
Prefix for storage. Defaults to "model". |
'model'
|
equation |
Optional[Dict[str, PDE]]
|
Equations. Defaults to None. |
None
|
print_log |
bool
|
Whether print saving log information, mainly for keeping log tidy without duplicate 'Finish saving checkpoint ...' log strings. Defaults to True. |
True
|
ema_model |
Optional[AveragedModel]
|
Optional[ema.AveragedModel]: Average model. Defaults to None. |
None
|
Examples:
>>> import ppsci
>>> import paddle
>>> from ppsci.utils import save_load
>>> model = ppsci.arch.MLP(("x", "y", "z"), ("u", "v", "w"), 5, 64, "tanh")
>>> optimizer = ppsci.optimizer.Adam(0.001)(model)
>>> save_load.save_checkpoint(model, optimizer, {"RMSE": 0.1}, output_dir="path/to/output/dir")
Source code in ppsci/utils/save_load.py
load_pretrain(model, path, equation=None)
¶
Load pretrained model from given path or url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Layer
|
Model with parameters. |
required |
path |
str
|
File path or url of pretrained model, i.e. |
required |
equation |
Optional[Dict[str, PDE]]
|
Equations. Defaults to None. |
None
|
Examples:
>>> import ppsci
>>> from ppsci.utils import save_load
>>> model = ppsci.arch.MLP(("x", "y"), ("u", "v", "p"), 9, 50, "tanh")
>>> save_load.load_pretrain(
... model=model,
... path="path/to/pretrain_model")