在已安装cuda的前提下,如何在Anaconda指定环境中安装pytorch cuda

前提

  1. 电脑支持cuda并且之前已经安装过cuda以及cudnn等
  2. 电脑已经安装并配置好anaconda环境
  3. 已创建好anaconda指定虚拟环境

1. 查找PC已安装的cuda版本

打开终端cmd,输入指令:nvcc -V

C:\Users\emos>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Tue_Mar__8_18:36:24_Pacific_Standard_Time_2022
Cuda compilation tools, release 11.6, V11.6.124
Build cuda_11.6.r11.6/compiler.31057947_0

由最后一行可知,电脑中安装的cuda版本为cuda11.6

2. 在PyTorch官网查找指令

进入PyTorch官网,如图
在这里插入图片描述
图中,只有CUDA 11.7及以上,我们可以从 Previous PyTorch Version 中查找对应版本。但实测,可以直接复制 Run this Command 并将其中的cu117改为cu116即可,即:

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu116

3. 在Anaconda指定虚拟环境中运行指令

假设我们已经创建的虚拟环境名称为 cudatest

  1. 首先打开Anaconda Prompt
  2. 输入指令 conda activate cudatest以激活环境
  3. 进入该虚拟环境后,输入第2个步骤中所查找的cuda下载指令,即
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu116
  1. 等待安装完成即可

4. 验证 pytorch cuda可用

在Anaconda Prompt激活虚拟环境后,输入 python进入python控制台。

(cudatest) C:\Users\Users_Name>python
Python 3.10.9 | packaged by Anaconda, Inc. | (main, Mar  8 2023, 10:42:25) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.is_available()
True
>>>

显示True即代表cuda可用

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值