安装cuda版本Pytorch+pycharm的anaconda环境配置

参考https://zhuanlan.zhihu.com/p/305854081
假设已经装了cuda
进入cmd,检验cuda版本

nvcc --version

电脑1 python3.9 在这里插入图片描述
电脑2 python3.11
在这里插入图片描述

下载地址:https://download.pytorch.org/whl/torch_stable.html

如果之前装了cpu的torch,可以直接下torchvision;如果之前没有torch,需先装torch,再装torchvision,直接安torchvision会自动先安装torch的cpu版本。

找到适配win64位和python3.11的,发现最新的是cuda118,同样适配cuda12.2:
在这里插入图片描述
从Anaconda prompt进入自己的Anaconda环境,安装

(base) C:\Users\yhm>cd C:\Users\yhm\Downloads
(base) C:\Users\yhm\Downloads>pip install torch-2.0.1+cu118-cp311-cp311-win_amd64.whl

在这里插入图片描述
继续安装torchvision
在这里插入图片描述
在这里插入图片描述
(忽略试错了的torch-2.0.1)
完成!
下载后检验

python
import torch
torch.cuda.is_available()
torch.__version__

在这里插入图片描述在这里插入图片描述
完成!
刷新一下anaconda页面,看到
在这里插入图片描述
说明已经安装在anaconda的base环境里了

pycharm的anaconda环境配置

参考https://blog.csdn.net/TuckX/article/details/115681862
新建project,点击Add interpreter

在这里插入图片描述

找到对应目录

在这里插入图片描述

编译器变成这样

在这里插入图片描述

从Files-settings里面可以看到所有安装包,torch也在

在这里插入图片描述

右下角查看正在使用的编译器

在这里插入图片描述

测试gpu加速代码,参考https://blog.csdn.net/qq_36162036/article/details/107407928

import torch
import time
from torch import autograd
#GPU加速
print(torch.__version__)
print(torch.cuda.is_available())

a=torch.randn(10000,1000)
b=torch.randn(1000,10000)
print(a)
print(b)
t0=time.time()
c=torch.matmul(a,b)
t1=time.time()

print(a.device,t1-t0,c.norm(2))

device=torch.device('cuda')
print(device)
a=a.to(device)
b=b.to(device)

t0=time.time()
c=torch.matmul(a,b)
t2=time.time()
print(a.device,t2-t0,c.norm(2))


t0=time.time()
c=torch.matmul(a,b)
t2=time.time()

print(a.device,t2-t0,c.norm(2))

结果
在这里插入图片描述
cuda加速了100倍

若出现pip安装包报错

Could not find a version that satisfies the requirement pymysql (from versions: none)
可用

pip install 包名 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

进行加速,-i前面的空格不能少

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值