第一课
什么是PyTorch?
PyTorch是一个基于Python的科学计算库,它有以下特点:
- 类似于NumPy,但是它可以使用GPU
- 可以用它定义深度学习模型,可以灵活地进行深度学习模型的训练和使用
Tensors
Tensor类似与NumPy的ndarray,唯一的区别是Tensor可以在GPU上加速运算。
import torch
构造一个未初始化的5x3矩阵:
x = torch.empty(5,3)
x
tensor([[ 0.0000e+00, -8.5899e+09, 0.0000e+00],
[-8.5899e+09, nan, 0.0000e+00],
[ 2.7002e-06, 1.8119e+02, 1.2141e+01],
[ 7.8503e+02, 6.7504e-07, 6.5200e-10],
[ 2.9537e-06, 1.7186e-04, nan]])
构建一个随机初始化的矩阵:
x = torch.rand(5,3)
x
tensor([[0.4628, 0.7432, 0.9785],
[0.2068, 0.4441, 0.9176],
[0.1027, 0.5275, 0.3884],
[0.9380, 0.2113, 0.2839],
[0.0094, 0.4001, 0.6483]])
构建一个全部为0,类型为long的矩阵:
x = torch.zeros(5,3,dtype=torch.long)
x
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
x = torch.zeros(5,3).long()
x.dtype
torch.int64
从数据直接直接构建tensor:
x = torch.tensor([5.5,3])
x
tensor([5.5000, 3.0000])
也可以从一个已有的tensor构建一个tensor。这些方法会重用原来tensor的特征,例如,数据类型,除非提供新的数据。
x = x.new_ones(5,3, dtype=torch.double)
x
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
x = torch.randn_like(x, dtype=torch.float)
x
tensor([[ 0.2411, -0.3961, -0.9206],
[-0.0508, 0.2653, 0.4685],
[ 0.5368, -0.3606, -0.0073],
[ 0.3383, 0.6826, 1.7368],
[-0.0811, -0.6957, -0.4566]])
得到tensor的形状:
x.shape
torch.Size([5, 3])
注意
``torch.Size`` 返回的是一个tuple
Operations
有很多种tensor运算。我们先介绍加法运算。
y = torch.rand(5,3)
y
tensor([[0.9456, 0.3996, 0.1981],
[0.8728, 0.7097, 0.3721],
[0.7489, 0.9502, 0.6241],
[0.5176, 0.0200, 0.5130],
[0.3552, 0.2710, 0.7392]])
x + y
tensor([[ 1.1866, 0.0035, -0.7225],
[ 0.8220, 0.9750, 0.8406],
[ 1.2857, 0.5896, 0.6168],
[ 0.8559, 0.7026, 2.2498],
[ 0.2741, -0.4248, 0.2826]])
另一种着加法的写法
torch.add(x, y)
tensor([[ 1.1866, 0.0035, -0.7225],
[ 0.8220, 0.9750, 0.8406],
[ 1.2857, 0.5896, 0.6168],
[ 0.8559, 0.7026, 2.2498],
[ 0.2741, -0.4248, 0.2826]])
加法:把输出作为一个变量
result = torch.empty(5,3)
torch.add(x, y, out=result)
# result = x + y
result
tensor([[ 1.1866, 0.0035, -0.7225],
[ 0.8220, 0.9750, 0.8406],
[ 1.2857, 0.5896, 0.6168],
[ 0.8559, 0.7026, 2.2498],
[ 0.2741, -0.4248, 0.2826]])
in-place加法
y.add_(x)
y
tensor([[ 1.1866, 0.0035, -0.7225],
[ 0.8220, 0.9750, 0.8406],
[ 1.2857, 0.5896, 0.6168],
[ 0.8559, 0.7026, 2.2498],
[ 0.2741, -0.4248, 0.2826]])
注意
任何in-place的运算都会以``_``结尾。 举例来说:``x.copy_(y)``, ``x.t_()``, 会改变 ``x``。
各种类似NumPy的indexing都可以在PyTorch tensor上面使用。
x[1:, 1:]
tensor([[ 0.2653, 0.4685],
[-0.3606, -0.0073],
[ 0.6826, 1.7368],
[-0.6957, -0.4566]])
Resizing: 如果你希望resize/reshape一个tensor,可以使用torch.view
:
x = torch.randn(4,4)
y = x.view(16)
z = x.view(-1,8)
z
tensor([[-0.5683, 1.3885, -2.0829, -0.7613, -1.9115, 0.3732,
-0.2055, -1.2300],
[-0.2612, -0.4682, -1.0596, 0.7447, 0.7603, -0.4281, 0.5495, 0.1025]])
如果你有一个只有一个元素的tensor,使用.item()
方法可以把里面的value变成Python数值。
x = torch.randn(1)
x
tensor([-1.1493])
x.item() # 如果x是一维张量,可以用.item取出它的值
-1.1493233442306519
z.transpose(1,0)
tensor([[-0.5683, -0.2612],
[ 1.3885, -0.4682],
[-2.0829, -1.0596],
[-0.7613, 0.7447],
[-1.9115, 0.7603],
[ 0.3732, -0.4281],
[-0.2055, 0.5495],
[-1.2300, 0.1025]])
更多阅读
各种Tensor operations, 包括transposing, indexing, slicing,
mathematical operations, linear algebra, random numbers在
<https://pytorch.org/docs/torch>
.
Numpy和Tensor之间的转化
在Torch Tensor和NumPy array之间相互转化非常容易。
Torch Tensor和NumPy array会共享内存,所以改变其中一项也会改变另一项。
把Torch Tensor转变成NumPy Array
a = torch.ones(5)
a
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
b
array([1., 1., 1., 1., 1.], dtype=float32)
改变numpy array里面的值。
b[1] = 2
b
array([1., 2., 1., 1., 1.], dtype=float32)
a
tensor([1., 2., 1., 1., 1.])
把NumPy ndarray转成Torch Tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
[2. 2. 2. 2. 2.]
b
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
所有CPU上的Tensor都支持转成numpy或者从numpy转成Tensor。
CUDA Tensors
使用.to
方法,Tensor可以被移动到别的device上。
if torch.cuda.is_available():
device = torch.device("cuda")
y = torch.ones_like(x, device=device)
x = x.to(device)
z = x + y
print(z)
print(z.to("cpu", torch.double))
False
y.to("cpu").data.numpy()
y.cpu().data.numpy()
model = model.cuda()
热身: 用numpy实现两层神经网络
一个全连接ReLU神经网络,一个隐藏层,没有bias。用来从x预测y,使用L2 Loss。
- h = W 1 X h = W_1X h=W1X
- a = m a x ( 0 , h ) a = max(0, h) a=max(0,h)
- y h a t = W 2 a y_{hat} = W_2a yhat=W2a
这一实现完全使用numpy来计算前向神经网络,loss,和反向传播。
- forward pass
- loss
- backward pass
numpy ndarray是一个普通的n维array。它不知道任何关于深度学习或者梯度(gradient)的知识,也不知道计算图(computation graph),只是一种用来计算数学运算的数据结构。
N, D_in, H, D_out = 64, 1000, 100, 10
# 随机创建一些训练数据
x = np.random.randn(N, D_in)
y = np.random.randn(N, D_out)
w1 = np.random.randn(D_in, H)
w2 = np.random.randn(H, D_out)
learning_rate = 1e-6
for it in range(500):
# Forward pass
h = x.dot(w1) # N * H
h_relu = np.maximum(h, 0) # N * H
y_pred = h_relu.dot(w2) # N * D_out
# compute loss
loss = np.square(y_pred - y).sum()
print(it, loss)
# Backward pass
# compute the gradient
grad_y_pred = 2.0 * (y_pred - y)
grad_w2 = h_relu.T.dot(grad_y_pred)
grad_h_relu = grad_y_pred.dot(w2.T)
grad_h = grad_h_relu.copy()
grad_h[h<0] = 0
grad_w1 = x.T.dot(grad_h)
# update weights of w1 and w2
w1 -= learning_rate * grad_w1
w2 -= learning_rate * grad_w2
0 27884200.296767063
1 22640088.482883573
2 19700057.605353158
3 16724246.083260009
4 13350994.753179666
5 9840421.182578644
…
495 4.256310261160519e-06
496 4.074118825087806e-06
497 3.89977645224059e-06
498 3.7328752882126904e-06
499 3.5731529879112403e-06
PyTorch: Tensors
这次我们使用PyTorch tensors来创建前向神经网络,计算损失,以及反向传播。
一个PyTorch Tensor很像一个numpy的ndarray。但是它和numpy ndarray最大的区别是,PyTorch Tensor可以在CPU或者GPU上运算。如果想要在GPU上运算,就需要把Tensor换成cuda类型。
N, D_in, H, D_out = 64, 1000, 100, 10
# 随机创建一些训练数据
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)
w1 = torch.randn(D_in, H)
w2 = torch.randn(H, D_out)
learning_rate = 1e-6
for it in range(500):
# Forward pass
h = x.mm(w1) # N * H
h_relu = h.clamp(min=0) # N * H
y_pred = h_relu.mm(w2) # N * D_out
# compute loss
loss = (y_pred - y).pow(2).sum().item()
print(it, loss)
# Backward pass
# compute the gradient
grad_y_pred = 2.0 * (y_pred - y)
grad_w2 = h_relu.t().mm(grad_y_pred)
grad_h_relu = grad_y_pred.mm(w2.t())
grad_h = grad_h_relu.clone()
grad_h[h<0] = 0
grad_w1 = x.t().mm(grad_h)
# update weights of w1 and w2
w1 -= learning_rate * grad_w1
w2 -= learning_rate * grad_w2
0 29762046.0
1 23066852.0
2 18711686.0
3 14801243.0
4 11081590.0
5 7897110.0
…
495 2.430865788483061e-05
496 2.4154409402399324e-05
497 2.4027987819863483e-05
498 2.394388684479054e-05
499 2.353984564251732e-05
简单的autograd
x = torch.tensor(1., requires_grad=True)
w = torch.tensor(2., requires_grad=True)
b = torch.tensor(3., requires_grad=True)
y = w*x + b # y = 2*1+3
y.backward()
# dy / dw = x
print(w.grad)
print(x.grad)
print(b.grad)
tensor(1.)
tensor(2.)
tensor(1.)
PyTorch: Tensor和autograd
PyTorch的一个重要功能就是autograd,也就是说只要定义了forward pass(前向神经网络),计算了loss之后,PyTorch可以自动求导计算模型所有参数的梯度。
一个PyTorch的Tensor表示计算图中的一个节点。如果x
是一个Tensor并且x.requires_grad=True
那么x.grad
是另一个储存着x
当前梯度(相对于一个scalar,常常是loss)的向量。
N, D_in, H, D_out = 64, 1000, 100, 10
# 随机创建一些训练数据
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)
w1 = torch.randn(D_in, H, requires_grad=True)
w2 = torch.randn(H, D_out, requires_grad=True)
learning_rate = 1e-6
for it in range(500):
# Forward pass
y_pred = x.mm(w1).clamp(min=0).mm(w2)
# compute loss
loss = (y_pred - y).pow(2).sum() # computation graph
print(it, loss.item())
# Backward pass
loss.backward()
# update weights of w1 and w2
with torch.no_grad():
w1 -= learning_rate * w1.grad
w2 -= learning_rate * w2.grad
w1.grad.zero_()
w2.grad.zero_()
0 30471860.0
1 26228534.0
2 23250712.0
3 19323444.0
4 14492299.0
5 9863642.0
…
495 0.00012426167086232454
496 0.00012191860878374428
497 0.00012020763824693859
498 0.0001179972241516225
499 0.00011646386701613665
PyTorch: nn
这次我们使用PyTorch中nn这个库来构建网络。
用PyTorch autograd来构建计算图和计算gradients,
然后PyTorch会帮我们自动计算gradient。
import torch.nn as nn
N, D_in, H, D_out = 64, 1000, 100, 10
# 随机创建一些训练数据
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)
model = torch.nn.Sequential(
torch.nn.Linear(D_in, H, bias=False), # w_1 * x + b_1
torch.nn.ReLU(),
torch.nn.Linear(H, D_out, bias=False),
)
torch.nn.init.normal_(model[0].weight)
torch.nn.init.normal_(model[2].weight)
# model = model.cuda()
loss_fn = nn.MSELoss(reduction='sum')
learning_rate = 1e-6
for it in range(500):
# Forward pass
y_pred = model(x) # model.forward()
# compute loss
loss = loss_fn(y_pred, y) # computation graph
print(it, loss.item())
# Backward pass
loss.backward()
# update weights of w1 and w2
with torch.no_grad():
for param in model.parameters(): # param (tensor, grad)
param -= learning_rate * param.grad
model.zero_grad()
0 28937890.0
1 23708218.0
2 25900276.0
3 31564730.0
4 36221696.0
5 34548928.0
…
495 0.0001501335937064141
496 0.00014750788977835327
497 0.00014515354996547103
498 0.00014255562564358115
499 0.0001401481422362849
model[0].weight
Parameter containing:
tensor([[-0.0218, 0.0212, 0.0243, …, 0.0230, 0.0247, 0.0168],
[-0.0144, 0.0177, -0.0221, …, 0.0161, 0.0098, -0.0172],
[ 0.0086, -0.0122, -0.0298, …, -0.0236, -0.0187, 0.0295],
…,
[ 0.0266, -0.0008, -0.0141, …, 0.0018, 0.0319, -0.0129],
[ 0.0296, -0.0005, 0.0115, …, 0.0141, -0.0088, -0.0106],
[ 0.0289, -0.0077, 0.0239, …, -0.0166, -0.0156, -0.0235]],
requires_grad=True)
PyTorch: optim
这一次我们不再手动更新模型的weights,而是使用optim这个包来帮助我们更新参数。
optim这个package提供了各种不同的模型优化方法,包括SGD+momentum, RMSProp, Adam等等。
import torch.nn as nn
N, D_in, H, D_out = 64, 1000, 100, 10
# 随机创建一些训练数据
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)
model = torch.nn.Sequential(
torch.nn.Linear(D_in, H, bias=False), # w_1 * x + b_1
torch.nn.ReLU(),
torch.nn.Linear(H, D_out, bias=False),
)
torch.nn.init.normal_(model[0].weight)
torch.nn.init.normal_(model[2].weight)
# model = model.cuda()
loss_fn = nn.MSELoss(reduction='sum')
# learning_rate = 1e-4 # Adam优化器的学习率一般是1e-4,不需要normal初始化
# optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
learning_rate = 1e-6 # SGD优化器的学习率一般是1e-4,前面加normal初始化效果会好很多
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
for it in range(500):
# Forward pass
y_pred = model(x) # model.forward()
# compute loss
loss = loss_fn(y_pred, y) # computation graph
print(it, loss.item())
optimizer.zero_grad()
# Backward pass
loss.backward()
# update model parameters
optimizer.step()
0 24436214.0
1 20115276.0
2 18840850.0
3 18223790.0
4 17027580.0
5 14675071.0
…
495 8.447348955087364e-05
496 8.305440132971853e-05
497 8.147572225425392e-05
498 8.059616084210575e-05
499 7.961507071740925e-05
PyTorch: 自定义 nn Modules
我们可以定义一个模型,这个模型继承自nn.Module类。如果需要定义一个比Sequential模型更加复杂的模型,就需要定义nn.Module模型。
import torch.nn as nn
N, D_in, H, D_out = 64, 1000, 100, 10
# 随机创建一些训练数据
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)
class TwoLayerNet(torch.nn.Module):
def __init__(self, D_in, H, D_out):
super(TwoLayerNet, self).__init__()
# define the model architecture
self.linear1 = torch.nn.Linear(D_in, H, bias=False)
self.linear2 = torch.nn.Linear(H, D_out, bias=False)
def forward(self, x):
y_pred = self.linear2(self.linear1(x).clamp(min=0))
return y_pred
model = TwoLayerNet(D_in, H, D_out)
loss_fn = nn.MSELoss(reduction='sum')
learning_rate = 1e-4
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for it in range(500):
# Forward pass
y_pred = model(x) # model.forward()
# compute loss
loss = loss_fn(y_pred, y) # computation graph
print(it, loss.item())
optimizer.zero_grad()
# Backward pass
loss.backward()
# update model parameters
optimizer.step()
0 675.5318603515625
1 659.0227661132812
2 642.9276123046875
3 627.2772216796875
4 612.0651245117188
5 597.2562255859375
…
495 4.05078225185207e-07
496 3.8108996136543283e-07
497 3.5824149335894617e-07
498 3.3695400247779617e-07
499 3.168272826314933e-07