Task 02- 文本预处理-语言模型-RNN基础

Task 02 文本预处理-语言模型-RNN基础

1 文本预处理

  • 目的:将无法直接输入的文本数据转化为数值形式进行处理
  • 基本步骤:文本读入 --> 分词 --> 建立字典 --> 词-字典索引转换

1.0 python语法

# 三元表达式
res = x if x > y else y

# for循环
l = []
for i in range(10):
	l.append(i * i)

# 列表生成式 (可用于字典)
l = [i * i for i in range(10)]

# 正则表达式(regular expression)
# re.sub(pattern, replacement, string)
re.sub('[^a-z]+', ' ', line.strip().lower())
# 去除line中首尾字符(strip),并将line中非a-z的字符替换为空格,并全部转换成小写(lower)

1.1 文本读入

with open('./text.txt', 'r') as f:
    lines = [re.sub('[^a-z]+', ' ', line.strip().lower()) for line in f]

1.2 分词(tokenize)

即将句子切分为若干个词(token),转换为一个词的序列。

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)

1.3 建立字典

  • 建立Vocab类,主要包含idx_to_token(其实是list)和token_to_idx的索引;

  • 特殊token:pad(序列填充符), bos(序列起始符), eos(序列终止符), unk(未知token符)

import collections

def count_corpus(sentences):
    tokens = [tk for st in sentences for tk in st]
    return collections.Counter(tokens)  # 返回一个字典,记录每个词的出现次数

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):
        '''输入token,返回索引编号'''
        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):
        '''输入索引编号,返回token'''
        if not isinstance(indices, (list, tuple)):
            return self.idx_to_token[indices]
        return [self.idx_to_token[index] for index in indices]


vocab = Vocab(tokens) # 建立Vocab对象

# Input:
vocab.__getitem__(['time','machine'])
# Output:
[2,3]

# Input:
vocab.to_tokens([2,3])
# Output:
['time','machine']

1.4 现有分词工具(NLP库)

spaCy - Industrial-Strength Natural Language Processing

import spacy

# Load English tokenizer, tagger, parser, NER and word vectors
nlp = spacy.load("en_core_web_sm")

# Process whole documents
doc = nlp(text)

print([token.text for token in doc])

NLTK - Natural Language ToolKit

import nltk
nltk.word_tokenize(sentence)

2 语言模型(language model)

语言模型(language model)是自然语言处理的重要技术;

自然语言文本常看作一段离散的时间序列;

假设一段长度为 T T 的⽂本中的词依次为 w 1 ; w 2 ; ; w T w_1; w_2;\cdots; w_T ,那么在离散的时间序列中, w t ( 1 t T ) w_t(1 ≤ t ≤ T) 可看作在时间步(time step) t t 的输出或标签 ;

语言模型计算序列的概率:
P ( w 1 , w 2 , , w T ) . P(w_1,w_2,\cdots,w_T).

2.1 语言模型计算

$$
\begin{align*}
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})
\end{align*}

$$

扫描二维码关注公众号,回复: 9149824 查看本文章
  • 一个含4个词的句子:

P ( w 1 , w 2 , w 3 , w 4 ) = P ( w 1 ) P ( w 2 w 1 ) P ( w 3 w 1 , w 2 ) P ( w 4 w 1 , w 2 , 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_1, w_2, w_3)

  • 其中, n n 为语料库总大小, n ( w 1 ) n(w_1) w 1 w_1 的出现频率,其他同理:

P ^ ( w 1 ) = n ( w 1 ) n P ^ ( w 2 w 1 ) = n ( w 1 , w 2 ) n ( w 1 ) \hat P(w_1) = \frac{n(w_1)}{n} \\ \hat P(w_2 \mid w_1) = \frac{n(w_1, w_2)}{n(w_1)}

2.2 n n 元语法(n-gram)

  • 序列长度增加,语言模型计算和存储多个词共同出现的概率的复杂度会呈指数级增加

  • n n 元语法基于 ( n 1 ) (n-1) 阶马尔可夫假设对语言模型进行简化计算:
    P ( w 1 , w 2 , , w T ) = t = 1 T P ( w t w t ( n 1 ) , , w t 1 ) u n i g r a m : P ( w 1 , w 2 , w 3 , w 4 ) = P ( w 1 ) P ( w 2 ) P ( w 3 ) P ( w 4 ) , b i g r a m : P ( w 1 , w 2 , w 3 , w 4 ) = P ( w 1 ) P ( w 2 w 1 ) P ( w 3 w 2 ) P ( w 4 w 3 ) , t r i g r a m : P ( w 1 , w 2 , w 3 , w 4 ) = P ( w 1 ) P ( w 2 w 1 ) P ( w 3 w 1 , w 2 ) P ( w 4 w 2 , w 3 ) P(w_1, w_2, \ldots, w_T) = \prod_{t=1}^T P(w_t \mid w_{t-(n-1)}, \ldots, w_{t-1}) \\ \begin{aligned} unigram:P(w_1, w_2, w_3, w_4) &= P(w_1) P(w_2) P(w_3) P(w_4) ,\\ bigram: 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) ,\\ trigram: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) \end{aligned}

2.3 序列数据的采样

  • 序列数据样本的特点:一个样本通常包含连续的字符,不同样本可能存在大面积的重复
  • 如果序列的长度为 T T ,时间步数为 n n ,那么一共有 ( T n ) (T−n) 个合法样本,但是这些样本有大量的重合,故需要对原始序列进行采样

2.3.1 随机采样

  • 随机抽取不重叠的样本
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)

2.3.2 相邻采样

  • 将原始序列上相邻的样本划分至不同批中
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

3 循环神经网络(Recurrent Neural Network, RNN)

3.1 表示方法

  • 常用于序列数据的建模与预测
  • 在MLP的基础上,通过**隐藏状态的传递**来表达序列关系,见下图中 H 1 , 2 , 3 , 4 , 5 H_{1,2,3,4,5}

在这里插入图片描述

3.2 模型运算

  • X t R n × d \boldsymbol{X}_t \in \mathbb{R}^{n \times d} 是时间步 t t 的小批量输入, H t R n × h \boldsymbol{H}_t \in \mathbb{R}^{n \times h} 是该时间步的隐藏变量,则:
    H t = ϕ ( X t W x h + H t 1 W h h + b h ) O t = H t W h q + b q \boldsymbol{H}_t = \phi(\boldsymbol{X}_t \boldsymbol{W}_{xh} + \boldsymbol{H}_{t-1} \boldsymbol{W}_{hh} + \boldsymbol{b}_h) \\ \boldsymbol{O}_t = \boldsymbol{H}_t \boldsymbol{W}_{hq} + \boldsymbol{b}_q \\
    其中, W h q R h × q , b q R 1 × q . \boldsymbol{W}_{hq} \in \mathbb{R}^{h \times q},\boldsymbol{b}_q \in \mathbb{R}^{1 \times q}.

3.3 模型构建

  • 参数初始化
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)
  • 定义模型
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,)

# rnn state初始化
def init_rnn_state(batch_size, num_hiddens, device):
    return (torch.zeros((batch_size, num_hiddens), device=device), )
  • 梯度裁剪(反向传播时,根据链式法则梯度将会累乘,为了防止梯度过大/梯度爆炸,对梯度大小进行约束)
def grad_clipping(params, theta, device):
    norm = torch.tensor([0.0], device=device)
    for param in params:
        norm += (param.grad.data ** 2).sum()
    norm = norm.sqrt().item()
    if norm > theta:
        for param in params:
            param.grad.data *= (theta / norm)
  • 前向计算
def predict_rnn(prefix, num_chars, rnn, params, init_rnn_state,
                num_hiddens, vocab_size, device, idx_to_char, char_to_idx):
    state = init_rnn_state(1, num_hiddens, device)
    output = [char_to_idx[prefix[0]]]   # output记录prefix加上预测的num_chars个字符
    for t in range(num_chars + len(prefix) - 1):
        # 将上一时间步的输出作为当前时间步的输入
        X = to_onehot(torch.tensor([[output[-1]]], device=device), vocab_size)
        # 计算输出和更新隐藏状态
        (Y, state) = rnn(X, state, params)
        # 下一个时间步的输入是prefix里的字符或者当前的最佳预测字符
        if t < len(prefix) - 1:
            output.append(char_to_idx[prefix[t + 1]])
        else:
            output.append(Y[0].argmax(dim=1).item())
    return ''.join([idx_to_char[i] for i in output])
  • 困惑度(perplexity)

P ( W ) = e i = 1 N y i log y ^ i N P(\bold W)=e^{-\frac{\sum_{i=1}^N y_i\log{\hat{y}_i}}{N}}

  • 定义模型训练
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))

3.4 PyTorch模型构建

import torch

rnn_layer = torch.nn.RNN(input_size=vocab_size, hidden_size=num_hiddens)
num_steps, batch_size = 35, 2
X = torch.rand(num_steps, batch_size, vocab_size)
state = None
Y, state_new = rnn_layer(X, state)
print(Y.shape, state_new.shape)


class RNNModel(nn.Module):
    def __init__(self, rnn_layer, vocab_size):
        super(RNNModel, self).__init__()
        self.rnn = rnn_layer
        self.hidden_size = rnn_layer.hidden_size * (2 if rnn_layer.bidirectional else 1) 
        self.vocab_size = vocab_size
        self.dense = nn.Linear(self.hidden_size, vocab_size)

    def forward(self, inputs, state):
        # inputs.shape: (batch_size, num_steps)
        X = to_onehot(inputs, vocab_size)
        X = torch.stack(X)  # X.shape: (num_steps, batch_size, vocab_size)
        hiddens, state = self.rnn(X, state)
        hiddens = hiddens.view(-1, hiddens.shape[-1])  # hiddens.shape: (num_steps * batch_size, hidden_size)
        output = self.dense(hiddens)
        return output, state
    
    
def predict_rnn_pytorch(prefix, num_chars, model, vocab_size, device, idx_to_char, char_to_idx):
    state = None
    output = [char_to_idx[prefix[0]]]  # output记录prefix加上预测的num_chars个字符
    for t in range(num_chars + len(prefix) - 1):
        X = torch.tensor([output[-1]], device=device).view(1, 1)
        (Y, state) = model(X, state)  # 前向计算不需要传入模型参数
        if t < len(prefix) - 1:
            output.append(char_to_idx[prefix[t + 1]])
        else:
            output.append(Y.argmax(dim=1).item())
    return ''.join([idx_to_char[i] for i in output])


def train_and_predict_rnn_pytorch(model, num_hiddens, vocab_size, device, corpus_indices, idx_to_char, char_to_idx, num_epochs, num_steps, lr, clipping_theta, batch_size, pred_period, pred_len, prefixes):
    loss = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=lr)
    model.to(device)
    for epoch in range(num_epochs):
        l_sum, n, start = 0.0, 0, time.time()
        data_iter = d2l.data_iter_consecutive(corpus_indices, batch_size, num_steps, device) # 相邻采样
        state = None
        for X, Y in data_iter:
            if state is not None:
                # 使用detach函数从计算图分离隐藏状态
                if isinstance (state, tuple): # LSTM, state:(h, c)  
                    state[0].detach_()
                    state[1].detach_()
                else: 
                    state.detach_()
            (output, state) = model(X, state) # output.shape: (num_steps * batch_size, vocab_size)
            y = torch.flatten(Y.T)
            l = loss(output, y.long())
            
            optimizer.zero_grad()
            l.backward()
            grad_clipping(model.parameters(), clipping_theta, device)
            optimizer.step()
            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_pytorch(
                    prefix, pred_len, model, vocab_size, device, idx_to_char,
                    char_to_idx))
            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_pytorch(
                    prefix, pred_len, model, vocab_size, device, idx_to_char,
                    char_to_idx))
发布了3 篇原创文章 · 获赞 0 · 访问量 28

猜你喜欢

转载自blog.csdn.net/qq_35489107/article/details/104312320
今日推荐