参考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前面的空格不能少