当前位置:   article > 正文

python垃圾分类源代码_【自然语言处理】的迁移学习:微调BERT来对垃圾邮件进行分类【含Python演示】...

train_dataloader = dataloader(train_data, sample=train_sampler, batch_size=b

599ab16eeae833d9ecd18fc1e4ce3422.png

随着深度学习的发展,递归神经网络(RNN和LSTM)和卷积神经网络(CNN)等神经网络结构已经完成了自然语言处理(NLP)的大部分任务,它在文本分类、语言建模、机器翻译等性能上都有了很大的提高。

然而,与计算机视觉(Computer Vision)中的深度学习性能相比,自然语言处理的深度学习模型的性能就差强人意了。

5bcf3aafeef267253d83298e0db2d28e.png

原因之一在于缺少大型带标记的文本数据集。目前,大多数带标记的文本数据集对于自然语言处理的深度学习模型来说都不够“大”,不足以训练深度神经网络,因为深度神经网络的参数量很大,如果在小数据集上训练这样的网络会导致过拟合

  • (对于过拟合的概念解释有很多,小编这里摘来《数据挖掘-概念与技术》中的解释便于大家理解,“即在机器学习期间,它可能并入了训练数据中的某些特殊的异常点,这些异常不在一般数据集中出现。”)
  • (overfittingt是这样一种现象:一个假设在训练数据上能够获得比其他假设更好的拟合,但是在训练数据外的数据集上却不能很好的拟合数据。此时我们就叫这个假设出现了overfitting的现象。出现这种现象的主要原因是训练数据中存在噪音或者训练数据太少。)

除此之外,自然语言处理落后于计算机视觉发展的另一个重要原因是它缺乏迁移学习(transfer learning)。要知道,迁移学习在计算机视觉深度学习中发挥了重要作用。借助Imagenet等大型标记数据集的强可用性,基于CNN的深度模型训练成为可能——目前,这些大型标记数据集已经被广泛用作于计算机视觉任务的预训练模型了。

而在自然语言处理的深度学习上,直到2018年谷歌提出Transformer模型,NLP深度学习才算有了新的飞跃。

fe513a299908af7e5cec78d8d1cd05b1.png

本文将通过实际演示来解释如何调整BERT来进行文本分类(Text Classification),包括以下几个部分:

  1. 自然语言处理中的迁移学习
  2. 模型微调(Model Fine-Tuning)是什么意思?
  3. BERT简介
  4. 【实际演示】微调BERT来对垃圾邮件进行分类

1.自然语言处理中的迁移学习

迁移学习是一种将深度学习模型在大数据集里训练,然后在另一个数据集上执行类似任务的技术。我们称这种深度学习模型为预训练模型(Pre-trained Models)

预训练模型最著名的例子是在ImageNet数据集里训练的计算机视觉(Computer Vision)深度学习模型。解决问题的最好方式是使用一个预先训练好的模型,而不是从头开始构建一个模型。拿日常工作和生活举例,想必大家为了顺利甚至完美地提案,一定会提前不断地进行准备和模拟吧?迁移学习是一个道理。

c7445b934135fee47824bb8a2330fa73.png

随着近年来自然语言处理的发展,迁移学习成为了一种可行的选择。

NLP中的大部分任务,如文本分类、语言建模、机器翻译等,都是序列建模任务(Sequence Modeling tasks)。这种传统的机器学习模型和神经网络无法捕捉文本中出现的顺序信息(sequential information)。因此,人们开始使用递归神经网络(RNN和LSTM),这些结构可以建模文本中出现的顺序信息。

d50c3881e1cb20730250418ea8629093.png
一个典型的RNN

然而,递归神经网络也有局限,其中的主要问题是RNNs不能并行化(parallelized),它们一次只能接受一个输入。对于文本序列,RNN或LSTM每次输入只能接受一次切分(Token),即逐个地传递序列。如果在一个大数据集里训练这样一个模型会花费很多时间。

水涨船高的时间成本使在NLP里使用迁移学习的呼声不断,终于,在2018年,谷歌在《Attention is All You Need》一文中介绍了Transformer模型,这个模型成为了NLP深度学习的里程碑。

26be4a413b58c4b100aefa78a337e8ed.png
Transformer模型结构

很快,基于Transformer的NLP任务模型又多又快地发展起来。

使用Transformer的模型有很多优点,其中最重要的以下两点——

  1. 这些模型不是单个切分地处理输入的序列,而是将整个序列作为一次输入——这对于基于RNN的模型来说是一次速度的飞跃,因为这意味着现在模型可以靠GPUs加速了!
  2. 我们不需要标记数据来预训练这些模型了——我们只需要提供大量未标记的文本数据来训练基于Transformer的模型。然后我们可以将这个训练模型套用在其他NLP任务中,如文本分类(Text Classification)、命名实体识别(Named Entity Recognition)、文本生成(Text Generation)等。这就是在自然语言处理中迁移学习的工作方式。

BERT和GPT-2是当下最流行的基于Transformer的模型,

而在本文中,我们将重点关注BERT并学习如何使用预先训练好的BERT模型来执行文本分类

2. 模型微调(Model Fine-Tuning)是什么意思?

BERT(Bidirectional Encoder Representations from Transformers)是一个具有大量参数的大型神经网络架构,其参数量可以从1亿到3亿多个。所以,在一个小数据集上从零开始训练BERT模型会导致过拟合。

所以训练BERT模型需要从大型数据集开始,然后使用相对小的数据集上进行再训练模型,这个过程被称为模型微调(Model Fine-Tuning)

模型微调的几种方法:

  • 训练整个架构:我们可以在(相对较小的)训练数据集上的进一步训练整个预训练模型,并输出到softmax层。这种方法会让误差在整个架构中反向传播,并且模型的预训练权重会根据新的数据集进行更新。
  • 训练部分层,同时冻结其他层:另一种使用预训练模型的方法是部分训练。我们保持保持模型初始层的权重不变,而只对更高层进行再训练。这种方法需要我们自己尝试需要冻结多少层,训练多少层。
  • 冻结整个架构:这种方法是冻结整个预训练模型,加上一些我们自己的神经网络层,然后训练这个新模型。注意,这里只有附加层的权重会在训练期间更新。

本教程使用的是第三种方法,我们将在微调期间冻结整个BERT层,在其加上一个密集层和softmax层。

(softmax经常用在神经网络的最后一层,作为输出层,进行多分类。此外,softmax在增强学习领域内,softmax经常被用作将某个值转化为激活概率,这类情况下,softmax的公式如下:)

4b867d8185ef8d37b349d3b8603ba79f.png
带温度参数的softmax函数

3. BERT简介

让我们来看看BERT研究团队如何描述其NLP框架的吧:

BERT全称为 Bidirectional Encoder Representations from Transformers(来自Transformer的双向编码器表示)。它通过对左右上下文的共同条件作用,来预先训练未标记文本的深层双向表示。因此,预先训练好的BERT模型可以通过一个额外的输出层进行微调,从而为NLP任务创建最先进的模型。

感觉是不是很深奥,我们一起梳理梳理吧!

首先,BERT全称是Bidirectional Encoder Representations from Transformers。这里的每个单词都有其意义,我们接下来会逐一介绍。目前,这一行需要记住的关键内容是——BERT是基于Transformer架构的。

其次,BERT预先训练了大量未标记的文本语料库,包括整个Wikipedia(25亿个单词!)和图书语料库(8亿个单词)。

预训练是BERT的出色之处。因为 当我们在一个大文本语料库里训练模型时,模型就能对语言如何生成有更深入透彻的理解——这对几乎所有自然语言处理任务而言都是重中之重。

第三,BERT是一个“深度双向”模型。双向意味着BERT在训练阶段可以同时从切词的左边和右边学习信息。

想要了解更多关于BERT体系结构及其预训练的信息,大家可以阅读下面这篇文章:

Demystifying BERT: A Comprehensive Guide to the Groundbreaking NLP Framework​www.analyticsvidhya.com
fa1510a4b6fa9e19c7ea51503bf5b6a5.png

4.【实际演示】微调BERT来对垃圾邮件进行分类

现在我们将在ransformer库的帮助下对BERT模型进行微调,以执行文本分类——

问题陈述

在日常生活中接收的各类信息中,不免会有垃圾邮件。而我们的任务就是建立一个系统,可以自动检测消息是否是垃圾邮件。用例的数据集可以点击这里下载

安装Transformer库

我们将安装Huggingface的Transformer库。这个库允许导入大量基于Transformer的预训练模型。只需执行下面的代码来安装:

!pip install transformers

导入库

  1. import numpy as np
  2. import pandas as pd
  3. import torch
  4. import torch.nn as nn
  5. from sklearn.model_selection import train_test_split
  6. from sklearn.metrics import classification_report
  7. import transformers
  8. from transformers import AutoModel, BertTokenizerFast
  9. # specify GPU
  10. device = torch.device("cuda")

加载数据集

将数据集读入pandas数据框

  1. df = pd.read_csv("spamdata_v2.csv")
  2. df.head()

229d64c9b6cdb77fa44851ad8188a319.png

该数据集由两列——“标签”和“文本”组成。“文本”列包含消息正文,“标签”列是一个二进制定类变量,1表示垃圾邮件,0表示该消息不是垃圾邮件。

现在我们将把这个数据集分成三个集——用于训练、验证和测试。

  1. # split train dataset into train, validation and test sets

训练集和验证集用来对模型进行微调,并对测试集进行预测。

导入BERT模型和BERT切分

我们将导入有着1.1亿个参数的BERT模型。其实还有一个更大的BERT模型叫做BERT-large,它有3.45亿个参数。

  1. # import BERT-base pretrained model
  2. bert = AutoModel.from_pretrained('bert-base-uncased')
  3. # Load the BERT tokenizer
  4. tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')

让我们来看看这个BERT切分是怎么工作的吧。先试着使用它对几个句子进行编码:

  1. # sample data
  2. text = ["this is a bert model tutorial", "we will fine-tune a bert model"]
  3. # encode text
  4. sent_id = tokenizer.batch_encode_plus(text, padding=True)
  5. # output
  6. print(sent_id)

这是输出结果:

{‘input_ids’: [[101, 2023, 2003, 1037, 14324, 2944, 14924, 4818, 102, 0],
[101, 2057, 2097, 2986, 1011, 8694, 1037, 14324, 2944, 102]],

‘attention_mask’: [[1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}

可以看见,输出是一个包含两个条目的dictionary。

' input_ids '包含输入句子的整数序列。整数101和102是特殊切分。我们将它们添加到两个序列中,0表示填充切分。' attention_mask '包含1和0,它告诉模型要注意与掩码值1对应的标记并忽略其余的。

切分句子

  1. # get length of all the messages in the train set
  2. seq_len = [len(i.split()) for i in train_text]
  3. pd.Series(seq_len).hist(bins = 30)

edd06aaacee0c42e9ea28c7e5686d5ec.png

我们可以清楚地看到,大多数句子的长度为25个字符或更少。而最大长度是175。如果我们选择175作为填充长度那么所有输入序列长度为175,大部分的标记在这些序列将填充标记不会帮助模型学习任何有用的东西,最重要的是,它会使训练速度较慢。

因此,我们将设25为填充长度。

  1. # tokenize and encode sequences in the training set
  2. tokens_train = tokenizer.batch_encode_plus(
  3. train_text.tolist(),
  4. max_length = 25,
  5. pad_to_max_length=True,
  6. truncation=True
  7. )
  8. # tokenize and encode sequences in the validation set
  9. tokens_val = tokenizer.batch_encode_plus(
  10. val_text.tolist(),
  11. max_length = 25,
  12. pad_to_max_length=True,
  13. truncation=True
  14. )
  15. # tokenize and encode sequences in the test set
  16. tokens_test = tokenizer.batch_encode_plus(
  17. test_text.tolist(),
  18. max_length = 25,
  19. pad_to_max_length=True,
  20. truncation=True

我们现在已经将训练,验证和测试集中的句子转换为每个长度为25个的切分整数序列。 接下来,我们需要将整数序列转换为张量。

  1. ## convert lists to tensors
  2. train_seq = torch.tensor(tokens_train['input_ids'])
  3. train_mask = torch.tensor(tokens_train['attention_mask'])
  4. train_y = torch.tensor(train_labels.tolist())
  5. val_seq = torch.tensor(tokens_val['input_ids'])
  6. val_mask = torch.tensor(tokens_val['attention_mask'])
  7. val_y = torch.tensor(val_labels.tolist())
  8. test_seq = torch.tensor(tokens_test['input_ids'])
  9. test_mask = torch.tensor(tokens_test['attention_mask'])
  10. test_y = torch.tensor(test_labels.tolist())

现在我们将为训练集和验证集创建dataloaders,这些dataloaders将在训练阶段将成批的训练数据和验证数据作为输入传递给模型。

  1. from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
  2. #define a batch size
  3. batch_size = 32
  4. # wrap tensors
  5. train_data = TensorDataset(train_seq, train_mask, train_y)
  6. # sampler for sampling the data during training
  7. train_sampler = RandomSampler(train_data)
  8. # dataLoader for train set
  9. train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size)
  10. # wrap tensors
  11. val_data = TensorDataset(val_seq, val_mask, val_y)
  12. # sampler for sampling the data during training
  13. val_sampler = SequentialSampler(val_data)
  14. # dataLoader for validation set
  15. val_dataloader = DataLoader(val_data, sampler = val_sampler, batch_size=batch_size)

定义模型架构

小编在前文说到了,本次使用的微调方法是第三种,即在对模型进行微调之前,会冻结模型的所有层。如果有小伙伴希望微调BERT模型的预训练权重,那么就不需要执行下面这段代码。

  1. # freeze all the parameters
  2. for param in bert.parameters():
  3. param.requires_grad = False

接下来,就到定义我们的模型架构的时候了

  1. class BERT_Arch(nn.Module):
  2. def __init__(self, bert):
  3. super(BERT_Arch, self).__init__()
  4. self.bert = bert
  5. # dropout layer
  6. self.dropout = nn.Dropout(0.1)
  7. # relu activation function
  8. self.relu = nn.ReLU()
  9. # dense layer 1
  10. self.fc1 = nn.Linear(768,512)
  11. # dense layer 2 (Output layer)
  12. self.fc2 = nn.Linear(512,2)
  13. #softmax activation function
  14. self.softmax = nn.LogSoftmax(dim=1)
  15. #define the forward pass
  16. def forward(self, sent_id, mask):
  17. #pass the inputs to the model
  18. _, cls_hs = self.bert(sent_id, attention_mask=mask)
  19. x = self.fc1(cls_hs)
  20. x = self.relu(x)
  21. x = self.dropout(x)
  22. # output layer
  23. x = self.fc2(x)
  24. # apply softmax activation
  25. x = self.softmax(x)
  26. return x
  1. # pass the pre-trained BERT to our define architecture
  2. model = BERT_Arch(bert)
  3. # push the model to GPU
  4. model = model.to(device)

我们将使用AdamW作为优化器。它是Adam优化器的改进版本。想要了解更多信息,请查阅本文。

  1. # optimizer from hugging face transformers
  2. from transformers import AdamW
  3. # define the optimizer
  4. optimizer = AdamW(model.parameters(),
  5. lr = 1e-5) # learning rate

在我们的数据集中有一个类出现了不平衡。大多数的观察结果并不是垃圾邮件。因此,我们将首先计算训练集合中标签的类权重,然后将这些权重传递给损失函数,这样它就能处理该类的不平衡了。

  1. from sklearn.utils.class_weight import compute_class_weight
  2. #compute the class weights
  3. class_weights = compute_class_weight('balanced', np.unique(train_labels), train_labels)
  4. print("Class Weights:",class_weights)

输出:[0.57743559 3.72848948]

  1. # converting list of class weights to a tensor
  2. weights= torch.tensor(class_weights,dtype=torch.float)
  3. # push to GPU
  4. weights = weights.to(device)
  5. # define the loss function
  6. cross_entropy = nn.NLLLoss(weight=weights)
  7. # number of training epochs
  8. epochs = 10

微调BERT

目前为止,我们已经定义了模型架构,指定了优化器和损失函数,并且我们的dataloaders也设定完毕。现在,我们必须分别定义两个函数来训练(微调)和评估模型

  1. # function to train the model
  2. def train():
  3. model.train()
  4. total_loss, total_accuracy = 0, 0
  5. # empty list to save model predictions
  6. total_preds=[]
  7. # iterate over batches
  8. for step,batch in enumerate(train_dataloader):
  9. # progress update after every 50 batches.
  10. if step % 50 == 0 and not step == 0:
  11. print(' Batch {:>5,} of {:>5,}.'.format(step, len(train_dataloader)))
  12. # push the batch to gpu
  13. batch = [r.to(device) for r in batch]
  14. sent_id, mask, labels = batch
  15. # clear previously calculated gradients
  16. model.zero_grad()
  17. # get model predictions for the current batch
  18. preds = model(sent_id, mask)
  19. # compute the loss between actual and predicted values
  20. loss = cross_entropy(preds, labels)
  21. # add on to the total loss
  22. total_loss = total_loss + loss.item()
  23. # backward pass to calculate the gradients
  24. loss.backward()
  25. # clip the the gradients to 1.0. It helps in preventing the exploding gradient problem
  26. torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
  27. # update parameters
  28. optimizer.step()
  29. # model predictions are stored on GPU. So, push it to CPU
  30. preds=preds.detach().cpu().numpy()
  31. # append the model predictions
  32. total_preds.append(preds)
  33. # compute the training loss of the epoch
  34. avg_loss = total_loss / len(train_dataloader)
  35. # predictions are in the form of (no. of batches, size of batch, no. of classes).
  36. # reshape the predictions in form of (number of samples, no. of classes)
  37. total_preds = np.concatenate(total_preds, axis=0)
  38. #returns the loss and predictions
  39. return avg_loss, total_preds

现在,就让我们开始微调模型吧!

  1. # function for evaluating the model
  2. def evaluate():
  3. print("nEvaluating...")
  4. # deactivate dropout layers
  5. model.eval()
  6. total_loss, total_accuracy = 0, 0
  7. # empty list to save the model predictions
  8. total_preds = []
  9. # iterate over batches
  10. for step,batch in enumerate(val_dataloader):
  11. # Progress update every 50 batches.
  12. if step % 50 == 0 and not step == 0:
  13. # Calculate elapsed time in minutes.
  14. elapsed = format_time(time.time() - t0)
  15. # Report progress.
  16. print(' Batch {:>5,} of {:>5,}.'.format(step, len(val_dataloader)))
  17. # push the batch to gpu
  18. batch = [t.to(device) for t in batch]
  19. sent_id, mask, labels = batch
  20. # deactivate autograd
  21. with torch.no_grad():
  22. # model predictions
  23. preds = model(sent_id, mask)
  24. # compute the validation loss between actual and predicted values
  25. loss = cross_entropy(preds,labels)
  26. total_loss = total_loss + loss.item()
  27. preds = preds.detach().cpu().numpy()
  28. total_preds.append(preds)
  29. # compute the validation loss of the epoch
  30. avg_loss = total_loss / len(val_dataloader)
  31. # reshape the predictions in form of (number of samples, no. of classes)
  32. total_preds = np.concatenate(total_preds, axis=0)
  33. return avg_loss, total_preds

输出:

  1. Training Loss: 0.592
  2. Validation Loss: 0.567
  3. Epoch 5 / 10
  4. Batch 50 of 122.
  5. Batch 100 of 122.
  6. Evaluating...
  7. Training Loss: 0.566
  8. Validation Loss: 0.543
  9. Epoch 6 / 10
  10. Batch 50 of 122.
  11. Batch 100 of 122.
  12. Evaluating...
  13. Training Loss: 0.552
  14. Validation Loss: 0.525
  15. Epoch 7 / 10
  16. Batch 50 of 122.
  17. Batch 100 of 122.
  18. Evaluating...
  19. Training Loss: 0.525
  20. Validation Loss: 0.498
  21. Epoch 8 / 10
  22. Batch 50 of 122.
  23. Batch 100 of 122.
  24. Evaluating...
  25. Training Loss: 0.507
  26. Validation Loss: 0.477
  27. Epoch 9 / 10
  28. Batch 50 of 122.
  29. Batch 100 of 122.
  30. Evaluating...
  31. Training Loss: 0.488
  32. Validation Loss: 0.461
  33. Epoch 10 / 10
  34. Batch 50 of 122.
  35. Batch 100 of 122.
  36. Evaluating...
  37. Training Loss: 0.474
  38. Validation Loss: 0.454

可以看到,在第10纪元后,验证损失扔在减少,这意味着你可以你可以尝试更多训练纪元。现在就让我们看看它在测试数据集上的表现如何吧:

预测

我们需要先加载在训练过程中的最佳模型权重:

  1. #load weights of best model
  2. path = 'saved_weights.pt'
  3. model.load_state_dict(torch.load(path))

使用微调模型对数据集做出预测:

  1. # get predictions for test data
  2. with torch.no_grad():
  3. preds = model(test_seq.to(device), test_mask.to(device))
  4. preds = preds.detach().cpu().numpy()

来看看模型的表现如何吧!

  1. preds = np.argmax(preds, axis = 1)
  2. print(classification_report(test_y, preds))

输出:

fc50e245779fce29cf0ec9987975bfa7.png

对于类1来说,召回率和精确度都相当高,这意味着该模型可以很好地预测该类。

然而,我们的目标是检测垃圾邮件,因此对第1类(垃圾邮件)样本的误分类要比对第0类样本的误分类更为重要。

让我们看看第1类的召回率——0.90,这意味着该模型能够正确地分类90%的垃圾邮件。但其精度稍低了些,这说明模型将一些0类消息(不是垃圾邮件)错误地归类为垃圾邮件了。

小结

我们对一个预先训练好的BERT模型进行了微调,将其使用在非常小的数据集上执行文本分类。大家可以在不同的数据集上对BERT进行微调,看看它的表现如何,甚至可以使用BERT来执行多类或多标签分类。

当然,如果小伙伴们有更大的数据集,当然可以去尝试训练整个BERT体系结构!

如果小伙伴们觉得本文有意思,欢迎点赞收藏留言!也欢迎各位小伙伴关注【自然语言处理】学习帐,小编会定期更新最新鲜的自然语言处理实操案例,让我们一起体会数据秩序之美吧!

【自然语言处理】学习帐​www.zhihu.com
98cd2edfe1327b93739ea4168897ea48.png
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/348819
推荐阅读
相关标签
  

闽ICP备14008679号