Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
d2l-zh
提交
ecf14db2
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,体验更适合开发者的 AI 搜索 >>
未验证
提交
ecf14db2
编写于
1月 14, 2018
作者:
A
Aston Zhang
提交者:
GitHub
1月 14, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update rnn (#155)
上级
b21bf053
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
20 addition
and
20 deletion
+20
-20
chapter_recurrent-neural-networks/gru-scratch.md
chapter_recurrent-neural-networks/gru-scratch.md
+4
-4
chapter_recurrent-neural-networks/lstm-scratch.md
chapter_recurrent-neural-networks/lstm-scratch.md
+4
-4
chapter_recurrent-neural-networks/rnn-gluon.md
chapter_recurrent-neural-networks/rnn-gluon.md
+12
-12
未找到文件。
chapter_recurrent-neural-networks/gru-scratch.md
浏览文件 @
ecf14db2
...
...
@@ -81,7 +81,7 @@ print('vocab size:', vocab_size)
我们使用onehot来将字符索引表示成向量。
```
{.python .input}
```
{.python .input
n=2
}
def get_inputs(data):
return [nd.one_hot(X, vocab_size) for X in data.T]
```
...
...
@@ -90,7 +90,7 @@ def get_inputs(data):
以下部分对模型参数进行初始化。参数
`hidden_dim`
定义了隐含状态的长度。
```
{.python .input n=
5
}
```
{.python .input n=
3
}
import mxnet as mx
# 尝试使用GPU
...
...
@@ -135,7 +135,7 @@ def get_params():
我们将前面的模型公式翻译成代码。
```
{.python .input n=
6
}
```
{.python .input n=
4
}
def gru_rnn(inputs, H, *params):
# inputs: num_steps 个尺寸为 batch_size * vocab_size 矩阵
# H: 尺寸为 batch_size * hidden_dim 矩阵
...
...
@@ -156,7 +156,7 @@ def gru_rnn(inputs, H, *params):
下面我们开始训练模型。我们假定谱写歌词的前缀分别为“分开”、“不分开”和“战争中部队”。这里采用的是相邻批量采样实验门控循环单元谱写歌词。
```
{.python .input n=
7
}
```
{.python .input n=
5
}
seq1 = '分开'
seq2 = '不分开'
seq3 = '战争中部队'
...
...
chapter_recurrent-neural-networks/lstm-scratch.md
浏览文件 @
ecf14db2
...
...
@@ -88,7 +88,7 @@ print('vocab size:', vocab_size)
我们使用onehot来将字符索引表示成向量。
```
{.python .input}
```
{.python .input
n=2
}
def get_inputs(data):
return [nd.one_hot(X, vocab_size) for X in data.T]
```
...
...
@@ -97,7 +97,7 @@ def get_inputs(data):
以下部分对模型参数进行初始化。参数
`hidden_dim`
定义了隐含状态的长度。
```
{.python .input n=
5
}
```
{.python .input n=
3
}
import mxnet as mx
# 尝试使用GPU
...
...
@@ -150,7 +150,7 @@ def get_params():
我们将前面的模型公式翻译成代码。
```
{.python .input n=
6
}
```
{.python .input n=
4
}
def lstm_rnn(inputs, state_h, state_c, *params):
# inputs: num_steps 个尺寸为 batch_size * vocab_size 矩阵
# H: 尺寸为 batch_size * hidden_dim 矩阵
...
...
@@ -177,7 +177,7 @@ def lstm_rnn(inputs, state_h, state_c, *params):
下面我们开始训练模型。我们假定谱写歌词的前缀分别为“分开”、“不分开”和“战争中部队”。这里采用的是相邻批量采样实验门控循环单元谱写歌词。
```
{.python .input n=
7
}
```
{.python .input n=
5
}
seq1 = '分开'
seq2 = '不分开'
seq3 = '战争中部队'
...
...
chapter_recurrent-neural-networks/rnn-gluon.md
浏览文件 @
ecf14db2
...
...
@@ -27,7 +27,7 @@ with zipfile.ZipFile('../data/ptb.zip', 'r') as zin:
下面定义了
`Dictionary`
类来映射词语和索引。
```
{.python .input}
```
{.python .input
n=2
}
class Dictionary(object):
def __init__(self):
self.word_to_idx = {}
...
...
@@ -45,7 +45,7 @@ class Dictionary(object):
以下的
`Corpus`
类按照读取的文本数据集建立映射词语和索引的词典,并将文本转换成词语索引的序列。这样,每个文本数据集就变成了
`NDArray`
格式的整数序列。
```
{.python .input}
```
{.python .input
n=3
}
class Corpus(object):
def __init__(self, path):
self.dictionary = Dictionary()
...
...
@@ -77,7 +77,7 @@ class Corpus(object):
看一下词典的大小。
```
{.python .input}
```
{.python .input
n=4
}
data = '../data/ptb/ptb.'
corpus = Corpus(data)
vocab_size = len(corpus.dictionary)
...
...
@@ -88,7 +88,7 @@ vocab_size
我们可以定义一个循环神经网络模型库。这样就可以支持各种不同的循环神经网络模型了。
```
{.python .input}
```
{.python .input
n=5
}
class RNNModel(gluon.Block):
"""循环神经网络模型库"""
def __init__(self, mode, vocab_size, embed_dim, hidden_dim,
...
...
@@ -130,7 +130,7 @@ class RNNModel(gluon.Block):
## 定义参数
我们接着定义模型参数。我们选择使用ReLU为激活函数的循环神经网络为例。这里我们把
`epochs`
设为是为了演示方便。
我们接着定义模型参数。我们选择使用ReLU为激活函数的循环神经网络为例。这里我们把
`epochs`
设为
1
是为了演示方便。
## 多层循环神经网络
...
...
@@ -145,13 +145,13 @@ $$f(\mathbf{X}_t, \mathbf{H}_{t-1}) = \mathbf{H}_t$$
假设输入为第0层,输出为第$L+1$层,在一共$L$个隐含层的循环神经网络中,上式中可以拓展成以下的函数:
$$f(
\m
athbf{H}_t^{(l-1)},
\m
athbf{H}_{t-1}^{(l)}) =
\m
athbf{H}_t$$
$$f(
\m
athbf{H}_t^{(l-1)},
\m
athbf{H}_{t-1}^{(l)}) =
\m
athbf{H}_t
^{(l)}
$$
如下图所示。
![](
../img/multi-layer-rnn.svg
)
```
{.python .input}
```
{.python .input
n=6
}
model_name = 'rnn_relu'
embed_dim = 100
...
...
@@ -170,7 +170,7 @@ eval_period = 500
我们将数据进一步处理为便于相邻批量采样的格式。
```
{.python .input}
```
{.python .input
n=7
}
# 尝试使用GPU
import sys
sys.path.append('..')
...
...
@@ -206,7 +206,7 @@ def get_batch(source, i):
在模型训练的每次迭代中,当前批量序列的初始隐含状态来自上一个相邻批量序列的输出隐含状态。为了使模型参数的梯度计算只依赖当前的批量序列,从而减小每次迭代的计算开销,我们可以使用
`detach`
函数来将隐含状态从计算图分离出来。
```
{.python .input}
```
{.python .input
n=8
}
def detach(state):
if isinstance(state, (tuple, list)):
state = [i.detach() for i in state]
...
...
@@ -219,7 +219,7 @@ def detach(state):
和之前一样,我们定义模型评价函数。
```
{.python .input}
```
{.python .input
n=9
}
def model_eval(data_source):
total_L = 0.0
ntotal = 0
...
...
@@ -236,7 +236,7 @@ def model_eval(data_source):
最后,我们可以训练模型并在每个epoch评价模型在验证集上的结果。我们可以参考验证集上的结果调参。
```
{.python .input n=
3
}
```
{.python .input n=
10
}
def train():
for epoch in range(epochs):
total_L = 0.0
...
...
@@ -276,7 +276,7 @@ def train():
训练完模型以后,我们就可以在测试集上评价模型了。
```
{.python .input}
```
{.python .input
n=11
}
train()
test_L = model_eval(test_data)
print('Test loss %.2f, test perplexity %.2f' % (test_L, math.exp(test_L)))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录