昇思25天学习打卡营第17天 | 基于MindSpore实现BERT对话情绪识别
文章目录
- [昇思25天学习打卡营第17天 | 基于MindSpore实现BERT对话情绪识别](#昇思25天学习打卡营第17天 | 基于MindSpore实现BERT对话情绪识别)
BERT模型
BERT(Bidirectional Encoder Representations from Transformers)是Google开发的一种新型语言模型,模型主要基于Transformer中的Encoder并加上双向的结构。
BERT的主要创新点在pre-train方法上,使用了
- Masked Language Model :随机把语料库中 15 % 15\% 15%的单词做Mask操作,对这些单词的Mask操作分为:
- 80 % 80\% 80%的单词直接用[Mask]替换;
- 10 % 10\% 10%的单词直接替换成另一个新的单词;
- 10 % 10\% 10%的单词保持不变。
- Next Sentence Prediction:目的是让模型理解两个句子之间的联系。训练的输入是句子A和B,B有一半的几率是A的下一句,BERT模型预测B是否为A的下一句。
对话情绪识别
对话情绪识别(Emotion Detection, EmoTect),专注于识别智能对话场景中用户的情绪。针对用户文本,自动判断该文本的情绪类别并给出相应的置信度。情绪类型分为积极、消极、中性。
BERT模型的文本情绪分类任务
数据集
python
import os
import mindspore
from mindspore.dataset import text, GeneratorDataset, transforms
from mindspore import nn, context
from mindnlp._legacy.engine import Trainer, Evaluator
from mindnlp._legacy.engine.callbacks import CheckpointCallback, BestModelCallback
from mindnlp._legacy.metrics import Accuracy
# prepare dataset
class SentimentDataset:
"""Sentiment Dataset"""
def __init__(self, path):
self.path = path
self._labels, self._text_a = [], []
self._load()
def _load(self):
with open(self.path, "r", encoding="utf-8") as f:
dataset = f.read()
lines = dataset.split("\n")
for line in lines[1:-1]:
label, text_a = line.split("\t")
self._labels.append(int(label))
self._text_a.append(text_a)
def __getitem__(self, index):
return self._labels[index], self._text_a[index]
def __len__(self):
return len(self._labels)
数据来自于百度飞桨团队,由两列组成,以制表符(\t
)分隔:
- 第一列是情绪分类的类别(0表示消极;1表示中性;2表示积极)
- 第二列是以空格分词的中文文本。
label--text_a
0--谁骂人了?我从来不骂人,我骂的都不是人,你是人吗 ?
1--我有事等会儿就回来和你聊
2--我见到你很高兴谢谢你帮我
数据下载
python
# download dataset
!wget https://baidu-nlp.bj.bcebos.com/emotion_detection-dataset-1.0.0.tar.gz -O emotion_detection.tar.gz
!tar xvf emotion_detection.tar.gz
数据加载与预处理
python
import numpy as np
def process_dataset(source, tokenizer, max_seq_len=64, batch_size=32, shuffle=True):
is_ascend = mindspore.get_context('device_target') == 'Ascend'
column_names = ["label", "text_a"]
dataset = GeneratorDataset(source, column_names=column_names, shuffle=shuffle)
# transforms
type_cast_op = transforms.TypeCast(mindspore.int32)
def tokenize_and_pad(text):
if is_ascend:
tokenized = tokenizer(text, padding='max_length', truncation=True, max_length=max_seq_len)
else:
tokenized = tokenizer(text)
return tokenized['input_ids'], tokenized['attention_mask']
# map dataset
dataset = dataset.map(operations=tokenize_and_pad, input_columns="text_a", output_columns=['input_ids', 'attention_mask'])
dataset = dataset.map(operations=[type_cast_op], input_columns="label", output_columns='labels')
# batch dataset
if is_ascend:
dataset = dataset.batch(batch_size)
else:
dataset = dataset.padded_batch(batch_size, pad_info={'input_ids': (None, tokenizer.pad_token_id),
'attention_mask': (None, 0)})
return dataset
from mindnlp.transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')
dataset_train = process_dataset(SentimentDataset("data/train.tsv"), tokenizer)
dataset_val = process_dataset(SentimentDataset("data/dev.tsv"), tokenizer)
dataset_test = process_dataset(SentimentDataset("data/test.tsv"), tokenizer, shuffle=False)
模型构建
通过BertForSequenceClassification构建用于情绪分类的BERT模型,加载预训练权重,设置情绪三分类的超参数自动构建模型。
python
from mindnlp.transformers import BertForSequenceClassification, BertModel
from mindnlp._legacy.amp import auto_mixed_precision
# set bert config and define parameters for training
model = BertForSequenceClassification.from_pretrained('bert-base-chinese', num_labels=3)
model = auto_mixed_precision(model, 'O1')
optimizer = nn.Adam(model.trainable_params(), learning_rate=2e-5)
metric = Accuracy()
# define callbacks to save checkpoints
ckpoint_cb = CheckpointCallback(save_path='checkpoint', ckpt_name='bert_emotect', epochs=1, keep_checkpoint_max=2)
best_model_cb = BestModelCallback(save_path='checkpoint', ckpt_name='bert_emotect_best', auto_load=True)
trainer = Trainer(network=model, train_dataset=dataset_train,
eval_dataset=dataset_val, metrics=metric,
epochs=5, optimizer=optimizer, callbacks=[ckpoint_cb, best_model_cb])
%%time
# start training
trainer.run(tgt_columns="labels")
模型验证
在验证集上对模型验证,查看模型在验证数据上的指标。
python
evaluator = Evaluator(network=model, eval_dataset=dataset_test, metrics=metric)
evaluator.run(tgt_columns="labels")
模型推理
python
dataset_infer = SentimentDataset("data/infer.tsv")
def predict(text, label=None):
label_map = {0: "消极", 1: "中性", 2: "积极"}
text_tokenized = Tensor([tokenizer(text).input_ids])
logits = model(text_tokenized)
predict_label = logits[0].asnumpy().argmax()
info = f"inputs: '{text}', predict: '{label_map[predict_label]}'"
if label is not None:
info += f" , label: '{label_map[label]}'"
print(info)
from mindspore import Tensor
for label, text in dataset_infer:
predict(text, label)
总结
这一节对BERT模型进行了介绍,其主要创新点为Masked Language Model和Next Sentence Prediction,这两种方法可以捕获词语和句子级别的特征表示。使用预训练的BERT模型,可以很方便的对下游任务进行Fine-tuning。这一节介绍了BERT在情绪分类任务上的应用。