Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
d2l-zh
提交
08e604cb
D
d2l-zh
项目概览
OpenDocCN
/
d2l-zh
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
d2l-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
08e604cb
编写于
12月 18, 2018
作者:
A
Aston Zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code change for 1.4.0rc
上级
9ddd1be9
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
148 addition
and
44 deletion
+148
-44
chapter_computer-vision/image-augmentation.md
chapter_computer-vision/image-augmentation.md
+1
-2
chapter_computer-vision/semantic-segmentation-and-dataset.md
chapter_computer-vision/semantic-segmentation-and-dataset.md
+2
-3
chapter_computer-vision/ssd.md
chapter_computer-vision/ssd.md
+127
-18
chapter_convolutional-neural-networks/alexnet.md
chapter_convolutional-neural-networks/alexnet.md
+2
-3
chapter_deep-learning-basics/fashion-mnist.md
chapter_deep-learning-basics/fashion-mnist.md
+2
-7
chapter_deep-learning-computation/read-write.md
chapter_deep-learning-computation/read-write.md
+2
-2
chapter_natural-language-processing/word2vec-gluon.md
chapter_natural-language-processing/word2vec-gluon.md
+10
-6
gluonbook/utils.py
gluonbook/utils.py
+2
-3
未找到文件。
chapter_computer-vision/image-augmentation.md
浏览文件 @
08e604cb
...
@@ -125,11 +125,10 @@ no_aug = gdata.vision.transforms.Compose([
...
@@ -125,11 +125,10 @@ no_aug = gdata.vision.transforms.Compose([
接下来我们定义一个辅助函数来方便读取图像并应用图像增广。Gluon的数据集提供的
`transform_first`
函数将图像增广应用在每个训练样本(图像和标签)的第一个元素,即图像之上。有关
`DataLoader`
的详细介绍,可参考更早的
[
“图像分类数据集(Fashion-MNIST)”
](
../chapter_deep-learning-basics/fashion-mnist.md
)
一节。
接下来我们定义一个辅助函数来方便读取图像并应用图像增广。Gluon的数据集提供的
`transform_first`
函数将图像增广应用在每个训练样本(图像和标签)的第一个元素,即图像之上。有关
`DataLoader`
的详细介绍,可参考更早的
[
“图像分类数据集(Fashion-MNIST)”
](
../chapter_deep-learning-basics/fashion-mnist.md
)
一节。
```
{.python .input n=34}
```
{.python .input n=34}
num_workers = 0 if sys.platform.startswith('win32') else 4
def load_cifar10(is_train, augs, batch_size):
def load_cifar10(is_train, augs, batch_size):
return gdata.DataLoader(
return gdata.DataLoader(
gdata.vision.CIFAR10(train=is_train).transform_first(augs),
gdata.vision.CIFAR10(train=is_train).transform_first(augs),
batch_size=batch_size, shuffle=is_train, num_workers=
num_workers
)
batch_size=batch_size, shuffle=is_train, num_workers=
4
)
```
```
### 使用多GPU训练模型
### 使用多GPU训练模型
...
...
chapter_computer-vision/semantic-segmentation-and-dataset.md
浏览文件 @
08e604cb
...
@@ -177,11 +177,10 @@ voc_test = VOCSegDataset(False, crop_size, voc_dir, colormap2label)
...
@@ -177,11 +177,10 @@ voc_test = VOCSegDataset(False, crop_size, voc_dir, colormap2label)
```
{.python .input n=11}
```
{.python .input n=11}
batch_size = 64
batch_size = 64
num_workers = 0 if sys.platform.startswith('win32') else 4
train_iter = gdata.DataLoader(voc_train, batch_size, shuffle=True,
train_iter = gdata.DataLoader(voc_train, batch_size, shuffle=True,
last_batch='discard', num_workers=
num_workers
)
last_batch='discard', num_workers=
4
)
test_iter = gdata.DataLoader(voc_test, batch_size, last_batch='discard',
test_iter = gdata.DataLoader(voc_test, batch_size, last_batch='discard',
num_workers=
num_workers
)
num_workers=
4
)
```
```
打印第一个小批量的形状。不同于图像分类和目标识别,这里的标签是一个三维的数组。
打印第一个小批量的形状。不同于图像分类和目标识别,这里的标签是一个三维的数组。
...
...
chapter_computer-vision/ssd.md
浏览文件 @
08e604cb
因为 它太大了无法显示 source diff 。你可以改为
查看blob
。
chapter_convolutional-neural-networks/alexnet.md
浏览文件 @
08e604cb
...
@@ -106,13 +106,12 @@ def load_data_fashion_mnist(batch_size, resize=None, root=os.path.join(
...
@@ -106,13 +106,12 @@ def load_data_fashion_mnist(batch_size, resize=None, root=os.path.join(
transformer = gdata.vision.transforms.Compose(transformer)
transformer = gdata.vision.transforms.Compose(transformer)
mnist_train = gdata.vision.FashionMNIST(root=root, train=True)
mnist_train = gdata.vision.FashionMNIST(root=root, train=True)
mnist_test = gdata.vision.FashionMNIST(root=root, train=False)
mnist_test = gdata.vision.FashionMNIST(root=root, train=False)
num_workers = 0 if sys.platform.startswith('win32') else 4
train_iter = gdata.DataLoader(
train_iter = gdata.DataLoader(
mnist_train.transform_first(transformer), batch_size, shuffle=True,
mnist_train.transform_first(transformer), batch_size, shuffle=True,
num_workers=
num_workers
)
num_workers=
4
)
test_iter = gdata.DataLoader(
test_iter = gdata.DataLoader(
mnist_test.transform_first(transformer), batch_size, shuffle=False,
mnist_test.transform_first(transformer), batch_size, shuffle=False,
num_workers=
num_workers
)
num_workers=
4
)
return train_iter, test_iter
return train_iter, test_iter
batch_size = 128
batch_size = 128
...
...
chapter_deep-learning-basics/fashion-mnist.md
浏览文件 @
08e604cb
...
@@ -88,17 +88,12 @@ show_fashion_mnist(X, get_fashion_mnist_labels(y))
...
@@ -88,17 +88,12 @@ show_fashion_mnist(X, get_fashion_mnist_labels(y))
```
{.python .input n=28}
```
{.python .input n=28}
batch_size = 256
batch_size = 256
transformer = gdata.vision.transforms.ToTensor()
transformer = gdata.vision.transforms.ToTensor()
if sys.platform.startswith('win'):
num_workers = 0 # 0 表示不用额外的进程来加速读取数据。
else:
num_workers = 4
train_iter = gdata.DataLoader(mnist_train.transform_first(transformer),
train_iter = gdata.DataLoader(mnist_train.transform_first(transformer),
batch_size, shuffle=True,
batch_size, shuffle=True,
num_workers=
num_workers
)
num_workers=
4
)
test_iter = gdata.DataLoader(mnist_test.transform_first(transformer),
test_iter = gdata.DataLoader(mnist_test.transform_first(transformer),
batch_size, shuffle=False,
batch_size, shuffle=False,
num_workers=
num_workers
)
num_workers=
4
)
```
```
我们将获取并读取Fashion-MNIST数据集的逻辑封装在
`gluonbook.load_data_fashion_mnist`
函数中供后面章节调用。该函数将返回
`train_iter`
和
`test_iter`
两个变量。随着本书内容的不断深入,我们会进一步改进该函数。它的完整实现将在
[
“深度卷积神经网络(AlexNet)”
](
../chapter_convolutional-neural-networks/alexnet.md
)
一节中描述。
我们将获取并读取Fashion-MNIST数据集的逻辑封装在
`gluonbook.load_data_fashion_mnist`
函数中供后面章节调用。该函数将返回
`train_iter`
和
`test_iter`
两个变量。随着本书内容的不断深入,我们会进一步改进该函数。它的完整实现将在
[
“深度卷积神经网络(AlexNet)”
](
../chapter_convolutional-neural-networks/alexnet.md
)
一节中描述。
...
...
chapter_deep-learning-computation/read-write.md
浏览文件 @
08e604cb
...
@@ -64,14 +64,14 @@ y = net(x)
...
@@ -64,14 +64,14 @@ y = net(x)
```
{.python .input}
```
{.python .input}
filename = 'mlp.params'
filename = 'mlp.params'
net.save_params(filename)
net.save_param
eter
s(filename)
```
```
接下来,我们再实例化一次定义好的多层感知机。跟随机初始化模型参数不同,我们在这里直接读取保存在文件里的参数。
接下来,我们再实例化一次定义好的多层感知机。跟随机初始化模型参数不同,我们在这里直接读取保存在文件里的参数。
```
{.python .input n=8}
```
{.python .input n=8}
net2 = MLP()
net2 = MLP()
net2.load_params(filename)
net2.load_param
eter
s(filename)
```
```
因为这两个实例都有同样的模型参数,那么对同一个输入
`x`
的计算结果将会是一样。我们来验证一下。
因为这两个实例都有同样的模型参数,那么对同一个输入
`x`
的计算结果将会是一样。我们来验证一下。
...
...
chapter_natural-language-processing/word2vec-gluon.md
浏览文件 @
08e604cb
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
```
{.python .input n=1}
```
{.python .input n=1}
import collections
import collections
import gluonbook as gb
import math
import math
from mxnet import autograd, gluon, nd
from mxnet import autograd, gluon, nd
from mxnet.gluon import data as gdata, loss as gloss, nn
from mxnet.gluon import data as gdata, loss as gloss, nn
...
@@ -181,10 +182,9 @@ def batchify(data):
...
@@ -181,10 +182,9 @@ def batchify(data):
```
{.python .input n=14}
```
{.python .input n=14}
batch_size = 512
batch_size = 512
num_workers = 0 if sys.platform.startswith('win32') else 4
dataset = gdata.ArrayDataset(all_centers, all_contexts, all_negatives)
dataset = gdata.ArrayDataset(all_centers, all_contexts, all_negatives)
data_iter = gdata.DataLoader(dataset, batch_size, shuffle=True,
data_iter = gdata.DataLoader(dataset, batch_size, shuffle=True,
batchify_fn=batchify, num_workers=
num_workers
)
batchify_fn=batchify, num_workers=
4
)
for batch in data_iter:
for batch in data_iter:
for name, data in zip(['centers', 'contexts_negatives', 'masks',
for name, data in zip(['centers', 'contexts_negatives', 'masks',
'labels'], batch):
'labels'], batch):
...
@@ -275,8 +275,10 @@ print('%.7f' % ((sigmd(1.1) + sigmd(-0.6) + sigmd(-2.2)) / 3))
...
@@ -275,8 +275,10 @@ print('%.7f' % ((sigmd(1.1) + sigmd(-0.6) + sigmd(-2.2)) / 3))
```
{.python .input n=22}
```
{.python .input n=22}
embed_size = 100
embed_size = 100
net = nn.Sequential()
net = nn.Sequential()
net.add(nn.Embedding(input_dim=len(idx_to_token), output_dim=embed_size),
net.add(nn.Embedding(input_dim=len(idx_to_token), output_dim=embed_size,
nn.Embedding(input_dim=len(idx_to_token), output_dim=embed_size))
sparse_grad=True),
nn.Embedding(input_dim=len(idx_to_token), output_dim=embed_size,
sparse_grad=True))
```
```
### 训练
### 训练
...
@@ -285,13 +287,15 @@ net.add(nn.Embedding(input_dim=len(idx_to_token), output_dim=embed_size),
...
@@ -285,13 +287,15 @@ net.add(nn.Embedding(input_dim=len(idx_to_token), output_dim=embed_size),
```
{.python .input n=23}
```
{.python .input n=23}
def train(net, lr, num_epochs):
def train(net, lr, num_epochs):
net.initialize(force_reinit=True)
ctx = gb.try_gpu()
net.initialize(ctx=ctx, force_reinit=True)
trainer = gluon.Trainer(net.collect_params(), 'adam',
trainer = gluon.Trainer(net.collect_params(), 'adam',
{'learning_rate': lr})
{'learning_rate': lr})
for epoch in range(num_epochs):
for epoch in range(num_epochs):
start_time, train_l_sum = time.time(), 0
start_time, train_l_sum = time.time(), 0
for batch in data_iter:
for batch in data_iter:
center, context_negative, mask, label = batch
center, context_negative, mask, label = [
data.as_in_context(ctx) for data in batch]
with autograd.record():
with autograd.record():
pred = skip_gram(center, context_negative, net[0], net[1])
pred = skip_gram(center, context_negative, net[0], net[1])
# 使用掩码变量 mask 来避免填充项对损失函数计算的影响。
# 使用掩码变量 mask 来避免填充项对损失函数计算的影响。
...
...
gluonbook/utils.py
浏览文件 @
08e604cb
...
@@ -235,14 +235,13 @@ def load_data_fashion_mnist(batch_size, resize=None, root=os.path.join(
...
@@ -235,14 +235,13 @@ def load_data_fashion_mnist(batch_size, resize=None, root=os.path.join(
mnist_train
=
gdata
.
vision
.
FashionMNIST
(
root
=
root
,
train
=
True
)
mnist_train
=
gdata
.
vision
.
FashionMNIST
(
root
=
root
,
train
=
True
)
mnist_test
=
gdata
.
vision
.
FashionMNIST
(
root
=
root
,
train
=
False
)
mnist_test
=
gdata
.
vision
.
FashionMNIST
(
root
=
root
,
train
=
False
)
num_workers
=
0
if
sys
.
platform
.
startswith
(
'win32'
)
else
4
train_iter
=
gdata
.
DataLoader
(
mnist_train
.
transform_first
(
transformer
),
train_iter
=
gdata
.
DataLoader
(
mnist_train
.
transform_first
(
transformer
),
batch_size
,
shuffle
=
True
,
batch_size
,
shuffle
=
True
,
num_workers
=
num_workers
)
num_workers
=
4
)
test_iter
=
gdata
.
DataLoader
(
mnist_test
.
transform_first
(
transformer
),
test_iter
=
gdata
.
DataLoader
(
mnist_test
.
transform_first
(
transformer
),
batch_size
,
shuffle
=
False
,
batch_size
,
shuffle
=
False
,
num_workers
=
num_workers
)
num_workers
=
4
)
return
train_iter
,
test_iter
return
train_iter
,
test_iter
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录