But I am now attempting to use a GPU trained language model on my local CPU to train a text classifier, and I am getting the same RuntimeError when I try to load the encoder:
Good point, I just added the option in master to pass a device (like load has).
If you don’t have a dev install, a workaround is to load your encoder file on the CPU then save it again:
I am new to fastai and deep learning, I have 2 models that were trained and saved on GPU based system. I am now attempting to load the pkl files that were saved on the GPU system and make predictions from a CPU based computer and I keep getting this error “Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=‘cpu’ to map your storages to the CPU.” FastAI version = 1.0.49 Torch version = 1.0.1
See code below.
import fastai
from fastai import *
from fastai.text import *
def predict(text, category):
if(category=='xyz' or category=='xyz2'):
learn = load_learner(path=".", fname='model1.pkl')
else:
learn = load_learner(path=".", fname='model2.pkl')
return str(learn.predict(text)[0])
text = 'my text goes here'
res = predict(text,'xyz')
print(res)
File "test.py", line 128, in <module>
net.load_state_dict(copyStateDict(torch.load(args.trained_model)))
File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 386, in load
return _load(f, map_location, pickle_module, **pickle_load_args)
File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 573, in _load
result = unpickler.load()
File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 536, in persistent_load
deserialized_objects[root_key] = restore_location(obj, location)
File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 119, in default_restore_location
result = fn(storage, location)
File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 95, in _cuda_deserialize
device = validate_cuda_device(location)
File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 79, in validate_cuda_device
raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.