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