1、Pytorch简介
1.0 如何使用本教程
别有心理压力,只要跟着我一步一步来,你会发现其实并没有想像中的那么困难。当然,如果你看完了这篇教程之后,发现自己明白了很多,却又几乎什么都记不得,那也是很正常的——我认为,这里只是让你明白基本的原理和怎么应用,以后你还需要多练习,多使用,才能熟练掌握pytorch的使用。
最重要的是——请给我30分钟,不会让你失望的 😃
1.1 PyTorch由来
当前深度学习的框架有很多,其中较为出名的是Google的TensorFlow、Facebook的PyTorch,还有就是百度的paddlepaddle,TensorFlow的地位在目前看来确实是举足轻重的,社区的庞大也是它非常火的原因,paddlepaddle作为国产的框架之光,我个人的期待还是很高的,有兴趣的可以看一下paddlepaddle的介绍——【PaddlePaddle】百度深度学习平台PaddlePaddle:等待你我一起划桨的AI大船。
因为TensorFlow和paddlepaddle比较相似,所以很多人都会拿Facebook的PyTorch和Google的Tensorflow进行比较。但是我们今天的主角是PyTorch,说到PyTorch其实应该先说Torch。
1.2 Torch简介
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都使用的是相同的底层,只是使用了不同的上层包装语言。
1.3 重新认识PyTorch
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包,提供两个高级功能:
- 具有强大的GPU加速的张量计算(如NumPy)
- 包含自动求导系统的的深度神经网络
- 您可以重用您最喜欢的Python包,如NumPy、SciPy和Cython,以便在需要时扩展PyTorch。
1.4 PyTorch和Tensorflow的对比
没有好的框架,只有合适的框架。
一般开源项目的热度指标从 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支持,沿维翻转张量等问题都已经不是问题了。
1.5 总结
- PyTorch算是相当简洁优雅且高效快速的框架
- 设计追求最少的封装,尽量避免重复造轮子
- 算是所有的框架中面向对象设计的最优雅的一个,设计最符合人们的思维,它让用户尽可能地专注于实现自己的想法
- 大佬支持,与Google的Tensorflow类似,Facebook的支持足以确保PyTorch获得持续的开发更新
- 不错的的文档,PyTorch作者亲自维护的论坛供用户交流和求教问题
- 入门简单
2、Pytorch环境搭建
PyTorch的安装十分简单,根据PyTorch官网,对系统选择和安装方式等灵活选择即可。这里以anaconda为例,Windows10 下 Anaconda和 PyCharm 的详细的安装教程(图文并茂),简单的说一下步骤和要点。
国内安装anaconda建议使用中科大镜像,快的不是一点半点。目前中科大、清华镜像都已经关闭。直接通过prompt下载很慢,并且经常会出现HTTPERROR导致下载失败。所以可以提前下载好压缩包,然后离线下载,这样就不容易出现下载到一半下载失败令人苦恼的情况。
2.1 安装Pytorch
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'
2.2 配置 Jupyter Notebook
新建的环境是没有安装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 这个改大一些否则有可能报错
2.3 测试
至此 Pytorch 的开发环境安装完成,可以在开始菜单中打开Jupyter Notebook,在New 菜单中创建文件时选择Pytorch for Deeplearning
,创建PyTorch的相关开发环境了
3-7章为官方网站的 Deep Learning with PyTorch: A 60 Minute Blitz 的中文翻译,目前在网上看到所有中文翻译版本都已经过时了,所以才又从新翻译了一遍,确保与官方同步。
此外,3-7章所有图片均来自于PyTorch官网,版权归PyTorch所有。
3、张量
%matplotlib inline
PyTorch是什么?
基于Python的科学计算包,服务于以下两种场景:
- 作为NumPy的替代品,可以使用GPU的强大计算能力
- 提供最大的灵活性和高速的深度学习研究平台
开始
Tensors(张量)
^^^^^^^
- Tensors与Numpy中的 ndarrays类似,但是在PyTorch中
- Tensors 可以使用GPU进行计算.
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)
你可以使用与NumPy索引方式相同的操作来进行对张量的操作
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 <https://pytorch.org/docs/torch>
_.
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)
4、自动求导
%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
5、神经网络
%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
节点。
重点如下:
- 定义一个网络
- 处理输入,调用backword
还剩:
- 计算损失
- 更新网络权重
损失函数
一个损失函数接受一对 (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 的说明累积的。
6、用cifar10训练一个分类器
%matplotlib inline
训练一个分类器
上一讲中已经看到如何去定义一个神经网络,计算损失值和更新网络的权重。
你现在可能在想下一步。
关于数据?
一般情况下处理图像、文本、音频和视频数据时,可以使用标准的Python包来加载数据到一个numpy数组中。然后把这个数组转换成 torch.*Tensor
。
- 图像可以使用 Pillow, OpenCV
- 音频可以使用 scipy, librosa
- 文本可以使用原始Python和Cython来加载,或者使用 NLTK或 SpaCy 处理
特别的,对于图像任务,我们创建了一个包 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
的第一个参数,它们需要是相同的数字),看看你得到了什么样的加速。
实现的目标:
- 深入了解了PyTorch的张量库和神经网络
- 训练了一个小网络来分类图片
译者注:后面我们教程会训练一个真正的网络,使识别率达到90%以上。
多GPU训练
如果你想使用所有的GPU得到更大的加速,请查看数据并行处理。
7、数据并行处理(多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上的多个模型。并在每个模型完成作业后,收集合并结果并返回。
8、推荐一些PyTorch学习资源
8.1 PyTorch学习教程、手册
- PyTorch英文版官方手册:对于英文比较好的同学,非常推荐该PyTorch官方文档,一步步带你从入门到精通。该文档详细的介绍了从基础知识到如何使用PyTorch构建深层神经网络,以及PyTorch语法和一些高质量的案例。
- PyTorch中文官方文档:阅读上述英文文档比较困难的同学也不要紧,我们为大家准备了比较官方的PyTorch中文文档,文档非常详细的介绍了各个函数,可作为一份PyTorch的速查宝典。
- 比较偏算法实战的PyTorch代码教程:在github上有很高的star。建议大家在阅读本文档之前,先学习上述两个PyTorch基础教程。
- 开源书籍:这是一本开源的书籍,目标是帮助那些希望和使用PyTorch进行深度学习开发和研究的朋友快速入门。但本文档不是内容不是很全,还在持续更新中。
- 简单易上手的PyTorch中文文档:非常适合新手学习。该文档从介绍什么是PyTorch开始,到神经网络、PyTorch的安装,再到图像分类器、数据并行处理,非常详细的介绍了PyTorch的知识体系,适合新手的学习入门。该文档的官网:http://pytorchchina.com 。
8.2 PyTorch视频教程
- B站PyTorch视频教程:首推的是B站中近期点击率非常高的一个PyTorch视频教程,虽然视频内容只有八集,但讲的深入浅出,十分精彩。只是没有中文字幕,小伙伴们是该练习一下英文了…
- 国外视频教程:另外一个国外大佬的视频教程,在YouTube上有很高的点击率,也是纯英文的视频,有没有觉得外国的教学视频不管是多么复杂的问题都能讲的很形象很简单?
- 莫烦:相信莫烦老师大家应该很熟了,他的Python、深度学习的系列视频在B站和YouTube上均有很高的点击率,该PyTorch视频教程也是去年刚出不久,推荐给新手朋友。
- 101学院:人工智能101学院的PyTorch系列视频课程,讲的比较详细、覆盖的知识点也比较广,感兴趣的朋友可以试听一下。
- 七月在线:最后,向大家推荐的是国内领先的人工智能教育平台——七月在线的PyTorch入门与实战系列课。课程虽然是收费课程,但课程包含PyTorch语法、深度学习基础、词向量基础、NLP和CV的项目应用、实战等,理论和实战相结合,确实比其它课程讲的更详细,推荐给大家。
8.3 NLP&PyTorch实战
- Pytorch text:Torchtext是一个非常好用的库,可以帮助我们很好的解决文本的预处理问题。此github存储库包含两部分:
- torchText.data:文本的通用数据加载器、抽象和迭代器(包括词汇和词向量)
- torchText.datasets:通用NLP数据集的预训练加载程序
我们只需要通过pip install torchtext安装好torchtext后,便可以开始体验Torchtext 的种种便捷之处。
- Pytorch-Seq2seq:Seq2seq是一个快速发展的领域,新技术和新框架经常在此发布。这个库是在PyTorch中实现的Seq2seq模型的框架,该框架为Seq2seq模型的训练和预测等都提供了模块化和可扩展的组件,此github项目是一个基础版本,目标是促进这些技术和应用程序的开发。
- BERT NER:BERT是2018年google 提出来的预训练语言模型,自其诞生后打破了一系列的NLP任务,所以其在nlp的领域一直具有很重要的影响力。该github库是BERT的PyTorch版本,内置了很多强大的预训练模型,使用时非常方便、易上手。
- Fairseq:Fairseq是一个序列建模工具包,允许研究人员和开发人员为翻译、总结、语言建模和其他文本生成任务训练自定义模型,它还提供了各种Seq2seq模型的参考实现。该github存储库包含有关入门、训练新模型、使用新模型和任务扩展Fairseq的说明,对该模型感兴趣的小伙伴可以点击上方链接学习。
- Quick-nlp:Quick-nlp是一个深受fast.ai库启发的深入学习Nlp库。它遵循与Fastai相同的API,并对其进行了扩展,允许快速、轻松地运行NLP模型。
- OpenNMT-py:这是OpenNMT的一个PyTorch实现,一个开放源码的神经网络机器翻译系统。它的设计是为了便于研究,尝试新的想法,以及在翻译,总结,图像到文本,形态学等许多领域中尝试新的想法。一些公司已经证明该代码可以用于实际的工业项目中,更多关于这个github的详细信息请参阅以上链接。
8.4 CV&PyTorch实战
- pytorch vision:Torchvision是独立于pytorch的关于图像操作的一些方便工具库。主要包括:vision.datasets 、vision.models、vision.transforms、vision.utils 几个包,安装和使用都非常简单,感兴趣的小伙伴们可以参考以上链接。
- OpenFacePytorch:此github库是OpenFace在Pytorch中的实现,代码要求输入的图像要与原始OpenFace相同的方式对齐和裁剪。
- TorchCV:TorchCV是一个基于PyTorch的计算机视觉深度学习框架,支持大部分视觉任务训练和部署,此github库为大多数基于深度学习的CV问题提供源代码,对CV方向感兴趣的小伙伴还在等什么?
- Pytorch-cnn-finetune:该github库是利用pytorch对预训练卷积神经网络进行微调,支持的架构和模型包括:ResNet 、DenseNet、Inception v3 、VGG、SqueezeNet 、AlexNet 等。
- Pt-styletransfer:这个github项目是Pytorch中的神经风格转换,具体有以下几个需要注意的地方:
- StyleTransferNet作为可由其他脚本导入的类;
- 支持VGG(这是在PyTorch中提供预训练的VGG模型之前)
- 可保存用于显示的中间样式和内容目标的功能
- 可作为图像检查图矩阵的函数
- 自动样式、内容和产品图像保存
- 一段时间内损失的Matplotlib图和超参数记录,以跟踪有利的结果
- Face-alignment:Face-alignment是一个用 pytorch 实现的 2D 和 3D 人脸对齐库,使用世界上最准确的面对齐网络从 Python 检测面部地标,能够在2D和3D坐标中检测点。该github库详细的介绍了使用Face-alignment进行人脸对齐的基本流程,欢迎感兴趣的同学学习。
8.5 PyTorch论文推荐
- Google_evolution:该论文实现了实现了由Esteban Real等人提出的图像分类器大规模演化的结果网络。在实验之前,需要我们安装好PyTorch、 Scikit-learn以及下载好 CIFAR10 dataset数据集。
- PyTorch-value-iteration-networks:该论文基于作者最初的Theano实现和Abhishek Kumar的Tensoflow实现,包含了在PyTorch中实现价值迭代网络(VIN)。Vin在NIPS 2016年获得最佳论文奖。
- Pytorch Highway:Highway Netowrks是允许信息高速无阻碍的通过各层,它是从Long Short Term Memory(LSTM) recurrent networks中的gate机制受到启发,可以让信息无阻碍的通过许多层,达到训练深层神经网络的效果,使深层神经网络不在仅仅具有浅层神经网络的效果。该论文是Highway network基于Pytorch的实现。
- Pyscatwave:Cupy/Pythorn的散射实现。散射网络是一种卷积网络,它的滤波器被预先定义为子波,不需要学习,可以用于图像分类等视觉任务。散射变换可以显著降低输入的空间分辨率(例如224x224->14x14),且双关功率损失明显为负。
- Pytorch_NEG_loss:该论文是Negative Sampling Loss的Pytorch实现。Negative Sampling是一种求解word2vec模型的方法,它摒弃了霍夫曼树,采用了Negative Sampling(负采样)的方法来求解,本论文是对Negative Sampling的loss函数的研究,感兴趣的小伙伴可点击上方论文链接学习。
- Pytorch_TDNN:该论文是对Time Delayed NN的Pytorch实现。论文详细的讲述了TDNN的原理以及实现过程。
8.6 PyTorch书籍推荐
相较于目前Tensorflow类型的书籍已经烂大街的状况,PyTorch类的书籍目前已出版的并没有那么多,笔者给大家推荐我认为还不错的四本PyTorch书籍。
- 《深度学习入门之PyTorch》,电子工业出版社,作者:廖星宇。这本《深度学习入门之PyTorch》是所有PyTorch书籍中出版的相对较早的一本,作者以自己的小白入门深度学习之路,深入浅出的讲解了PyTorch的语法、原理以及实战等内容,适合新手的入门学习。但不足的是,书中有很多不严谨以及生搬硬套的地方,需要读者好好甄别。
推荐指数:★★★ - 《PyTorch深度学习》,人民邮电出版社,作者:王海玲、刘江峰。该书是一本英译书籍,原作者是两位印度的大佬,该书除了PyTorch基本语法、函数外,还涵盖了ResNET、Inception、DenseNet等在内的高级神经网络架构以及它们的应用案例。该书适合数据分析师、数据科学家等相对有一些理论基础和实战经验的读者学习,不太建议作为新手的入门选择。
推荐指数:★★★ - 《深度学习框架PyTorch入门与实践》,电子工业出版社,作者:陈云。这是一本2018年上市的PyTorch书籍,包含理论入门和实战项目两大部分,相较于其它同类型书籍,该书案例非常的翔实,包括:Kaggle竞赛中经典项目、GAN生成动漫头像、AI滤镜、RNN写诗、图像描述任务等。理论+实战的内容设置也更适合深度学习入门者和从业者学习。
推荐指数:★★★★ - 《PyTorch机器学习从入门到实战》,机械工业出版社,作者:校宝在线、孙琳等。该书同样是一本理论结合实战的Pytorch教程,相较于前一本入门+实战教程,本书的特色在于关于深度学习的理论部分讲的非常详细,后边的实战项目更加的综合。总体而言,本书也是一本适合新手学习的不错的PyTorch入门书籍。
推荐指数:★★★
如果想要更多的资源,欢迎关注 @我是管小亮,文字强迫症MAX~
回复【福利】即可获取我为你准备的大礼,包括C++,编程四大件,NLP,深度学习等等的资料。
想看更多文(段)章(子),欢迎关注微信公众号「程序员管小亮」~