pytorch-cuda安装报错

2022.12.8在win+cuda11.8下安装最新Pytorch GPU版时遇到包不兼容的问题,该文记录安装的整个流程

一、按照cuda版本在官网找命令

Pytorch官网命令如下
在win、cuda11.8下安装GPU版Pytorch

conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

分析该命令,-c后面为安装包的源,-c pytorch表示使用官方源,可能出现速度慢等问题

二、换源

因此一般先将conda切换清华源

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/

三、Bug描述

换源后去掉-c执行命令如下

conda install pytorch torchvision torchaudio pytorch-cuda=11.7

报错包不兼容,出现冲突

Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
Examining torchaudio:  43%|██████████████████████████████████████████████▎                                                             | 3/7 [00:22<00:24,  6.10s/it]/failed                                                                                                                                                                

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - torchaudio -> python[version='2.7.*|3.5.*|3.6.*|>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|3.4.*']

Your python: python=3.7

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

The following specifications were found to be incompatible with each other:

Output in format: Requested package -> Available versions

Package typing conflicts for:
torchvision -> typing_extensions -> typing[version='>=3.7.4']
pytorch -> typing_extensions -> typing[version='>=3.7.4']

Package pytorch conflicts for:
torchaudio -> pytorch[version='1.10.0|1.10.1|1.10.2|1.11.0|1.12.0|1.12.1|1.13.0|1.9.1|1.9.0|1.8.1|1.8.0|1.7.1|1.7.0|1.6.0']
torchvision -> pytorch[version='1.10.0|1.10.1|1.10.2|1.11.0|1.12.0|1.12.1|1.13.0|1.9.1|1.9.0|1.8.1|1.8.0|1.7.1|1.7.0|1.6.0|1.5.1|1.5.0|1.4.0|1.3.1|1.3.0|1.2.0|>=1.1.0|>=1.0.0|>=0.4']

Package pytorch-cuda conflicts for:
pytorch -> pytorch-cuda[version='>=11.6,<11.7|>=11.7,<11.8']
torchaudio -> pytorch-cuda[version='11.6.*|11.7.*']
torchvision -> pytorch==1.13.0 -> pytorch-cuda[version='>=11.6,<11.7|>=11.7,<11.8']
torchaudio -> pytorch==1.13.0 -> pytorch-cuda[version='>=11.6,<11.7|>=11.7,<11.8']
torchvision -> pytorch-cuda[version='11.6.*|11.7.*']

Package requests conflicts for:
python=3.7 -> pip -> requests
torchvision -> request

四、解决方法

参考之前安装成功的命令为

conda install pytorch torchvision torchaudio cudatoolkit=11.3

尝试执行

conda install pytorch torchvision torchaudio cudatoolkit=11.7

报错找不到包

PackagesNotFoundError: The following packages are not available from current channels:
  - cudatoolkit=11.7

推测原因可能是cuda11.7将cudatoolkit=11.7换为pytorch-cuda=11.7,而-c nvidia为pytorch-cuda的源。

执行如下命令成功安装

 conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c nvidia  

五、查看是否使用GPU

import torch

use_gpu = torch.cuda.is_available()
print(use_gpu)
print("devices count:", torch.cuda.device_count())

输出

True
devices count: 1
  • 30
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
安装PyTorchCUDA,可以按照以下步骤进行操作: 1. 安装CUDA驱动:首先,你需要确保你的显卡支持CUDA。然后,前往NVIDIA官方网站下载适合你的操作系统版本的CUDA驱动程序,并按照安装向导进行安装。 2. 安装Anaconda:在安装PyTorch之前,建议你使用Anaconda来管理你的Python环境。前往Anaconda官方网站下载适合你的操作系统版本的Anaconda安装程序,并按照安装向导进行安装。 3. 创建一个新的虚拟环境:打开终端或命令提示符,并运行以下命令创建一个名为"torch"的新虚拟环境: ``` conda create -n torch python=3.8 ``` 然后,激活这个虚拟环境: ``` conda activate torch ``` 4. 安装PyTorch:运行以下命令来安装PyTorch(使用pip或conda): 如果你的计算机支持CUDA: ``` conda install pytorch torchvision cudatoolkit=xx.x -c pytorch ``` 请将 "xx.x" 替换为你安装CUDA版本号。例如,如果你安装的是CUDA 11.1,则使用以下命令: ``` conda install pytorch torchvision cudatoolkit=11.1 -c pytorch ``` 如果你的计算机不支持CUDA,可以使用以下命令来安装CPU版本的PyTorch: ``` conda install pytorch torchvision cpuonly -c pytorch ``` 5. 验证安装安装完成后,可以在Python中导入PyTorch库并验证安装是否成功。打开Python解释器或运行一个Python脚本,并尝试导入PyTorch: ``` import torch print(torch.__version__) ``` 如果没有报错并且成功输出了PyTorch的版本号,那么恭喜你,安装成功! 请注意,这些步骤仅适用于使用Anaconda管理Python环境的情况。如果你使用其他方式来管理你的Python环境,可以根据官方文档来安装PyTorchCUDA
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值