bert-sklearn icon indicating copy to clipboard operation
bert-sklearn copied to clipboard

Unable to load GPU trained model on CPU

Open nihit97natu opened this issue 4 years ago • 2 comments

I trained this model on google colab on a GPU. I am trying to load the model on my local machine(CPU) using load_model() but unable to load it. I am unable to to use pytorch.save() function too to save the model. It gives some error unable to pickle threads. How can I solve this?

nihit97natu avatar Sep 22 '20 04:09 nihit97natu

Hi @nihit97natu,

I have the same problem. Can you find any solution for this? Thanks

metalrt avatar Jan 26 '21 15:01 metalrt

class CPU_Unpickler(pickle.Unpickler):
    def find_class(self, module, name):
        if module == 'torch.storage' and name == '_load_from_bytes':
            return lambda b: torch.load(io.BytesIO(b), map_location='cpu')
        else:
            return super().find_class(module, name)

with open(pathname + '/model.pkl', 'rb') as fp:
        model = CPU_Unpickler(fp).load()

LiudmylaN avatar May 30 '22 04:05 LiudmylaN