PyTorch简介
PyTorch是一个动态的建图的工具。不像Tensorflow那样,先建静态图,然后通过feed和run重复执行建好的图。相对来说,PyTorch具有更好的灵活性。中文文档链接link。
Python 3.6 安装
Anaconda 安装
Anaconda是一个自动管理python 第三方库的软件工具,类似pip,但是更加方便,第三方库的版本兼容性管理更完善。
下载连接:https://www.anaconda.com/distribution/#download-section

CUDA 安装
CUDA是英伟达提供的一套运算平台,安装CUDA套件可以根据调用英伟达显卡来进行计算。
下载链接:https://developer.nvidia.com/cuda-downloads
Pytorch安装
下载链接:https://pytorch.org/,根据自己的计算机选择合适的下载包

pip3 install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html
安装测试
本测试内容包含GPU加速,自动求导,和网络层。
import timeimport torchprint(torch.__version__)print(torch.cuda.is_available())a = torch.randn(10000, 1000)b = torch.randn(1000, 2000)t0 = time.time()# 默认使用CPU模式进行计算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()# 默认使用CPU模式进行计算c = torch.matmul(a, b)t1 = time.time()print(a.device, t1 - t0, c.norm(2))# 第二次计算t0 = time.time()# 默认使用CPU模式进行计算c = torch.matmul(a, b)t1 = time.time()print(a.device, t1 - t0, c.norm(2))
查看结果:
可以发现cuda第一次执行比CPU还要长,这是因为CUDA第一次执行的时候要完成一些初始化工作,第二次执行只有5.99毫秒,就非常快了。
