pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False

错误一: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.

解决方法:在torch.load(xxx)后面添加map_location=torch.device('cpu')或者map_location='cpu'

示例
pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False_第1张图片
pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False_第2张图片

state_dict = torch.load(self.model_path)

在torch.load括号后面添加map_location=torch.device('cpu')或者map_location='cpu'

在这里修改如下:

state_dict = torch.load(self.model_path,map_location='cpu')
或
state_dict = torch.load(self.model_path,map_location='cpu')

修改之后,运行可能会出现以下错误:

错误二:AssertionError: Torch not compiled with CUDA enabled

解决方法:去掉所有的.cuda()

示例
pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False_第3张图片
点击蓝色部分字体,进入代码,定位到出错位置,可以看到这边转换为cuda运行,将其注释掉即可
pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False_第4张图片
注释掉,并保存
pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False_第5张图片
继续运行程序,找出.cuda(),并将其去掉
pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False_第6张图片
pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False_第7张图片
pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False_第8张图片

你可能感兴趣的:(cuda,人工智能)