.. _sec_lstm:
长短期记忆网络(LSTM)
======================
长期以来,隐变量模型存在着长期信息保存和短期输入缺失的问题。
解决这一问题的最早方法之一是长短期存储器(long short-term memory,LSTM)
:cite:`Hochreiter.Schmidhuber.1997`\ 。 它有许多与门控循环单元(
:numref:`sec_gru`\ )一样的属性。
有趣的是,长短期记忆网络的设计比门控循环单元稍微复杂一些,
却比门控循环单元早诞生了近20年。
门控记忆元
----------
可以说,长短期记忆网络的设计灵感来自于计算机的逻辑门。
长短期记忆网络引入了\ *记忆元*\ (memory
cell),或简称为\ *单元*\ (cell)。
有些文献认为记忆元是隐状态的一种特殊类型,
它们与隐状态具有相同的形状,其设计目的是用于记录附加的信息。
为了控制记忆元,我们需要许多门。
其中一个门用来从单元中输出条目,我们将其称为\ *输出门*\ (output
gate)。
另外一个门用来决定何时将数据读入单元,我们将其称为\ *输入门*\ (input
gate)。 我们还需要一种机制来重置单元的内容,由\ *遗忘门*\ (forget
gate)来管理, 这种设计的动机与门控循环单元相同,
能够通过专用机制决定什么时候记忆或忽略隐状态中的输入。
让我们看看这在实践中是如何运作的。
输入门、忘记门和输出门
~~~~~~~~~~~~~~~~~~~~~~
就如在门控循环单元中一样, 当前时间步的输入和前一个时间步的隐状态
作为数据送入长短期记忆网络的门中, 如 :numref:`lstm_0`\ 所示。
它们由三个具有sigmoid激活函数的全连接层处理,
以计算输入门、遗忘门和输出门的值。
因此,这三个门的值都在\ :math:`(0, 1)`\ 的范围内。
.. _lstm_0:
.. figure:: ../img/lstm-0.svg
长短期记忆模型中的输入门、遗忘门和输出门
我们来细化一下长短期记忆网络的数学表达。
假设有\ :math:`h`\ 个隐藏单元,批量大小为\ :math:`n`\ ,输入数为\ :math:`d`\ 。
因此,输入为\ :math:`\mathbf{X}_t \in \mathbb{R}^{n \times d}`\ ,
前一时间步的隐状态为\ :math:`\mathbf{H}_{t-1} \in \mathbb{R}^{n \times h}`\ 。
相应地,时间步\ :math:`t`\ 的门被定义如下:
输入门是\ :math:`\mathbf{I}_t \in \mathbb{R}^{n \times h}`\ ,
遗忘门是\ :math:`\mathbf{F}_t \in \mathbb{R}^{n \times h}`\ ,
输出门是\ :math:`\mathbf{O}_t \in \mathbb{R}^{n \times h}`\ 。
它们的计算方法如下:
.. math::
\begin{aligned}
\mathbf{I}_t &= \sigma(\mathbf{X}_t \mathbf{W}_{xi} + \mathbf{H}_{t-1} \mathbf{W}_{hi} + \mathbf{b}_i),\\
\mathbf{F}_t &= \sigma(\mathbf{X}_t \mathbf{W}_{xf} + \mathbf{H}_{t-1} \mathbf{W}_{hf} + \mathbf{b}_f),\\
\mathbf{O}_t &= \sigma(\mathbf{X}_t \mathbf{W}_{xo} + \mathbf{H}_{t-1} \mathbf{W}_{ho} + \mathbf{b}_o),
\end{aligned}
其中\ :math:`\mathbf{W}_{xi}, \mathbf{W}_{xf}, \mathbf{W}_{xo} \in \mathbb{R}^{d \times h}`
和\ :math:`\mathbf{W}_{hi}, \mathbf{W}_{hf}, \mathbf{W}_{ho} \in \mathbb{R}^{h \times h}`\ 是权重参数,
:math:`\mathbf{b}_i, \mathbf{b}_f, \mathbf{b}_o \in \mathbb{R}^{1 \times h}`\ 是偏置参数。
候选记忆元
~~~~~~~~~~
由于还没有指定各种门的操作,所以先介绍\ *候选记忆元*\ (candidate memory
cell) :math:`\tilde{\mathbf{C}}_t \in \mathbb{R}^{n \times h}`\ 。
它的计算与上面描述的三个门的计算类似,
但是使用\ :math:`\tanh`\ 函数作为激活函数,函数的值范围为\ :math:`(-1, 1)`\ 。
下面导出在时间步\ :math:`t`\ 处的方程:
.. math:: \tilde{\mathbf{C}}_t = \text{tanh}(\mathbf{X}_t \mathbf{W}_{xc} + \mathbf{H}_{t-1} \mathbf{W}_{hc} + \mathbf{b}_c),
其中\ :math:`\mathbf{W}_{xc} \in \mathbb{R}^{d \times h}`\ 和
:math:`\mathbf{W}_{hc} \in \mathbb{R}^{h \times h}`\ 是权重参数,
:math:`\mathbf{b}_c \in \mathbb{R}^{1 \times h}`\ 是偏置参数。
候选记忆元的如 :numref:`lstm_1`\ 所示。
.. _lstm_1:
.. figure:: ../img/lstm-1.svg
长短期记忆模型中的候选记忆元
记忆元
~~~~~~
在门控循环单元中,有一种机制来控制输入和遗忘(或跳过)。
类似地,在长短期记忆网络中,也有两个门用于这样的目的:
输入门\ :math:`\mathbf{I}_t`\ 控制采用多少来自\ :math:`\tilde{\mathbf{C}}_t`\ 的新数据,
而遗忘门\ :math:`\mathbf{F}_t`\ 控制保留多少过去的
记忆元\ :math:`\mathbf{C}_{t-1} \in \mathbb{R}^{n \times h}`\ 的内容。
使用按元素乘法,得出:
.. math:: \mathbf{C}_t = \mathbf{F}_t \odot \mathbf{C}_{t-1} + \mathbf{I}_t \odot \tilde{\mathbf{C}}_t.
如果遗忘门始终为\ :math:`1`\ 且输入门始终为\ :math:`0`\ ,
则过去的记忆元\ :math:`\mathbf{C}_{t-1}`
将随时间被保存并传递到当前时间步。 引入这种设计是为了缓解梯度消失问题,
并更好地捕获序列中的长距离依赖关系。
这样我们就得到了计算记忆元的流程图,如 :numref:`lstm_2`\ 。
.. _lstm_2:
.. figure:: ../img/lstm-2.svg
在长短期记忆网络模型中计算记忆元
隐状态
~~~~~~
最后,我们需要定义如何计算隐状态
:math:`\mathbf{H}_t \in \mathbb{R}^{n \times h}`\ ,
这就是输出门发挥作用的地方。
在长短期记忆网络中,它仅仅是记忆元的\ :math:`\tanh`\ 的门控版本。
这就确保了\ :math:`\mathbf{H}_t`\ 的值始终在区间\ :math:`(-1, 1)`\ 内:
.. math:: \mathbf{H}_t = \mathbf{O}_t \odot \tanh(\mathbf{C}_t).
只要输出门接近\ :math:`1`\ ,我们就能够有效地将所有记忆信息传递给预测部分,
而对于输出门接近\ :math:`0`\ ,我们只保留记忆元内的所有信息,而不需要更新隐状态。
:numref:`lstm_3`\ 提供了数据流的图形化演示。
.. _lstm_3:
.. figure:: ../img/lstm-3.svg
在长短期记忆模型中计算隐状态
从零开始实现
------------
现在,我们从零开始实现长短期记忆网络。 与
:numref:`sec_rnn_scratch`\ 中的实验相同, 我们首先加载时光机器数据集。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
from mxnet import np, npx
from mxnet.gluon import rnn
from d2l import mxnet as d2l
npx.set_np()
batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
import torch
from torch import nn
from d2l import torch as d2l
batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
import tensorflow as tf
from d2l import tensorflow as d2l
batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
import warnings
from d2l import paddle as d2l
warnings.filterwarnings("ignore")
import paddle
import paddle.nn.functional as Function
from paddle import nn
batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)
.. raw:: html
.. raw:: html
初始化模型参数
~~~~~~~~~~~~~~
接下来,我们需要定义和初始化模型参数。
如前所述,超参数\ ``num_hiddens``\ 定义隐藏单元的数量。
我们按照标准差\ :math:`0.01`\ 的高斯分布初始化权重,并将偏置项设为\ :math:`0`\ 。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def get_lstm_params(vocab_size, num_hiddens, device):
num_inputs = num_outputs = vocab_size
def normal(shape):
return np.random.normal(scale=0.01, size=shape, ctx=device)
def three():
return (normal((num_inputs, num_hiddens)),
normal((num_hiddens, num_hiddens)),
np.zeros(num_hiddens, ctx=device))
W_xi, W_hi, b_i = three() # 输入门参数
W_xf, W_hf, b_f = three() # 遗忘门参数
W_xo, W_ho, b_o = three() # 输出门参数
W_xc, W_hc, b_c = three() # 候选记忆元参数
# 输出层参数
W_hq = normal((num_hiddens, num_outputs))
b_q = np.zeros(num_outputs, ctx=device)
# 附加梯度
params = [W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc,
b_c, W_hq, b_q]
for param in params:
param.attach_grad()
return params
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def get_lstm_params(vocab_size, num_hiddens, device):
num_inputs = num_outputs = vocab_size
def normal(shape):
return torch.randn(size=shape, device=device)*0.01
def three():
return (normal((num_inputs, num_hiddens)),
normal((num_hiddens, num_hiddens)),
torch.zeros(num_hiddens, device=device))
W_xi, W_hi, b_i = three() # 输入门参数
W_xf, W_hf, b_f = three() # 遗忘门参数
W_xo, W_ho, b_o = three() # 输出门参数
W_xc, W_hc, b_c = three() # 候选记忆元参数
# 输出层参数
W_hq = normal((num_hiddens, num_outputs))
b_q = torch.zeros(num_outputs, device=device)
# 附加梯度
params = [W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc,
b_c, W_hq, b_q]
for param in params:
param.requires_grad_(True)
return params
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def get_lstm_params(vocab_size, num_hiddens):
num_inputs = num_outputs = vocab_size
def normal(shape):
return tf.Variable(tf.random.normal(shape=shape, stddev=0.01,
mean=0, dtype=tf.float32))
def three():
return (normal((num_inputs, num_hiddens)),
normal((num_hiddens, num_hiddens)),
tf.Variable(tf.zeros(num_hiddens), dtype=tf.float32))
W_xi, W_hi, b_i = three() # 输入门参数
W_xf, W_hf, b_f = three() # 遗忘门参数
W_xo, W_ho, b_o = three() # 输出门参数
W_xc, W_hc, b_c = three() # 候选记忆元参数
# 输出层参数
W_hq = normal((num_hiddens, num_outputs))
b_q = tf.Variable(tf.zeros(num_outputs), dtype=tf.float32)
# 附加梯度
params = [W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc,
b_c, W_hq, b_q]
return params
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def get_lstm_params(vocab_size, num_hiddens):
num_inputs = num_outputs = vocab_size
def normal(shape):
return paddle.randn(shape=shape)*0.01
def three():
return (normal((num_inputs, num_hiddens)),
normal((num_hiddens, num_hiddens)),
paddle.zeros([num_hiddens]))
W_xi, W_hi, b_i = three() # 输入门参数
W_xf, W_hf, b_f = three() # 遗忘门参数
W_xo, W_ho, b_o = three() # 输出门参数
W_xc, W_hc, b_c = three() # 候选记忆元参数
# 输出层参数
W_hq = normal((num_hiddens, num_outputs))
b_q = paddle.zeros([num_outputs])
# 附加梯度
params = [W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc,
b_c, W_hq, b_q]
for param in params:
param.stop_gradient = False
return params
.. raw:: html
.. raw:: html
定义模型
~~~~~~~~
在初始化函数中, 长短期记忆网络的隐状态需要返回一个\ *额外*\ 的记忆元,
单元的值为0,形状为(批量大小,隐藏单元数)。
因此,我们得到以下的状态初始化。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def init_lstm_state(batch_size, num_hiddens, device):
return (np.zeros((batch_size, num_hiddens), ctx=device),
np.zeros((batch_size, num_hiddens), ctx=device))
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def init_lstm_state(batch_size, num_hiddens, device):
return (torch.zeros((batch_size, num_hiddens), device=device),
torch.zeros((batch_size, num_hiddens), device=device))
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def init_lstm_state(batch_size, num_hiddens):
return (tf.zeros(shape=(batch_size, num_hiddens)),
tf.zeros(shape=(batch_size, num_hiddens)))
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def init_lstm_state(batch_size, num_hiddens):
return (paddle.zeros([batch_size, num_hiddens]),
paddle.zeros([batch_size, num_hiddens]))
.. raw:: html
.. raw:: html
实际模型的定义与我们前面讨论的一样: 提供三个门和一个额外的记忆元。
请注意,只有隐状态才会传递到输出层,
而记忆元\ :math:`\mathbf{C}_t`\ 不直接参与输出计算。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def lstm(inputs, state, params):
[W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc, b_c,
W_hq, b_q] = params
(H, C) = state
outputs = []
for X in inputs:
I = npx.sigmoid(np.dot(X, W_xi) + np.dot(H, W_hi) + b_i)
F = npx.sigmoid(np.dot(X, W_xf) + np.dot(H, W_hf) + b_f)
O = npx.sigmoid(np.dot(X, W_xo) + np.dot(H, W_ho) + b_o)
C_tilda = np.tanh(np.dot(X, W_xc) + np.dot(H, W_hc) + b_c)
C = F * C + I * C_tilda
H = O * np.tanh(C)
Y = np.dot(H, W_hq) + b_q
outputs.append(Y)
return np.concatenate(outputs, axis=0), (H, C)
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def lstm(inputs, state, params):
[W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc, b_c,
W_hq, b_q] = params
(H, C) = state
outputs = []
for X in inputs:
I = torch.sigmoid((X @ W_xi) + (H @ W_hi) + b_i)
F = torch.sigmoid((X @ W_xf) + (H @ W_hf) + b_f)
O = torch.sigmoid((X @ W_xo) + (H @ W_ho) + b_o)
C_tilda = torch.tanh((X @ W_xc) + (H @ W_hc) + b_c)
C = F * C + I * C_tilda
H = O * torch.tanh(C)
Y = (H @ W_hq) + b_q
outputs.append(Y)
return torch.cat(outputs, dim=0), (H, C)
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def lstm(inputs, state, params):
W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc, b_c, W_hq, b_q = params
(H, C) = state
outputs = []
for X in inputs:
X=tf.reshape(X,[-1,W_xi.shape[0]])
I = tf.sigmoid(tf.matmul(X, W_xi) + tf.matmul(H, W_hi) + b_i)
F = tf.sigmoid(tf.matmul(X, W_xf) + tf.matmul(H, W_hf) + b_f)
O = tf.sigmoid(tf.matmul(X, W_xo) + tf.matmul(H, W_ho) + b_o)
C_tilda = tf.tanh(tf.matmul(X, W_xc) + tf.matmul(H, W_hc) + b_c)
C = F * C + I * C_tilda
H = O * tf.tanh(C)
Y = tf.matmul(H, W_hq) + b_q
outputs.append(Y)
return tf.concat(outputs, axis=0), (H,C)
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
def lstm(inputs, state, params):
[W_xi, W_hi, b_i, W_xf, W_hf, b_f, W_xo, W_ho, b_o, W_xc, W_hc, b_c,
W_hq, b_q] = params
(H, C) = state
outputs = []
for X in inputs:
I = Function.sigmoid((X @ W_xi) + (H @ W_hi) + b_i)
F = Function.sigmoid((X @ W_xf) + (H @ W_hf) + b_f)
O = Function.sigmoid((X @ W_xo) + (H @ W_ho) + b_o)
C_tilda = paddle.tanh((X @ W_xc) + (H @ W_hc) + b_c)
C = F * C + I * C_tilda
H = O * paddle.tanh(C)
Y = (H @ W_hq) + b_q
outputs.append(Y)
return paddle.concat(outputs, axis=0), (H, C)
.. raw:: html
.. raw:: html
训练和预测
~~~~~~~~~~
让我们通过实例化 :numref:`sec_rnn_scratch`\ 中
引入的\ ``RNNModelScratch``\ 类来训练一个长短期记忆网络, 就如我们在
:numref:`sec_gru`\ 中所做的一样。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
vocab_size, num_hiddens, device = len(vocab), 256, d2l.try_gpu()
num_epochs, lr = 500, 1
model = d2l.RNNModelScratch(len(vocab), num_hiddens, device, get_lstm_params,
init_lstm_state, lstm)
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
perplexity 1.1, 8639.9 tokens/sec on gpu(0)
time travelleryou can show black is white by argument said filby
travelleryou can show black is white by argument said filby
.. figure:: output_lstm_86eb9f_63_1.svg
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
vocab_size, num_hiddens, device = len(vocab), 256, d2l.try_gpu()
num_epochs, lr = 500, 1
model = d2l.RNNModelScratch(len(vocab), num_hiddens, device, get_lstm_params,
init_lstm_state, lstm)
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
perplexity 1.3, 17736.0 tokens/sec on cuda:0
time traveller for so it will leong go it we melenot ir cove i s
traveller care be can so i ngrecpely as along the time dime
.. figure:: output_lstm_86eb9f_66_1.svg
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
vocab_size, num_hiddens, device_name = len(vocab), 256, d2l.try_gpu()._device_name
num_epochs, lr = 500, 1
strategy = tf.distribute.OneDeviceStrategy(device_name)
with strategy.scope():
model = d2l.RNNModelScratch(len(vocab), num_hiddens, init_lstm_state, lstm, get_lstm_params)
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, strategy)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
perplexity 1.1, 4257.0 tokens/sec on /GPU:0
time traveller a feredat andllss the burthed mo an whatthill pea
traveller a sorie dy ne it be mome about in time for instan
.. figure:: output_lstm_86eb9f_69_1.svg
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
vocab_size, num_hiddens, device = len(vocab), 256, d2l.try_gpu()
num_epochs, lr = 500, 1.0
model = d2l.RNNModelScratch(len(vocab), num_hiddens, get_lstm_params,
init_lstm_state, lstm)
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
困惑度 1.1, 12455.7 词元/秒 Place(gpu:0)
time traveller for so it will be convenient to speak of himwas e
traveller for go the brught is foutthe tomered s smemedty i
.. figure:: output_lstm_86eb9f_72_1.svg
.. raw:: html
.. raw:: html
简洁实现
--------
使用高级API,我们可以直接实例化\ ``LSTM``\ 模型。
高级API封装了前文介绍的所有配置细节。 这段代码的运行速度要快得多,
因为它使用的是编译好的运算符而不是Python来处理之前阐述的许多细节。
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
lstm_layer = rnn.LSTM(num_hiddens)
model = d2l.RNNModel(lstm_layer, len(vocab))
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
perplexity 1.1, 174969.3 tokens/sec on gpu(0)
time traveller held in his hand was a lige and there are able to
traveller ther things said the mecan lo nom med ce time in
.. figure:: output_lstm_86eb9f_78_1.svg
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
num_inputs = vocab_size
lstm_layer = nn.LSTM(num_inputs, num_hiddens)
model = d2l.RNNModel(lstm_layer, len(vocab))
model = model.to(device)
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
perplexity 1.1, 234815.0 tokens/sec on cuda:0
time traveller for so it will be convenient to speak of himwas e
travelleryou can show black is white by argument said filby
.. figure:: output_lstm_86eb9f_81_1.svg
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
lstm_cell = tf.keras.layers.LSTMCell(num_hiddens,
kernel_initializer='glorot_uniform')
lstm_layer = tf.keras.layers.RNN(lstm_cell, time_major=True,
return_sequences=True, return_state=True)
device_name = d2l.try_gpu()._device_name
strategy = tf.distribute.OneDeviceStrategy(device_name)
with strategy.scope():
model = d2l.RNNModel(lstm_layer, vocab_size=len(vocab))
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, strategy)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
perplexity 1.0, 9251.7 tokens/sec on /GPU:0
time travelleryou can show black is white by argument said filby
travelleryou can show black is white by argument said filby
.. figure:: output_lstm_86eb9f_84_1.svg
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
num_inputs = vocab_size
lstm_layer = nn.LSTM(num_inputs, num_hiddens, time_major=True)
model = d2l.RNNModel(lstm_layer, len(vocab))
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
困惑度 1.0, 153769.5 词元/秒 Place(gpu:0)
time traveller for so it will be convenient to speak of himwas e
traveller abcent of the battmestime frale has is intamm gra
.. figure:: output_lstm_86eb9f_87_1.svg
.. raw:: html
.. raw:: html
长短期记忆网络是典型的具有重要状态控制的隐变量自回归模型。
多年来已经提出了其许多变体,例如,多层、残差连接、不同类型的正则化。
然而,由于序列的长距离依赖性,训练长短期记忆网络
和其他序列模型(例如门控循环单元)的成本是相当高的。
在后面的内容中,我们将讲述更高级的替代模型,如Transformer。
小结
----
- 长短期记忆网络有三种类型的门:输入门、遗忘门和输出门。
- 长短期记忆网络的隐藏层输出包括“隐状态”和“记忆元”。只有隐状态会传递到输出层,而记忆元完全属于内部信息。
- 长短期记忆网络可以缓解梯度消失和梯度爆炸。
练习
----
1. 调整和分析超参数对运行时间、困惑度和输出顺序的影响。
2. 如何更改模型以生成适当的单词,而不是字符序列?
3. 在给定隐藏层维度的情况下,比较门控循环单元、长短期记忆网络和常规循环神经网络的计算成本。要特别注意训练和推断成本。
4. 既然候选记忆元通过使用\ :math:`\tanh`\ 函数来确保值范围在\ :math:`(-1,1)`\ 之间,那么为什么隐状态需要再次使用\ :math:`\tanh`\ 函数来确保输出值范围在\ :math:`(-1,1)`\ 之间呢?
5. 实现一个能够基于时间序列进行预测而不是基于字符序列进行预测的长短期记忆网络模型。
.. raw:: html
.. raw:: html
`Discussions `__
.. raw:: html
.. raw:: html
`Discussions `__
.. raw:: html
.. raw:: html
`Discussions `__
.. raw:: html
.. raw:: html