Saves the model, parameters, optimizer state to path. If `training` is set to False, only inference model will be saved. Args: path (str): The file prefix to save model. The format is 'dirname/file_prefix' or 'file_prefix'. if empty str.
(self, path: str, training: bool = True)
| 2372 | program.set_state_dict(state_dict) |
| 2373 | |
| 2374 | def save(self, path: str, training: bool = True) -> None: |
| 2375 | """ |
| 2376 | Saves the model, parameters, optimizer state to path. |
| 2377 | If `training` is set to False, only inference model will be saved. |
| 2378 | |
| 2379 | Args: |
| 2380 | path (str): The file prefix to save model. The format |
| 2381 | is 'dirname/file_prefix' or 'file_prefix'. if empty str. |
| 2382 | A exception will be raised. |
| 2383 | training (bool, optional): Whether to save for training. If not, save |
| 2384 | for inference only. If `training` is set to True, the optimizer state |
| 2385 | will be saved. Otherwise, only the model and parameters are saved. |
| 2386 | This function will silently overwrite existing file at the target |
| 2387 | location. Default: True. |
| 2388 | |
| 2389 | Returns: |
| 2390 | None |
| 2391 | |
| 2392 | Examples: |
| 2393 | |
| 2394 | .. code-block:: pycon |
| 2395 | |
| 2396 | >>> import paddle |
| 2397 | >>> import paddle.vision.transforms as T |
| 2398 | >>> from paddle.distributed.fleet import auto |
| 2399 | >>> from paddle.vision.datasets import MNIST |
| 2400 | |
| 2401 | >>> transform = T.Compose([ |
| 2402 | ... T.Transpose(), |
| 2403 | ... T.Normalize([127.5], [127.5]) |
| 2404 | >>> ]) |
| 2405 | >>> train_dataset = MNIST(mode='train', transform=transform) |
| 2406 | |
| 2407 | >>> model = paddle.vision.models.LeNet() |
| 2408 | >>> loss = paddle.nn.CrossEntropyLoss() |
| 2409 | >>> optimizer = paddle.optimizer.Adam( |
| 2410 | ... learning_rate=0.001, |
| 2411 | ... parameters=model.parameters(), |
| 2412 | ... ) |
| 2413 | >>> metrics = paddle.metric.Accuracy(topk=(1, 2)) |
| 2414 | |
| 2415 | >>> engine = auto.Engine(model, loss, optimizer, metrics) |
| 2416 | >>> engine.fit(train_dataset, epochs=1, batch_size=64) |
| 2417 | >>> engine.save("./my_model") |
| 2418 | |
| 2419 | """ |
| 2420 | if training: |
| 2421 | assert self._mode in self._dist_contexts |
| 2422 | dist_context = self._dist_contexts[self._mode] |
| 2423 | serial_program = dist_context.serial_main_program |
| 2424 | dist_main_prog = dist_context.dist_main_programs[self._cur_rank] |
| 2425 | self._saver.save( |
| 2426 | path, |
| 2427 | serial_program=serial_program, |
| 2428 | dist_main_program=dist_main_prog, |
| 2429 | dist_context=dist_context, |
| 2430 | ) |
| 2431 | else: |
no test coverage detected