将由GPU保存的模型加载到CPU上。
将torch.load()函数中的map_location参数设置为torch.device('cpu')
device = torch.device('cpu')
#实例化定义好的模型
model = TheModelClass(*args, **kwargs)
#将gpu保存的模型加载到cpu上
model.load_state_dict(torch.load(PATH, map_location=device))
将GPU保存的模型加载到GPU上
device = torch.device("cuda")
#实例化一个模型
model = TheModelClass(*args, **kwargs)
#加载模型
model.load_state_dict(torch.load(PATH))
#将模型转到gpu上
model.to(device)
将由CPU保存的模型加载到GPU上。
确保对输入的tensors调用input = input.to(device)方法。map_location是将模型加载到GPU上,model.to(torch.device('cuda'))是将模型参数加载为CUDA的tensor。最后保证使用.to(torch.device('cuda'))方法将需要使用的参数放入CUDA
device = torch.device("cuda")
model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH, map_location="cuda:0")) # Choose whatever GPU device number you want
model.to(device)
8605

被折叠的 条评论
为什么被折叠?



