查看自己cuda版本,

Pytorch1.5.1版本安装的方法步骤

我的cuda是11版本了,所以可以安装11版本以下的任何版本。

进入pytorch官网

官网网址:https://pytorch.org/

Pytorch1.5.1版本安装的方法步骤

2020年11月19号,更新

最简单的是直接按官网给的Run this Command命令,直接安装,如下:

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch

解释:-c pytorch,意思是从pytorch网站下载,速度感人,有办法的那就方便多了。

按照上面图这样选择,安装pytorch有GPU加速的版本,安装命令可以改下,后面加个豆瓣源,这样下载速度快些。

pip install torch===1.5.1 torchvision===0.6.1 -f https://download.pytorch.org/whl/torch_stable.html -i https://pypi.douban.com/simple

或者直接用conda安装,去掉后面的 -c pytorch

conda install pytorch torchvision cudatoolkit=10.2 

如果上面方法都下载慢,那就按下面方法来。(适用于win版本,Linux的可以返回上一层寻找对应的版本)

先进清华源https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/"text-align: center">Pytorch1.5.1版本安装的方法步骤

还要下载对应的torchvision===0.6.1,如图

Pytorch1.5.1版本安装的方法步骤

下载好就在命令行进入你下载的路径目录里面安装,并输入下面代码进行离线安装。

conda install --offline 对应的安装包文件名字

安装完后还要安装cudatoolkit=10.2

conda install cudatoolkit=10.2

然后运行测试代码:

# TEST
import torch
from torch.backends import cudnn

x = torch.Tensor([1.0])
xx = x.cuda()
print(torch.__version__)
print(torch.version.cuda)
print(torch.cuda.is_available())
print(xx)
print(cudnn.is_acceptable(xx))

结果:

1.5.1
10.2
True
tensor([1.], device='cuda:0')
True

安装成功!

GPU加速代码

import torch
import time

print(torch.__version__)
print(torch.cuda.is_available())

a = torch.randn(10000, 1000)
b = torch.randn(1000, 2000)

t0 = time.time()
c = torch.matmul(a, b) # 矩阵乘法
t1 = time.time()
print(a.device, t1 - t0, c.norm(2))

t0 = time.time()
c = torch.matmul(a, b) # 矩阵乘法
t1 = time.time()
print(a.device, t1 - t0, c.norm(2))

device = torch.device('cuda')
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))

结果:

1.5.1
True
cpu 0.13901472091674805 tensor(140929.9688)
cpu 0.16696977615356445 tensor(140929.9688)
cuda:0 0.22500324249267578 tensor(141330.6875, device='cuda:0')
cuda:0 0.003974437713623047 tensor(141330.6875, device='cuda:0')

运行两次是cuda有个预热的过程,第二次的时间明显减少了。和CPU相比,更快。

自动求导

代码:

import torch
from torch import autograd

x = torch.tensor(1.)
a = torch.tensor(1., requires_grad=True)
b = torch.tensor(2., requires_grad=True)
c = torch.tensor(3., requires_grad=True)

y = a ** 2 * x + b * x + c

print('before:', a.grad, b.grad, c.grad)
grads = autograd.grad(y, [a, b, c])
print('after :', grads[0], grads[1], grads[2])

结果:

before: None None None
after : tensor(2.) tensor(1.) tensor(1.)

可以看出pytorch比TensorFlow1.X好理解,适合人类思维,功能也都全。

标签:
Pytorch1.5.1安装,Pytorch,安装

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“Pytorch1.5.1版本安装的方法步骤”

暂无“Pytorch1.5.1版本安装的方法步骤”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。