save(obj, f, pickle_module=pickle, pickle_protocol=pickle.DEFAULT_PROTOCOL)[源代码]

Save an object to disk file. The saved object must be a Module, Module.state_dict or Optimizer.state_dict. See 保存与加载模型(S&L) for more details.

  • obj – object to be saved.

  • f – 文件名字符串或一个需要保存的文件对象。

  • pickle_module – 用于序列化的模块。

  • pickle_protocol – the protocol to use for pickling.




不同的 Python 版本可能使用不同的 DEFAULT/HIGHEST pickle 协议。如果你想在另一个Python版本中 load 保存的对象,请确保你使用了相同的协议。

你可以选择直接使用 ``pickle ``模块

This interface is a wrapper of pickle.dump. If you want to use pickle, See pickle for more information about how to set pickle_protocol:


If you want to save object in a higher protocol version which current version Python not support, you can install other pickle module instead of the build-in one. Take pickle5 as an example:

>>> import pickle5 as pickle  

It’s a backport of the pickle 5 protocol (PEP 574) and other pickle changes. So you can use it to save object in pickle 5 protocol and load it in Python 3.8+.

或者你可以这样使用 ``pickle5``(只用于此接口):

import pickle5
import megengine, f, pickle_module=pickle5, pickle_protocol=5)