【PyTorch】深度学习与Pytorch实战笔记
学习笔记01:初见PyTorch
检查GPU加速是否可用
import torch
print(torch.__version__)
print(torch.cuda.is_available())
对比CPU与GPU方式速度区别
import torch
import time
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))
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))# 测试第二次
自动求导
y
=
a
2
x
+
b
x
+
c
y=a^{2} x+b x+c
y=a2x+bx+c,求
x
=
1
x=1
x=1时的导数
即求导:
{
∂
y
∂
a
=
2
a
⋅
x
=
2
a
x
∂
y
∂
b
=
x
∂
y
∂
c
=
1
\left\{\begin{array}{l}\frac{\partial y}{\partial a}=2 a \cdot x=2 a x \\ \frac{\partial y}{\partial b}=x \\ \frac{\partial y}{\partial c}=1\end{array}\right.
⎩⎨⎧∂a∂y=2a⋅x=2ax∂b∂y=x∂c∂y=1
x
=
1
x=1
x=1,得到
{
∂
y
∂
a
=
2
a
∂
y
∂
b
=
1
∂
y
∂
c
=
1
\left\{\begin{array}{l}\frac{\partial y}{\partial a} =2a \\ \frac{\partial y}{\partial b} = 1\\ \frac{\partial y}{\partial c} = 1\end{array}\right.
⎩⎨⎧∂a∂y=2a∂b∂y=1∂c∂y=1
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])