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 directly

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


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

# 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)