megengine.save

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 – the module to use for pickling.

  • pickle_protocol – the protocol to use for pickling.

If you are using MegEngine with different Python versions

Different Python version may use different DEFAULT/HIGHEST pickle protocol. If you want to load the saved object in another Python version, please make sure you have used the same protocol.

You can select to use pickle module directly

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+.

Or you can use pickle5 in this way (only used with this interface):

import pickle5
import megengine

megengine.save(obj, f, pickle_module=pickle5, pickle_protocol=5)