某网站网站的设计与实现,wordpress 粉丝,iis做网站视,太原网络推广价格文章目录 语言模型1.学习语言模型2.马尔可夫模型与N元语法3.自然语言统计4.读取长序列数据4.1随机采样4.2顺序分区 语言模型
学习视频#xff1a;语言模型【动手学深度学习v2】 官方笔记#xff1a;语言模型和数据集
在【文本预处理】中了解了如何将文本数据映射为词元语言模型【动手学深度学习v2】 官方笔记语言模型和数据集
在【文本预处理】中了解了如何将文本数据映射为词元以及将这些词元可以视为一系列离散的观测例如单词或字符。假设长度为T的文本序列中的词元依次为 x 1 , x 2 , . . , x T x_1,x_2,..,x_T x1,x2,..,xT于是 x t ( 1 ≤ t ≤ T ) x_t(1≤t≤T) xt(1≤t≤T)可以被认为是文本序列在时间步t处的观测或标签 在给定这样的文本序列时语言模型language model的目标是估计序列的联合概率 P ( x 1 , x 2 , . . . , x T ) P(x_1,x_2,...,x_T) P(x1,x2,...,xT) 1.学习语言模型
使用计数来建模 2.马尔可夫模型与N元语法 总结 语言模型估计文本序列的联合概率使用统计方法时常采用n元语法 3.自然语言统计
看看在真实数据上如果进行自然语言统计根据前面介绍的时光机器数据集构建词表并打印前10个最常用的频率最高的单词
import random
import torch
from d2l import torch as d2ltokens 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, xlabeltoken: x, ylabelfrequency: n(x),xscalelog, yscalelog)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][((the, time, traveller), 59),((the, time, machine), 30),((the, medical, man), 24),((it, seemed, to), 16),((it, was, a), 15),((here, and, there), 15),((seemed, to, me), 14),((i, did, not), 14),((i, saw, the), 13),((i, began, to), 13)]最后我们直观地对比三种模型中的词元频率一元语法、二元语法和三元语法。
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], xlabeltoken: x,ylabelfrequency: n(x), xscalelog, yscalelog,legend[unigram, bigram, trigram])上述表明
1.除了一元语法词单词序列似乎也遵循齐普夫定律尽管公式中的指数 α \alpha α更小指数的大小受序列长度的影响
2.词表中n元组的数量并没有那么大这说明语言中存在相当多的结构这些结构给了我们应用模型的希望
3.很多n元组很少出现这使得拉普拉斯平滑非常不适合语言建模作为代替我们将使用基于深度学习的模型
4.读取长序列数据 4.1随机采样
在随机采样中每个样本都是在原始的长序列上任意捕获的子序列。 在迭代过程中来自两个相邻的、随机的、小批量中的子序列不一定在原始序列上相邻。 对于语言建模目标是基于到目前为止我们看到的词元来预测下一个词元 因此标签是移位了一个词元的原始序列。
下面的代码每次可以从数据中随机生成一个小批量。 在这里参数batch_size指定了每个小批量中子序列样本的数目 参数num_steps是每个子序列中预定义的时间步数。
def seq_data_iter_random(corpus, batch_size, num_steps): #save使用随机抽样生成一个小批量子序列# 从随机偏移量开始对序列进行分区随机范围包括num_steps-1corpus 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_sizefor 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_size2, num_steps5):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]])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_sizeXs 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_stepsfor 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_size2, num_steps5):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_randomelse:self.data_iter_fn d2l.seq_data_iter_sequentialself.corpus, self.vocab d2l.load_corpus_time_machine(max_tokens)self.batch_size, self.num_steps batch_size, num_stepsdef __iter__(self):return self.data_iter_fn(self.corpus, self.batch_size, self.num_steps)最后我们定义了一个函数load_data_time_machine 它同时返回数据迭代器和词表 因此可以与其他带有load_data前缀的函数
def load_data_time_machine(batch_size, num_steps, #saveuse_random_iterFalse, max_tokens10000):返回时光机器数据集的迭代器和词表data_iter SeqDataLoader(batch_size, num_steps, use_random_iter, max_tokens)return data_iter, data_iter.vocab