课程链接:https://www.bilibili.com/video/BV1ob4y1p7JL?share_source=copy_web&vd_source=2de85c4d0eae5a5c849288fe6d651bd6
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)) # c.norm(2) 是指计算c的L2范式
# 使用GPU设备
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)) # c.norm(2) 是指计算c的L2范式
t0 = time.time()
c = torch.matmul(a, b) # 矩阵乘法
t2 = time.time()
print(a.device, t2-t0, c.norm(2)) # c.norm(2) 是指计算c的L2范式
'''
cpu 0.09226155281066895 tensor(141286.1719)
cuda:0 1.3737411499023438 tensor(141350.2344, device='cuda:0')
cuda:0 0.0 tensor(141350.2344, device='cuda:0')
第一次运行GPU会有运行环境的时间开销,所以以第二次为准
'''
自动求导
import torch
from torch import autograd
x = torch.tensor(1)
a = torch.tensor(1., requires_grad=True) # Only Tensors of floating point and complex dtype can require gradients
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])
常用网络层