动手学深度学习—循环神经网络RNN详解

循环神经网络

在这里插入图片描述

循环神经网络的步骤:

  1. 处理数据 将数据按照批量大小和时间步数进行处理,最后得到迭代器,即每一个迭代的大小是批量大小×时间步数,迭代次数根据整个数据的大小决定,最后得出处理的数据(参照第三节语言模型和数据集)
  2. 初始化模型参数 虽然上面图片显示的是由很多个输入-隐藏层-输出组成的 其实权重参数和偏置参数都是同一个 隐藏层的计算是rnn的使用 输入的值是(批量大小,28个vocab)输出也是
  3. 循环神经网络模型 即设置公式 可以是rnn LSTM GRU等 所以上图有多少列一般是指有多少个时间 步数(仅限于训练时)就是代码中循环的次数 for X in inputs:每次循环使用的都是同一个参数 训练时训练的也是同一个参数 得到的只是一组相当于一列参数的值
def rnn(inputs, state, params):
    # inputs的形状:(时间步数量,批量大小,词表大小)
    W_xh, W_hh, b_h, W_hq, b_q = params
    H, = state
    outputs = []
    # X的形状:(批量大小,词表大小)
    for X in inputs:  # 一个时间步一个时间步的计算  
        H = torch.tanh(torch.mm(X, W_xh) + torch.mm(H, W_hh) + b_h)
        Y = torch.mm(H, W_hq) + b_q
        outputs.append(Y)
    return torch.cat(outputs, dim=0), (H,)
  1. 预测 虽然初始化模型参数是根据时间步数决定的有多少列rnn 但其实就是循环了时间步数次,当在进行预测的时候 直接设置需要预测的步数即可 就是循环的次数
  2. 训练 训练的时候就是时间步数是多少就会有多少列 但是使用时 就是需要多少有多少次循环 有多少列 是为了前面的隐藏层的信息 隐藏层的信息越多 对后面的结果越有益处

1. 序列模型

1.1 马尔可夫模型

假设已知 τ τ τ个序列预测下一个或下几个数据(假设当前数据只跟前 τ τ τ个数据有关)
在这里插入图片描述

1.2 潜变量模型

假设一个潜变量h h t h_t ht来表示过去信息 h t = f ( x 1 , x 2 , . . . , x t − 1 ) h_t=f(x_1,x_2,...,x_{t-1}) ht=f(x1,x2,...,xt1)这样 x t = p ( x t ∣ h t ) x_t=p(x_t|h_t) xt=p(xtht)

在这里插入图片描述

2. 文本预处理

解析文本的步骤:

1.将文本作为字符串加载到内存中

2.将字符串拆分为词元(如单词和字符)

3.建立一个词表,将拆分的词元映射到数字索引

4.将文本转换为数字索引序列,方便模型操作

2.1 读取数据集

#@save
d2l.DATA_HUB['time_machine'] = (d2l.DATA_URL + 'timemachine.txt',
                                '090b5e7e70c295757f55df93cb0a180b9691891a')

def read_time_machine():  #@save
    """将时间机器数据集加载到文本行的列表中"""
    with open(d2l.download('time_machine'), 'r') as f:
        lines = f.readlines()
    return [re.sub('[^A-Za-z]+', ' ', line).strip().lower() for line in lines]

lines = read_time_machine()
print(f'# 文本总行数: {len(lines)}')
print(lines[0])
print(lines[10])

2.2 词元化

下面的tokenize函数将文本行列表(lines)作为输入,列表中的每个元素是一个文本序列(如一条文本行)。每个文本序列又被拆分成一个词元列表,词元(token)是文本的基本单位。最后,返回一个由词元列表组成的列表,每个词元都是一个字符串。

def tokenize(lines, token='word'):  #@save
    """将文本行拆分为单词或字符词元"""
    if token == 'word':
        return [line.split() for line in lines]
    elif token == 'char':
        return [list(line) for line in lines]
    else:
        print('错误:未知词元类型:' + token)

tokens = tokenize(lines)
for i in range(11):
    print(tokens[i])

2.3 词表

词元的类型是字符串,而模型需要的输入是数字,因此这种类型不方便模型使用。现在,构建一个字典,通常也叫做词表(vocabulary),用来将字符串类型的词元映射到从0开始的数字索引中。我们先将训练集中的所有文档合并在一起,对它们的唯一词元进行统计,得到的统计结果称为语料(corpus)。然后,根据每个唯一词元的出现频率,为其分配一个数字索引。很少出现的词元通常被移除,可以降低复杂性。另外,语料库中不存在或已删除的任何词元都将映射到一个特定的未知词元“”。我们可以选择增加一个列表,用于保存那些被保留的词元, 例如:填充词元(“”); 序列开始词元(“”); 序列结束词元(“”)。

(理解:将文本中的单词进行编号 然后用编号表示文本 语料库就是所有的单词 但是不重复 词表就是记录每一个单词的索引的 相当于密码本)

class Vocab:  #@save
    """文本词表"""
    def __init__(self, tokens=None, min_freq=0, reserved_tokens=None):
        if tokens is None:
            tokens = []
        if reserved_tokens is None:
            reserved_tokens = []
        # 按出现频率排序
        counter = count_corpus(tokens)
        self._token_freqs = sorted(counter.items(), key=lambda x: x[1],
                                   reverse=True)
        # 未知词元的索引为0
        self.idx_to_token = ['<unk>'] + reserved_tokens
        self.token_to_idx = {token: idx
                             for idx, token in enumerate(self.idx_to_token)}
        for token, freq in self._token_freqs:
            if freq < min_freq:
                break
            if token not in self.token_to_idx:
                self.idx_to_token.append(token)
                self.token_to_idx[token] = len(self.idx_to_token) - 1

    def __len__(self):
        return len(self.idx_to_token)

    def __getitem__(self, tokens):
        if not isinstance(tokens, (list, tuple)):
            return self.token_to_idx.get(tokens, self.unk)
        return [self.__getitem__(token) for token in tokens]

    def to_tokens(self, indices):
        if not isinstance(indices, (list, tuple)):
            return self.idx_to_token[indices]
        return [self.idx_to_token[index] for index in indices]

    @property
    def unk(self):  # 未知词元的索引为0
        return 0

    @property
    def token_freqs(self):
        return self._token_freqs

def count_corpus(tokens):  #@save
    """统计词元的频率"""
    # 这里的tokens是1D列表或2D列表
    if len(tokens) == 0 or isinstance(tokens[0], list):
        # 将词元列表展平成一个列表
        tokens = [token for line in tokens for token in line]
    return collections.Counter(tokens)

我们首先使用时光机器数据集作为语料库来构建词表,然后打印前几个高频词元及其索引。

vocab = Vocab(tokens)
print(list(vocab.token_to_idx.items())[:10])

现在,我们可以将每一条文本行转换成一个数字索引列表。

for i in [0, 10]:
    print('文本:', tokens[i])
    print('索引:', vocab[tokens[i]])

输出:

文本: ['the', 'time', 'machine', 'by', 'h', 'g', 'wells']
索引: [1, 19, 50, 40, 2183, 2184, 400]
文本: ['twinkled', 'and', 'his', 'usually', 'pale', 'face', 'was', 'flushed', 'and', 'animated', 'the']
索引: [2186, 3, 25, 1044, 362, 113, 7, 1421, 3, 1045, 1]

2.4 整合所有功能

在使用上述函数时,我们将所有功能打包到load_corpus_time_machine函数中, 该函数返回corpus(词元索引列表)和vocab(时光机器语料库的词表)。 我们在这里所做的改变是:

  1. 为了简化后面章节中的训练,我们使用字符(而不是单词)实现文本词元化;
  2. 时光机器数据集中的每个文本行不一定是一个句子或一个段落,还可能是一个单词,因此返回的corpus仅处理为单个列表,而不是使用多词元列表构成的一个列表。
def load_corpus_time_machine(max_tokens=-1):  #@save
    """返回时光机器数据集的词元索引列表和词表"""
    lines = read_time_machine()
    tokens = tokenize(lines, 'char')  # 将文本按字符进行了分词处理
    vocab = Vocab(tokens)
    # 因为时光机器数据集中的每个文本行不一定是一个句子或一个段落,
    # 所以将所有文本行展平到一个列表中
    corpus = [vocab[token] for line in tokens for token in line] # 这是一个列表推导式,用于将文本词元转换为词表中的索引。它遍历了 tokens 中的每一行和每一个词元,将词元映射到词表 vocab 中的索引,并存储在 corpus 中。
    if max_tokens > 0:
        corpus = corpus[:max_tokens]
    return corpus, vocab

corpus, vocab = load_corpus_time_machine()
len(corpus), len(vocab)

for i in [0, 10]:
    print('文本:', tokens[i])
    print('索引:', vocab[tokens[i]])

输出:

(170580, 28)

文本: ['the', 'time', 'machine', 'by', 'h', 'g', 'wells']
索引: [0, 0, 0, 0, 9, 18, 0]
文本: ['twinkled', 'and', 'his', 'usually', 'pale', 'face', 'was', 'flushed', 'and', 'animated', 'the']
索引: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

这部分就是将上面的功能整合一个示例。使用26个字母表示词元 不是26个字母中的记录为0

3. 语言模型和数据集

在实际使用中我们希望给出前几个词元即可预测后面的词元。假设 长度为T的文本序列中的词元依次为 x 1 , x 2 , … , x T x_1, x_2, \ldots, x_T x1,x2,,xT。于是, x t ( 1 ≤ t ≤ T ) x_t(1 \leq t \leq T) xt(1tT)可以被认为是文本序列在时间步长 t t t处的观测或标签。在给定这样的文本序列时,语言模型的目标是估计序列的联合概率 P ( x 1 , x 2 , … … , x T ) P(x_1,x_2,……,x_T) P(x1,x2,……,xT)

例如,只需要一次抽取一个词元 x t ∼ P ( x t ∣ x t − 1 , … , x 1 ) x_t \sim P(x_t \mid x_{t-1}, \ldots, x_1) xtP(xtxt1,,x1),一个理想的语言模型就能够基于模型本身生成自然文本。从这样的模型中提取的文本都将作为自然语言(如英文文本)来传递。只需要基于前面的对话片段中的文本,就足以生成一个有意义的对话。

3.1 学习语言模型

问题是:如何对一个文档甚至是一个词元序列进行建模。假设在单词级别对文本数据进行词元化,我们可以依靠在第一节中对序列模型的分析。让我们从基本概率规则开始:

P ( x 1 , x 2 , … , x T ) = ∏ t = 1 T P ( x t ∣ x 1 , … , x t − 1 ) . P(x_1, x_2, \ldots, x_T) = \prod_{t=1}^T P(x_t \mid x_1, \ldots, x_{t-1}). P(x1,x2,,xT)=t=1TP(xtx1,,xt1).

例如,包含了四个单词的一个文本序列的概率是:

P(deep,learning,is,fun)=P(deep)P(learning∣deep)P(is∣deep,learning)P(fun∣deep,learning,is).

为了训练语言模型,我们需要计算单词的概率, 以及给定前面几个单词后出现某个单词的条件概率。 这些概率本质上就是语言模型的参数。

训练集中词的概率可以根据给定词的相对词频来计算。例如,我们可以先统计 x 1 x_1 x1出现的频次 n ( x 1 ) n(x_1) n(x1)然后再统计 x 1 , x 2 x_1,x_2 x1,x2出现的频次 n ( x 1 , x 2 ) n(x_1,x_2) n(x1,x2),但是由于有的时候固定的两个单词连续起来的频次非常少,所以我们需要将这些单词组合记为非零计数,否则将无法在语言模型中使用它们。

一种常见的策略是执行某种形式的拉普拉斯平滑,具体方法是在所有计数中添加一个小常量。用 n n n表示训练集中的单词总数,用 m m m表示唯一单词的数量。

P ^ ( x ) = n ( x ) + ϵ 1 / m n + ϵ 1 , P ^ ( x ′ ∣ x ) = n ( x , x ′ ) + ϵ 2 P ^ ( x ′ ) n ( x ) + ϵ 2 , P ^ ( x ′ ′ ∣ x , x ′ ) = n ( x , x ′ , x ′ ′ ) + ϵ 3 P ^ ( x ′ ′ ) n ( x , x ′ ) + ϵ 3 . \begin{split}\begin{aligned} \hat{P}(x) & = \frac{n(x) + \epsilon_1/m}{n + \epsilon_1}, \\ \hat{P}(x' \mid x) & = \frac{n(x, x') + \epsilon_2 \hat{P}(x')}{n(x) + \epsilon_2}, \\ \hat{P}(x'' \mid x,x') & = \frac{n(x, x',x'') + \epsilon_3 \hat{P}(x'')}{n(x, x') + \epsilon_3}. \end{aligned}\end{split} P^(x)P^(xx)P^(x′′x,x)=n+ϵ1n(x)+ϵ1/m,=n(x)+ϵ2n(x,x)+ϵ2P^(x),=n(x,x)+ϵ3n(x,x,x′′)+ϵ3P^(x′′).

但是这样的操作很容易使得模型变得无效

3.2 马尔可夫模型与 n n n元语法

将第一节对马尔可夫模型的讨论应用于语言建模。如果 P ( x t + 1 ∣ x t , … … , x 1 ) = P ( x t + 1 ∣ x t ) P(x_{t+1}|x_t,……,x_1)=P(x_{t+1}|x_t) P(xt+1xt,……,x1)=P(xt+1xt),则序列上的分布满足一阶马尔可夫性质。阶数越高,对应的依赖关系就越长。

一元语法 ( u n i g r a m ) P ^ ( x ) = n ( x ) + ϵ 1 / m n + ϵ 1 , 二元语法 ( b i g r a m ) P ^ ( x ′ ∣ x ) = n ( x , x ′ ) + ϵ 2 P ^ ( x ′ ) n ( x ) + ϵ 2 , 三元语法 ( t r i g r a m ) P ^ ( x ′ ′ ∣ x , x ′ ) = n ( x , x ′ , x ′ ′ ) + ϵ 3 P ^ ( x ′ ′ ) n ( x , x ′ ) + ϵ 3 . \begin{split}\begin{aligned} 一元语法(unigram) \hat{P}(x) & = \frac{n(x) + \epsilon_1/m}{n + \epsilon_1}, \\ 二元语法(bigram)\hat{P}(x' \mid x) & = \frac{n(x, x') + \epsilon_2 \hat{P}(x')}{n(x) + \epsilon_2}, \\ 三元语法(trigram)\hat{P}(x'' \mid x,x') & = \frac{n(x, x',x'') + \epsilon_3 \hat{P}(x'')}{n(x, x') + \epsilon_3}. \end{aligned}\end{split} 一元语法(unigram)P^(x)二元语法(bigram)P^(xx)三元语法(trigram)P^(x′′x,x)=n+ϵ1n(x)+ϵ1/m,=n(x)+ϵ2n(x,x)+ϵ2P^(x),=n(x,x)+ϵ3n(x,x,x′′)+ϵ3P^(x′′).

其中一元语法中对应马尔科夫中的 τ = 0 \tau=0 τ=0,二元语法中对应马尔科夫中的 τ = 1 \tau=1 τ=1,三元语法对应的马尔可夫中的 τ = 2 \tau=2 τ=2以此类推。

3.3 自然语言统计

import random
import torch
from d2l import torch as d2l

tokens = d2l.tokenize(d2l.read_time_machine())
# 因为每个文本行不一定是一个句子或一个段落,因此我们把所有文本行拼接到一起
corpus = [token for line in tokens for token in line]
vocab = d2l.Vocab(corpus)
vocab.token_freqs[:10]
[('the', 2261),
 ('i', 1267),
 ('and', 1245),
 ('of', 1155),
 ('a', 816),
 ('to', 695),
 ('was', 552),
 ('in', 541),
 ('that', 443),
 ('my', 440)]

最流行的词看起来很无聊, 这些词通常被称为停用词(stop words),因此可以被过滤掉。 尽管如此,它们本身仍然是有意义的,我们仍然会在模型中使用它们。 此外,还有个明显的问题是词频衰减的速度相当地快。 例如,最常用单词的词频对比,第10个还不到第1个的1/5。 为了更好地理解,我们可以画出的词频图:

freqs = [freq for token, freq in vocab.token_freqs]
d2l.plot(freqs, xlabel='token: x', ylabel='frequency: n(x)',
         xscale='log', yscale='log')

在这里插入图片描述

通过此图我们可以发现:词频以一种明确的方式迅速衰减。将前几个单词作为例外消除后,剩余的所有单词大致遵循双对数坐标图上的一条直线。这意味着单词的频率满足齐普夫定律,即第 i i i个最常用的单词频率 n i n_i ni为:

n i ∝ 1 i α , n_i \propto \frac{1}{i^\alpha}, niiα1,

等价于 log ⁡ n i = − α log ⁡ i + c , \log n_i = -\alpha \log i + c, logni=αlogi+c,

其中 α \alpha α是刻画分布的指数, c c c是常数。这告诉我们想要通过计数统计和平滑建模单词是不可行的,因为这样建模的结果会大大高估尾部单词的频率,也就是所谓的不常用单词。那么其他的词元组合,比如二元语法、三元语法大概率也会出现相同的问题。

bigram_tokens = [pair for pair in zip(corpus[:-1], corpus[1:])]
bigram_vocab = d2l.Vocab(bigram_tokens)
bigram_vocab.token_freqs[:10]

输出:

[(('of', 'the'), 309),
 (('in', 'the'), 169),
 (('i', 'had'), 130),
 (('i', 'was'), 112),
 (('and', 'the'), 109),
 (('the', 'time'), 102),
 (('it', 'was'), 99),
 (('to', 'the'), 85),
 (('as', 'i'), 78),
 (('of', 'a'), 73)]

在十个最频繁的词对中,有九个是由两个停用词组成的, 只有一个与“the time”有关。 我们再进一步看看三元语法的频率是否表现出相同的行为方式。

trigram_tokens = [triple for triple in zip(corpus[:-2], corpus[1:-1], corpus[2:])]
trigram_vocab = d2l.Vocab(trigram_tokens)
trigram_vocab.token_freqs[:10]

最后,我们直观地对比三种模型中的词元频率:一元语法、二元语法和三元语法。

bigram_freqs = [freq for token, freq in bigram_vocab.token_freqs]
trigram_freqs = [freq for token, freq in trigram_vocab.token_freqs]
d2l.plot([freqs, bigram_freqs, trigram_freqs], xlabel='token: x',
         ylabel='frequency: n(x)', xscale='log', yscale='log',
         legend=['unigram', 'bigram', 'trigram'])

在这里插入图片描述

  1. 除了一元语法词,单词序列似乎也遵循齐普夫定律,尽管公式 n i ∝ 1 i α n_i \propto \frac{1}{i^\alpha} niiα1中的指数 α \alpha α更小(指数的大小受序列长度的影响)
  2. 词表中 n n n元组的数量并没有那么大,这说明语言中存在相当多的结构,这些结构给了我们应用模型的希望
  3. 很多 n n n元组很少出现,这使得拉普拉斯平滑非常不适合语言建模。作为代替,我们将使用基于深度学习的模型。

3.4 读取长序列数据

由于序列数据本质上是连续的,因此我们在处理数据时需要解决这个问题。我们需要将一整个文本序列拆分成多个短的序列。但是有些序列本质上是连续的,那我们该如何拆分呢,还要保证尽量能找到所有连续的序列 ,即如何随机生成一个小批量数据的特征和标签以供读取。

首先,由于文本是任意长的,例如整本《时光机器》(the time machine),于是任意长的序列可以被我们划分为具有相同时间步数的子序列。当训练神经网络时,这样的小批量子序列将被输入到模型中。假设网络一次只处理具有 n n n个时间步的子序列。下图画出了从原始文本序列获得子序列的的所有不同方式,其中 n = 5 n=5 n=5,并且每个时间步的词元对应于一个字符。

在这里插入图片描述

那我们应该选择图上的哪一个呢?其实,哪一个都一样好。然而,我们只选择一个偏移量,在训练的时候是有限的,将某些连续的序列拆开就不能查找到了。因此,我们可以从随机偏移量开始划分序列,以同时获得覆盖性和随机性。下面,将描述如何实现随机采样和顺序分区策略。

3.4.1 随机采样

在随机采样中,每个样本都是在原始的长序列上任意捕获的子序列。在迭代过程中,来自两个相邻的、随机的、小批量的子序列不一定在原始序列上相邻对于语言建模,目标是基于到目前为止我们看到的词元来预测下一个词元,因此标签是移位了一个词元的原始序列。

下面的代码每次可以从数据中随机生成一个小批量。在这里,参数batch_size指定了每个小批量中子序列样本的数目,参数num_steps是每个子序列中预定义的时间步数。

def seq_data_iter_random(corpus, batch_size, num_steps):  #@save
    """使用随机抽样生成一个小批量子序列"""
    # 从随机偏移量开始对序列进行分区,随机范围包括num_steps-1
    corpus = corpus[random.randint(0, num_steps - 1):]
    # 减去1,是因为我们需要考虑标签
    num_subseqs = (len(corpus) - 1) // num_steps
    # 长度为num_steps的子序列的起始索引
    initial_indices = list(range(0, num_subseqs * num_steps, num_steps))
    # 在随机抽样的迭代过程中,
    # 来自两个相邻的、随机的、小批量中的子序列不一定在原始序列上相邻
    random.shuffle(initial_indices)

    def data(pos):
        # 返回从pos位置开始的长度为num_steps的序列
        return corpus[pos: pos + num_steps]

    num_batches = num_subseqs // batch_size
    for i in range(0, batch_size * num_batches, batch_size):
        # 在这里,initial_indices包含子序列的随机起始索引
        initial_indices_per_batch = initial_indices[i: i + batch_size]
        X = [data(j) for j in initial_indices_per_batch]
        Y = [data(j + 1) for j in initial_indices_per_batch]
        yield torch.tensor(X), torch.tensor(Y)

下面我们生成一个从0到34的序列。 假设批量大小为2,时间步数为5,这意味着可以生成 ⌊(35−1)/5⌋=6个“特征-标签”子序列对。 如果设置小批量大小为2,我们只能得到3个小批量。

my_seq = list(range(35))
for X, Y in seq_data_iter_random(my_seq, batch_size=2, num_steps=5):
    print('X: ', X, '\nY:', Y)

输出:

X:  tensor([[13, 14, 15, 16, 17],
        [28, 29, 30, 31, 32]])
Y: tensor([[14, 15, 16, 17, 18],
        [29, 30, 31, 32, 33]])
X:  tensor([[ 3,  4,  5,  6,  7],
        [18, 19, 20, 21, 22]])
Y: tensor([[ 4,  5,  6,  7,  8],
        [19, 20, 21, 22, 23]])
X:  tensor([[ 8,  9, 10, 11, 12],
        [23, 24, 25, 26, 27]])
Y: tensor([[ 9, 10, 11, 12, 13],
        [24, 25, 26, 27, 28]])
3.4.2 顺序分区

在迭代过程中,除了对原始序列可以随机抽样外, 我们还可以保证两个相邻的小批量中的子序列在原始序列上也是相邻的。 这种策略在基于小批量的迭代过程中保留了拆分的子序列的顺序,因此称为顺序分区。

def seq_data_iter_sequential(corpus, batch_size, num_steps):  #@save
    """使用顺序分区生成一个小批量子序列"""
    # 从随机偏移量开始划分序列
    offset = random.randint(0, num_steps)
    num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size
    Xs = torch.tensor(corpus[offset: offset + num_tokens])
    Ys = torch.tensor(corpus[offset + 1: offset + 1 + num_tokens])
    Xs, Ys = Xs.reshape(batch_size, -1), Ys.reshape(batch_size, -1)
    num_batches = Xs.shape[1] // num_steps
    for i in range(0, num_steps * num_batches, num_steps):
        X = Xs[:, i: i + num_steps]
        Y = Ys[:, i: i + num_steps]
        yield X, Y

基于相同的设置,通过顺序分区读取每个小批量的子序列的特征X和标签Y。 通过将它们打印出来可以发现: 迭代期间来自两个相邻的小批量中的子序列在原始序列中确实是相邻的。

for X, Y in seq_data_iter_sequential(my_seq, batch_size=2, num_steps=5):
    print('X: ', X, '\nY:', Y)

输出:

X:  tensor([[ 0,  1,  2,  3,  4],
        [17, 18, 19, 20, 21]])
Y: tensor([[ 1,  2,  3,  4,  5],
        [18, 19, 20, 21, 22]])
X:  tensor([[ 5,  6,  7,  8,  9],
        [22, 23, 24, 25, 26]])
Y: tensor([[ 6,  7,  8,  9, 10],
        [23, 24, 25, 26, 27]])
X:  tensor([[10, 11, 12, 13, 14],
        [27, 28, 29, 30, 31]])
Y: tensor([[11, 12, 13, 14, 15],
        [28, 29, 30, 31, 32]])

现在,我们将上面的两个采样函数包装到一个类中, 以便稍后可以将其用作数据迭代器。

class SeqDataLoader:  #@save
    """加载序列数据的迭代器"""
    def __init__(self, batch_size, num_steps, use_random_iter, max_tokens):
        if use_random_iter:
            self.data_iter_fn = d2l.seq_data_iter_random
        else:
            self.data_iter_fn = d2l.seq_data_iter_sequential
        self.corpus, self.vocab = d2l.load_corpus_time_machine(max_tokens)
        self.batch_size, self.num_steps = batch_size, num_steps

    def __iter__(self):
        return self.data_iter_fn(self.corpus, self.batch_size, self.num_steps)

最后,我们定义了一个函数load_data_time_machine, 它同时返回数据迭代器和词表, 因此可以与其他带有load_data前缀的函数 (如 3.5节中定义的 d2l.load_data_fashion_mnist)类似地使用。

def load_data_time_machine(batch_size, num_steps,  #@save
                           use_random_iter=False, max_tokens=10000):
    """返回时光机器数据集的迭代器和词表"""
    data_iter = SeqDataLoader(
        batch_size, num_steps, use_random_iter, max_tokens)
    return data_iter, data_iter.vocab

4. 循环神经网络

上一节介绍了 n n n元语法模型,其中单词 x t x_t xt在时间步 t t t的条件概率仅取决于前面 n − 1 n-1 n1个单词。对于时间步 t − ( n − 1 ) t-(n-1) t(n1)之前的单词,如果想让其可能产生的影响合并到 x t x_t xt上,需要增加 n n n,然而模型参数的数量也会随之呈指数增长,因为词表 V \mathcal{V} V需要存储 ∣ V ∣ n |\mathcal{V}|^n Vn个数字,因此与其将 P ( x t ∣ x t − 1 , … , x t − n + 1 ) P(x_t \mid x_{t-1}, \ldots, x_{t-n+1}) P(xtxt1,,xtn+1)模型化,不如使用隐变量模型:

P ( x t ∣ x t − 1 , … , x 1 ) ≈ P ( x t ∣ h t − 1 ) , P(x_t \mid x_{t-1}, \ldots, x_1) \approx P(x_t \mid h_{t-1}), P(xtxt1,,x1)P(xtht1),

其中 h t h_t ht是隐状态,也称为隐藏变量,它存储了到时间步 t − 1 t-1 t1的序列信息。通常,可以基于当前输入 x t x_t xt和先前隐状态 h t − 1 h_{t-1} ht1来计算时间步 t t t处的任何时间的隐状态:

h t = f ( x t , h t − 1 ) h_t=f(x_t,h_{t-1}) ht=f(xt,ht1)

对于上式中的函数 f f f,隐变量模型不是近似值。毕竟 h t h_t ht是可以仅仅存储到目前为止观察到的所有数据,然而这样的操作可能会使计算机和存储的代价都变得昂贵。

我们在 多层感知机中 讨论过的具有隐藏单元的隐藏层。 值得注意的是,隐藏层和隐状态指的是两个截然不同的概念。 如上所述,隐藏层是在从输入到输出的路径上(以观测角度来理解)的隐藏的层,而隐状态则是在给定步骤所做的任何事情(以技术角度来定义)的输入,并且这些状态只能通过先前时间步的数据来计算。

循环神经网络(recurrent neural networks ,RNNs)是具有隐状态的神经网络。

4.1 无隐状态的神经网络

只有单隐藏层的多层感知机。设隐藏层的激活函数为 ϕ \phi ϕ,给定一个小批量样本 X ∈ R n × d \mathbf{X} \in \mathbb{R}^{n \times d} XRn×d其中批量大小为 n n n,输入维度为 d d d,则隐藏层的输出 H ∈ R n × h \mathbf{H} \in \mathbb{R}^{n \times h} HRn×h通过下式计算:

H = ϕ ( X W x h + b h ) . \mathbf{H} = \phi(\mathbf{X} \mathbf{W}_{xh} + \mathbf{b}_h). H=ϕ(XWxh+bh).

隐藏层的权重参数为 W x h ∈ R d × h \mathbf{W}_{xh} \in \mathbb{R}^{d \times h} WxhRd×h,偏置参数为 H = ϕ ( X W x h + b h ) \mathbf{H} = \phi(\mathbf{X} \mathbf{W}_{xh} + \mathbf{b}_h) H=ϕ(XWxh+bh),以及隐藏单元的数目为 h h h。因此求和时可以应用广播机制(线性神经网络)。接下来,将隐藏变量 H \mathbf{H} H用作输出层的输入。输出层的公式为:

O = H W h q + b q , \mathbf{O} = \mathbf{H} \mathbf{W}_{hq} + \mathbf{b}_q, O=HWhq+bq,

其中, O ∈ R n × q \mathbf{O} \in \mathbb{R}^{n \times q} ORn×q是输出变量, W h q ∈ R h × q \mathbf{W}_{hq} \in \mathbb{R}^{h \times q} WhqRh×q是权重参数, b q ∈ R 1 × q \mathbf{b}_q \in \mathbb{R}^{1 \times q} bqR1×q是输出层的偏置参数。如果是分类问题,可以用softmax( O \mathbf O O)来计算输出类别的概率分布。

4.2 有隐状态的循环神经网络

假设在时间步 t t t有小批量输入 X t ∈ R n × d \mathbf{X}_t \in \mathbb{R}^{n \times d} XtRn×d。换言之,对于 n n n个序列样本的小批量, X t \mathbf {X}_t Xt的每一行对应于来自该序列的时间步 t t t处的一个样本。用 H t ∈ R n × h \mathbf{H}_t \in \mathbb{R}^{n \times h} HtRn×h表示时间步 t t t的隐藏变量。与多层感知机不同的是,这里保存了前一个时间步的隐藏变量 H t − 1 \mathbf{H}_{t-1} Ht1,并引入了一个新的权重参数 W h h ∈ R h × h \mathbf{W}_{hh} \in \mathbb{R}^{h \times h} WhhRh×h,来描述在当前时间步使用前一个时间步的隐藏变量。具体来说,当前时间步隐藏变量由当前时间步的输入与前一个时间步的隐藏变量一起计算得出:

H t = ϕ ( X t W x h + H t − 1 W h h + b h ) . \mathbf{H}_t = \phi(\mathbf{X}_t \mathbf{W}_{xh} + \mathbf{H}_{t-1} \mathbf{W}_{hh} + \mathbf{b}_h). Ht=ϕ(XtWxh+Ht1Whh+bh).

从相邻时间步的隐藏变量 H t H_t Ht H t − 1 H_{t−1} Ht1之间的关系可知, 这些变量捕获并保留了序列直到其当前时间步的历史信息, 就如当前时间步下神经网络的状态或记忆, 因此这样的隐藏变量被称为隐状态(hidden state)。由于在当前时间步中,隐状态使用的定义与前一个时间步中使用的定义相同,因此上式的计算是循环的(recurrent)。于是基于循环计算的隐状态神经网络被命名为循环神经网络(recurrent neural network)。在循环神经网络中执行上式计算的层称为循环层。

对于时间步t,输出层的输出类似于多层感知机中的计算:

O t = H t W h q + b q . \mathbf{O}_t = \mathbf{H}_t \mathbf{W}_{hq} + \mathbf{b}_q. Ot=HtWhq+bq.

循环神经网络的参数包括隐藏层的权重 W x h ∈ R d × h , W h h ∈ R h × h \mathbf{W}_{xh} \in \mathbb{R}^{d \times h}, \mathbf{W}_{hh} \in \mathbb{R}^{h \times h} WxhRd×h,WhhRh×h和偏置 b h ∈ R 1 × h \mathbf{b}_h \in \mathbb{R}^{1 \times h} bhR1×h,以及输出层的权重 W h q ∈ R h × q \mathbf{W}_{hq} \in \mathbb{R}^{h \times q} WhqRh×q和偏置 b q ∈ R 1 × q \mathbf{b}_q \in \mathbb{R}^{1 \times q} bqR1×q。 值得一提的是,即使在不同的时间步,循环神经网络也总是使用这些模型参数。 因此,循环神经网络的参数开销不会随着时间步的增加而增加。

图8.4.1展示了循环神经网络在三个相邻时间步的计算逻辑。 在任意时间步 t t t,隐状态的计算可以被视为:

  1. 拼接当前时间步 t t t的输入 X t \mathbf{X}_t Xt和前一时间步 t − 1 t-1 t1的隐状态 H t − 1 \mathbf{H}_{t-1} Ht1
  2. 将拼接的结果送入带有激活函数 ϕ \phi ϕ的全连接层。全连接层的输出是当前时间步 t t t的隐状态 H t \mathbf{H}_t Ht

在本例中,模型参数是 W x h \mathbf{W}_{xh} Wxh W h h \mathbf{W}_{hh} Whh,以及 b h \mathbf{b}_h bh,所有这些参数都来自上式。当前时间步 t t t的隐状态 H t \mathbf{H}_t Ht 将参与计算下一时间步 t + 1 t+1 t+1的隐状态 H t + 1 \mathbf H_{t+1} Ht+1。 而且 H t \mathbf H_t Ht还将送入全连接输出层, 用于计算当前时间步t的输出 O t \mathbf O_t Ot

在这里插入图片描述

4.3 困惑度(Perplexity)

一个序列中所有的 n n n个词元的交叉熵损失的平均值来衡量:

1 n ∑ t = 1 n − log ⁡ P ( x t ∣ x t − 1 , … , x 1 ) , \frac{1}{n} \sum_{t=1}^n -\log P(x_t \mid x_{t-1}, \ldots, x_1), n1t=1nlogP(xtxt1,,x1),

自然语言处理的科学家更喜欢使用一个叫做困惑度(perplexity)的量。

exp ⁡ ( − 1 n ∑ t = 1 n log ⁡ P ( x t ∣ x t − 1 , … , x 1 ) ) . \exp\left(-\frac{1}{n} \sum_{t=1}^n \log P(x_t \mid x_{t-1}, \ldots, x_1)\right). exp(n1t=1nlogP(xtxt1,,x1)).

困惑度的最好的理解是“下一个词元的实际选择数的调和平均数”。 我们看看一些案例。

  • 在最好的情况下,模型总是完美地估计标签词元的概率为1。 在这种情况下,模型的困惑度为1。
  • 在最坏的情况下,模型总是预测标签词元的概率为0。 在这种情况下,困惑度是正无穷大。
  • 在基线上,该模型的预测是词表的所有可用词元上的均匀分布。 在这种情况下,困惑度等于词表中唯一词元的数量。 事实上,如果我们在没有任何压缩的情况下存储序列, 这将是我们能做的最好的编码方式。 因此,这种方式提供了一个重要的上限, 而任何实际模型都必须超越这个上限。

在接下来的小节中,我们将基于循环神经网络实现字符级语言模型, 并使用困惑度来评估这样的模型。

5. 循环神经网络从零开始实现

读取数据集:

%matplotlib inline
import math
import torch
from torch import nn
from torch.nn import functional as F
from d2l import torch as d2l

batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)

5.1 独热编码

简单来说就是将vocabulary中的28个字母用01编码表示。

索引为0和2的独热向量编码如下所示:

F.one_hot(torch.tensor([0, 2]), len(vocab))

输出:

tensor([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
         0, 0, 0, 0]])

每次采用的小批量数据形状是二维张量:(批量大小,时间步数)。one_hot函数将这样一个小批量数据转换成三维张量,张量的最后一个维度等于词表大小(len(vocab))。我们经常转换输入的维度,以便获得形状为(时间步数,批量大小,词表大小)的输出。

X = torch.arange(10).reshape((2, 5))
F.one_hot(X.T, 28).shape

输出:

torch.Size([5, 2, 28])

5.2 初始化模型参数

隐藏单元数num_hiddens是一个可调的超参数。 当训练语言模型时,输入和输出来自相同的词表。 因此,它们具有相同的维度,即词表的大小。

def get_params(vocab_size, num_hiddens, device):
    num_inputs = num_outputs = vocab_size

    def normal(shape):
        return torch.randn(size=shape, device=device) * 0.01

    # 隐藏层参数
    W_xh = normal((num_inputs, num_hiddens))
    W_hh = normal((num_hiddens, num_hiddens))
    b_h = torch.zeros(num_hiddens, device=device)
    # 输出层参数
    W_hq = normal((num_hiddens, num_outputs))
    b_q = torch.zeros(num_outputs, device=device)
    # 附加梯度
    params = [W_xh, W_hh, b_h, W_hq, b_q]
    for param in params:
        param.requires_grad_(True)
    return params

5.3 循环神经网络 模型

为了定义循环神经网络模型,首先需要一个init_rnn_state函数在初始化时返回隐状态。这个函数的返回是一个张量,张量全用0填充,形状为(批量大小、隐藏单元数)。

def init_rnn_state(batch_size, num_hiddens, device):
    return (torch.zeros((batch_size, num_hiddens), device=device), )

下面的rnn函数定义了如何在一个时间步内计算隐状态和输出。循环神经网络模型通过inputs最外层的维度实现循环,以便逐时间步更新小批量数据的隐状态H。此外,这里使用tanh函数作为激活函数。当元素在实数上满足均匀分布时,tanh函数的平均值为0.

def rnn(inputs, state, params):
    # inputs的形状:(时间步数量,批量大小,词表大小)
    W_xh, W_hh, b_h, W_hq, b_q = params
    H, = state
    outputs = []
    # X的形状:(批量大小,词表大小)
    for X in inputs:  # 一个时间步一个时间步的计算  
        H = torch.tanh(torch.mm(X, W_xh) + torch.mm(H, W_hh) + b_h)
        Y = torch.mm(H, W_hq) + b_q
        outputs.append(Y)
    return torch.cat(outputs, dim=0), (H,)

以上代码 相当于将一句话分成32个小批量 一个小批量有35个字 在预测的时候是同一个时间步预测 也就是每个批量的第一个字一起预测 第二个字一起预测

class RNNModelScratch: #@save
    """从零开始实现的循环神经网络模型"""
    def __init__(self, vocab_size, num_hiddens, device,
                 get_params, init_state, forward_fn):
        self.vocab_size, self.num_hiddens = vocab_size, num_hiddens
        self.params = get_params(vocab_size, num_hiddens, device)
        self.init_state, self.forward_fn = init_state, forward_fn

    def __call__(self, X, state):
        X = F.one_hot(X.T, self.vocab_size).type(torch.float32)
        return self.forward_fn(X, state, self.params)

    def begin_state(self, batch_size, device):
        return self.init_state(batch_size, self.num_hiddens, device)

最后检查输出是否具有正确的形状。

num_hiddens = 512
net = RNNModelScratch(len(vocab), num_hiddens, d2l.try_gpu(), get_params,
                      init_rnn_state, rnn)
state = net.begin_state(X.shape[0], d2l.try_gpu())
Y, new_state = net(X.to(d2l.try_gpu()), state)
Y.shape, len(new_state), new_state[0].shape

输出:

(torch.Size([10, 28]), 1, torch.Size([2, 512]))

**总结:**以上代码的X的输入是上面代码生成的0-10 形状是2×5 经过rnn网络的预测得到十个y的结果 每一个y的结果都是28个独热编码 每一个循环生成一个时间步 之后在拼接到一起

以下是最后的y的结果

outputs: [tensor([[-8.3122e-04,  1.4398e-03,  1.4256e-03, -3.5242e-03,  3.0909e-03,
         -2.7585e-03, -1.9031e-03, -1.6739e-03, -4.8097e-04,  2.4966e-03,
          3.2992e-03,  4.0206e-03, -1.8794e-03,  2.6391e-03,  4.9025e-03,
          2.0751e-04,  2.5424e-03, -2.7713e-03, -5.8939e-05,  2.2642e-03,
         -3.1387e-03, -4.6669e-04, -2.3050e-03, -6.0795e-03,  1.6276e-03,
         -6.8935e-03,  2.6596e-03, -3.4543e-03],
        [ 3.2961e-03,  1.9873e-03, -1.5879e-03,  1.0310e-03,  1.7934e-03,
          2.0771e-03, -1.2342e-03, -7.5378e-04, -2.4798e-03, -5.6734e-04,
          2.5202e-03, -1.5615e-03, -1.6480e-03,  5.3560e-04, -2.2712e-03,
          1.5767e-03, -1.8876e-03, -1.3172e-03,  6.2936e-04, -1.9860e-03,
          3.2445e-04, -2.3579e-03,  1.2664e-03,  6.5282e-04,  4.3102e-04,
          1.3872e-03,  1.5368e-03, -4.4891e-03]], device='cuda:0',
       grad_fn=<AddBackward0>), tensor([[ 2.9474e-04,  1.0904e-03,  3.7034e-03,  1.3961e-03,  3.1671e-04,
          1.2213e-03,  1.2591e-03,  1.4390e-04, -7.4193e-04, -4.7396e-04,
         -1.9184e-03,  2.3120e-03, -2.6239e-04, -1.3091e-03, -2.6551e-03,
          3.2924e-03,  4.6826e-03, -2.6182e-05, -2.4456e-03, -4.4115e-05,
         -1.7848e-04,  3.4671e-03,  2.3505e-03,  1.6755e-03,  4.1045e-04,
          3.6036e-03,  2.1186e-04, -4.2588e-03],
        [-1.0274e-03,  2.2788e-03,  4.5509e-04,  1.2842e-03,  4.5755e-04,
          3.3458e-04, -7.0318e-03, -6.1450e-04,  1.1754e-03,  9.6733e-04,
         -1.9125e-03, -3.1705e-04, -1.5736e-03,  1.5849e-03, -2.6751e-03,
          1.6684e-03,  1.9232e-03, -2.3366e-03, -3.5476e-03, -9.8963e-04,
         -3.0234e-03, -6.9333e-04,  2.9309e-03,  9.1022e-04, -3.5731e-04,
          4.7597e-03, -2.3427e-04,  3.0278e-03]], device='cuda:0',
       grad_fn=<AddBackward0>), tensor([[-2.9495e-04,  8.4225e-03, -9.8689e-04,  9.8263e-04,  2.3562e-03,
          1.4982e-04,  1.7489e-03, -2.5012e-03, -9.6382e-04,  2.3439e-03,
         -3.6762e-03,  5.0303e-03,  2.7815e-04, -2.4317e-03, -3.7479e-03,
          1.9866e-03,  1.0463e-03,  2.6426e-03, -4.9176e-04, -2.6789e-03,
          1.8001e-03, -2.2785e-03, -3.7372e-03,  1.5025e-04,  2.6802e-03,
         -1.2167e-03, -2.3401e-03, -6.3387e-03],
        [ 2.0640e-03,  1.0370e-03, -3.9573e-03, -2.0357e-03, -2.1458e-04,
          2.1580e-04, -3.5785e-03, -4.6468e-03,  6.4391e-04,  5.4453e-03,
          5.1079e-04, -5.0171e-03, -9.6084e-04,  1.4906e-03, -8.3957e-04,
         -2.9616e-04, -1.2667e-03, -3.9027e-05,  2.1129e-03, -3.4988e-03,
         -2.0421e-03,  3.0413e-03,  7.3809e-03, -2.4304e-04,  1.5639e-04,
          1.7789e-04,  4.4616e-03,  1.0557e-03]], device='cuda:0',
       grad_fn=<AddBackward0>), tensor([[-2.9381e-03,  2.8707e-03,  3.4183e-06, -2.0131e-03,  4.5254e-03,
         -3.8689e-03,  2.3160e-04, -2.6557e-03,  1.8582e-03,  1.3058e-03,
          1.3086e-03,  3.1845e-03, -3.0525e-03,  2.4332e-03,  4.8977e-03,
          3.7075e-03,  3.3662e-03,  1.6483e-03, -1.7885e-03, -1.8961e-03,
          9.2044e-04, -4.8569e-05, -4.0251e-03, -5.8912e-04, -5.1224e-03,
         -2.0371e-03, -8.0246e-04,  1.7482e-03],
        [-1.6286e-03,  2.1060e-03, -1.4089e-04, -4.4804e-03,  2.2385e-03,
          3.0510e-03, -5.8582e-03,  2.7038e-03,  1.1979e-03,  4.5698e-03,
         -1.9280e-03,  3.5757e-03, -6.1484e-04,  1.5590e-03, -6.0712e-04,
         -1.0331e-03,  8.0428e-04,  2.7478e-03,  2.2707e-03,  1.9296e-03,
         -3.8185e-05, -1.3654e-03,  4.1659e-03, -3.3089e-03,  4.1918e-03,
         -4.0451e-03, -2.0248e-03,  5.3052e-04]], device='cuda:0',
       grad_fn=<AddBackward0>), tensor([[-2.1359e-03,  2.1019e-03,  2.9346e-03,  1.1135e-03, -2.0754e-03,
         -8.2477e-04,  2.7727e-03,  1.4472e-03, -1.9705e-03,  4.6515e-04,
         -1.8181e-03, -7.9074e-04, -1.0280e-03,  3.9789e-03, -1.6426e-03,
          2.4543e-03,  1.3621e-03, -1.2370e-03,  5.0549e-06, -1.2810e-03,
          2.4912e-03,  6.6438e-04, -4.5578e-03,  3.3261e-03,  5.9231e-04,
          2.4017e-03,  1.5608e-04, -2.0779e-03],
        [ 4.8077e-03, -4.0345e-03,  2.7502e-03, -4.1750e-04, -5.7829e-04,
          1.9763e-03, -3.8561e-03,  2.7919e-03,  2.0784e-03, -1.6336e-03,
          5.7183e-04,  4.0877e-05, -1.1132e-03, -1.8245e-03, -3.2490e-04,
          3.7405e-03, -1.2204e-03,  4.9382e-03,  2.4922e-03, -2.0450e-03,
          4.0956e-03,  2.0318e-03,  1.2194e-03, -3.3002e-03, -6.9184e-04,
          1.3477e-04,  1.5593e-03,  2.5861e-03]], device='cuda:0',
       grad_fn=<AddBackward0>)]
tensor([[-8.3122e-04,  1.4398e-03,  1.4256e-03, -3.5242e-03,  3.0909e-03,
         -2.7585e-03, -1.9031e-03, -1.6739e-03, -4.8097e-04,  2.4966e-03,
          3.2992e-03,  4.0206e-03, -1.8794e-03,  2.6391e-03,  4.9025e-03,
          2.0751e-04,  2.5424e-03, -2.7713e-03, -5.8939e-05,  2.2642e-03,
         -3.1387e-03, -4.6669e-04, -2.3050e-03, -6.0795e-03,  1.6276e-03,
         -6.8935e-03,  2.6596e-03, -3.4543e-03],
        [ 3.2961e-03,  1.9873e-03, -1.5879e-03,  1.0310e-03,  1.7934e-03,
          2.0771e-03, -1.2342e-03, -7.5378e-04, -2.4798e-03, -5.6734e-04,
          2.5202e-03, -1.5615e-03, -1.6480e-03,  5.3560e-04, -2.2712e-03,
          1.5767e-03, -1.8876e-03, -1.3172e-03,  6.2936e-04, -1.9860e-03,
          3.2445e-04, -2.3579e-03,  1.2664e-03,  6.5282e-04,  4.3102e-04,
          1.3872e-03,  1.5368e-03, -4.4891e-03],
        [ 2.9474e-04,  1.0904e-03,  3.7034e-03,  1.3961e-03,  3.1671e-04,
          1.2213e-03,  1.2591e-03,  1.4390e-04, -7.4193e-04, -4.7396e-04,
         -1.9184e-03,  2.3120e-03, -2.6239e-04, -1.3091e-03, -2.6551e-03,
          3.2924e-03,  4.6826e-03, -2.6182e-05, -2.4456e-03, -4.4115e-05,
         -1.7848e-04,  3.4671e-03,  2.3505e-03,  1.6755e-03,  4.1045e-04,
          3.6036e-03,  2.1186e-04, -4.2588e-03],
        [-1.0274e-03,  2.2788e-03,  4.5509e-04,  1.2842e-03,  4.5755e-04,
          3.3458e-04, -7.0318e-03, -6.1450e-04,  1.1754e-03,  9.6733e-04,
         -1.9125e-03, -3.1705e-04, -1.5736e-03,  1.5849e-03, -2.6751e-03,
          1.6684e-03,  1.9232e-03, -2.3366e-03, -3.5476e-03, -9.8963e-04,
         -3.0234e-03, -6.9333e-04,  2.9309e-03,  9.1022e-04, -3.5731e-04,
          4.7597e-03, -2.3427e-04,  3.0278e-03],
        [-2.9495e-04,  8.4225e-03, -9.8689e-04,  9.8263e-04,  2.3562e-03,
          1.4982e-04,  1.7489e-03, -2.5012e-03, -9.6382e-04,  2.3439e-03,
         -3.6762e-03,  5.0303e-03,  2.7815e-04, -2.4317e-03, -3.7479e-03,
          1.9866e-03,  1.0463e-03,  2.6426e-03, -4.9176e-04, -2.6789e-03,
          1.8001e-03, -2.2785e-03, -3.7372e-03,  1.5025e-04,  2.6802e-03,
         -1.2167e-03, -2.3401e-03, -6.3387e-03],
        [ 2.0640e-03,  1.0370e-03, -3.9573e-03, -2.0357e-03, -2.1458e-04,
          2.1580e-04, -3.5785e-03, -4.6468e-03,  6.4391e-04,  5.4453e-03,
          5.1079e-04, -5.0171e-03, -9.6084e-04,  1.4906e-03, -8.3957e-04,
         -2.9616e-04, -1.2667e-03, -3.9027e-05,  2.1129e-03, -3.4988e-03,
         -2.0421e-03,  3.0413e-03,  7.3809e-03, -2.4304e-04,  1.5639e-04,
          1.7789e-04,  4.4616e-03,  1.0557e-03],
        [-2.9381e-03,  2.8707e-03,  3.4183e-06, -2.0131e-03,  4.5254e-03,
         -3.8689e-03,  2.3160e-04, -2.6557e-03,  1.8582e-03,  1.3058e-03,
          1.3086e-03,  3.1845e-03, -3.0525e-03,  2.4332e-03,  4.8977e-03,
          3.7075e-03,  3.3662e-03,  1.6483e-03, -1.7885e-03, -1.8961e-03,
          9.2044e-04, -4.8569e-05, -4.0251e-03, -5.8912e-04, -5.1224e-03,
         -2.0371e-03, -8.0246e-04,  1.7482e-03],
        [-1.6286e-03,  2.1060e-03, -1.4089e-04, -4.4804e-03,  2.2385e-03,
          3.0510e-03, -5.8582e-03,  2.7038e-03,  1.1979e-03,  4.5698e-03,
         -1.9280e-03,  3.5757e-03, -6.1484e-04,  1.5590e-03, -6.0712e-04,
         -1.0331e-03,  8.0428e-04,  2.7478e-03,  2.2707e-03,  1.9296e-03,
         -3.8185e-05, -1.3654e-03,  4.1659e-03, -3.3089e-03,  4.1918e-03,
         -4.0451e-03, -2.0248e-03,  5.3052e-04],
        [-2.1359e-03,  2.1019e-03,  2.9346e-03,  1.1135e-03, -2.0754e-03,
         -8.2477e-04,  2.7727e-03,  1.4472e-03, -1.9705e-03,  4.6515e-04,
         -1.8181e-03, -7.9074e-04, -1.0280e-03,  3.9789e-03, -1.6426e-03,
          2.4543e-03,  1.3621e-03, -1.2370e-03,  5.0549e-06, -1.2810e-03,
          2.4912e-03,  6.6438e-04, -4.5578e-03,  3.3261e-03,  5.9231e-04,
          2.4017e-03,  1.5608e-04, -2.0779e-03],
        [ 4.8077e-03, -4.0345e-03,  2.7502e-03, -4.1750e-04, -5.7829e-04,
          1.9763e-03, -3.8561e-03,  2.7919e-03,  2.0784e-03, -1.6336e-03,
          5.7183e-04,  4.0877e-05, -1.1132e-03, -1.8245e-03, -3.2490e-04,
          3.7405e-03, -1.2204e-03,  4.9382e-03,  2.4922e-03, -2.0450e-03,
          4.0956e-03,  2.0318e-03,  1.2194e-03, -3.3002e-03, -6.9184e-04,
          1.3477e-04,  1.5593e-03,  2.5861e-03]], device='cuda:0',
       grad_fn=<CatBackward0>)

我们可以看到输出形状是(时间步数×批量大小,词表大小), 而隐状态形状保持不变,即(批量大小,隐藏单元数)。

5.4 预测

首先定义预测函数来生成prefix之后的新字符,其中的prefix是一个用户提供的包含多个字符的字符串。在循环遍历prefix中的开始字符时,不断地将隐状态传递到下一个时间步,但是不生成任何输出。这被称为预热期,因为在此期间模型会自我更新(例如,更新隐状态),但不会进行预测。预热期结束后,隐状态的值通常比刚开始的初始值更适合预测,从而预测字符并输出它们。

def predict_ch8(prefix, num_preds, net, vocab, device):  #@save
    """在prefix后面生成新字符"""
    state = net.begin_state(batch_size=1, device=device)
    outputs = [vocab[prefix[0]]] # 
    get_input = lambda: torch.tensor([outputs[-1]], device=device).reshape((1, 1))
    for y in prefix[1:]:  # 预热期
        _, state = net(get_input(), state)
        outputs.append(vocab[y])
    for _ in range(num_preds):  # 预测num_preds步
        y, state = net(get_input(), state)
        outputs.append(int(y.argmax(dim=1).reshape(1)))
    return ''.join([vocab.idx_to_token[i] for i in outputs])
predict_ch8('time traveller ', 10, net, vocab, d2l.try_gpu())

输出结果:

'time traveller aaaaaaaaaa'

​ **分析:**第一个for循环也就是预热期是为了获得隐藏层的状态信息 第二个for循环 (预测num_preds步)是在预测下一个char字符

详情请看代码运行结果

5.5 梯度裁剪

g ← min ⁡ ( 1 , θ ∥ g ∥ ) g . \mathbf{g} \leftarrow \min\left(1, \frac{\theta}{\|\mathbf{g}\|}\right) \mathbf{g}. gmin(1,gθ)g.

def grad_clipping(net, theta):  #@save
    """裁剪梯度"""
    if isinstance(net, nn.Module):
        params = [p for p in net.parameters() if p.requires_grad]
    else:
        params = net.params
    norm = torch.sqrt(sum(torch.sum((p.grad ** 2)) for p in params))
    if norm > theta:
        for param in params:
            param.grad[:] *= theta / norm

5.6 训练

在训练模型之前,让我们定义一个函数在一个迭代周期内训练模型。

  1. 序列数据的不同采样方法(随机采样和顺序分区)将导致隐状态初始化的差异
  2. 在更新模型参数之前裁剪梯度。这样操作的目的是,即使训练过程中某个点上发生了梯度爆炸,也能保证模型不会发散
  3. 用困惑度来评价模型。这样的度量确保了不同长度的序列具有可比性

具体来说,当使用顺序分区时,只在每个迭代周期的开始位置初始化隐状态。由于下一个小批量数据中第 i i i个子序列样本与当前第 i i i个子序列样本相邻,因此当前小批量数据最后一个样本的隐状态将用于初始化下一个小批量数据第一个样本的隐状态。这样存储在隐状态中的序列的历史信息可以在一个迭代周期内流经相邻的子序列。然而在任何一点隐状态的计算,都依赖于同一迭代周期中前面所有小批量数据,这使得梯度计算变得复杂。为了降低计算量,在处理任何一个小批量数据之前,先分离梯度,使得隐状态的梯度计算总是限制在一个小批量数据的时间步内。

当使用随机抽样时,因为每个样本都是在一个随机位置抽样的,因此需要为每个迭代周期重新初始化隐状态。

#@save
def train_epoch_ch8(net, train_iter, loss, updater, device, use_random_iter):
    """训练网络一个迭代周期(定义见第8章)"""
    state, timer = None, d2l.Timer()
    metric = d2l.Accumulator(2)  # 训练损失之和,词元数量
    for X, Y in train_iter:
        if state is None or use_random_iter:
            # 在第一次迭代或使用随机抽样时初始化state
            state = net.begin_state(batch_size=X.shape[0], device=device)
        else:
            if isinstance(net, nn.Module) and not isinstance(state, tuple):
                # state对于nn.GRU是个张量
                state.detach_()
            else:
                # state对于nn.LSTM或对于我们从零开始实现的模型是个张量
                for s in state:
                    s.detach_()
        y = Y.T.reshape(-1)
        X, y = X.to(device), y.to(device)
        y_hat, state = net(X, state)
        l = loss(y_hat, y.long()).mean()
        if isinstance(updater, torch.optim.Optimizer):
            updater.zero_grad()
            l.backward()
            grad_clipping(net, 1)
            updater.step()
        else:
            l.backward()
            grad_clipping(net, 1)
            # 因为已经调用了mean函数
            updater(batch_size=1)
        metric.add(l * y.numel(), y.numel())
    return math.exp(metric[0] / metric[1]), metric[1] / timer.stop()

循环神经网络模型的训练函数既支持从零开始实现,也可以使用高级API来实现。

#@save
def train_ch8(net, train_iter, vocab, lr, num_epochs, device,
              use_random_iter=False):
    """训练模型(定义见第8章)"""
    loss = nn.CrossEntropyLoss()
    animator = d2l.Animator(xlabel='epoch', ylabel='perplexity',
                            legend=['train'], xlim=[10, num_epochs])
    # 初始化
    if isinstance(net, nn.Module):
        updater = torch.optim.SGD(net.parameters(), lr)
    else:
        updater = lambda batch_size: d2l.sgd(net.params, lr, batch_size)
    predict = lambda prefix: predict_ch8(prefix, 50, net, vocab, device)
    # 训练和预测
    for epoch in range(num_epochs):
        ppl, speed = train_epoch_ch8(
            net, train_iter, loss, updater, device, use_random_iter)
        if (epoch + 1) % 10 == 0:
            print(predict('time traveller'))
            animator.add(epoch + 1, [ppl])
    print(f'困惑度 {ppl:.1f}, {speed:.1f} 词元/秒 {str(device)}')
    print(predict('time traveller'))
    print(predict('traveller'))
num_epochs, lr = 500, 1
train_ch8(net, train_iter, vocab, lr, num_epochs, d2l.try_gpu())

输出:

困惑度 1.0, 67212.6 词元/秒 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

在这里插入图片描述

随机抽样的结果

net = RNNModelScratch(len(vocab), num_hiddens, d2l.try_gpu(), get_params,
                      init_rnn_state, rnn)
train_ch8(net, train_iter, vocab, lr, num_epochs, d2l.try_gpu(),
          use_random_iter=True)

输出:

在这里插入图片描述

6. 循环神经网络的简洁实现

import torch
from torch import nn
from torch.nn import functional as F
from d2l import torch as d2l

batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)

6.1 定义模型

高级API提供了循环神经网络的实现。 我们构造一个具有256个隐藏单元的单隐藏层的循环神经网络层rnn_layer。 事实上,我们还没有讨论多层循环神经网络的意义。 现在仅需要将多层理解为一层循环神经网络的输出被用作下一层循环神经网络的输入就足够了。

num_hiddens = 256
rnn_layer = nn.RNN(len(vocab), num_hiddens)

state = torch.zeros((1, batch_size, num_hiddens))
state.shape

输出:

torch.Size([1, 32, 256])

通过一个隐状态和一个输入,我们就可以用更新后的隐状态计算输出。 需要强调的是,rnn_layer的“输出”(Y)不涉及输出层的计算: 它是指每个时间步的隐状态,这些隐状态可以用作后续输出层的输入。

X = torch.rand(size=(num_steps, batch_size, len(vocab)))
Y, state_new = rnn_layer(X, state)
Y.shape, state_new.shape
(torch.Size([35, 32, 256]), torch.Size([1, 32, 256]))
#@save
class RNNModel(nn.Module):
    """循环神经网络模型"""
    def __init__(self, rnn_layer, vocab_size, **kwargs):
        super(RNNModel, self).__init__(**kwargs)
        self.rnn = rnn_layer
        self.vocab_size = vocab_size
        self.num_hiddens = self.rnn.hidden_size
        # 如果RNN是双向的(之后将介绍),num_directions应该是2,否则应该是1
        if not self.rnn.bidirectional:
            self.num_directions = 1
            self.linear = nn.Linear(self.num_hiddens, self.vocab_size)
        else:
            self.num_directions = 2
            self.linear = nn.Linear(self.num_hiddens * 2, self.vocab_size)

    def forward(self, inputs, state):
        X = F.one_hot(inputs.T.long(), self.vocab_size)
        X = X.to(torch.float32)
        Y, state = self.rnn(X, state)
        # 全连接层首先将Y的形状改为(时间步数*批量大小,隐藏单元数)
        # 它的输出形状是(时间步数*批量大小,词表大小)。
        output = self.linear(Y.reshape((-1, Y.shape[-1])))
        return output, state

    def begin_state(self, device, batch_size=1):
        if not isinstance(self.rnn, nn.LSTM):
            # nn.GRU以张量作为隐状态
            return  torch.zeros((self.num_directions * self.rnn.num_layers,
                                 batch_size, self.num_hiddens),
                                device=device)
        else:
            # nn.LSTM以元组作为隐状态
            return (torch.zeros((
                self.num_directions * self.rnn.num_layers,
                batch_size, self.num_hiddens), device=device),
                    torch.zeros((
                        self.num_directions * self.rnn.num_layers,
                        batch_size, self.num_hiddens), device=device))

6.2 训练与预测

device = d2l.try_gpu()
net = RNNModel(rnn_layer, vocab_size=len(vocab))
net = net.to(device)
d2l.predict_ch8('time traveller', 10, net, vocab, device)

输出:

'time travellerbbabbkabyg

很明显,这种模型根本不能输出好的结果。 接下来,我们使用 8.5节中 定义的超参数调用train_ch8,并且使用高级API训练模型。

num_epochs, lr = 500, 1
d2l.train_ch8(net, train_iter, vocab, lr, num_epochs, device)
perplexity 1.3, 404413.8 tokens/sec on cuda:0
time travellerit would be remarkably convenient for the historia
travellery of il the hise fupt might and st was it loflers

在这里插入图片描述

 return (torch.zeros((
            self.num_directions * self.rnn.num_layers,
            batch_size, self.num_hiddens), device=device),
                torch.zeros((
                    self.num_directions * self.rnn.num_layers,
                    batch_size, self.num_hiddens), device=device))


  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值