别有心理压力,只要跟着我一步一步来,你会发现其实并没有想像中的那么困难。当然,如果你看完了这篇教程之后,发现自己明白了很多,却又几乎什么都记不得,那也是很正常的——我认为,这里只是让你明白基本的原理和怎么应用,以后你还需要多练习,多使用,才能熟练掌握pytorch的使用。
最重要的是——请给我30分钟,不会让你失望的
当前深度学习的框架有很多,其中较为出名的是Google的TensorFlow、Facebook的PyTorch,还有就是百度的paddlepaddle,TensorFlow的地位在目前看来确实是举足轻重的,社区的庞大也是它非常火的原因,paddlepaddle作为国产的框架之光,我个人的期待还是很高的,有兴趣的可以看一下paddlepaddle的介绍——【PaddlePaddle】百度深度学习平台PaddlePaddle:等待你我一起划桨的AI大船。
因为TensorFlow和paddlepaddle比较相似,所以很多人都会拿Facebook的PyTorch和Google的Tensorflow进行比较。但是我们今天的主角是PyTorch,说到PyTorch其实应该先说Torch。
A Tensor library like Numpy, unlike Numpy it has strong GPU support. Lua is a wrapper for Torch (Yes! you need to have a good understanding of Lua), and for that you will need LuaRocks package manager.
Torch是一个与Numpy类似的张量(Tensor)操作库,与Numpy不同的是Torch对GPU支持的很好,Lua是Torch的上层包装。
Torch is not going anywhere. PyTorch and Torch use the same C libraries that contain all the performance: TH, THC, THNN, THCUNN and they will continue to be shared.
We still and will have continued engineering on Torch itself, and we have no immediate plan to remove that.
PyTorch和Torch使用包含所有相同性能的C库:TH, THC, THNN, THCUNN,并且它们将继续共享这些库。
这样的回答就很明确了,其实PyTorch和Torch都使用的是相同的底层,只是使用了不同的上层包装语言。
PyTorch is an open source machine learning library for Python, based on Torch, used for applications such as natural language processing. It is primarily developed by Facebook’s artificial-intelligence research group, and Uber’s “Pyro” software for probabilistic programming is built on it.
PyTorch是一个基于Torch的Python开源机器学习库,用于自然语言处理等应用程序。 它主要由Facebook的人工智能研究小组开发。Uber的"Pyro"也是使用的这个库。
PyTorch is a Python package that provides two high-level features:
Tensor computation (like NumPy) with strong GPU acceleration
Deep neural networks built on a tape-based autograd system
You can reuse your favorite Python packages such as NumPy, SciPy and Cython to extend PyTorch when needed.
PyTorch是一个Python包,提供两个高级功能:
没有好的框架,只有合适的框架。
一般开源项目的热度指标从 GitHub 可以略见一斑,PyTorch 的数据是这样的(截止19.8.23):
star(可以认为是技术人的点赞)有31004人;
forks(想要自己基于此做修改)有7586人;
commits(代码总共的更新次数)有20095次;
contributors(代码贡献者)有1139个。
star(可以认为是技术人的点赞)有132923人;
forks(想要自己基于此做修改)有76885人;
commits(代码总共的更新次数)有63438次;
contributors(代码贡献者)有2153个。
结果似乎不言而喻了,TensorFlow的热度确实要远远高与PyTorch,这篇知乎文章有个简单的对比。
但是技术是发展的,知乎上的对比是发布于 2017-08-20的,比如Tensorflow在1.5版的时候就引入了Eager Execution机制实现了动态图,PyTorch的可视化,windows支持,沿维翻转张量等问题都已经不是问题了。
PyTorch的安装十分简单,根据PyTorch官网,对系统选择和安装方式等灵活选择即可。这里以anaconda为例,Windows10 下 Anaconda和 PyCharm 的详细的安装教程(图文并茂),简单的说一下步骤和要点。
国内安装anaconda建议使用中科大镜像,快的不是一点半点。目前中科大、清华镜像都已经关闭。直接通过prompt下载很慢,并且经常会出现HTTPERROR导致下载失败。所以可以提前下载好压缩包,然后离线下载,这样就不容易出现下载到一半下载失败令人苦恼的情况。
Anaconda安装完成后,开始创建环境,这里以win10 系统为例。打开Anaconda Prompt:
# pytorch为环境名,这里创建python3.6版。
conda create - n pytorch python = 3.6
# 切换到pytorch环境
activate pytorch
# ***以下为1.0版本安装***
# 安装GPU版本,根据cuda版本选择cuda80,cuda92,如果cuda是9.0版,则不需要
# 直接conda install pytorch -c pytorch即可
# win下查看cuda版本命令nvcc -V
conda install pytorch cuda92 - c pytorch
# cpu版本使用
conda install pytorch-cpu -c pytorch
# torchvision 是torch提供的计算机视觉工具包,后面介绍
pip install torchvision
# *** 官方更新了1.01 所以安装方式也有小的变更
# torchversion提供了conda的安装包,可以用conda直接安装了
# cuda支持也升级到了10.0
# 安装方式如下:
# cpu版本
conda install pytorch - cpu torchvision - cpu - c pytorch
# GPU版
conda install pytorch torchvision cudatoolkit = 10.0 - c pytorch
# cudatoolkit后跟着相应的cuda版本
# 目前测试 8.0、9.0、9.1、9.2、10.0都可安装成功
验证输入python 进入
import torch
torch.__version__
# 得到结果'1.1.0'
新建的环境是没有安装ipykernel的,所以无法注册到Jupyter Notebook中,所以先要准备下环境:
# 安装ipykernel
conda install ipykernel
# 写入环境
python -m ipykernel install --name pytorch --display-name "Pytorch for Deeplearning"
下一步就是定制 Jupyter Notebook:
# 切换回基础环境
activate base
# 创建jupyter notebook配置文件
jupyter notebook --generate-config
## 这里会显示创建jupyter_notebook_config.py的具体位置
打开文件,修改
c.NotebookApp.notebook_dir = '' 默认目录位置
c.NotebookApp.iopub_data_rate_limit = 100000000 这个改大一些否则有可能报错
至此 Pytorch 的开发环境安装完成,可以在开始菜单中打开Jupyter Notebook,在New 菜单中创建文件时选择Pytorch for Deeplearning
,创建PyTorch的相关开发环境了
3-7章为官方网站的 Deep Learning with PyTorch: A 60 Minute Blitz 的中文翻译,目前在网上看到所有中文翻译版本都已经过时了,所以才又从新翻译了一遍,确保与官方同步。
此外,3-7章所有图片均来自于PyTorch官网,版权归PyTorch所有。
%matplotlib inline
PyTorch是什么?
基于Python的科学计算包,服务于以下两种场景:
开始
Tensors(张量)
^^^^^^^
from __future__ import print_function
import torch
创建一个 5x3 矩阵, 但是未初始化:
x = torch.empty(5, 3)
print(x)
创建一个随机初始化的矩阵:
x = torch.rand(5, 3)
print(x)
创建一个0填充的矩阵:
x = torch.zeros(5, 3)
print(x)
创建一个0填充的矩阵,数据类型为long
:
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
创建tensor并使用现有数据初始化:
x = torch.tensor([5.5, 3])
print(x)
根据现有的张量创建张量。 这些方法将重用输入张量的属性,例如, dtype,除非设置新的值进行覆盖
x = x.new_ones(5, 3, dtype=torch.double) # new_* 方法来创建对象
print(x)
x = torch.randn_like(x, dtype=torch.float) # 覆盖 dtype!
print(x) # 对象的size 是相同的,只是值和类型发生了变化
获取 size
译者注:使用size方法与Numpy的shape属性返回的相同,张量也支持shape属性,后面会详细介绍
print(x.size())
torch.Size([5, 3])
^^^^^^^^^^
操作有多种语法。
我们将看一下加法运算。
加法1:
y = torch.rand(5, 3)
print(x + y)
加法2:
print(torch.add(x, y))
提供输出tensor作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
替换
# adds x to y
y.add_(x)
print(y)
print(x[:, 1])
torch.view
: 可以改变张量的维度和大小
译者注:torch.view与Numpy的reshape类似
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # size -1 从其他维度推断
print(x.size(), y.size(), z.size())
如果你有只有一个元素的张量,使用.item()
来得到Python数据类型的数值
x = torch.randn(1)
print(x)
print(x.item())
Read later:
100+ Tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random numbers, etc., are described here
_.
100+张量操作,包括转置、索引、切片,数学运算、线性代数、随机数等,描述为“here https://pytorch.org/docs/torch”。
NumPy 转换
Converting a Torch Tensor to a NumPy array and vice versa is a breeze.
The Torch Tensor and NumPy array will share their underlying memory locations, and changing one will change the other.
Converting a Torch Tensor to a NumPy Array ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
将Torch Tensor转换成NumPy array,反之亦然,这是轻而易举的。
Torch Tensor和NumPy array将共享它们的底层内存位置,更改其中一个将更改另一个。
将Torch Tensor转换为NumPy array。
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)
See how the numpy array changed in value.
a.add_(1)
print(a)
print(b)
NumPy Array 转化成 Torch Tensor
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
使用from_numpy自动转化
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
所有的 Tensor 类型默认都是基于CPU, CharTensor 类型不支持到 NumPy 的转换.
CUDA 张量
使用.to
方法 可以将Tensor移动到任何设备中
# is_available 函数判断是否有cuda可以使用
# ``torch.device``将张量移动到指定的设备中
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA 设备对象
y = torch.ones_like(x, device=device) # 直接从GPU创建张量
x = x.to(device) # 或者直接使用``.to("cuda")``将张量移动到cuda中
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` 也会对变量的类型做更改
tensor([0.7632], device='cuda:0')
tensor([0.7632], dtype=torch.float64)
%matplotlib inline
Autograd: 自动求导机制
PyTorch 中所有神经网络的核心是 autograd
包。我们先简单介绍一下这个包,然后训练第一个简单的神经网络。
autograd
包为张量上的所有操作提供了自动求导。它是一个在运行时定义的框架,这意味着反向传播是根据你的代码来确定如何运行,并且每次迭代可以是不同的。
示例
张量(Tensor)
torch.Tensor
是这个包的核心类。如果设置 .requires_grad
为 True
,那么将会追踪所有对于该张量的操作。 当完成计算后通过调用 .backward()
,自动计算所有的梯度,这个张量的所有梯度将会自动积累到 .grad
属性。
要阻止张量跟踪历史记录,可以调用.detach()
方法将其与计算历史记录分离,并禁止跟踪它将来的计算记录。
为了防止跟踪历史记录(和使用内存),可以将代码块包装在with torch.no_grad():
中。在评估模型时特别有用,因为模型可能具有requires_grad = True
的可训练参数,但是我们不需要梯度计算。
在自动梯度计算中还有另外一个重要的类Function
.
Tensor
and Function
are interconnected and build up an acyclic graph, that encodes a complete history of computation. Each tensor has a .grad_fn
attribute that references a Function
that has created the Tensor
(except for Tensors created by the user - their grad_fn is None
).
Tensor
和Function
互相连接并生成一个非循环图,它表示和存储了完整的计算历史。每个张量都有一个.grad_fn
属性,这个属性引用了一个创建了Tensor
的Function
(除非这个张量是用户手动创建的,即,这个张量的grad_fn
是None
)。
如果需要计算导数,你可以在Tensor
上调用.backward()
。 如果Tensor
是一个标量(即它包含一个元素数据)则不需要为backward()
指定任何参数,但是如果它有更多的元素,你需要指定一个gradient
参数来匹配张量的形状。
译者注:在其他的文章中你可能会看到说将Tensor包裹到Variable中提供自动梯度计算,Variable 这个在0.4.1版中已经被标注为过期了,现在可以直接使用Tensor,官方文档在这里:
https://pytorch.org/docs/stable/autograd.html#variable-deprecated
具体的后面会有详细说明
import torch
创建一个张量并设置 requires_grad=True 用来追踪他的计算历史
x = torch.ones(2, 2, requires_grad=True)
print(x)
对张量进行操作:
y = x + 2
print(y)
结果y已经被计算出来了,所以,grad_fn已经被自动生成了。
print(y.grad_fn)
对y进行一个操作
z = y * y * 3
out = z.mean()
print(z, out)
.requires_grad_( ... )
可以改变现有张量的 requires_grad 属性。 如果没有指定的话,默认输入的 flag 是 False。
a = torch.randn(2, 2)
a = ((a * 3) / (a - 1))
print(a.requires_grad)
print(a.grad_fn)
a.requires_grad_(True)
print(a.requires_grad)
b = (a * a).sum()
print(b.grad_fn)
False
None
True
<SumBackward0 object at 0x000002325360B438>
梯度
反向传播
因为 out
是一个纯量(scalar),out.backward()
等于out.backward(torch.tensor(1))
。
out.backward()
print gradients d ( o u t ) / d x d(out)/dx d(out)/dx
print(x.grad)
得到矩阵 4.5
.调用 out
Tensor “ o o o”.
得到 o = 1 4 ∑ i z i o = \frac{1}{4}\sum_i z_i o=41∑izi, z i = 3 ( x i + 2 ) 2 z_i = 3(x_i+2)^2 zi=3(xi+2)2 and z i ∣ x i = 1 = 27 z_i\bigr\rvert_{x_i=1} = 27 zi∣∣xi=1=27.
因此, ∂ o ∂ x i = 3 2 ( x i + 2 ) \frac{\partial o}{\partial x_i} = \frac{3}{2}(x_i+2) ∂xi∂o=23(xi+2), 因此, ∂ o ∂ x i ∣ x i = 1 = 9 2 = 4.5 \frac{\partial o}{\partial x_i}\bigr\rvert_{x_i=1} = \frac{9}{2} = 4.5 ∂xi∂o∣∣xi=1=29=4.5.
可以使用 autograd 做更多的操作
x = torch.randn(3, requires_grad=True)
y = x * 2
while y.data.norm() < 1000:
y = y * 2
print(y)
gradients = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
y.backward(gradients)
print(x.grad)
如果.requires_grad=True
但是你又不希望进行autograd的计算,那么可以将变量包裹在 with torch.no_grad()
中:
print(x.requires_grad)
print((x ** 2).requires_grad)
with torch.no_grad():
print((x ** 2).requires_grad)
稍后阅读:
autograd
和 Function
的官方文档 https://pytorch.org/docs/autograd
%matplotlib inline
Neural Networks
使用torch.nn包来构建神经网络。
上一讲已经讲过了autograd
,nn
包依赖autograd
包来定义模型并求导。一个nn.Module
包含各个层和一个forward(input)
方法,该方法返回output
。
它是一个简单的前馈神经网络,它接受一个输入,然后一层接着一层地传递,最后输出计算的结果。
神经网络的典型训练过程如下:
weight = weight - learning_rate * gradient
。定义网络
开始定义一个网络:
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution kernel
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)
Net(
(conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
(conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
(fc1): Linear(in_features=400, out_features=120, bias=True)
(fc2): Linear(in_features=120, out_features=84, bias=True)
(fc3): Linear(in_features=84, out_features=10, bias=True)
)
在模型中必须要定义 forward
函数,backward
函数(用来计算梯度)会被 autograd
自动创建。可以在 forward
函数中使用任何针对 Tensor 的操作。
net.parameters()
返回可被学习的参数(权重)列表和值
params = list(net.parameters())
print(len(params))
print(params[0].size()) # conv1's .weight
测试随机输入32×32。
注:这个网络(LeNet)期望的输入大小是32×32,如果使用MNIST数据集来训练这个网络,请把图片大小重新调整到32×32。
input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)
将所有参数的梯度缓存清零,然后进行随机梯度的的反向传播:
net.zero_grad()
out.backward(torch.randn(1, 10))
回顾:
torch.Tensor
:一个用过自动调用 backward()
实现支持自动梯度计算的 多维数组 ,并且保存关于这个向量的梯度 w.r.t.nn.Module
:神经网络模块。封装参数、移动到GPU上运行、导出、加载等。nn.Parameter
:一种变量,当把它赋值给一个Module
时,被 自动 地注册为一个参数。autograd.Function
:实现一个自动求导操作的前向和反向定义,每个变量操作至少创建一个函数节点,每一个Tensor
的操作都回创建一个接到创建Tensor
和 编码其历史 的函数的Function
节点。重点如下:
还剩:
损失函数
一个损失函数接受一对 (output, target) 作为输入,计算一个值来估计网络的输出和目标值相差多少。
译者注:output为网络的输出,target为实际值
nn包中有很多不同的损失函数。
nn.MSELoss
是一个比较简单的损失函数,它计算输出和目标间的均方误差,例如:
output = net(input)
target = torch.randn(10) # 随机值作为样例
target = target.view(1, -1) # 使target和output的shape相同
criterion = nn.MSELoss()
loss = criterion(output, target)
print(loss)
Now, if you follow loss
in the backward direction, using its .grad_fn
attribute, you will see a graph of computations that looks like this:
现在,如果使用
.grad_fn
属性沿loss
向后移动,您将看到一个如下所示的计算图表:
::
input -> conv2d -> relu -> maxpool2d -> conv2d -> relu -> maxpool2d
-> view -> linear -> relu -> linear -> relu -> linear
-> MSELoss
-> loss
So, when we call loss.backward()
, the whole graph is differentiated w.r.t. the loss, and all Tensors in the graph that has requires_grad=True
will have their .grad
Tensor accumulated with the gradient.
因此,当我们称之为
loss.backward()
时,整个图是有区别的,损失和图中所有requires_grad=True
的张量都会随着梯度累积起来。
For illustration, let us follow a few steps backward:
为了举例说明,让我们
backward
几步:
print(loss.grad_fn) # MSELoss
print(loss.grad_fn.next_functions[0][0]) # Linear
print(loss.grad_fn.next_functions[0][0].next_functions[0][0]) # ReLU
<MseLossBackward object at 0x0000027CAC714550>
<AddmmBackward object at 0x0000027CAC7147F0>
<AccumulateGrad object at 0x0000027CAC714550>
反向传播
调用loss.backward()获得反向传播的误差。
但是在调用前需要清除已存在的梯度,否则梯度将被累加到已存在的梯度。
现在,我们将调用loss.backward(),并查看conv1层的偏差(bias)项在反向传播前后的梯度。
net.zero_grad() # 清除梯度
print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)
loss.backward()
print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)
conv1.bias.grad before backward
tensor([0., 0., 0., 0., 0., 0.])
conv1.bias.grad after backward
tensor([ 0.0074, -0.0249, -0.0107, 0.0326, -0.0017, -0.0059])
如何使用损失函数
稍后阅读:
nn
包,包含了各种用来构成深度神经网络构建块的模块和损失函数,完整的文档请查看here。
剩下的最后一件事:
更新权重
在实践中最简单的权重更新规则是随机梯度下降(SGD):
weight = weight - learning_rate * gradient
我们可以使用简单的Python代码实现这个规则:
learning_rate = 0.01
for f in net.parameters():
f.data.sub_(f.grad.data * learning_rate)
但是当使用神经网络是想要使用各种不同的更新规则时,比如SGD、Nesterov-SGD、Adam、RMSPROP等,PyTorch中构建了一个包torch.optim
实现了所有的这些规则。使用它们非常简单:
import torch.optim as optim
# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)
# in your training loop:
optimizer.zero_grad() # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step() # Does the update
… Note::
Observe how gradient buffers had to be manually set to zero using optimizer.zero_grad()
. This is because gradients are accumulated as explained in Backprop
_ section.
观察使用
optimizer.zero_grad()
手动将渐变缓冲区设置为零的方式。这是因为梯度是按照Backprop
_ section 的说明累积的。
%matplotlib inline
训练一个分类器
上一讲中已经看到如何去定义一个神经网络,计算损失值和更新网络的权重。
你现在可能在想下一步。
关于数据?
一般情况下处理图像、文本、音频和视频数据时,可以使用标准的Python包来加载数据到一个numpy数组中。然后把这个数组转换成 torch.*Tensor
。
特别的,对于图像任务,我们创建了一个包 torchvision
,它包含了处理一些基本图像数据集的方法。这些数据集包括 Imagenet, CIFAR10, MNIST 等。除了数据加载以外,torchvision
还包含了图像转换器, torchvision.datasets
和 torch.utils.data.DataLoader
。
torchvision
包不仅提供了巨大的便利,也避免了代码的重复。
在这个教程中,我们使用CIFAR10数据集,它有如下10个类别:‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’。CIFAR-10的图像都是 3x32x32 大小的,即,3颜色通道,32x32像素。
训练一个图像分类器
依次按照下列顺序进行:
torchvision
加载和归一化CIFAR10训练集和测试集读取和归一化 CIFAR10
使用torchvision
可以非常容易地加载CIFAR10。
import torch
import torchvision
import torchvision.transforms as transforms
torchvision的输出是[0,1]的PILImage图像,我们把它转换为归一化范围为[-1, 1]的张量。
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./data\cifar-10-python.tar.gz
Files already downloaded and verified
我们展示一些训练图像。
import matplotlib.pyplot as plt
import numpy as np
# 展示图像的函数
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
# 获取随机数据
dataiter = iter(trainloader)
images, labels = dataiter.next()
# 展示图像
imshow(torchvision.utils.make_grid(images))
# 显示图像标签
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
定义一个卷积神经网络
从之前的神经网络一节复制神经网络代码,并修改为输入3通道图像。
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
定义损失函数和优化器
我们使用交叉熵作为损失函数,使用带动量的随机梯度下降。
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
训练网路
有趣的时刻开始了。
我们只需在数据迭代器上循环,将数据输入给网络,并优化。
for epoch in range(2): # 多批次循环
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# 获取输入
inputs, labels = data
# 梯度置0
optimizer.zero_grad()
# 正向传播,反向传播,优化
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# 打印状态信息
running_loss += loss.item()
if i % 2000 == 1999: # 每2000批次打印一次
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
在测试集上测试网络
我们在整个训练集上进行了2次训练,但是我们需要检查网络是否从数据集中学习到有用的东西。通过预测神经网络输出的类别标签与实际情况标签进行对比来进行检测。如果预测正确,我们把该样本添加到正确预测列表。
第一步,显示测试集中的图片并熟悉图片内容。
dataiter = iter(testloader)
images, labels = dataiter.next()
# 显示图片
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
outputs = net(images)
输出是10个标签的能量。
一个类别的能量越大,神经网络越认为它是这个类别。所以让我们得到最高能量的标签。
_, predicted = torch.max(outputs, 1)
print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))
Predicted: plane plane plane plane
结果看来不错。
接下来让看看网络在整个测试集上的结果如何。
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
Accuracy of the network on the 10000 test images: 9 %
结果看起来不错,至少比随机选择要好,随机选择的正确率为10%。似乎网络学习到了一些东西。
在识别哪一个类的时候好,哪一个不好呢?
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels).squeeze()
for i in range(4):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
for i in range(10):
print('Accuracy of %5s : %2d %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))
Accuracy of plane : 99 %
Accuracy of car : 0 %
Accuracy of bird : 0 %
Accuracy of cat : 0 %
Accuracy of deer : 0 %
Accuracy of dog : 0 %
Accuracy of frog : 0 %
Accuracy of horse : 0 %
Accuracy of ship : 0 %
Accuracy of truck : 0 %
下一步?
我们如何在GPU上运行神经网络呢?
在GPU上训练
把一个神经网络移动到GPU上训练就像把一个Tensor转换GPU上一样简单。并且这个操作会递归遍历有所模块,并将其参数和缓冲区转换为CUDA张量。
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 确认我们的电脑支持CUDA,然后显示CUDA信息:
print(device)
本节的其余部分假定device
是CUDA设备。
然后这些方法将递归遍历所有模块并将模块的参数和缓冲区转换成CUDA张量:
net.to(device)
记住:inputs 和 targets 也要转换。
inputs, labels = inputs.to(device), labels.to(device)
为什么我们没注意到GPU的速度提升很多?那是因为网络非常的小。
实践:
尝试增加你的网络的宽度(第一个nn.Conv2d
的第2个参数,第二个nn.Conv2d
的第一个参数,它们需要是相同的数字),看看你得到了什么样的加速。
实现的目标:
译者注:后面我们教程会训练一个真正的网络,使识别率达到90%以上。
多GPU训练
如果你想使用所有的GPU得到更大的加速,请查看数据并行处理。
数据并行(选读)
Authors: Sung Kim and Jenny Kang
在这个教程里,我们将学习如何使用 DataParallel
来使用多GPU。
PyTorch非常容易就可以使用多GPU,用如下方式把一个模型放到GPU上:
device = torch.device("cuda:0")
model.to(device)
GPU:
然后复制所有的张量到GPU上:
mytensor = my_tensor.to(device)
请注意,只调用my_tensor.to(device)
并没有复制张量到GPU上,而是返回了一个copy。所以你需要把它赋值给一个新的张量并在GPU上使用这个张量。
在多GPU上执行前向和反向传播是自然而然的事。但是PyTorch默认将只使用一个GPU。
使用DataParallel
可以轻易的让模型并行运行在多个GPU上。
model = nn.DataParallel(model)
这才是这篇教程的核心,接下来我们将更详细的介绍它。
导入和参数
导入PyTorch模块和定义参数。
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
# Parameters and DataLoaders
input_size = 5
output_size = 2
batch_size = 30
data_size = 100
Device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
虚拟数据集
制作一个虚拟(随机)数据集,你只需实现 __getitem__
class RandomDataset(Dataset):
def __init__(self, size, length):
self.len = length
self.data = torch.randn(length, size)
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return self.len
rand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),
batch_size=batch_size, shuffle=True)
简单模型
作为演示,我们的模型只接受一个输入,执行一个线性操作,然后得到结果。
说明:DataParallel
能在任何模型(CNN,RNN,Capsule Net等)上使用。
我们在模型内部放置了一条打印语句来打印输入和输出向量的大小。
请注意批次的秩为0时打印的内容。
class Model(nn.Module):
# Our model
def __init__(self, input_size, output_size):
super(Model, self).__init__()
self.fc = nn.Linear(input_size, output_size)
def forward(self, input):
output = self.fc(input)
print("\tIn Model: input size", input.size(),
"output size", output.size())
return output
创建一个模型和数据并行
这是本教程的核心部分。
首先,我们需要创建一个模型实例和检测我们是否有多个GPU。如果有多个GPU,使用nn.DataParallel
来包装我们的模型。然后通过mmodel.to(device)
把模型放到GPU上。
model = Model(input_size, output_size)
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.to(device)
Model(
(fc): Linear(in_features=5, out_features=2, bias=True)
)
运行模型
现在可以看到输入和输出张量的大小。
for data in rand_loader:
input = data.to(device)
output = model(input)
print("Outside: input size", input.size(),
"output_size", output.size())
In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
结果
当没有或者只有一个GPU时,对30个输入和输出进行批处理,得到了期望的一样得到30个输入和输出,但是如果你有多个GPU,你得到如下的结果。
2 GPUs
~
If you have 2, you will see:
… code:: bash
# on 2 GPUs
Let's use 2 GPUs!
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
3 GPUs
~
If you have 3 GPUs, you will see:
… code:: bash
Let's use 3 GPUs!
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
8 GPUs
~~
If you have 8, you will see:
… code:: bash
Let's use 8 GPUs!
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])
总结
DataParallel会自动的划分数据,并将作业发送到多个GPU上的多个模型。并在每个模型完成作业后,收集合并结果并返回。
相较于目前Tensorflow类型的书籍已经烂大街的状况,PyTorch类的书籍目前已出版的并没有那么多,笔者给大家推荐我认为还不错的四本PyTorch书籍。