昇思25天学习打卡营第26天| 基于 MindSpore 实现 BERT 对话情绪识别快速入门学习 - 自定义推理数据集介绍

环境配置

%%capture captured_output
# 实验环境已经预装了mindspore==2.2.14,如需更换mindspore版本,可更改下面mindspore的版本号
!pip uninstall mindspore -y
!pip install -i https://pypi.mirrors.ustc.edu.cn/simple mindspore==2.2.14
# 该案例在 mindnlp 0.3.1 版本完成适配,如果发现案例跑不通,可以指定mindnlp版本,执行`!pip install mindnlp==0.3.1`
!pip install mindnlp

Looking in indexes: https://pypi.mirrors.ustc.edu.cn/simple
Requirement already satisfied: mindnlp in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (0.3.1)
Requirement already satisfied: mindspore in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (2.2.14)
Requirement already satisfied: tqdm in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (4.66.4)
Requirement already satisfied: requests in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (2.32.3)
Requirement already satisfied: datasets in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (2.19.2)
Requirement already satisfied: evaluate in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (0.4.2)
Requirement already satisfied: tokenizers in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (0.19.1)
Requirement already satisfied: safetensors in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (0.4.3)
Requirement already satisfied: sentencepiece in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (0.2.0)
Requirement already satisfied: regex in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (2024.5.15)
Requirement already satisfied: addict in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (2.4.0)
Requirement already satisfied: ml-dtypes in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (0.4.0)
Requirement already satisfied: pyctcdecode in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (0.5.0)
Requirement already satisfied: jieba in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (0.42.1)
Requirement already satisfied: pytest7.2.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindnlp) (7.2.0)
Requirement already satisfied: attrs>=19.2.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pytest
7.2.0->mindnlp) (23.2.0)
Requirement already satisfied: iniconfig in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pytest7.2.0->mindnlp) (2.0.0)
Requirement already satisfied: packaging in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pytest
7.2.0->mindnlp) (23.2)
Requirement already satisfied: pluggy<2.0,>=0.12 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pytest7.2.0->mindnlp) (1.5.0)
Requirement already satisfied: exceptiongroup>=1.0.0rc8 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pytest
7.2.0->mindnlp) (1.2.0)
Requirement already satisfied: tomli>=1.0.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pytest==7.2.0->mindnlp) (2.0.1)
Requirement already satisfied: filelock in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (3.14.0)
Requirement already satisfied: numpy>=1.17 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (1.26.4)
Requirement already satisfied: pyarrow>=12.0.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (16.1.0)
Requirement already satisfied: pyarrow-hotfix in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (0.6)
Requirement already satisfied: dill<0.3.9,>=0.3.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (0.3.8)
Requirement already satisfied: pandas in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (2.2.2)
Requirement already satisfied: xxhash in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (3.4.1)
Requirement already satisfied: multiprocess in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (0.70.16)
Requirement already satisfied: fsspec<=2024.3.1,>=2023.1.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from fsspec[http]<=2024.3.1,>=2023.1.0->datasets->mindnlp) (2024.3.1)
Requirement already satisfied: aiohttp in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (3.9.5)
Requirement already satisfied: huggingface-hub>=0.21.2 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (0.23.3)
Requirement already satisfied: pyyaml>=5.1 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from datasets->mindnlp) (6.0.1)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from requests->mindnlp) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from requests->mindnlp) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from requests->mindnlp) (2.2.1)
Requirement already satisfied: certifi>=2017.4.17 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from requests->mindnlp) (2024.6.2)
Requirement already satisfied: protobuf>=3.13.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindspore->mindnlp) (5.27.0)
Requirement already satisfied: asttokens>=2.0.4 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindspore->mindnlp) (2.0.5)
Requirement already satisfied: pillow>=6.2.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindspore->mindnlp) (10.3.0)
Requirement already satisfied: scipy>=1.5.4 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindspore->mindnlp) (1.13.1)
Requirement already satisfied: psutil>=5.6.1 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindspore->mindnlp) (5.9.0)
Requirement already satisfied: astunparse>=1.6.3 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from mindspore->mindnlp) (1.6.3)
Requirement already satisfied: pygtrie<3.0,>=2.1 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pyctcdecode->mindnlp) (2.5.0)
Requirement already satisfied: hypothesis<7,>=6.14 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pyctcdecode->mindnlp) (6.103.1)
Requirement already satisfied: six in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from asttokens>=2.0.4->mindspore->mindnlp) (1.16.0)
Requirement already satisfied: wheel<1.0,>=0.23.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from astunparse>=1.6.3->mindspore->mindnlp) (0.43.0)
Requirement already satisfied: aiosignal>=1.1.2 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from aiohttp->datasets->mindnlp) (1.3.1)
Requirement already satisfied: frozenlist>=1.1.1 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from aiohttp->datasets->mindnlp) (1.4.1)
Requirement already satisfied: multidict<7.0,>=4.5 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from aiohttp->datasets->mindnlp) (6.0.5)
Requirement already satisfied: yarl<2.0,>=1.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from aiohttp->datasets->mindnlp) (1.9.4)
Requirement already satisfied: async-timeout<5.0,>=4.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from aiohttp->datasets->mindnlp) (4.0.3)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from huggingface-hub>=0.21.2->datasets->mindnlp) (4.11.0)
Requirement already satisfied: sortedcontainers<3.0.0,>=2.1.0 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from hypothesis<7,>=6.14->pyctcdecode->mindnlp) (2.4.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pandas->datasets->mindnlp) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pandas->datasets->mindnlp) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages (from pandas->datasets->mindnlp) (2024.1)
!pip show mindspore
Name: mindspore
Version: 2.2.14
Summary: MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.
Home-page: https://www.mindspore.cn
Author: The MindSpore Authors
Author-email: contact@mindspore.cn
License: Apache 2.0
Location: /home/nginx/miniconda/envs/jupyter/lib/python3.9/site-packages
Requires: asttokens, astunparse, numpy, packaging, pillow, protobuf, psutil, scipy
Required-by: mindnlp

在这里插入图片描述
在这里插入图片描述

基于 MindSpore 实现 BERT 对话情绪识别

模型简介

BERT全称是来自变换器的双向编码器表征量(Bidirectional Encoder Representations from Transformers),它是Google于2018年末开发并发布的一种新型语言模型。与BERT模型相似的预训练语言模型例如问答、命名实体识别、自然语言推理、文本分类等在许多自然语言处理任务中发挥着重要作用。模型是基于Transformer中的Encoder并加上双向的结构,因此一定要熟练掌握Transformer的Encoder的结构。

BERT模型的主要创新点都在pre-train方法上,即用了Masked Language Model和Next Sentence Prediction两种方法分别捕捉词语和句子级别的representation。

在用Masked Language Model方法训练BERT的时候,随机把语料库中15%的单词做Mask操作。对于这15%的单词做Mask操作分为三种情况:80%的单词直接用[Mask]替换、10%的单词直接替换成另一个新的单词、10%的单词保持不变。

因为涉及到Question Answering (QA) 和 Natural Language Inference (NLI)之类的任务,增加了Next Sentence Prediction预训练任务,目的是让模型理解两个句子之间的联系。与Masked Language Model任务相比,Next Sentence Prediction更简单些,训练的输入是句子A和B,B有一半的几率是A的下一句,输入这两个句子,BERT模型预测B是不是A的下一句。

BERT预训练之后,会保存它的Embedding table和12层Transformer权重(BERT-BASE)或24层Transformer权重(BERT-LARGE)。使用预训练好的BERT模型可以对下游任务进行Fine-tuning,比如:文本分类、相似度判断、阅读理解等。

对话情绪识别(Emotion Detection,简称EmoTect),专注于识别智能对话场景中用户的情绪,针对智能对话场景中的用户文本,自动判断该文本的情绪类别并给出相应的置信度,情绪类型分为积极、消极、中性。 对话情绪识别适用于聊天、客服等多个场景,能够帮助企业更好地把握对话质量、改善产品的用户交互体验,也能分析客服服务质量、降低人工质检成本。

下面以一个文本情感分类任务为例子来说明BERT模型的整个应用过程。

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
Building prefix dict from the default dictionary ...
Loading model from cache /tmp/jieba.cache
Loading model cost 1.019 seconds.
Prefix dict has been built successfully.
# 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表示积极),第二列是以空格分词的中文文本,如下示例,文件为 utf8 编码。

label–text_a

0–谁骂人了?我从来不骂人,我骂的都不是人,你是人吗 ?

1–我有事等会儿就回来和你聊

2–我见到你很高兴谢谢你帮我

这部分主要包括数据集读取,数据格式转换,数据 Tokenize 处理和 pad 操作。

# 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

–2024-06-12 03:09:55-- https://baidu-nlp.bj.bcebos.com/emotion_detection-dataset-1.0.0.tar.gz
Resolving baidu-nlp.bj.bcebos.com (baidu-nlp.bj.bcebos.com)… 113.200.2.111, 119.249.103.5, 2409:8c04:1001:1203:0:ff:b0bb:4f27
Connecting to baidu-nlp.bj.bcebos.com (baidu-nlp.bj.bcebos.com)|113.200.2.111|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 1710581 (1.6M) [application/x-gzip]
Saving to: ‘emotion_detection.tar.gz’

emotion_detection.t 100%[===================>] 1.63M 378KB/s in 4.4s

2024-06-12 03:10:00 (381 KB/s) - ‘emotion_detection.tar.gz’ saved [1710581/1710581]

data/
data/test.tsv
data/infer.tsv
data/dev.tsv
data/train.tsv
data/vocab.txt

在这里插入图片描述

数据加载和数据预处理

新建 process_dataset 函数用于数据加载和数据预处理,具体内容可见下面代码注释。

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

在这里插入图片描述

昇腾NPU环境下暂不支持动态Shape,数据预处理部分采用静态Shape处理:

from mindnlp.transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')
tokenizer.pad_token_id
0
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)
dataset_train.get_col_names()

[‘input_ids’, ‘attention_mask’, ‘labels’]
print(next(dataset_train.create_tuple_iterator()))
[Tensor(shape=[32, 64], dtype=Int64, value=
[[ 101, 872, 1599 … 0, 0, 0],
[ 101, 3264, 969 … 0, 0, 0],
[ 101, 6820, 3300 … 0, 0, 0],

[ 101, 2792, 809 … 0, 0, 0],
[ 101, 872, 3315 … 0, 0, 0],
[ 101, 5496, 2094 … 0, 0, 0]]), Tensor(shape=[32, 64], dtype=Int64, value=
[[1, 1, 1 … 0, 0, 0],
[1, 1, 1 … 0, 0, 0],
[1, 1, 1 … 0, 0, 0],

[1, 1, 1 … 0, 0, 0],
[1, 1, 1 … 0, 0, 0],
[1, 1, 1 … 0, 0, 0]]), Tensor(shape=[32], dtype=Int32, value= [1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1])]

模型构建

通过 BertForSequenceClassification 构建用于情感分类的 BERT 模型,加载预训练权重,设置情感三分类的超参数自动构建模型。后面对模型采用自动混合精度操作,提高训练的速度,然后实例化优化器,紧接着实例化评价指标,设置模型训练的权重保存策略,最后就是构建训练器,模型开始训练。

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)
The following parameters in checkpoint files are not loaded:
['cls.predictions.bias', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.dense.weight', 'cls.seq_relationship.bias', 'cls.seq_relationship.weight', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.LayerNorm.weight']
The following parameters in models are missing parameter:
['classifier.weight', 'classifier.bias']
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")

在这里插入图片描述

The train will start from the checkpoint saved in ‘checkpoint’.
0%| | 0/302 [00:00<?, ?it/s]
Checkpoint: ‘bert_emotect_epoch_0.ckpt’ has been saved in epoch: 0.
0%| | 0/34 [00:00<?, ?it/s]
Evaluate Score: {‘Accuracy’: 0.9398148148148148}
---------------Best Model: ‘bert_emotect_best.ckpt’ has been saved in epoch: 0.---------------
0%| | 0/302 [00:00<?, ?it/s]
Checkpoint: ‘bert_emotect_epoch_1.ckpt’ has been saved in epoch: 1.
0%| | 0/34 [00:00<?, ?it/s]
Evaluate Score: {‘Accuracy’: 0.9546296296296296}
---------------Best Model: ‘bert_emotect_best.ckpt’ has been saved in epoch: 1.---------------
0%| | 0/302 [00:00<?, ?it/s]
The maximum number of stored checkpoints has been reached.
Checkpoint: ‘bert_emotect_epoch_2.ckpt’ has been saved in epoch: 2.
0%| | 0/34 [00:00<?, ?it/s]
Evaluate Score: {‘Accuracy’: 0.9740740740740741}
---------------Best Model: ‘bert_emotect_best.ckpt’ has been saved in epoch: 2.---------------
0%| | 0/302 [00:00<?, ?it/s]
The maximum number of stored checkpoints has been reached.
Checkpoint: ‘bert_emotect_epoch_3.ckpt’ has been saved in epoch: 3.
0%| | 0/34 [00:00<?, ?it/s]
Evaluate Score: {‘Accuracy’: 0.9796296296296296}
---------------Best Model: ‘bert_emotect_best.ckpt’ has been saved in epoch: 3.---------------
0%| | 0/302 [00:00<?, ?it/s]
The maximum number of stored checkpoints has been reached.
Checkpoint: ‘bert_emotect_epoch_4.ckpt’ has been saved in epoch: 4.
0%| | 0/34 [00:00<?, ?it/s]
Evaluate Score: {‘Accuracy’: 0.9907407407407407}
---------------Best Model: ‘bert_emotect_best.ckpt’ has been saved in epoch: 4.---------------
Loading best model from ‘checkpoint’ with ‘[‘Accuracy’]’: [0.9907407407407407]…
---------------The model is already load the best model from ‘bert_emotect_best.ckpt’.---------------
CPU times: user 21min 59s, sys: 12min 38s, total: 34min 37s
Wall time: 14min 2s

模型验证

将验证数据集加再进训练好的模型,对数据集进行验证,查看模型在验证数据上面的效果,此处的评价指标为准确率。

evaluator = Evaluator(network=model, eval_dataset=dataset_test, metrics=metric)
evaluator.run(tgt_columns="labels")
  0%|          | 0/33 [00:00<?, ?it/s]
Evaluate Score: {'Accuracy': 0.888030888030888}

在这里插入图片描述

模型推理

遍历推理数据集,将结果与标签进行统一展示。

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)

inputs: ‘我 要 客观’, predict: ‘中性’ , label: ‘中性’
inputs: ‘靠 你 真是 说 废话 吗’, predict: ‘消极’ , label: ‘消极’
inputs: ‘口嗅 会’, predict: ‘中性’ , label: ‘中性’
inputs: ‘每次 是 表妹 带 窝 飞 因为 窝路痴’, predict: ‘中性’ , label: ‘中性’
inputs: ‘别说 废话 我 问 你 个 问题’, predict: ‘消极’ , label: ‘消极’
inputs: ‘4967 是 新加坡 那 家 银行’, predict: ‘中性’ , label: ‘中性’
inputs: ‘是 我 喜欢 兔子’, predict: ‘积极’ , label: ‘积极’
inputs: ‘你 写 过 黄山 奇石 吗’, predict: ‘中性’ , label: ‘中性’
inputs: ‘一个一个 慢慢来’, predict: ‘中性’ , label: ‘中性’
inputs: ‘我 玩 过 这个 一点 都 不 好玩’, predict: ‘消极’ , label: ‘消极’
inputs: ‘网上 开发 女孩 的 QQ’, predict: ‘中性’ , label: ‘中性’
inputs: ‘背 你 猜 对 了’, predict: ‘中性’ , label: ‘中性’
inputs: ‘我 讨厌 你 , 哼哼 哼 。 。’, predict: ‘消极’ , label: ‘消极’

自定义推理数据集

自己输入推理数据,展示模型的泛化能力。

predict("家人们咱就是说一整个无语住了 绝绝子叠buff")
inputs: '家人们咱就是说一整个无语住了 绝绝子叠buff', predict: '中性'

在这里插入图片描述

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: BERT-CRF是一种基于深度学习的序列标注模型,可以实现对自然语言文本进行序列标注。在实现BERT-CRF模型时,需要准备一个数据集,用于训练和评估模型的性能。 数据集BERT-CRF模型中非常重要的一部分,它决定了模型的性能和泛化能力。数据集应当包含一组有标注的样本,每个样本都应该是一个输入序列和其对应的标注序列。 对于自然语言处理任务,常用的数据集包括CoNLL-2003、OntoNotes、ACE2005等。这些数据集包含了大量的有标注文本数据,并被广泛应用于序列标注任务中。在准备数据集时,需要根据具体的任务和数据集格式对数据集进行处理。 对于CoNLL-2003数据集,其格式为每个词占据一行,每行包含9个字段,分别为:单词、词性、分块标签和4个NER标记。在处理数据集时,需要将每个样本中的文本与其对应的NER标签分离,并进行适当的编码和分割。 在BERT-CRF实现过程中,还需要考虑如何将输入向量化,并将其转换为能够被BERT模型接受的格式。一种常用的方法是使用bert-serving来将原始文本转换为BERT向量,然后将向量输入到CRF模型中进行标注。 总之,BERT-CRF实现需要准备一个有效的训练数据集数据集应当包含有标注的样本,并符合模型的输入格式。同时,还需要针对具体的任务和数据集格式对数据集进行适当的预处理和编码。 ### 回答2: BERT-CRF是一种基于BERT模型和条件随机场(CRF)的序列标注模型。在实现bert-crf之前,我们需要一个数据集来进行训练和测试。数据集就是文本序列上每个词的标注结果,例如分句、分词、命名实体标注、词性标注等。下面我们来介绍一下如何准备一个数据集。 首先,选择一个合适的任务,例如中文命名实体识别(NER)。NER任务是指识别文本数据中具有特定意义的实体,如人名、组织机构名、地名等。选择该任务的原因是其具有广泛的应用场景,适合用来演示bert-crf模型的实现流程。 接下来是数据收集和预处理。我们需要收集一些包括实体标注信息的文本语料库,并进行预处理,例如分词、去除停用词、标注实体、划分训练集和测试集等。在这一步可以使用一些工具来简化操作,例如jieba分词、StanfordNLP、pyltp等。最终得到的文本序列和标注序列是该数据集的核心部分。 然后是特征工程,即将文本序列和标注序列转化为模型可接受的特征格式。具体来说,需要将文本序列中每个词转化为对应的BERT向量表达形式,同时将标注序列转化为one-hot编码形式。这些特征都可以通过使用相应的Python工具来进行处理。 最后是模型训练和测试。BERT-CRF模型的训练可以使用已经训练好的BERT权重作为初始值,并在预训练期的基础上进行finetuning。模型测试时可以使用在预处理阶段划分的测试集进行验证,最终将预测的标注序列与真实标注序列进行比较,并计算评价指标,如精度、召回率、F1值等。 总之,准备一个数据集BERT-CRF模型实现的重要一步。数据集的质量将直接影响模型的表现效果和应用效果。因此,数据集的准确性和完备性都需要得到重视。 ### 回答3: BERT-CRF是一种自然语言处理技术,其基本想是结合 BERT(Bidirectional Encoder Representations from Transformers)预训练模型和条件随机场(CRF)来完成对于自然语言序列标注的任务。 在这种技术中,BERT被用来对输入文本进行特征提取,并且将提取的特征序列作为CRF模型的输入,CRF负责对序列进行标注。 对于BERT-CRF,数据集的构建非常重要。数据集必须包含大量的标记数据,即标有正确标注的文本的数据,以确保CRF模型的准确性和效果。构建数据集的主要步骤如下: 1. 定义标记标准 在构建数据集之前,需要定义标记标准。在自然语言处理任务中,标记通常包含实体标记、词义标记、词性标记等。标志标准将大大影响数据集的构建和模型的学习效果,因此必须尽可能严格定义。 2. 选择文本样本 选择文本样本时,需要选择具有代表性的样本来训练模型,应尽可能覆盖各种文本类型和语言风格。这些文本样本应来自于各种来源,例如新闻报道、论坛、社交媒体等。 3. 标记数据 将选定的文本样本转换为适合模型学习的标记数据。还可以利用现成的标记工具进行标记化,例如Stanford NER、spaCy等。 4. 数据预处理 对标注好的数据进行清洗、切分、建立词典等预处理操作,使其适合于BERT-CRF模型进行学习和训练。这些任务可以使用Python等语言的自然语言处理库来完成。 5. 划分数据集数据集划分为训练集、开发集和测试集,通过不断调整模型参数和超参数,并在开发集上测试结果来优化模型,最终在测试集上进行评估。 总而言之,BERT-CRF技术在自然语言处理领域的应用,需要基于一组标记良好且具有代表性的数据集。通过上述步骤,我们可以建立一个完善的数据集来支持BERT-CRF模型的学习和训练。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值