深度学习与PyTorch入门实战 - 网易云课堂
dragen1860/Deep-Learning-with-PyTorch-Tutorials: 深度学习与PyTorch入门实战视频教程 配套源代码和PPT
主讲:龙良曲
1 深度学习框架简介
2 PyTorch功能演示
2.1 GPU加速
from pyxllib.xl import PerfTest
import torch
class Process(PerfTest):
def __init__(self):
self.a = torch.randn(10000, 1000)
self.b = torch.randn(1000, 20000)
def perf_cpu(self):
return torch.matmul(self.a, self.b).norm(2)
def perf_gpu(self):
# device =torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# torch.device('cpu')、torch.device('cuda:0')
device = torch.device('cuda')
return torch.matmul(self.a.to(device), self.b.to(device)).norm(2)
def perf_gpu2(self):
# gpu第一次加载比较慢,可以再执行一次看看
return self.perf_gpu()
print(f'{torch.__version__ = }')
# torch.__version__ = '1.7.1'
Process().perf()
# cpu 用时(秒) 2.042 运行结果:tensor(414725.4062)
# gpu 用时(秒) 1.648 运行结果:tensor(447307.6562, device='cuda:0')
# 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.)
2.3 常用网络层
nn.Linear
nn.Conv2d
nn.LSTM
nn.ReLU
nn.Sigmoid
nn.Softmax
nn.CrossEntropyLoss
nn.MSE