报错:
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
分析原因:
1、在加载model训练后模型时,出现调用CUDA加载,但电脑中未安装CUDA。
假如使用CPU训练后的模型,建议使用以下方法:
# print(torch.cuda.is_available())
device = 'cuda:0' if torch.cuda.is_available() else 'cpu:0'
model = ConvNet().to(device)
model.load_state_dict(torch.load('model.ckpt'))
2、在训练时采用的的GPU的CUDA训练,但在加载时使用cpu加载导致出错。
建议GPU训练,GPU的CUDA加载训练后的模型。
建议CPU训练,CPU加载训练后的模型。