megengine.save¶
- save(obj, f, pickle_module=pickle, pickle_protocol=pickle.DEFAULT_PROTOCOL)[source]¶
Save an object to disk file. The saved object must be a
Module
,Module.state_dict
orOptimizer.state_dict
. See Save and Load Models (S&L) for more details.- Parameters
obj – object to be saved.
f – a string of file name or a text file object to which
obj
is saved to.pickle_module – the module to use for pickling.
pickle_protocol – the protocol to use for pickling.
- Returns
None.
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 directlyThis interface is a wrapper of
pickle.dump
. If you want to usepickle
, Seepickle
for more information about how to setpickle_protocol
:pickle.HIGHEST_PROTOCOL
- the highest protocol version available.pickle.DEFAULT_PROTOCOL
- the default protocol version used for pickling.
Examples
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)