Skip to content

Commit

Permalink
Merge pull request #1182 from d2l-ai/master
Browse files Browse the repository at this point in the history
Release 2.0.0-beta1
  • Loading branch information
astonzhang committed Jul 31, 2022
2 parents 450277f + 47f7d28 commit e42ebff
Show file tree
Hide file tree
Showing 38 changed files with 85 additions and 80 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](http://ci.d2l.ai/job/d2l-zh/job/master/badge/icon)](http://ci.d2l.ai/job/d2l-zh/job/master/)

[第一版:zh-v1.D2L.ai](https://zh-v1.d2l.ai/) | [第二版预览版:zh.D2L.ai](https://zh.d2l.ai) | 安装和使用书中源代码:[第一版](https://zh-v1.d2l.ai/chapter_prerequisite/install.html) [第二版](https://zh.d2l.ai/chapter_installation/index.html) | 当前版本: v2.0.0-alpha2
[第一版:zh-v1.D2L.ai](https://zh-v1.d2l.ai/) | [第二版预览版:zh.D2L.ai](https://zh.d2l.ai) | 安装和使用书中源代码:[第一版](https://zh-v1.d2l.ai/chapter_prerequisite/install.html) [第二版](https://zh.d2l.ai/chapter_installation/index.html) | 当前版本: v2.0.0-beta1

<h5 align="center"><i>理解深度学习的最佳方法是学以致用。</i></h5>

Expand Down
2 changes: 1 addition & 1 deletion chapter_appendix-tools-for-deep-learning/aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
1. 安装CUDA(或使用预装CUDA的Amazon机器映像)。
1. 安装深度学习框架和其他库以运行本书的代码。

此过程也适用于其他实例(和其他云),尽管需要一些细微的修改。在继续操作之前,你需要创建一个aws帐户,有关更多详细信息,请参阅 :numref:`sec_sagemaker`
此过程也适用于其他实例(和其他云),尽管需要一些细微的修改。在继续操作之前,你需要创建一个AWS帐户,有关更多详细信息,请参阅 :numref:`sec_sagemaker`

## 创建和运行EC2实例

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:label:`sec_self-attention-and-positional-encoding`

在深度学习中,我们经常使用卷积神经网络(CNN)或循环神经网络(RNN)对序列进行编码。
想象一下,有了注意力机制之后,我们将词元序列输入注意力池化中
想象一下,有了注意力机制之后,我们将词元序列输入注意力汇聚中
以便同一组词元同时充当查询、键和值。
具体来说,每个查询都会关注所有的键-值对并生成一个注意力输出。
由于查询、键和值来自同一组输入,因此被称为
Expand Down Expand Up @@ -43,7 +43,7 @@ $\mathbf{y}_1, \ldots, \mathbf{y}_n$,其中:

$$\mathbf{y}_i = f(\mathbf{x}_i, (\mathbf{x}_1, \mathbf{x}_1), \ldots, (\mathbf{x}_n, \mathbf{x}_n)) \in \mathbb{R}^d$$

根据 :eqref:`eq_attn-pooling`中定义的注意力池化函数$f$。
根据 :eqref:`eq_attn-pooling`中定义的注意力汇聚函数$f$。
下面的代码片段是基于多头注意力对一个张量完成自注意力的计算,
张量的形状为(批量大小,时间步的数目或词元序列的长度,$d$)。
输出与输入的张量形状相同。
Expand Down
14 changes: 7 additions & 7 deletions chapter_computer-vision/anchor.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def multibox_prior(data, sizes, ratios):
ratio_tensor = d2l.tensor(ratios, ctx=device)
# 为了将锚点移动到像素的中心,需要设置偏移量。
# 因为一个像素的的高为1且宽为1,我们选择偏移我们的中心0.5
# 因为一个像素的高为1且宽为1,我们选择偏移我们的中心0.5
offset_h, offset_w = 0.5, 0.5
steps_h = 1.0 / in_height # 在y轴上缩放步长
steps_w = 1.0 / in_width # 在x轴上缩放步长
Expand Down Expand Up @@ -101,15 +101,15 @@ def multibox_prior(data, sizes, ratios):
ratio_tensor = d2l.tensor(ratios, device=device)
# 为了将锚点移动到像素的中心,需要设置偏移量。
# 因为一个像素的的高为1且宽为1,我们选择偏移我们的中心0.5
# 因为一个像素的高为1且宽为1,我们选择偏移我们的中心0.5
offset_h, offset_w = 0.5, 0.5
steps_h = 1.0 / in_height # 在y轴上缩放步长
steps_w = 1.0 / in_width # 在x轴上缩放步长
# 生成锚框的所有中心点
center_h = (torch.arange(in_height, device=device) + offset_h) * steps_h
center_w = (torch.arange(in_width, device=device) + offset_w) * steps_w
shift_y, shift_x = torch.meshgrid(center_h, center_w)
shift_y, shift_x = torch.meshgrid(center_h, center_w, indexing='ij')
shift_y, shift_x = shift_y.reshape(-1), shift_x.reshape(-1)
# 生成“boxes_per_pixel”个高和宽,
Expand Down Expand Up @@ -324,8 +324,8 @@ def assign_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5):
anchors_bbox_map = np.full((num_anchors,), -1, dtype=np.int32, ctx=device)
# 根据阈值,决定是否分配真实边界框
max_ious, indices = np.max(jaccard, axis=1), np.argmax(jaccard, axis=1)
anc_i = np.nonzero(max_ious >= 0.5)[0]
box_j = indices[max_ious >= 0.5]
anc_i = np.nonzero(max_ious >= iou_threshold)[0]
box_j = indices[max_ious >= iou_threshold]
anchors_bbox_map[anc_i] = box_j
col_discard = np.full((num_anchors,), -1)
row_discard = np.full((num_gt_boxes,), -1)
Expand All @@ -352,8 +352,8 @@ def assign_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5):
device=device)
# 根据阈值,决定是否分配真实边界框
max_ious, indices = torch.max(jaccard, dim=1)
anc_i = torch.nonzero(max_ious >= 0.5).reshape(-1)
box_j = indices[max_ious >= 0.5]
anc_i = torch.nonzero(max_ious >= iou_threshold).reshape(-1)
box_j = indices[max_ious >= iou_threshold]
anchors_bbox_map[anc_i] = box_j
col_discard = torch.full((num_anchors,), -1)
row_discard = torch.full((num_gt_boxes,), -1)
Expand Down
4 changes: 2 additions & 2 deletions chapter_computer-vision/image-augmentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ test_augs = torchvision.transforms.Compose([
```

:begin_tab:`mxnet`
接下来,我们定义了一个辅助函数,以便于读取图像和应用图像增广。Gluon数据集提供的`transform_first`函数将图像增广应用于每个训练示例的第一个元素(图像和标签),即图像顶部的元素。有关`DataLoader`的详细介绍,请参阅 :numref:`sec_fashion_mnist`
接下来,我们定义了一个辅助函数,以便于读取图像和应用图像增广。Gluon数据集提供的`transform_first`函数将图像增广应用于每个训练样本的第一个元素(由图像和标签组成),即应用在图像上。有关`DataLoader`的详细介绍,请参阅 :numref:`sec_fashion_mnist`
:end_tab:

:begin_tab:`pytorch`
接下来,我们[**定义一个辅助函数,以便于读取图像和应用图像增广**]。PyTorch数据集提供的`transform`函数应用图像增广来转化图像。有关`DataLoader`的详细介绍,请参阅 :numref:`sec_fashion_mnist`
接下来,我们[**定义一个辅助函数,以便于读取图像和应用图像增广**]。PyTorch数据集提供的`transform`参数应用图像增广来转化图像。有关`DataLoader`的详细介绍,请参阅 :numref:`sec_fashion_mnist`
:end_tab:

```{.python .input}
Expand Down
2 changes: 1 addition & 1 deletion chapter_computer-vision/rcnn.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ rois = torch.Tensor([[0, 0, 0, 20, 20], [0, 0, 10, 30, 30]])
```

由于`X`的高和宽是输入图像高和宽的$1/10$,因此,两个提议区域的坐标先按`spatial_scale`乘以0.1。
然后,在`X`上分别标出这两个兴趣区域`X[:, :, 1:4, 0:4]``X[:, :, 1:4, 0:4]`
然后,在`X`上分别标出这两个兴趣区域`X[:, :, 0:3, 0:3]``X[:, :, 1:4, 0:4]`
最后,在$2\times 2$的兴趣区域汇聚层中,每个兴趣区域被划分为子窗口网格,并进一步抽取相同形状$2\times 2$的特征。

```{.python .input}
Expand Down
2 changes: 1 addition & 1 deletion chapter_convolutional-modern/batch-norm.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ $$\begin{aligned} \hat{\boldsymbol{\mu}}_\mathcal{B} &= \frac{1}{|\mathcal{B}|}
### 全连接层

通常,我们将批量规范化层置于全连接层中的仿射变换和激活函数之间。
设全连接层的输入为u,权重参数和偏置参数分别为$\mathbf{W}$和$\mathbf{b}$,激活函数为$\phi$,批量规范化的运算符为$\mathrm{BN}$。
设全连接层的输入为x,权重参数和偏置参数分别为$\mathbf{W}$和$\mathbf{b}$,激活函数为$\phi$,批量规范化的运算符为$\mathrm{BN}$。
那么,使用批量规范化的全连接层的输出的计算详情如下:

$$\mathbf{h} = \phi(\mathrm{BN}(\mathbf{W}\mathbf{x} + \mathbf{b}) ).$$
Expand Down
2 changes: 1 addition & 1 deletion chapter_convolutional-neural-networks/lenet.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ LeNet被广泛用于自动取款机(ATM)机中,帮助识别处理支票的
![LeNet中的数据流。输入是手写数字,输出为10种可能结果的概率。](../img/lenet.svg)
:label:`img_lenet`

每个卷积块中的基本单元是一个卷积层、一个sigmoid激活函数和平均汇聚层。请注意,虽然ReLU和最大汇聚层更有效,但它们在20世纪90年代还没有出现。每个卷积层使用$5\times 5$卷积核和一个sigmoid激活函数。这些层将输入映射到多个二维特征输出,通常同时增加通道的数量。第一卷积层有6个输出通道,而第二个卷积层有16个输出通道。每个$2\times2$池操作(步骤2)通过空间下采样将维数减少4倍。卷积的输出形状由批量大小、通道数、高度、宽度决定。
每个卷积块中的基本单元是一个卷积层、一个sigmoid激活函数和平均汇聚层。请注意,虽然ReLU和最大汇聚层更有效,但它们在20世纪90年代还没有出现。每个卷积层使用$5\times 5$卷积核和一个sigmoid激活函数。这些层将输入映射到多个二维特征输出,通常同时增加通道的数量。第一卷积层有6个输出通道,而第二个卷积层有16个输出通道。每个$2\times2$池操作(步幅2)通过空间下采样将维数减少4倍。卷积的输出形状由批量大小、通道数、高度、宽度决定。

为了将卷积块的输出传递给稠密块,我们必须在小批量中展平每个样本。换言之,我们将这个四维输入转换成全连接层所期望的二维输入。这里的二维表示的第一个维度索引小批量中的样本,第二个维度给出每个样本的平面向量表示。LeNet的稠密块有三个全连接层,分别有120、84和10个输出。因为我们在执行分类任务,所以输出层的10维对应于最后输出结果的数量。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ conv2d = tf.keras.layers.Conv2D(1, kernel_size=(3,5), padding='valid',
comp_conv2d(conv2d, X).shape
```

为了简洁起见,当输入高度和宽度两侧的填充数量分别为$p_h$和$p_w$时,我们称之为填充$(p_h, p_w)$。当$p_h = p_w = p$时,填充是$p$。同理,当高度和宽度上的步幅分别为$s_h$和$s_w$时,我们称之为步幅$(s_h, s_w)$。当时的步幅为$s_h = s_w = s$时,步幅为$s$。默认情况下,填充为0,步幅为1。在实践中,我们很少使用不一致的步幅或填充,也就是说,我们通常有$p_h = p_w$和$s_h = s_w$。
为了简洁起见,当输入高度和宽度两侧的填充数量分别为$p_h$和$p_w$时,我们称之为填充$(p_h, p_w)$。当$p_h = p_w = p$时,填充是$p$。同理,当高度和宽度上的步幅分别为$s_h$和$s_w$时,我们称之为步幅$(s_h, s_w)$。特别地,当$s_h = s_w = s$时,我们称步幅为$s$。默认情况下,填充为0,步幅为1。在实践中,我们很少使用不一致的步幅或填充,也就是说,我们通常有$p_h = p_w$和$s_h = s_w$。

## 小结

Expand Down
2 changes: 1 addition & 1 deletion chapter_deep-learning-computation/model-construction.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class MySequential(nn.Module):
super().__init__()
for idx, module in enumerate(args):
# 这里,module是Module子类的一个实例。我们把它保存在'Module'类的成员
# 变量_modules中。module的类型是OrderedDict
# 变量_modules中。_module的类型是OrderedDict
self._modules[str(idx)] = module
def forward(self, X):
Expand Down
4 changes: 2 additions & 2 deletions chapter_deep-learning-computation/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,14 @@ print(net[1].weight.data())

```{.python .input}
#@tab pytorch
def xavier(m):
def init_xavier(m):
if type(m) == nn.Linear:
nn.init.xavier_uniform_(m.weight)
def init_42(m):
if type(m) == nn.Linear:
nn.init.constant_(m.weight, 42)
net[0].apply(xavier)
net[0].apply(init_xavier)
net[2].apply(init_42)
print(net[0].weight.data[0])
print(net[2].weight.data)
Expand Down
2 changes: 1 addition & 1 deletion chapter_introduction/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ agent的动作会影响后续的观察,而奖励只与所选的动作相对应
[罗纳德·费舍尔(1890-1962)](https://en.wikipedia.org/wiki/Ronald_-Fisher)对统计理论和在遗传学中的应用做出了重大贡献。
他的许多算法(如线性判别分析)和公式(如费舍尔信息矩阵)至今仍被频繁使用。
甚至,费舍尔在1936年发布的鸢尾花卉数据集,有时仍然被用来解读机器学习算法。
他也是优生学的倡导者,这提醒我们:使用数据科学虽然在道德上存在疑问,但是与数据科学在工业和自然科学中的生产性使用一样,有着悠久的历史
他也是优生学的倡导者,这提醒我们:数据科学在道德上存疑的使用,与其在工业和自然科学中的生产性使用一样,有着悠远而持久的历史

机器学习的第二个影响来自[克劳德·香农(1916--2001)](https://en.wikipedia.org/wiki/Claude_Shannon)的信息论和[艾伦·图灵(1912-1954)](https://en.wikipedia.org/wiki/Alan_Turing)的计算理论。
图灵在他著名的论文《计算机器与智能》 :cite:`Turing.1950` 中提出了“机器能思考吗?”的问题。
Expand Down
4 changes: 2 additions & 2 deletions chapter_linear-networks/linear-regression.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,9 @@ $$-\log P(\mathbf y \mid \mathbf X) = \sum_{i=1}^n \frac{1}{2} \log(2 \pi \sigma
这种想法归功于我们对真实生物神经系统的研究。

当今大多数深度学习的研究几乎没有直接从神经科学中获得灵感。
我们援引斯图尔特·罗素和彼得·诺维格谁,在他们的经典人工智能教科书
我们援引斯图尔特·罗素和彼得·诺维格在他们的经典人工智能教科书
*Artificial Intelligence:A Modern Approach* :cite:`Russell.Norvig.2016`
中所说:虽然飞机可能受到鸟类的启发,但几个世纪以来,鸟类学并不是航空创新的主要驱动力。
中所说的:虽然飞机可能受到鸟类的启发,但几个世纪以来,鸟类学并不是航空创新的主要驱动力。
同样地,如今在深度学习中的灵感同样或更多地来自数学、统计学和计算机科学。

## 小结
Expand Down
20 changes: 11 additions & 9 deletions chapter_linear-networks/softmax-regression.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,24 @@ $$
这些违反了 :numref:`sec_prob`中所说的概率基本公理。

要将输出视为概率,我们必须保证在任何数据上的输出都是非负的且总和为1。
此外,我们需要一个训练目标,来鼓励模型精准地估计概率。
在分类器输出0.5的所有样本中,我们希望这些样本有一半实际上属于预测的类。
此外,我们需要一个训练的目标函数,来激励模型精准地估计概率。
例如,
在分类器输出0.5的所有样本中,我们希望这些样本是刚好有一半实际上属于预测的类别。
这个属性叫做*校准*(calibration)。

社会科学家邓肯·卢斯于1959年在*选择模型*(choice model)的理论基础上
发明的*softmax函数*正是这样做的:
softmax函数将未规范化的预测变换为非负并且总和为1,同时要求模型保持可导。
我们首先对每个未规范化的预测求幂,这样可以确保输出非负。
为了确保最终输出的总和为1,我们再对每个求幂后的结果除以它们的总和。如下式:
softmax函数能够将未规范化的预测变换为非负数并且总和为1,同时让模型保持
可导的性质。
为了完成这一目标,我们首先对每个未规范化的预测求幂,这样可以确保输出非负。
为了确保最终输出的概率值总和为1,我们再让每个求幂后的结果除以它们的总和。如下式:

$$\hat{\mathbf{y}} = \mathrm{softmax}(\mathbf{o})\quad \text{其中}\quad \hat{y}_j = \frac{\exp(o_j)}{\sum_k \exp(o_k)}$$
:eqlabel:`eq_softmax_y_and_o`

这里,对于所有的$j$总有$0 \leq \hat{y}_j \leq 1$。
因此,$\hat{\mathbf{y}}$可以视为一个正确的概率分布。
softmax运算不会改变未规范化的预测$\mathbf{o}$之间的顺序,只会确定分配给每个类别的概率。
softmax运算不会改变未规范化的预测$\mathbf{o}$之间的大小次序,只会确定分配给每个类别的概率。
因此,在预测过程中,我们仍然可以用下式来选择最有可能的类别。

$$
Expand All @@ -137,11 +139,11 @@ $$
## 小批量样本的矢量化
:label:`subsec_softmax_vectorization`

为了提高计算效率并且充分利用GPU,我们通常会针对小批量数据执行矢量计算
为了提高计算效率并且充分利用GPU,我们通常会对小批量样本的数据执行矢量计算
假设我们读取了一个批量的样本$\mathbf{X}$,
其中特征维度(输入数量)为$d$,批量大小为$n$。
此外,假设我们在输出中有$q$个类别。
那么小批量特征为$\mathbf{X} \in \mathbb{R}^{n \times d}$,
那么小批量样本的特征为$\mathbf{X} \in \mathbb{R}^{n \times d}$,
权重为$\mathbf{W} \in \mathbb{R}^{d \times q}$,
偏置为$\mathbf{b} \in \mathbb{R}^{1\times q}$。
softmax回归的矢量计算表达式为:
Expand All @@ -155,7 +157,7 @@ $$ \begin{aligned} \mathbf{O} &= \mathbf{X} \mathbf{W} + \mathbf{b}, \\ \hat{\ma
那么softmax运算可以*按行*(rowwise)执行:
对于$\mathbf{O}$的每一行,我们先对所有项进行幂运算,然后通过求和对它们进行标准化。
在 :eqref:`eq_minibatch_softmax_reg`中,
$\mathbf{X} \mathbf{W} + \mathbf{b}$的求和会使用广播
$\mathbf{X} \mathbf{W} + \mathbf{b}$的求和会使用广播机制
小批量的未规范化预测$\mathbf{O}$和输出概率$\hat{\mathbf{Y}}$
都是形状为$n \times q$的矩阵。

Expand Down
4 changes: 2 additions & 2 deletions chapter_multilayer-perceptrons/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ $P(y \mid \mathbf{x})$的分布可能会因我们的位置不同而得到不同
然后这家初创公司问我们是否可以帮助他们建立一个用于检测疾病的分类器。

正如我们向他们解释的那样,用近乎完美的精度来区分健康和患病人群确实很容易。
然而,这是可能因为受试者在年龄、激素水平、体力活动、
然而,这可能是因为受试者在年龄、激素水平、体力活动、
饮食、饮酒以及其他许多与疾病无关的因素上存在差异。
这对检测疾病的分类器可能并不适用。
这些抽样可能会遇到极端的协变量偏移。
Expand Down Expand Up @@ -236,7 +236,7 @@ $$\mathop{\mathrm{minimize}}_f \frac{1}{n} \sum_{i=1}^n \beta_i l(f(\mathbf{x}_i
这是用于二元分类的softmax回归(见 :numref:`sec_softmax`)的一个特例。
综上所述,我们学习了一个分类器来区分从$p(\mathbf{x})$抽取的数据
和从$q(\mathbf{x})$抽取的数据。
如果无法区分这两个分布,则意味着想相关的样本可能来自这两个分布中的任何一个
如果无法区分这两个分布,则意味着相关的样本可能来自这两个分布中的任何一个
另一方面,任何可以很好区分的样本都应该相应地显著增加或减少权重。

为了简单起见,假设我们分别从$p(\mathbf{x})$和$q(\mathbf{x})$
Expand Down
2 changes: 1 addition & 1 deletion chapter_multilayer-perceptrons/environment_origin.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ Likewise, a user's behavior on a news site will depend on what we showed her pre
Recently,
control theory (e.g., PID variants) has also been used
to automatically tune hyperparameters
to achive better disentangling and reconstruction quality,
to achieve better disentangling and reconstruction quality,
and improve the diversity of generated text and the reconstruction quality of generated images :cite:`Shao.Yao.Sun.ea.2020`.


Expand Down
2 changes: 1 addition & 1 deletion chapter_multilayer-perceptrons/kaggle-house-price.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Kaggle的房价预测比赛是一个很好的起点。
数据集要大得多,也有更多的特征。

本节我们将详细介绍数据预处理、模型设计和超参数选择。
通过亲身实践,你将获得一手经验,这些经验将指导你数据科学家职业生涯
通过亲身实践,你将获得一手经验,这些经验将有益数据科学家的职业成长

## 下载和缓存数据集

Expand Down
2 changes: 1 addition & 1 deletion chapter_multilayer-perceptrons/mlp-concise.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ net = tf.keras.models.Sequential([
```

[**训练过程**]的实现与我们实现softmax回归时完全相同,
这种模块化设计使我们能够将与和模型架构有关的内容独立出来
这种模块化设计使我们能够将与模型架构有关的内容独立出来

```{.python .input}
batch_size, lr, num_epochs = 256, 0.1, 10
Expand Down

0 comments on commit e42ebff

Please sign in to comment.