Skip to content

Commit 450277f

Browse files
authored
Merge pull request #1123 from d2l-ai/master
Sync d2l-en v0.17.5
2 parents 28e1114 + 842bcdb commit 450277f

File tree

13 files changed

+56
-25
lines changed

13 files changed

+56
-25
lines changed

chapter_installation/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ pip install mxnet==1.7.0.post1
8888
你可以按如下方式安装PyTorch的CPU或GPU版本:
8989

9090
```bash
91-
pip install torch==1.10.2
92-
pip install torchvision==0.11.3
91+
pip install torch==1.11.0
92+
pip install torchvision==0.12.0
9393
```
9494

9595

@@ -109,7 +109,7 @@ pip install tensorflow-probability==0.16.0
109109
我们的下一步是安装`d2l`包,以方便调取本书中经常使用的函数和类:
110110

111111
```bash
112-
pip install d2l==0.17.4
112+
pip install d2l==0.17.5
113113
```
114114

115115

chapter_linear-networks/linear-regression.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ $\eta$表示*学习率*(learning rate)。
223223
%matplotlib inline
224224
from d2l import mxnet as d2l
225225
import math
226-
import numpy as np
226+
from mxnet import np
227227
import time
228228
```
229229

@@ -350,7 +350,18 @@ def normal(x, mu, sigma):
350350
我们现在(**可视化正态分布**)。
351351

352352
```{.python .input}
353-
#@tab all
353+
#@tab mxnet
354+
# 再次使用numpy进行可视化
355+
x = np.arange(-7, 7, 0.01)
356+
# Mean and standard deviation pairs
357+
params = [(0, 1), (0, 2), (3, 1)]
358+
d2l.plot(x.asnumpy(), [normal(x, mu, sigma).asnumpy() for mu, sigma in params], xlabel='x',
359+
ylabel='p(x)', figsize=(4.5, 2.5),
360+
legend=[f'mean {mu}, std {sigma}' for mu, sigma in params])
361+
```
362+
363+
```{.python .input}
364+
#@tab pytorch, tensorflow
354365
# 再次使用numpy进行可视化
355366
x = np.arange(-7, 7, 0.01)
356367

chapter_multilayer-perceptrons/kaggle-house-price.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ def train(net, train_features, train_labels, test_features, test_labels,
439439
具体地说,它选择第$i$个切片作为验证数据,其余部分作为训练数据。
440440
注意,这并不是处理数据的最有效方法,如果我们的数据集大得多,会有其他解决办法。
441441

442-
443442
```{.python .input}
444443
#@tab all
445444
def get_k_fold_data(k, i, X, y):
@@ -514,7 +513,7 @@ print(f'{k}-折验证: 平均训练log rmse: {float(train_l):f}, '
514513

515514
```{.python .input}
516515
#@tab all
517-
def train_and_pred(train_features, test_feature, train_labels, test_data,
516+
def train_and_pred(train_features, test_features, train_labels, test_data,
518517
num_epochs, lr, weight_decay, batch_size):
519518
net = get_net()
520519
train_ls, _ = train(net, train_features, train_labels, None, None,

chapter_natural-language-processing-applications/natural-language-inference-bert.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def load_pretrained_model(pretrained_model, num_hiddens, ffn_num_hiddens,
9191
return bert, vocab
9292
```
9393

94-
为了便于在大多数机器上演示,我们将在本节中加载和微调经过预训练BERT的小版本(“bert.mall”)。在练习中,我们将展示如何微调大得多的“bert.base”以显著提高测试精度。
94+
为了便于在大多数机器上演示,我们将在本节中加载和微调经过预训练BERT的小版本(“bert.small”)。在练习中,我们将展示如何微调大得多的“bert.base”以显著提高测试精度。
9595

9696
```{.python .input}
9797
#@tab all
@@ -310,7 +310,7 @@ d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs,
310310

311311
## 练习
312312

313-
1. 如果您的计算资源允许,请微调一个更大的预训练BERT模型,该模型与原始的BERT基础模型一样大。修改`load_pretrained_model`函数中的参数设置:将“bert.mall”替换为“bert.base”,将`num_hiddens=256``ffn_num_hiddens=512``num_heads=4``num_layers=2`的值分别增加到768、3072、12和12。通过增加微调迭代轮数(可能还会调优其他超参数),你可以获得高于0.86的测试精度吗?
313+
1. 如果您的计算资源允许,请微调一个更大的预训练BERT模型,该模型与原始的BERT基础模型一样大。修改`load_pretrained_model`函数中的参数设置:将“bert.small”替换为“bert.base”,将`num_hiddens=256``ffn_num_hiddens=512``num_heads=4``num_layers=2`的值分别增加到768、3072、12和12。通过增加微调迭代轮数(可能还会调优其他超参数),你可以获得高于0.86的测试精度吗?
314314
1. 如何根据一对序列的长度比值截断它们?将此对截断方法与`SNLIBERTDataset`类中使用的方法进行比较。它们的利弊是什么?
315315

316316
:begin_tab:`mxnet`

chapter_natural-language-processing-applications/sentiment-analysis-rnn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 情感分析:使用递归神经网络
1+
# 情感分析:使用循环神经网络
22
:label:`sec_sentiment_rnn`
33

44
与词相似度和类比任务一样,我们也可以将预先训练的词向量应用于情感分析。由于 :numref:`sec_sentiment`中的IMDb评论数据集不是很大,使用在大规模语料库上预训练的文本表示可以减少模型的过拟合。作为 :numref:`fig_nlp-map-sa-rnn`中所示的具体示例,我们将使用预训练的GloVe模型来表示每个词元,并将这些词元表示送入多层双向循环神经网络以获得文本序列表示,该文本序列表示将被转换为情感分析输出 :cite:`Maas.Daly.Pham.ea.2011`。对于相同的下游应用,我们稍后将考虑不同的架构选择。

chapter_optimization/optimization-intro.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,24 @@ annotate('saddle point', (0, -0.2), (-0.52, -5.0))
9898
如下例所示,较高维度的鞍点甚至更加隐蔽。考虑这个函数$f(x, y) = x^2 - y^2$。它的鞍点为$(0, 0)$。这是关于$y$的最大值,也是关于$x$的最小值。此外,它*看起来*像马鞍,这就是这个数学属性的名字由来。
9999

100100
```{.python .input}
101-
#@tab all
101+
#@tab mxnet
102+
x, y = d2l.meshgrid(
103+
d2l.linspace(-1.0, 1.0, 101), d2l.linspace(-1.0, 1.0, 101))
104+
z = x**2 - y**2
105+
ax = d2l.plt.figure().add_subplot(111, projection='3d')
106+
ax.plot_wireframe(x.asnumpy(), y.asnumpy(), z.asnumpy(),
107+
**{'rstride': 10, 'cstride': 10})
108+
ax.plot([0], [0], [0], 'rx')
109+
ticks = [-1, 0, 1]
110+
d2l.plt.xticks(ticks)
111+
d2l.plt.yticks(ticks)
112+
ax.set_zticks(ticks)
113+
d2l.plt.xlabel('x')
114+
d2l.plt.ylabel('y');
115+
```
116+
117+
```{.python .input}
118+
#@tab pytorch, tensorflow
102119
x, y = d2l.meshgrid(
103120
d2l.linspace(-1.0, 1.0, 101), d2l.linspace(-1.0, 1.0, 101))
104121
z = x**2 - y**2

chapter_preface/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
许多教科书教授一系列的主题,每一个都非常详细。例如,Chris Bishop的优秀教科书 :cite:`Bishop.2006` ,对每个主题都教得很透彻,以至于要读到线性回归这一章需要大量的工作。虽然专家们喜欢这本书正是因为它的透彻性,但对于初学者来说,这一特性限制了它作为介绍性文本的实用性。
2626

27-
在这本书中,我们将适时教授大部分概念。换句话说,你将在实现某些实际目的所需的非常时刻学习概念。虽然我们在开始时花了一些时间来教授基础基础知识,如线性代数和概率,但我们希望你在担心更深奥的概率分布之前,先体会一下训练第一个模型的满足感
27+
在这本书中,我们将适时教授大部分概念。换句话说,你将在实现某些实际目的所需的非常时刻学习概念。虽然我们在开始时花了一些时间来教授基础的背景知识,如线性代数和概率,但我们希望你在思考更深奥的概率分布之前,先体会一下训练模型的满足感
2828

29-
除了提供基本数学背景速成课程的几节初步课程外,后续的每一章都介绍了合理数量的新概念,并提供一个独立工作的例子——使用真实的数据集。这带来了组织上的挑战。某些模型可能在逻辑上组合在单节中。而一些想法可能最好是通过连续允许几个模型来传授。另一方面,坚持“一个工作例子一节”的策略有一个很大的好处:这使你可以通过利用我们的代码尽可能轻松地启动你自己的研究项目。只需复制这一节的内容并开始修改即可。
29+
除了提供基本数学背景速成课程的几节初步课程外,后续的每一章都介绍了适量的新概念,并提供可独立工作的例子——使用真实的数据集。这带来了组织上的挑战。某些模型可能在逻辑上组合在单节中。而一些想法可能最好是通过连续允许几个模型来传授。另一方面,坚持“一个工作例子一节”的策略有一个很大的好处:这使你可以通过利用我们的代码尽可能轻松地启动你自己的研究项目。只需复制这一节的内容并开始修改即可。
3030

3131
我们将根据需要将可运行代码与背景材料交错。通常,在充分解释工具之前,我们常常会在提供工具这一方面犯错误(我们将在稍后解释背景)。例如,在充分解释*随机梯度下降*为什么有用或为什么有效之前,我们可以使用它。这有助于给从业者提供快速解决问题所需的弹药,同时需要读者相信我们的一些决定。
3232

@@ -63,6 +63,7 @@ from collections import defaultdict
6363
from IPython import display
6464
import math
6565
from matplotlib import pyplot as plt
66+
from matplotlib_inline import backend_inline
6667
import os
6768
import pandas as pd
6869
import random

chapter_preliminaries/calculus.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
```{.python .input}
5454
%matplotlib inline
5555
from d2l import mxnet as d2l
56-
from IPython import display
56+
from matplotlib_inline import backend_inline
5757
from mxnet import np, npx
5858
npx.set_np()
5959
@@ -65,7 +65,7 @@ def f(x):
6565
#@tab pytorch
6666
%matplotlib inline
6767
from d2l import torch as d2l
68-
from IPython import display
68+
from matplotlib_inline import backend_inline
6969
import numpy as np
7070
7171
def f(x):
@@ -76,7 +76,7 @@ def f(x):
7676
#@tab tensorflow
7777
%matplotlib inline
7878
from d2l import tensorflow as d2l
79-
from IPython import display
79+
from matplotlib_inline import backend_inline
8080
import numpy as np
8181
8282
def f(x):
@@ -145,7 +145,7 @@ $$\frac{d}{dx} \left[\frac{f(x)}{g(x)}\right] = \frac{g(x) \frac{d}{dx} [f(x)] -
145145
#@tab all
146146
def use_svg_display(): #@save
147147
"""使用svg格式在Jupyter中显示绘图"""
148-
display.set_matplotlib_formats('svg')
148+
backend_inline.set_matplotlib_formats('svg')
149149
```
150150

151151
我们定义`set_figsize`函数来设置图表大小。

d2l/mxnet.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import requests
2020
from IPython import display
2121
from matplotlib import pyplot as plt
22+
from matplotlib_inline import backend_inline
2223

2324
d2l = sys.modules[__name__]
2425

@@ -29,7 +30,7 @@ def use_svg_display():
2930
"""使用svg格式在Jupyter中显示绘图
3031
3132
Defined in :numref:`sec_calculus`"""
32-
display.set_matplotlib_formats('svg')
33+
backend_inline.set_matplotlib_formats('svg')
3334

3435
def set_figsize(figsize=(3.5, 2.5)):
3536
"""设置matplotlib的图表大小

d2l/tensorflow.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import requests
2020
from IPython import display
2121
from matplotlib import pyplot as plt
22+
from matplotlib_inline import backend_inline
2223

2324
d2l = sys.modules[__name__]
2425

@@ -29,7 +30,7 @@ def use_svg_display():
2930
"""使用svg格式在Jupyter中显示绘图
3031
3132
Defined in :numref:`sec_calculus`"""
32-
display.set_matplotlib_formats('svg')
33+
backend_inline.set_matplotlib_formats('svg')
3334

3435
def set_figsize(figsize=(3.5, 2.5)):
3536
"""设置matplotlib的图表大小

0 commit comments

Comments
 (0)