Loading weights for CPU model while trained on GPU
This is not a very complicated issue, but I am not sure what is the best way to load the weights into the cpu when the model was trained on a GPU, thus here is my solution: <SCODE>model = torch.load('mymodel')
self.model = model.cpu().double()
<ECODE>
jtremblay
<SCODE>torch.load('my_file.pt', map_location=lambda storage, loc: storage)
<ECODE>
fmassa
achaiah
fmassa
Perfect, thanks for the clarification.  I ended up using the conversion suggested in your linked post.
achaiah
Thanks for the suggestion!
jadore801120
torch.load('my_file.pt', map_location=lambda storage, loc: storage)
tralgu
I have a related question, I have a shared model trained on GPU, and another process needs this model for inference on CPU. So I use a shared model and use the following command to load this shared model <SCODE>    cpu_model.load_state_dict(gpu_model.cpu().state_dict())
<ECODE> however, this won’t work and returns CUDA error(3), initialization error, what happened?
Peter_Ham
If I load model with your hack and set model.train(‘True’) and trying even inference it fails. So it do not work for all cases (finetune on CPU after training on GPU not working).
Marat
<SCODE>use_cuda = torch.cuda.is_available()
DEVICE = torch.device('cuda' if use_cuda else 'cpu')   # 'cpu' in this case

cpu_model = your_model()
cpu_model.load_state_dict(torch.load(path_to_your_saved_gpu_model, map_location=DEVICE)
<ECODE>
TSLsun
thanks, worked for me
A_Rza_SH
prashanth