PyTorch 入门教程

2020-10-5 15:18:58 来源: GNSSer 发布人:

大部分翻译自:

https://github.com/jcjohnson/pytorch-examples

这个存储库通过自包含的示例介绍了PyTorch的基本概念。
Pythorch的核心功能有两个:
n维张量,类似于numpy,但可以在gpu上运行
神经网络的自动微分构造与训练
我们将使用一个完全连接的ReLU网络作为我们的运行示例。该网络将有一个单一的隐藏层,并将通过梯度下降训练,以适应随机数据,使网络输出和真实输出之间的欧几里德距离最小。

热身:numpy

在介绍PyTorch之前,我们将首先使用numpy实现该网络。
Numpy提供了一个n维数组对象,以及许多操作这些数组的函数。Numpy是一个科学计算的通用框架;它对计算图、深度学习或梯度一无所知。但是,我们可以很容易地使用numpy,通过使用numpy操作在网络中手动实现正向和反向传递,从而使两层网络适应随机数据:

# Code in file tensor/two_layer_net_numpy.py
import numpy as np

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random input and output data
x = np.random.randn(N, D_in)
y = np.random.randn(N, D_out)

# Randomly initialize weights
w1 = np.random.randn(D_in, H)
w2 = np.random.randn(H, D_out)

learning_rate = 1e-6
for t in range(500):
# Forward pass: compute predicted y
h = x.dot(w1)
h_relu = np.maximum(h, 0)
y_pred = h_relu.dot(w2)

# Compute and print loss
loss = np.square(y_pred - y).sum()
print(t, loss)

# Backprop to compute gradients of w1 and w2 with respect to loss
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
w1 -= learning_rate * grad_w1
w2 -= learning_rate * grad_w2


Pythorch:张量

Numpy是一个很好的框架,但是它不能利用gpu来加速它的数值计算。对于现代深度神经网络,gpu通常提供50倍或更高的加速,所以不幸的是numpy不足以进行现代深度学习。
这里我们介绍最基本的Pythorch概念:张量。Pythorch张量在概念上与numpy数组相同:张量是n维数组,Pythorch提供了许多操作这些张量的函数。任何你想用numpy执行的计算也可以用PyTorch张量来完成;你应该把它们看作是科学计算的通用工具。
然而,与numpy不同,PyTorch张量可以利用gpu来加速数值计算。要在GPU上运行PyTorch张量,可以在构造张量时使用device参数将张量放置在GPU上。
这里我们使用Pythorch张量来拟合随机数据的两层网络。与上面的numpy示例一样,我们使用PyTorch张量上的操作手动实现网络中的正向和反向传递:

# Code in file tensor/two_layer_net_tensor.py
import torch

device = torch.device('cpu')
# device = torch.device('cuda') # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random input and output data
x = torch.randn(N, D_in, device=device)
y = torch.randn(N, D_out, device=device)

# Randomly initialize weights
w1 = torch.randn(D_in, H, device=device)
w2 = torch.randn(H, D_out, device=device)

learning_rate = 1e-6
for t in range(500):
# Forward pass: compute predicted y
h = x.mm(w1)
h_relu = h.clamp(min=0)
y_pred = h_relu.mm(w2)

# Compute and print loss; loss is a scalar, and is stored in a PyTorch Tensor
# of shape (); we can get its value as a Python number with loss.item().
loss = (y_pred - y).pow(2).sum()
print(t, loss.item())

# Backprop to compute gradients of w1 and w2 with respect to loss
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 using gradient descent
w1 -= learning_rate * grad_w1
w2 -= learning_rate * grad_w2


Pythorch:Autograd(自动分级)

在上面的例子中,我们必须手动实现神经网络的正向和反向传递。对于一个小型的两层网络来说,手动实现向后传递不是什么大问题,但是对于大型复杂网络来说,很快就会变得非常棘手。
谢天谢地,我们可以使用自动微分来自动计算神经网络中的反向传递。PyTorch中的autograd包正好提供了这种功能。当使用autograd时,网络的前向过程将定义一个计算图;图中的节点将是张量,边将是从输入张量生成输出张量的函数。通过这个图的反向传播可以很容易地计算梯度。
这听起来很复杂,在实践中使用起来非常简单。如果我们想计算关于某个张量的梯度,那么我们在构造张量时设置requires_grad=True。对该张量的任何PyTorch操作都将导致构造一个计算图,从而允许我们稍后通过该图执行反向传播。如果x是一个张量,且requires_grad=True,那么在反向传播之后,x.grad将是另一个保持x相对于某个标量值的梯度的张量。
有时您可能希望防止Pythorch在使用requires_grad=True对张量执行某些操作时构建计算图;例如,在训练神经网络时,我们通常不希望通过权重更新步骤进行反向传播。在这种情况下,我们可以使用火炬。没有梯度()上下文管理器,以防止构造计算图。
这里我们使用PyTorch张量和autograd来实现我们的两层网络;现在我们不再需要手动实现网络的反向传递:

# Code in file autograd/two_layer_net_autograd.py
import torch

device = torch.device('cpu')
# device = torch.device('cuda') # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold input and outputs
x = torch.randn(N, D_in, device=device)
y = torch.randn(N, D_out, device=device)

# Create random Tensors for weights; setting requires_grad=True means that we
# want to compute gradients for these Tensors during the backward pass.
w1 = torch.randn(D_in, H, device=device, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, requires_grad=True)

learning_rate = 1e-6
for t in range(500):
# Forward pass: compute predicted y using operations on Tensors. Since w1 and
# w2 have requires_grad=True, operations involving these Tensors will cause
# PyTorch to build a computational graph, allowing automatic computation of
# gradients. Since we are no longer implementing the backward pass by hand we
# don't need to keep references to intermediate values.
y_pred = x.mm(w1).clamp(min=0).mm(w2)

# Compute and print loss. Loss is a Tensor of shape (), and loss.item()
# is a Python number giving its value.
loss = (y_pred - y).pow(2).sum()
print(t, loss.item())

# Use autograd to compute the backward pass. This call will compute the
# gradient of loss with respect to all Tensors with requires_grad=True.
# After this call w1.grad and w2.grad will be Tensors holding the gradient
# of the loss with respect to w1 and w2 respectively.
loss.backward()

# Update weights using gradient descent. For this step we just want to mutate
# the values of w1 and w2 in-place; we don't want to build up a computational
# graph for the update steps, so we use the torch.no_grad() context manager
# to prevent PyTorch from building a computational graph for the updates
with torch.no_grad():
w1 -= learning_rate * w1.grad
w2 -= learning_rate * w2.grad

# Manually zero the gradients after running the backward pass
w1.grad.zero_()
w2.grad.zero_()

Pythorch:定义新的自动加载函数

在幕后,每一个原始的自学习算子实际上是两个作用于张量的函数。forward函数从输入张量计算输出张量。后向函数接收输出张量相对于某个标量值的梯度,并计算输入张量相对于该标量值的梯度。
在PyTorch中,我们可以通过定义火炬自动加载功能实现前向和后向功能。然后我们可以通过构造一个实例并像函数一样调用它,传递包含输入数据的张量,从而使用新的autograd操作符。
在本例中,我们定义了自己的自定义自动加载函数来执行ReLU非线性,并使用它来实现我们的两层网络:

# Code in file autograd/two_layer_net_custom_function.py
import torch

class MyReLU(torch.autograd.Function):
"""
We can implement our own custom autograd Functions by subclassing
torch.autograd.Function and implementing the forward and backward passes
which operate on Tensors.
"""
@staticmethod
def forward(ctx, x):
"""
In the forward pass we receive a context object and a Tensor containing the
input; we must return a Tensor containing the output, and we can use the
context object to cache objects for use in the backward pass.
"""
ctx.save_for_backward(x)
return x.clamp(min=0)

@staticmethod
def backward(ctx, grad_output):
"""
In the backward pass we receive the context object and a Tensor containing
the gradient of the loss with respect to the output produced during the
forward pass. We can retrieve cached data from the context object, and must
compute and return the gradient of the loss with respect to the input to the
forward function.
"""
x, = ctx.saved_tensors
grad_x = grad_output.clone()
grad_x[x < 0] = 0
return grad_x


device = torch.device('cpu')
# device = torch.device('cuda') # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold input and output
x = torch.randn(N, D_in, device=device)
y = torch.randn(N, D_out, device=device)

# Create random Tensors for weights.
w1 = torch.randn(D_in, H, device=device, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, requires_grad=True)

learning_rate = 1e-6
for t in range(500):
# Forward pass: compute predicted y using operations on Tensors; we call our
# custom ReLU implementation using the MyReLU.apply function
y_pred = MyReLU.apply(x.mm(w1)).mm(w2)

# Compute and print loss
loss = (y_pred - y).pow(2).sum()
print(t, loss.item())

# Use autograd to compute the backward pass.
loss.backward()

with torch.no_grad():
# Update weights using gradient descent
w1 -= learning_rate * w1.grad
w2 -= learning_rate * w2.grad

# Manually zero the gradients after running the backward pass
w1.grad.zero_()
w2.grad.zero_()


TensorFlow:静态图

PyTorch autograd看起来很像TensorFlow:在这两个框架中,我们定义了一个计算图,并使用自动微分来计算梯度。两者最大的区别在于,TensorFlow的计算图是静态的,PyTorch使用的是动态计算图。
在TensorFlow中,我们定义了一次计算图,然后一遍又一遍地执行同一个图,可能会给图输入不同的数据。在PyTorch中,每个前向传递都定义了一个新的计算图。
静态图是很好的,因为您可以预先优化图;例如,一个框架可能决定融合一些图形操作以提高效率,或者想出一个策略,将图形分布到多个gpu或多台机器上。如果您反复重用同一个图,那么这种潜在的高成本的预先优化可以随着同一个图的反复运行而分摊。
静态图和动态图不同的一个方面是控制流。对于某些模型,我们可能希望对每个数据点执行不同的计算;例如,对于每个数据点,递归网络可以展开为不同的时间步数;这种展开可以作为一个循环来实现。对于静态图,循环构造需要是图的一部分;因此,TensorFlow提供了诸如tf.扫描将循环嵌入图中。使用动态图,情况更简单:因为我们为每个示例动态地构建图,所以我们可以使用普通的命令流控制来执行针对每个输入的不同的计算。
与上面的Pythorch autograd示例相比,这里我们使用TensorFlow来拟合一个简单的两层网络:


# Code in file autograd/tf_two_layer_net.py
import tensorflow as tf
import numpy as np

# First we set up the computational graph:

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create placeholders for the input and target data; these will be filled
# with real data when we execute the graph.
x = tf.placeholder(tf.float32, shape=(None, D_in))
y = tf.placeholder(tf.float32, shape=(None, D_out))

# Create Variables for the weights and initialize them with random data.
# A TensorFlow Variable persists its value across executions of the graph.
w1 = tf.Variable(tf.random_normal((D_in, H)))
w2 = tf.Variable(tf.random_normal((H, D_out)))

# Forward pass: Compute the predicted y using operations on TensorFlow Tensors.
# Note that this code does not actually perform any numeric operations; it
# merely sets up the computational graph that we will later execute.
h = tf.matmul(x, w1)
h_relu = tf.maximum(h, tf.zeros(1))
y_pred = tf.matmul(h_relu, w2)

# Compute loss using operations on TensorFlow Tensors
loss = tf.reduce_sum((y - y_pred) ** 2.0)

# Compute gradient of the loss with respect to w1 and w2.
grad_w1, grad_w2 = tf.gradients(loss, [w1, w2])

# Update the weights using gradient descent. To actually update the weights
# we need to evaluate new_w1 and new_w2 when executing the graph. Note that
# in TensorFlow the the act of updating the value of the weights is part of
# the computational graph; in PyTorch this happens outside the computational
# graph.
learning_rate = 1e-6
new_w1 = w1.assign(w1 - learning_rate * grad_w1)
new_w2 = w2.assign(w2 - learning_rate * grad_w2)

# Now we have built our computational graph, so we enter a TensorFlow session to
# actually execute the graph.
with tf.Session() as sess:
# Run the graph once to initialize the Variables w1 and w2.
sess.run(tf.global_variables_initializer())

# Create numpy arrays holding the actual data for the inputs x and targets y
x_value = np.random.randn(N, D_in)
y_value = np.random.randn(N, D_out)
for _ in range(500):
# Execute the graph many times. Each time it executes we want to bind
# x_value to x and y_value to y, specified with the feed_dict argument.
# Each time we execute the graph we want to compute the values for loss,
# new_w1, and new_w2; the values of these Tensors are returned as numpy
# arrays.
loss_value, _, _ = sess.run([loss, new_w1, new_w2],
feed_dict={x: x_value, y: y_value})
print(loss_value)


Pythorch:nn

计算图和autograd是定义复杂算子和自动获取导数的一个非常强大的范例;但是对于大型神经网络来说,raw autograd可能有点太低了。
在构建神经网络时,我们经常考虑将计算分成若干层,其中一些具有可学习的参数,这些参数将在学习过程中得到优化。
在TensorFlow中,像Keras、TensorFlow Slim和TFLearn这样的包在原始计算图上提供了更高层次的抽象,这些抽象对于构建神经网络非常有用。
在PyTorch中,nn包也有同样的用途。nn包定义了一组模块,这些模块大致相当于神经网络层。模块接收输入张量并计算输出张量,但也可以保持内部状态,例如包含可学习参数的张量。神经网络包还定义了一组有用的损失函数,这些函数通常在训练神经网络时使用。
在本例中,我们使用nn包来实现我们的两层网络:

# Code in file nn/two_layer_net_nn.py
import torch

device = torch.device('cpu')
# device = torch.device('cuda') # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold inputs and outputs
x = torch.randn(N, D_in, device=device)
y = torch.randn(N, D_out, device=device)

# Use the nn package to define our model as a sequence of layers. nn.Sequential
# is a Module which contains other Modules, and applies them in sequence to
# produce its output. Each Linear Module computes output from input using a
# linear function, and holds internal Tensors for its weight and bias.
# After constructing the model we use the .to() method to move it to the
# desired device.
model = torch.nn.Sequential(
torch.nn.Linear(D_in, H),
torch.nn.ReLU(),
torch.nn.Linear(H, D_out),
).to(device)

# The nn package also contains definitions of popular loss functions; in this
# case we will use Mean Squared Error (MSE) as our loss function. Setting
# reduction='sum' means that we are computing the *sum* of squared errors rather
# than the mean; this is for consistency with the examples above where we
# manually compute the loss, but in practice it is more common to use mean
# squared error as a loss by setting reduction='elementwise_mean'.
loss_fn = torch.nn.MSELoss(reduction='sum')

learning_rate = 1e-4
for t in range(500):
# Forward pass: compute predicted y by passing x to the model. Module objects
# override the __call__ operator so you can call them like functions. When
# doing so you pass a Tensor of input data to the Module and it produces
# a Tensor of output data.
y_pred = model(x)

# Compute and print loss. We pass Tensors containing the predicted and true
# values of y, and the loss function returns a Tensor containing the loss.
loss = loss_fn(y_pred, y)
print(t, loss.item())

# Zero the gradients before running the backward pass.
model.zero_grad()

# Backward pass: compute gradient of the loss with respect to all the learnable
# parameters of the model. Internally, the parameters of each Module are stored
# in Tensors with requires_grad=True, so this call will compute gradients for
# all learnable parameters in the model.
loss.backward()

# Update the weights using gradient descent. Each parameter is a Tensor, so
# we can access its data and gradients like we did before.
with torch.no_grad():
for param in model.parameters():
param.data -= learning_rate * param.grad


PyTorch:优化

到目前为止,我们已经通过手动改变持有可学习参数的张量来更新模型的权重。对于简单的优化算法(如随机梯度下降)来说,这不是一个巨大的负担,但在实践中,我们经常使用更复杂的优化器(如AdaGrad、RMSProp、Adam等)来训练神经网络。
PyTorch中的optim包抽象了优化算法的思想,并提供了常用优化算法的实现。
在本例中,我们将像前面一样使用nn包来定义我们的模型,但是我们将使用optim包提供的Adam算法来优化模型:

# Code in file nn/two_layer_net_optim.py
import torch

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold inputs and outputs.
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)

# Use the nn package to define our model and loss function.
model = torch.nn.Sequential(
torch.nn.Linear(D_in, H),
torch.nn.ReLU(),
torch.nn.Linear(H, D_out),
)
loss_fn = torch.nn.MSELoss(reduction='sum')

# Use the optim package to define an Optimizer that will update the weights of
# the model for us. Here we will use Adam; the optim package contains many other
# optimization algorithms. The first argument to the Adam constructor tells the
# optimizer which Tensors it should update.
learning_rate = 1e-4
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
for t in range(500):
# Forward pass: compute predicted y by passing x to the model.
y_pred = model(x)

# Compute and print loss.
loss = loss_fn(y_pred, y)
print(t, loss.item())

# Before the backward pass, use the optimizer object to zero all of the
# gradients for the Tensors it will update (which are the learnable weights
# of the model)
optimizer.zero_grad()

# Backward pass: compute gradient of the loss with respect to model parameters
loss.backward()

# Calling the step function on an Optimizer makes an update to its parameters
optimizer.step()


Pythorch:自定义nn模块

有时,您需要指定比一系列现有模块更复杂的模型;对于这些情况,您可以通过子类化来定义自己的模块nn.模块以及定义一个forward,该forward接收输入张量并使用其他模块或张量上的其他自动加载操作生成输出张量。
在本例中,我们将两层网络实现为自定义模块子类:

# Code in file nn/two_layer_net_module.py
import torch

class TwoLayerNet(torch.nn.Module):
def __init__(self, D_in, H, D_out):
"""
In the constructor we instantiate two nn.Linear modules and assign them as
member variables.
"""
super(TwoLayerNet, self).__init__()
self.linear1 = torch.nn.Linear(D_in, H)
self.linear2 = torch.nn.Linear(H, D_out)

def forward(self, x):
"""
In the forward function we accept a Tensor of input data and we must return
a Tensor of output data. We can use Modules defined in the constructor as
well as arbitrary (differentiable) operations on Tensors.
"""
h_relu = self.linear1(x).clamp(min=0)
y_pred = self.linear2(h_relu)
return y_pred

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold inputs and outputs
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)

# Construct our model by instantiating the class defined above.
model = TwoLayerNet(D_in, H, D_out)

# Construct our loss function and an Optimizer. The call to model.parameters()
# in the SGD constructor will contain the learnable parameters of the two
# nn.Linear modules which are members of the model.
loss_fn = torch.nn.MSELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4)
for t in range(500):
# Forward pass: Compute predicted y by passing x to the model
y_pred = model(x)

# Compute and print loss
loss = loss_fn(y_pred, y)
print(t, loss.item())

# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
loss.backward()
optimizer.step()


Pythorch:控制流量+重量分担

作为动态图和权重共享的一个例子,我们实现了一个非常奇怪的模型:一个完全连接的ReLU网络,在每一个前向过程中选择一个介于1和4之间的随机数,并使用多个隐藏层,重复使用相同的权重来计算最里面的隐藏层。
因为该模型可以使用普通的Python流控制来实现循环,并且在定义前向传递时只需多次重用同一个模块,就可以实现最内层之间的权重共享。
我们可以很容易地将此模型实现为模块子类:

# Code in file nn/dynamic_net.py
import random
import torch

class DynamicNet(torch.nn.Module):
def __init__(self, D_in, H, D_out):
"""
In the constructor we construct three nn.Linear instances that we will use
in the forward pass.
"""
super(DynamicNet, self).__init__()
self.input_linear = torch.nn.Linear(D_in, H)
self.middle_linear = torch.nn.Linear(H, H)
self.output_linear = torch.nn.Linear(H, D_out)

def forward(self, x):
"""
For the forward pass of the model, we randomly choose either 0, 1, 2, or 3
and reuse the middle_linear Module that many times to compute hidden layer
representations.

Since each forward pass builds a dynamic computation graph, we can use normal
Python control-flow operators like loops or conditional statements when
defining the forward pass of the model.

Here we also see that it is perfectly safe to reuse the same Module many
times when defining a computational graph. This is a big improvement from Lua
Torch, where each Module could be used only once.
"""
h_relu = self.input_linear(x).clamp(min=0)
for _ in range(random.randint(0, 3)):
h_relu = self.middle_linear(h_relu).clamp(min=0)
y_pred = self.output_linear(h_relu)
return y_pred


# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold inputs and outputs.
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)

# Construct our model by instantiating the class defined above
model = DynamicNet(D_in, H, D_out)

# Construct our loss function and an Optimizer. Training this strange model with
# vanilla stochastic gradient descent is tough, so we use momentum
criterion = torch.nn.MSELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)
for t in range(500):
# Forward pass: Compute predicted y by passing x to the model
y_pred = model(x)

# Compute and print loss
loss = criterion(y_pred, y)
print(t, loss.item())

# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
loss.backward()
optimizer.step()










阅读次数: 108

下一篇: 用PyTorch从零搭建图像分类模型
上一篇: anaconda3 目录赋予权限

尚无评论!

返回上一页面