torch.jit.load¶
- torch.jit.load(f, map_location=None, _extra_files=None, _restore_shapes=False)[source][source]¶
加载一个
ScriptModule
或ScriptFunction
,它们先前使用torch.jit.save
保存。所有先前保存的模块,无论其设备如何,都首先加载到 CPU 上,然后再移动到它们保存时所在的设备。如果此操作失败(例如,因为运行时系统没有某些设备),则会引发异常。
- 参数
f – 类似文件的对象(必须实现 read、readline、tell 和 seek),或包含文件名的字符串
map_location (字符串 或 torch.device) – torch.jit.save 中
map_location
的简化版本,用于动态地将存储重映射到另一组设备。_extra_files (文件名到内容的字典) – 地图中给出的额外文件名将被加载,其内容将存储在提供的地图中。
_restore_shapes (bool) – 是否在加载时使用存储的输入来重溯模块
- 返回
一个
ScriptModule
对象。
示例: .. testcode
import torch import io torch.jit.load('scriptmodule.pt') # Load ScriptModule from io.BytesIO object with open('scriptmodule.pt', 'rb') as f: buffer = io.BytesIO(f.read()) # Load all tensors to the original device torch.jit.load(buffer) # Load all tensors onto CPU, using a device buffer.seek(0) torch.jit.load(buffer, map_location=torch.device('cpu')) # Load all tensors onto CPU, using a string buffer.seek(0) torch.jit.load(buffer, map_location='cpu') # Load with extra files. extra_files = {'foo.txt': ''} # values will be replaced with data torch.jit.load('scriptmodule.pt', _extra_files=extra_files) print(extra_files['foo.txt'])