megengine.load¶
- load(f, map_location=None, pickle_module=pickle)[源代码]¶
Load an object saved with
save
from a file.- 参数
f – 文件名字符串或一个需要加载的文件对象。
map_location – defines device mapping. See examples for usage.
pickle_module – the module to use for pickling.
注解
如果你要调用
set_default_device
, 请在load
. 之前调用它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.load
. 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.
实际案例
This example shows how to load tenors to different devices:
import megengine as mge # Load tensors to the same device as defined in model.pkl mge.load('model.pkl') # Load all tensors to gpu0. mge.load('model.pkl', map_location='gpu0') # Load all tensors originally on gpu0 to cpu0 mge.load('model.pkl', map_location={'gpu0':'cpu0'}) # Load all tensors to cpu0 mge.load('model.pkl', map_location=lambda dev: 'cpu0')
If you are using a lower version of Python (<3.8), you can use other pickle module like
pickle5
to load object saved in pickle 5 protocol:>>> import pickle5 as pickle
Or you can use
pickle5
in this way (only used with this interface):import pickle5 import megengine megengine.load(obj, pickle_module=pickle5)