Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
d2l-zh
提交
ce2d6513
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,发现更多精彩内容 >>
提交
ce2d6513
编写于
7月 15, 2018
作者:
M
muli
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
triggle rebuild
上级
d48acd16
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
16 addition
and
18 deletion
+16
-18
utils.py
utils.py
+16
-18
未找到文件。
utils.py
浏览文件 @
ce2d6513
...
...
@@ -35,7 +35,7 @@ class DataLoader(object):
for
i
in
range
(
n
//
self
.
batch_size
):
if
self
.
transform
is
not
None
:
yield
self
.
transform
(
X
[
i
*
self
.
batch_size
:(
i
+
1
)
*
self
.
batch_size
],
yield
self
.
transform
(
X
[
i
*
self
.
batch_size
:(
i
+
1
)
*
self
.
batch_size
],
y
[
i
*
self
.
batch_size
:(
i
+
1
)
*
self
.
batch_size
])
else
:
yield
(
X
[
i
*
self
.
batch_size
:(
i
+
1
)
*
self
.
batch_size
],
...
...
@@ -59,7 +59,7 @@ def load_data_fashion_mnist(batch_size, resize=None, root="~/.mxnet/datasets/fas
mnist_train
=
gluon
.
data
.
vision
.
FashionMNIST
(
root
=
root
,
train
=
True
,
transform
=
None
)
mnist_test
=
gluon
.
data
.
vision
.
FashionMNIST
(
root
=
root
,
train
=
False
,
transform
=
None
)
# Transform later to avoid memory explosion.
# Transform later to avoid memory explosion.
train_data
=
DataLoader
(
mnist_train
,
batch_size
,
shuffle
=
True
,
transform
=
transform_mnist
)
test_data
=
DataLoader
(
mnist_test
,
batch_size
,
shuffle
=
False
,
transform
=
transform_mnist
)
return
(
train_data
,
test_data
)
...
...
@@ -129,7 +129,7 @@ def evaluate_accuracy(data_iter, net, ctx=[mx.cpu()]):
y
=
y
.
astype
(
'float32'
)
acc
+=
(
net
(
X
).
argmax
(
axis
=
1
)
==
y
).
sum
().
copyto
(
mx
.
cpu
())
n
+=
y
.
size
acc
.
wait_to_read
()
acc
.
wait_to_read
()
return
acc
.
asscalar
()
/
n
...
...
@@ -221,7 +221,7 @@ def show_images(imgs, nrows, ncols, figsize=None):
def
data_iter_random
(
corpus_indices
,
batch_size
,
num_steps
,
ctx
=
None
):
"""Sample mini-batches in a random order from sequential data."""
# Subtract 1 because label indices are corresponding input indices + 1.
# Subtract 1 because label indices are corresponding input indices + 1.
num_examples
=
(
len
(
corpus_indices
)
-
1
)
//
num_steps
epoch_size
=
num_examples
//
batch_size
# Randomize samples.
...
...
@@ -246,12 +246,12 @@ def data_iter_consecutive(corpus_indices, batch_size, num_steps, ctx=None):
corpus_indices
=
nd
.
array
(
corpus_indices
,
ctx
=
ctx
)
data_len
=
len
(
corpus_indices
)
batch_len
=
data_len
//
batch_size
indices
=
corpus_indices
[
0
:
batch_size
*
batch_len
].
reshape
((
batch_size
,
batch_len
))
# Subtract 1 because label indices are corresponding input indices + 1.
# Subtract 1 because label indices are corresponding input indices + 1.
epoch_size
=
(
batch_len
-
1
)
//
num_steps
for
i
in
range
(
epoch_size
):
i
=
i
*
num_steps
data
=
indices
[:,
i
:
i
+
num_steps
]
...
...
@@ -293,7 +293,7 @@ def predict_rnn(rnn, prefix, num_chars, params, hidden_dim, ctx, idx_to_char,
return
''
.
join
([
idx_to_char
[
i
]
for
i
in
output
])
def
train_and_predict_rnn
(
rnn
,
is_random_iter
,
epochs
,
num_steps
,
hidden_dim
,
def
train_and_predict_rnn
(
rnn
,
is_random_iter
,
epochs
,
num_steps
,
hidden_dim
,
learning_rate
,
clipping_norm
,
batch_size
,
pred_period
,
pred_len
,
seqs
,
get_params
,
get_inputs
,
ctx
,
corpus_indices
,
idx_to_char
,
char_to_idx
,
...
...
@@ -304,10 +304,10 @@ def train_and_predict_rnn(rnn, is_random_iter, epochs, num_steps, hidden_dim,
else
:
data_iter
=
data_iter_consecutive
params
=
get_params
()
softmax_cross_entropy
=
gluon
.
loss
.
SoftmaxCrossEntropyLoss
()
for
e
in
range
(
1
,
epochs
+
1
):
for
e
in
range
(
1
,
epochs
+
1
):
# If consecutive sampling is used, in the same epoch, the hidden state
# is initialized only at the beginning of the epoch.
if
not
is_random_iter
:
...
...
@@ -315,7 +315,7 @@ def train_and_predict_rnn(rnn, is_random_iter, epochs, num_steps, hidden_dim,
if
is_lstm
:
state_c
=
nd
.
zeros
(
shape
=
(
batch_size
,
hidden_dim
),
ctx
=
ctx
)
train_loss
,
num_examples
=
0
,
0
for
data
,
label
in
data_iter
(
corpus_indices
,
batch_size
,
num_steps
,
for
data
,
label
in
data_iter
(
corpus_indices
,
batch_size
,
num_steps
,
ctx
):
# If random sampling is used, the hidden state has to be
# initialized for each mini-batch.
...
...
@@ -327,7 +327,7 @@ def train_and_predict_rnn(rnn, is_random_iter, epochs, num_steps, hidden_dim,
# outputs shape: (batch_size, vocab_size)
if
is_lstm
:
outputs
,
state_h
,
state_c
=
rnn
(
get_inputs
(
data
),
state_h
,
state_c
,
*
params
)
state_c
,
*
params
)
else
:
outputs
,
state_h
=
rnn
(
get_inputs
(
data
),
state_h
,
*
params
)
# Let t_ib_j be the j-th element of the mini-batch at time i.
...
...
@@ -348,7 +348,7 @@ def train_and_predict_rnn(rnn, is_random_iter, epochs, num_steps, hidden_dim,
num_examples
+=
loss
.
size
if
e
%
pred_period
==
0
:
print
(
"Epoch %d. Training perplexity %f"
%
(
e
,
print
(
"Epoch %d. Training perplexity %f"
%
(
e
,
exp
(
train_loss
/
num_examples
)))
for
seq
in
seqs
:
print
(
' - '
,
predict_rnn
(
rnn
,
seq
,
pred_len
,
params
,
...
...
@@ -361,7 +361,7 @@ def set_fig_size(mpl, figsize=(3.5, 2.5)):
mpl
.
rcParams
[
'figure.figsize'
]
=
figsize
def
data_iter
(
batch_size
,
num_examples
,
features
,
labels
):
def
data_iter
(
batch_size
,
num_examples
,
features
,
labels
):
"""遍历数据集。"""
indices
=
list
(
range
(
num_examples
))
random
.
shuffle
(
indices
)
...
...
@@ -387,7 +387,7 @@ def optimize(batch_size, trainer, num_epochs, decay_epoch, log_interval,
data_iter
=
gdata
.
DataLoader
(
dataset
,
batch_size
,
shuffle
=
True
)
loss
=
gloss
.
L2Loss
()
ls
=
[
loss
(
net
(
features
),
labels
).
mean
().
asnumpy
()]
for
epoch
in
range
(
1
,
num_epochs
+
1
):
for
epoch
in
range
(
1
,
num_epochs
+
1
):
# 学习率自我衰减。
if
decay_epoch
and
epoch
>
decay_epoch
:
trainer
.
set_learning_rate
(
trainer
.
learning_rate
*
0.1
)
...
...
@@ -405,11 +405,9 @@ def optimize(batch_size, trainer, num_epochs, decay_epoch, log_interval,
def
semilogy
(
x_vals
,
y_vals
,
x_label
,
y_label
,
figsize
=
(
3.5
,
2.5
)):
"""绘图(y取对数)。"""
"""绘图(y取对数)。"""
set_fig_size
(
mpl
,
figsize
)
plt
.
semilogy
(
x_vals
,
y_vals
)
plt
.
xlabel
(
x_label
)
plt
.
ylabel
(
y_label
)
plt
.
show
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录