Advertisement

NLP课程

阅读量:

NLP课程

  • 文本预处理

    • 语言模型
    • n元语法
    • 时序数据的采样
  • 循环神经网络

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

文本预处理

文本是一类序列数据,一篇文章可以看作是字符或单词的序列,本节将介绍文本数据的常见预处理步骤,预处理通常包括四个步骤:

  • 读入文本
  • 分词
  • 建立字典,将每个词映射到一个唯一的索引(index)
  • 将文本从词的序列转换为索引的序列,方便输入模型

以下代码是读取文章的demo

复制代码
    import collections
    import re
    
    def read_time_machine():
    with open('/home/kesci/input/timemachine7163/timemachine.txt', 'r') as f:
        lines = [re.sub('[^a-z]+', ' ', line.strip().lower()) for line in f]
    return lines
    lines = read_time_machine()
    print('# sentences %d' % len(lines))
    
    
    py

分词

复制代码
    def tokenize(sentences, token='word'):
    """Split sentences into word or char tokens"""
    if token == 'word':
        return [sentence.split(' ') for sentence in sentences]
    elif token == 'char':
        return [list(sentence) for sentence in sentences]
    else:
        print('ERROR: unkown token type '+token)
    
    tokens = tokenize(lines)
    tokens[0:2]
    
    
    py
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/EijHZ3UIuhbfk80X2PG6OrQL4Wav.png)

建立字典并映射唯一引索

复制代码
    class Vocab(object):
    def __init__(self, tokens, min_freq=0, use_special_tokens=False):
        counter = count_corpus(tokens)  # : 
        self.token_freqs = list(counter.items())
        self.idx_to_token = []
        if use_special_tokens:
            # padding, begin of sentence, end of sentence, unknown
            self.pad, self.bos, self.eos, self.unk = (0, 1, 2, 3)
            self.idx_to_token += ['', '', '', '']
        else:
            self.unk = 0
            self.idx_to_token += ['']
        self.idx_to_token += [token for token, freq in self.token_freqs
                        if freq >= min_freq and token not in self.idx_to_token]
        self.token_to_idx = dict()
        for idx, token in enumerate(self.idx_to_token):
            self.token_to_idx[token] = idx
    
    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]
    
    def count_corpus(sentences):
    tokens = [tk for st in sentences for tk in st]
    return collections.Counter(tokens)  # 返回一个字典,记录每个词的出现次数
    
    
    py
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/qbPO8ujmeJk2xQLEpo7fFI0hCatS.png)

将词转化为引索

复制代码
    for i in range(8, 10):
    print('words:', tokens[i])
    print('indices:', vocab[tokens[i]])
    
    
    py

用NLTK做分词:

复制代码
    from nltk.tokenize import word_tokenize
    from nltk import data
    data.path.append('/home/kesci/input/nltk_data3784/nltk_data')
    print(word_tokenize(text))
    
    
    py

总结:关于类的构建很有学习价值,可以多看看

语言模型

假设序列w_1, w_2, \ldots, w_T中的每个词是依次生成的,我们有
P(w_1, w_2, \ldots, w_T)= \prod_{t=1}^T P(w_t \mid w_1, \ldots, w_{t-1})\\ = P(w_1)P(w_2 \mid w_1) \cdots P(w_T \mid w_1w_2\cdots w_{T-1})
例如,一段含有4个词的文本序列的概率
P(w_1, w_2, w_3, w_4) = P(w_1) P(w_2 \mid w_1) P(w_3 \mid w_1, w_2) P(w_4 \mid w_1, w_2, w_3).
语言模型的参数就是词的概率以及给定前几个词情况下的条件概率。设训练数据集为一个大型文本语料库,如维基百科的所有条目,词的概率可以通过该词在训练数据集中的相对词频来计算,例如,w_1的概率可以计算为:
\hat P(w_1) = \frac{n(w_1)}{n}

其中n(w_1)为语料库中以w_1作为第一个词的文本的数量,n为语料库中文本的总数量。

类似的,给定w_1情况下,w_2的条件概率可以计算为:
\hat P(w_2 \mid w_1) = \frac{n(w_1, w_2)}{n(w_1)}
其中n(w_1, w_2)为语料库中以w_1作为第一个词,w_2作为第二个词的文本的数量。

n元语法

序列长度增加,计算和存储多个词共同出现的概率的复杂度会呈指数级增加。n元语法通过马尔可夫假设简化模型,马尔科夫假设是指一个词的出现只与前面n个词相关,即n阶马尔可夫链(Markov chain of order n),如果n=1,那么有P(w_3 \mid w_1, w_2) = P(w_3 \mid w_2)。基于n-1阶马尔可夫链,我们可以将语言模型改写为

P(w_1, w_2, \ldots, w_T) = \prod_{t=1}^T P(w_t \mid w_{t-(n-1)}, \ldots, w_{t-1}) .

以上也叫n元语法(n-grams),它是基于n - 1阶马尔可夫链的概率语言模型。例如,当n=2时,含有4个词的文本序列的概率就可以改写为:

P(w_1, w_2, w_3, w_4) = P(w_1) P(w_2 \mid w_1) P(w_3 \mid w_1, w_2) P(w_4 \mid w_1, w_2, w_3)\\ = P(w_1) P(w_2 \mid w_1) P(w_3 \mid w_2) P(w_4 \mid w_3)

n分别为1、2和3时,我们将其分别称作一元语法(unigram)、二元语法(bigram)和三元语法(trigram)。例如,长度为4的序列w_1, w_2, w_3, w_4在一元语法、二元语法和三元语法中的概率分别为

P(w_1, w_2, w_3, w_4) = P(w_1) P(w_2) P(w_3) P(w_4) ,\\ P(w_1, w_2, w_3, w_4) = P(w_1) P(w_2 \mid w_1) P(w_3 \mid w_2) P(w_4 \mid w_3) ,\\ P(w_1, w_2, w_3, w_4) = P(w_1) P(w_2 \mid w_1) P(w_3 \mid w_1, w_2) P(w_4 \mid w_2, w_3) .

n较小时,n元语法往往并不准确。例如,在一元语法中,由三个词组成的句子“你走先”和“你先走”的概率是一样的。然而,当n较大时,n元语法需要计算并存储大量的词频和多词相邻频率。

时序数据的采样

在训练中我们需要每次随机读取小批量样本和标签。与之前章节的实验数据不同的是,时序数据的一个样本通常包含连续的字符。假设时间步数为5,样本序列为5个字符,即“想”“要”“有”“直”“升”。该样本的标签序列为这些字符分别在训练集中的下一个字符,即“要”“有”“直”“升”“机”,即X=“想要有直升”,Y=“要有直升机”。

现在我们考虑序列“想要有直升机,想要和你飞到宇宙去”,如果时间步数为5,有以下可能的样本和标签:

  • X:“想要有直升”,Y:“要有直升机”
  • X:“要有直升机”,Y:“有直升机,”
  • X:“有直升机,”,Y:“直升机,想”
  • X:“要和你飞到”,Y:“和你飞到宇”
  • X:“和你飞到宇”,Y:“你飞到宇宙”
  • X:“你飞到宇宙”,Y:“飞到宇宙去”

可以看到,如果序列的长度为T,时间步数为n,那么一共有T-n个合法的样本,但是这些样本有大量的重合,我们通常采用更加高效的采样方式。我们有两种方式对时序数据进行采样,分别是随机采样和相邻采样。

  • 随机采样与相邻采样demo
    随机采样
    下面的代码每次从数据里随机采样一个小批量。其中批量大小batch_size是每个小批量的样本数,num_steps是每个样本所包含的时间步数。
    在随机采样中,每个样本是原始序列上任意截取的一段序列,相邻的两个随机小批量在原始序列上的位置不一定相毗邻。
复制代码
    import torch
    import random
    def data_iter_random(corpus_indices, batch_size, num_steps, device=None):
    # 减1是因为对于长度为n的序列,X最多只有包含其中的前n - 1个字符
    num_examples = (len(corpus_indices) - 1) // num_steps  # 下取整,得到不重叠情况下的样本个数
    example_indices = [i * num_steps for i in range(num_examples)]  # 每个样本的第一个字符在corpus_indices中的下标
    random.shuffle(example_indices)
    
    def _data(i):
        # 返回从i开始的长为num_steps的序列
        return corpus_indices[i: i + num_steps]
    if device is None:
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    
    for i in range(0, num_examples, batch_size):
        # 每次选出batch_size个随机样本
        batch_indices = example_indices[i: i + batch_size]  # 当前batch的各个样本的首字符的下标
        X = [_data(j) for j in batch_indices]
        Y = [_data(j + 1) for j in batch_indices]
        yield torch.tensor(X, device=device), torch.tensor(Y, device=device)
    
    
    py
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/uANlD1y8IfXR7CZSMVbWzcKiw9ao.png)
复制代码
    def data_iter_consecutive(corpus_indices, batch_size, num_steps, device=None):
    if device is None:
        device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    corpus_len = len(corpus_indices) // batch_size * batch_size  # 保留下来的序列的长度
    corpus_indices = corpus_indices[: corpus_len]  # 仅保留前corpus_len个字符
    indices = torch.tensor(corpus_indices, device=device)
    indices = indices.view(batch_size, -1)  # resize成(batch_size, )
    batch_num = (indices.shape[1] - 1) // num_steps
    for i in range(batch_num):
        i = i * num_steps
        X = indices[:, i: i + num_steps]
        Y = indices[:, i + 1: i + num_steps + 1]
        yield X, Y
    
    
    py
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/WerKtIRj10UGuNpiZwLlJmdvD7CV.png)

注解:相邻采样是指把数据集线分为batchsize的大小的分数,每个batch取1个作为样本。

循环神经网络

本节介绍循环神经网络,下图展示了如何基于循环神经网络实现语言模型。我们的目的是基于当前的输入与过去的输入序列,预测序列的下一个字符。循环神经网络引入一个隐藏变量H,用H_{t}表示H在时间步t的值。H_{t}的计算基于X_{t}H_{t-1},可以认为H_{t}记录了到当前字符为止的序列信息,利用H_{t}对序列的下一个字符进行预测。
Image Name

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

我们先尝试从零开始实现一个基于字符级循环神经网络的语言模型,这里我们使用周杰伦的歌词作为语料,首先我们读入数据:

复制代码
    import torch
    import torch.nn as nn
    import time
    import math
    import sys
    sys.path.append("/home/kesci/input")
    import d2l_jay9460 as d2l
    (corpus_indices, char_to_idx, idx_to_char, vocab_size) = d2l.load_data_jay_lyrics()
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    
    
    python
    
    
复制代码
    def one_hot(x, n_class, dtype=torch.float32):
    result = torch.zeros(x.shape[0], n_class, dtype=dtype, device=x.device)  # shape: (n, n_class)
    result.scatter_(1, x.long().view(-1, 1), 1)  # result[i, x[i, 0]] = 1
    return result
    
    x = torch.tensor([0, 2])
    x_one_hot = one_hot(x, vocab_size)
    print(x_one_hot)
    print(x_one_hot.shape)
    print(x_one_hot.sum(axis=1))
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/GwTtUeJHOxvFCu8j06XPsclAakQB.png)
复制代码
    def to_onehot(X, n_class):
    return [one_hot(X[:, i], n_class) for i in range(X.shape[1])]
    
    X = torch.arange(10).view(2, 5)
    inputs = to_onehot(X, vocab_size)
    print(len(inputs), inputs[0].shape)
    
    
    python
    
    
复制代码
    num_inputs, num_hiddens, num_outputs = vocab_size, 256, vocab_size
    # num_inputs: d
    # num_hiddens: h, 隐藏单元的个数是超参数
    # num_outputs: q
    
    def get_params():
    def _one(shape):
        param = torch.zeros(shape, device=device, dtype=torch.float32)
        nn.init.normal_(param, 0, 0.01)
        return torch.nn.Parameter(param)
    
    # 隐藏层参数
    W_xh = _one((num_inputs, num_hiddens))
    W_hh = _one((num_hiddens, num_hiddens))
    b_h = torch.nn.Parameter(torch.zeros(num_hiddens, device=device))
    # 输出层参数
    W_hq = _one((num_hiddens, num_outputs))
    b_q = torch.nn.Parameter(torch.zeros(num_outputs, device=device))
    return (W_xh, W_hh, b_h, W_hq, b_q)
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/j1rCEDY7StnFRIGw4pizVBKZhd8M.png)
复制代码
    def rnn(inputs, state, params):
    # inputs和outputs皆为num_steps个形状为(batch_size, vocab_size)的矩阵
    W_xh, W_hh, b_h, W_hq, b_q = params
    H, = state
    outputs = []
    for X in inputs:
        H = torch.tanh(torch.matmul(X, W_xh) + torch.matmul(H, W_hh) + b_h)
        Y = torch.matmul(H, W_hq) + b_q
        outputs.append(Y)
    return outputs, (H,)
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/HgkiVQoSsvwzK3rTp4CR0jdJcfGq.png)
复制代码
    def init_rnn_state(batch_size, num_hiddens, device):
    return (torch.zeros((batch_size, num_hiddens), device=device), )
    print(X.shape)
    print(num_hiddens)
    print(vocab_size)
    state = init_rnn_state(X.shape[0], num_hiddens, device)
    inputs = to_onehot(X.to(device), vocab_size)
    params = get_params()
    outputs, state_new = rnn(inputs, state, params)
    print(len(inputs), inputs[0].shape)
    print(len(outputs), outputs[0].shape)
    print(len(state), state[0].shape)
    print(len(state_new), state_new[0].shape)
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/XTSU6FV1uPiEAoNRepn0hOIDsWaj.png)
复制代码
    def train_and_predict_rnn(rnn, get_params, init_rnn_state, num_hiddens,
                          vocab_size, device, corpus_indices, idx_to_char,
                          char_to_idx, is_random_iter, num_epochs, num_steps,
                          lr, clipping_theta, batch_size, pred_period,
                          pred_len, prefixes):
    if is_random_iter:
        data_iter_fn = d2l.data_iter_random
    else:
        data_iter_fn = d2l.data_iter_consecutive
    params = get_params()
    loss = nn.CrossEntropyLoss()
    
    for epoch in range(num_epochs):
        if not is_random_iter:  # 如使用相邻采样,在epoch开始时初始化隐藏状态
            state = init_rnn_state(batch_size, num_hiddens, device)
        l_sum, n, start = 0.0, 0, time.time()
        data_iter = data_iter_fn(corpus_indices, batch_size, num_steps, device)
        for X, Y in data_iter:
            if is_random_iter:  # 如使用随机采样,在每个小批量更新前初始化隐藏状态
                state = init_rnn_state(batch_size, num_hiddens, device)
            else:  # 否则需要使用detach函数从计算图分离隐藏状态
                for s in state:
                    s.detach_()
            # inputs是num_steps个形状为(batch_size, vocab_size)的矩阵
            inputs = to_onehot(X, vocab_size)
            # outputs有num_steps个形状为(batch_size, vocab_size)的矩阵
            (outputs, state) = rnn(inputs, state, params)
            # 拼接之后形状为(num_steps * batch_size, vocab_size)
            outputs = torch.cat(outputs, dim=0)
            # Y的形状是(batch_size, num_steps),转置后再变成形状为
            # (num_steps * batch_size,)的向量,这样跟输出的行一一对应
            y = torch.flatten(Y.T)
            # 使用交叉熵损失计算平均分类误差
            l = loss(outputs, y.long())
            
            # 梯度清0
            if params[0].grad is not None:
                for param in params:
                    param.grad.data.zero_()
            l.backward()
            grad_clipping(params, clipping_theta, device)  # 裁剪梯度
            d2l.sgd(params, lr, 1)  # 因为误差已经取过均值,梯度不用再做平均
            l_sum += l.item() * y.shape[0]
            n += y.shape[0]
    
        if (epoch + 1) % pred_period == 0:
            print('epoch %d, perplexity %f, time %.2f sec' % (
                epoch + 1, math.exp(l_sum / n), time.time() - start))
            for prefix in prefixes:
                print(' -', predict_rnn(prefix, pred_len, rnn, params, init_rnn_state,
                    num_hiddens, vocab_size, device, idx_to_char, char_to_idx))
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-07-12/XngfAzVLrF9qoBkQNKdSc37x0CbG.png)
复制代码
    num_epochs, num_steps, batch_size, lr, clipping_theta = 250, 35, 32, 1e2, 1e-2
    pred_period, pred_len, prefixes = 50, 50, ['分开', '不分开']
    
    train_and_predict_rnn(rnn, get_params, init_rnn_state, num_hiddens,
                      vocab_size, device, corpus_indices, idx_to_char,
                      char_to_idx, True, num_epochs, num_steps, lr,
                      clipping_theta, batch_size, pred_period, pred_len,
                      prefixes)
    
    
    python
    
    

全部评论 (0)

还没有任何评论哟~