深度学习与PyTorch入门实战 - 网易云课堂
dragen1860/Deep-Learning-with-PyTorch-Tutorials: 深度学习与PyTorch入门实战视频教程 配套源代码和PPT
主讲:龙良曲

初见PyTorch.pdf

1 深度学习框架简介

image.png

2 PyTorch功能演示

2.1 GPU加速

  1. from pyxllib.xl import PerfTest
  2. import torch
  3. class Process(PerfTest):
  4. def __init__(self):
  5. self.a = torch.randn(10000, 1000)
  6. self.b = torch.randn(1000, 20000)
  7. def perf_cpu(self):
  8. return torch.matmul(self.a, self.b).norm(2)
  9. def perf_gpu(self):
  10. # device =torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  11. # torch.device('cpu')、torch.device('cuda:0')
  12. device = torch.device('cuda')
  13. return torch.matmul(self.a.to(device), self.b.to(device)).norm(2)
  14. def perf_gpu2(self):
  15. # gpu第一次加载比较慢,可以再执行一次看看
  16. return self.perf_gpu()
  17. print(f'{torch.__version__ = }')
  18. # torch.__version__ = '1.7.1'
  19. Process().perf()
  20. # cpu 用时(秒) 2.042 运行结果:tensor(414725.4062)
  21. # gpu 用时(秒) 1.648 运行结果:tensor(447307.6562, device='cuda:0')
  22. # gpu2 用时(秒) 0.029 运行结果:tensor(447307.6562, device='cuda:0')

2.2 自动求导

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)
# before: None None None

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

image.png

2.3 常用网络层

nn.Linear
nn.Conv2d
nn.LSTM

nn.ReLU
nn.Sigmoid

nn.Softmax
nn.CrossEntropyLoss
nn.MSE