Attempting to deserialize object on a CUDA device but torch.cuda.is_available()
时间: 2023-11-13 22:54:53 浏览: 106
这个错误通常是因为在CPU上训练模型,然后尝试在GPU上加载模型时出现的。解决方法是在加载模型时使用`map_location=torch.device('cpu')`将模型映射到CPU上。例如:
```
model = torch.load('model.pth', map_location=torch.device('cpu'))
```
相关问题
Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False.
回答: 该错误提示表示尝试在CUDA设备上反序列化对象,但torch.cuda.is_available()返回False,即没有可用的CUDA设备。要解决这个问题,可以使用torch.load函数时添加参数map_location=torch.device('cpu'),将存储映射到CPU上。这样可以确保在没有CUDA设备的CPU-only机器上正常加载对象。<em>1</em><em>2</em><em>3</em>
#### 引用[.reference_title]
- *1* [pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False](https://blog.csdn.net/qq_27149279/article/details/106037681)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
- *2* *3* [RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is Fal](https://blog.csdn.net/qq_44936246/article/details/126375749)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item]
[ .reference_list ]
Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False
这个错误意味着你正在尝试将一个在CUDA设备上序列化的对象反序列化,但是torch.cuda.is_available()返回的值为False,即CUDA不可用。
要解决这个问题,你可以在torch.load()函数后面添加map_location=torch.device('cpu')或者map_location='cpu'参数来将存储映射到CPU上。例如:
state_dict = torch.load(self.model_path, map_location=torch.device('cpu')) 或
state_dict = torch.load(self.model_path, map_location='cpu')
如果你看到了"AssertionError: Torch not compiled with CUDA enabled"错误,这意味着你的PyTorch没有启用CUDA。你可以去掉代码中所有的.cuda()函数调用来解决这个问题。你可以注释掉这些代码并保存,然后重新运行程序。
另外,你需要根据你的服务器上的CUDA版本选择适合的PyTorch版本。确保你安装了与服务器CUDA版本兼容的PyTorch版本。比如,如果你的服务器CUDA版本为11.0,你需要找到与之兼容的PyTorch版本,并进行相应的安装。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [pytorch出错: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False](https://blog.csdn.net/qq_27149279/article/details/106037681)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False.](https://blog.csdn.net/weixin_40438421/article/details/130514552)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文