假设我们只保存了模型的参数(model.state_dict())到文件名为modelparameters.pth, model = Net()
1. cpu -> cpu或者gpu -> gpu:
checkpoint = torch.load('modelparameters.pth')
model.load_state_dict(checkpoint)
2. cpu -> gpu 1
checkpoint =torch.load('modelparameters.pth', map_location=lambda storage, loc: storage.cuda(1))
model.load_state_dict(checkpoint)
3. gpu 1 -> gpu 0
checkpoint =torch.load('modelparameters.pth', map_location={'cuda:1':'cuda:0'})
model.load_state_dict(checkpoint)
4. gpu -> cpu
checkpoint =torch.load('modelparameters.pth', map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint)
5. multi-gpu - > single gpu
Sometimes the error occur: Unexpected key(s) in state_dict: "module.features. ...".,Expected ".features....". 这种情况往往存在于用多GPU训练,但是用单