当前位置:   article > 正文

PaddleNLP评论观点抽取和属性级情感分析

评论观点抽取

项目地址:PaddleNLP评论观点抽取和属性级情感分析 - 飞桨AI Studio星河社区 (baidu.com) 

情感分析旨在对带有情感色彩的主观性文本进行分析、处理、归纳和推理,其广泛应用于消费决策、舆情分析、个性化推荐等领域,具有很高的商业价值。

依托百度领先的情感分析技术,食行生鲜自动生成菜品评论标签辅助用户购买,并指导运营采购部门调整选品和促销策略;房天下向购房者和开发商直观展示楼盘的用户口碑情况,并对好评楼盘置顶推荐;国美搭建服务智能化评分系统,客服运营成本减少40%,负面反馈处理率100%。

情感分析相关的任务有语句级情感分析、评论对象抽取、观点抽取等等。一般来讲,被人们所熟知的情感分析任务是语句级别的情感分析,该任务是在宏观上去分析整句话的感情色彩,其粒度可能相对比较粗。

因为在人们进行评论的时候,往往针对某一产品或服务进行多个属性的评论,对每个属性的评论可能也会褒贬不一,因此针对属性级别的情感分析在真实的场景中会更加实用,同时更能给到企业用户或商家更加具体的建议。例如这句关于薯片的评论。

这个薯片味道真的太好了,口感很脆,只是包装很一般。

可以看到,顾客在口感、包装和味道 三个属性上对薯片进行了评价,顾客在味道和口感两个方面给出了好评,但是在包装上给出了负面的评价。只有通过这种比较细粒度的分析,商家才能更有针对性的发现问题,进而改进自己的产品或服务。

基于这样的考虑,本项目提出了一种细粒度的情感分析能力,对于给定的文本,首先会抽取该文本中的评论观点,然后分析不同观点的情感极性。

1. 方案设计

那么应该如何细粒度的分析语句中不同方面的评论呢?本实践的解决方案大致分为两个环节,首先需要进行评论观点抽取,接下来,便可以根据该评论观点去分析相应观点的情感极性。

1.1 评论观点抽取

在本实践中,我们将采用序列标注的方式进行评论观点抽取,具体而言,会抽取评论中的属性以及属性对应的观点,为此我们基于BIO的序列标注体系进行了标签的拓展:B-Aspect, I-Aspect, B-Opinion, I-Opinion, O,其中前两者用于标注评论属性,后两者用于标注相应观点。

如图1所示,首先将文本串传入SKEP模型中,利用SKEP模型对该文本串进行语义编码后,然后基于每个位置的输出去预测相应的标签。

图1 评价观点抽取模型 

1.2 属性级情感分类

在抽取完评论观点之后,便可以有针对性的对各个属性进行评论。具体来讲,本实践将抽取出的评论属性和评论观点进行拼接,然后和原始语句进行拼接作为一条独立的训练语句。

如图2所示,首先将评论属性和观点词进行拼接为"味道好",然后将"味道好"和原文进行拼接,然后传入SKEP模型,并使用"CLS"位置的向量进行细粒度情感倾向。

图2 属性级情感分类模型

2. 评论观点抽取模型

2.1 数据处理

2.1.1 数据集介绍

本实践中包含训练集、评估和测试3项数据集,以及1个标签词典。其中标签词典记录了本实践中用于抽取评论对象和观点词时使用的标签。

另外,本实践将采用序列标注的方式完成此任务,所以本数据集中需要包含两列数据:文本串和相应的序列标签数据,下面给出了一条样本。

还不错价格不高,服务好 O B-Opinion O B-Aspect I-Aspect B-Opinion I-Opinion O B-Aspect I-Aspect B-Opinion

2.2.2 数据加载

本节我们将训练、评估和测试数据集,以及标签词典加载到内存中。相关代码如下:

  1. import os
  2. import argparse
  3. from functools import partial
  4. import paddle
  5. import paddle.nn.functional as F
  6. from paddlenlp.metrics import ChunkEvaluator
  7. from paddlenlp.datasets import load_dataset
  8. from paddlenlp.data import Pad, Stack, Tuple
  9. from paddlenlp.transformers import SkepTokenizer, SkepModel, LinearDecayWithWarmup
  10. from utils.utils import set_seed
  11. from utils import data_ext, data_cls
  12. train_path = "./data/data121190/train_ext.txt"
  13. dev_path = "./data/data121190/dev_ext.txt"
  14. test_path = "./data/data121190/test_ext.txt"
  15. label_path = "./data/data121190/label_ext.dict"
  16. # load and process data
  17. label2id, id2label = data_ext.load_dict(label_path)
  18. train_ds = load_dataset(data_ext.read, data_path=train_path, lazy=False)
  19. dev_ds = load_dataset(data_ext.read, data_path=dev_path, lazy=False)
  20. test_ds = load_dataset(data_ext.read, data_path=test_path, lazy=False)
  21. # print examples
  22. print(train_ds[0])
  23. print(train_ds[2])
{'text': ['兑', '换', '很', '方', '便', ','], 'label': ['B-Aspect', 'I-Aspect', 'O', 'B-Opinion', 'I-Opinion', 'O']}
{'text': ['骑', '车', '不', '错', '的', '地', '方'], 'label': ['O', 'O', 'B-Opinion', 'I-Opinion', 'O', 'B-Aspect', 'I-Aspect']}

2.2.3 将数据转换成特征形式

在将数据加载完成后,接下来,我们将各项数据集转换成适合输入模型的特征形式,即将文本字符串数据转换成字典id的形式。这里我们要加载paddleNLP中的SkepTokenizer,其将帮助我们完成这个字符串到字典id的转换。

  1. model_name = "skep_ernie_1.0_large_ch"
  2. batch_size = 8
  3. max_seq_len = 512
  4. tokenizer = SkepTokenizer.from_pretrained(model_name)
  5. trans_func = partial(data_ext.convert_example_to_feature, tokenizer=tokenizer, label2id=label2id, max_seq_len=max_seq_len)
  6. train_ds = train_ds.map(trans_func, lazy=False)
  7. dev_ds = dev_ds.map(trans_func, lazy=False)
  8. test_ds = test_ds.map(trans_func, lazy=False)
  9. # print examples
  10. print(train_ds[0])
  11. print(train_ds[2])
([1, 2697, 806, 321, 58, 518, 4, 2], [0, 0, 0, 0, 0, 0, 0, 0], 8, [0, 1, 2, 0, 3, 4, 0, 0])
([1, 1570, 320, 16, 990, 5, 31, 58, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0], 9, [0, 0, 0, 3, 4, 0, 1, 2, 0])

2.2.4 构造DataLoader

接下来,我们需要根据加载至内存的数据构造DataLoader,该DataLoader将支持以batch的形式将数据进行划分,从而以batch的形式训练相应模型。

  1. batchify_fn = lambda samples, fn=Tuple(
  2. Pad(axis=0, pad_val=tokenizer.pad_token_id),
  3. Pad(axis=0, pad_val=tokenizer.pad_token_type_id),
  4. Stack(dtype="int64"),
  5. Pad(axis=0, pad_val= -1)
  6. ): fn(samples)
  7. train_batch_sampler = paddle.io.BatchSampler(train_ds, batch_size=batch_size, shuffle=True)
  8. dev_batch_sampler = paddle.io.BatchSampler(dev_ds, batch_size=batch_size, shuffle=False)
  9. test_batch_sampler = paddle.io.BatchSampler(test_ds, batch_size=batch_size, shuffle=False)
  10. train_loader = paddle.io.DataLoader(train_ds, batch_sampler=train_batch_sampler, collate_fn=batchify_fn)
  11. dev_loader = paddle.io.DataLoader(dev_ds, batch_sampler=dev_batch_sampler, collate_fn=batchify_fn)
  12. test_loader = paddle.io.DataLoader(test_ds, batch_sampler=test_batch_sampler, collate_fn=batchify_fn)

2.3 模型构建

本案例中,我们将基于SKEP模型实现图1所展示的评论观点抽取功能。具体来讲,我们将处理好的文本数据输入SKEP模型中,SKEP将会对文本的每个token进行编码,产生对应向量序列。接下来,我们将基于该向量序列进行预测每个位置上的输出标签。相应代码如下。

  1. class SkepForTokenClassification(paddle.nn.Layer):
  2. def __init__(self, skep, num_classes=2, dropout=None):
  3. super(SkepForTokenClassification, self).__init__()
  4. self.num_classes = num_classes
  5. self.skep = skep
  6. self.dropout = paddle.nn.Dropout(dropout if dropout is not None else self.skep.config["hidden_dropout_prob"])
  7. self.classifier = paddle.nn.Linear(self.skep.config["hidden_size"], num_classes)
  8. def forward(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None):
  9. sequence_output, _ = self.skep(input_ids, token_type_ids=token_type_ids, position_ids=position_ids, attention_mask=attention_mask)
  10. sequence_output = self.dropout(sequence_output)
  11. logits = self.classifier(sequence_output)
  12. return logits

2.4 训练配置

接下来,定义情感分析模型训练时的环境,包括:配置训练参数、配置模型参数,定义模型的实例化对象,指定模型训练迭代的优化算法等,相关代码如下。

  1. # model hyperparameter setting
  2. num_epoch = 3
  3. learning_rate = 3e-5
  4. weight_decay = 0.01
  5. warmup_proportion = 0.1
  6. max_grad_norm = 1.0
  7. log_step = 20
  8. eval_step = 100
  9. seed = 1000
  10. checkpoint = "./checkpoint/"
  11. set_seed(seed)
  12. use_gpu = True if paddle.get_device().startswith("gpu") else False
  13. if use_gpu:
  14. paddle.set_device("gpu:0")
  15. if not os.path.exists(checkpoint):
  16. os.mkdir(checkpoint)
  17. skep = SkepModel.from_pretrained(model_name)
  18. model = SkepForTokenClassification(skep, num_classes=len(label2id))
  19. num_training_steps = len(train_loader) * num_epoch
  20. lr_scheduler = LinearDecayWithWarmup(learning_rate=learning_rate, total_steps=num_training_steps, warmup=warmup_proportion)
  21. decay_params = [p.name for n, p in model.named_parameters() if not any(nd in n for nd in ["bias", "norm"])]
  22. grad_clip = paddle.nn.ClipGradByGlobalNorm(max_grad_norm)
  23. optimizer = paddle.optimizer.AdamW(learning_rate=lr_scheduler, parameters=model.parameters(), weight_decay=weight_decay, apply_decay_param_fun=lambda x: x in decay_params, grad_clip=grad_clip)
  24. metric = ChunkEvaluator(label2id.keys())

2.5 模型训练与测试

本节我们将定义一个train函数和evaluate函数,其将分别进行训练和评估模型。在训练过程中,每隔log_steps步打印一次日志,每隔eval_steps步进行评估一次模型,并始终保存验证效果最好的模型。相关代码如下:

  1. def evaluate(model, data_loader, metric):
  2. model.eval()
  3. metric.reset()
  4. for idx, batch_data in enumerate(data_loader):
  5. input_ids, token_type_ids, seq_lens, labels = batch_data
  6. logits = model(input_ids, token_type_ids=token_type_ids)
  7. # count metric
  8. predictions = logits.argmax(axis=2)
  9. num_infer_chunks, num_label_chunks, num_correct_chunks = metric.compute(seq_lens, predictions, labels)
  10. metric.update(num_infer_chunks.numpy(), num_label_chunks.numpy(), num_correct_chunks.numpy())
  11. precision, recall, f1 = metric.accumulate()
  12. return precision, recall, f1
  13. def train():
  14. # start to train model
  15. global_step, best_f1 = 1, 0.
  16. model.train()
  17. for epoch in range(1, num_epoch+1):
  18. for batch_data in train_loader():
  19. input_ids, token_type_ids, _, labels = batch_data
  20. # logits: batch_size, seql_len, num_tags
  21. logits = model(input_ids, token_type_ids=token_type_ids)
  22. loss = F.cross_entropy(logits.reshape([-1, len(label2id)]), labels.reshape([-1]), ignore_index=-1)
  23. loss.backward()
  24. lr_scheduler.step()
  25. optimizer.step()
  26. optimizer.clear_grad()
  27. if global_step > 0 and global_step % log_step == 0:
  28. print(f"epoch: {epoch} - global_step: {global_step}/{num_training_steps} - loss:{loss.numpy().item():.6f}")
  29. if (global_step > 0 and global_step % eval_step == 0) or global_step == num_training_steps:
  30. precision, recall, f1 = evaluate(model, dev_loader, metric)
  31. model.train()
  32. if f1 > best_f1:
  33. print(f"best F1 performence has been updated: {best_f1:.5f} --> {f1:.5f}")
  34. best_f1 = f1
  35. paddle.save(model.state_dict(), f"{checkpoint}/best_ext.pdparams")
  36. print(f'evalution result: precision: {precision:.5f}, recall: {recall:.5f}, F1: {f1:.5f}')
  37. global_step += 1
  38. paddle.save(model.state_dict(), f"{checkpoint}/final_ext.pdparams")
  39. train()
epoch: 1 - global_step: 20/300 - loss:0.775109
epoch: 1 - global_step: 40/300 - loss:0.654723
epoch: 1 - global_step: 60/300 - loss:0.417590
epoch: 1 - global_step: 80/300 - loss:0.326240
epoch: 1 - global_step: 100/300 - loss:0.236355
best F1 performence has been updated: 0.00000 --> 0.71855
evalution result: precision: 0.65176, recall: 0.80058,  F1: 0.71855
epoch: 2 - global_step: 120/300 - loss:0.206213
epoch: 2 - global_step: 140/300 - loss:0.243644
epoch: 2 - global_step: 160/300 - loss:0.250103
epoch: 2 - global_step: 180/300 - loss:0.147552
epoch: 2 - global_step: 200/300 - loss:0.287132
best F1 performence has been updated: 0.71855 --> 0.76334
evalution result: precision: 0.72468, recall: 0.80636,  F1: 0.76334
epoch: 3 - global_step: 220/300 - loss:0.092556
epoch: 3 - global_step: 240/300 - loss:0.190176
epoch: 3 - global_step: 260/300 - loss:0.189123
epoch: 3 - global_step: 280/300 - loss:0.184761
epoch: 3 - global_step: 300/300 - loss:0.150254
best F1 performence has been updated: 0.76334 --> 0.77901
evalution result: precision: 0.74603, recall: 0.81503,  F1: 0.77901

接下来,我们将加载训练过程中评估效果最好的模型,并使用测试集进行测试。相关代码如下。

  1. # load model
  2. model_path = "./checkpoint/best_ext.pdparams"
  3. loaded_state_dict = paddle.load(model_path)
  4. skep = SkepModel.from_pretrained(model_name)
  5. model = SkepForTokenClassification(skep, num_classes=len(label2id))
  6. model.load_dict(loaded_state_dict)

3. 细粒度情感分类

3.1 数据处理

3.1.1 数据集介绍

本实践中包含训练集、评估和测试3项数据集,以及1个标签词典。其中标签词典记录了两类情感标签:正向和负向。

另外,数据集中需要包含3列数据:文本串和相应的序列标签数据,下面给出了一条样本,其中第1列是情感标签,第2列是评论属性和观点,第3列是原文。

1 口味清淡 口味很清淡,价格也比较公道

3.2.2 数据加载

本节我们将训练、评估和测试数据集,以及标签词典加载到内存中。相关代码如下:

  1. import os
  2. import argparse
  3. from functools import partial
  4. import paddle
  5. import paddle.nn.functional as F
  6. from paddlenlp.metrics import AccuracyAndF1
  7. from paddlenlp.datasets import load_dataset
  8. from paddlenlp.data import Pad, Stack, Tuple
  9. from paddlenlp.transformers import SkepTokenizer, SkepModel, LinearDecayWithWarmup
  10. from utils.utils import set_seed, decoding, is_aspect_first, concate_aspect_and_opinion, format_print
  11. from utils import data_ext, data_cls
  12. train_path = "./data/data121242/train_cls.txt"
  13. dev_path = "./data/data121242/dev_cls.txt"
  14. test_path = "./data/data121242/test_cls.txt"
  15. label_path = "./data/data121242/label_cls.dict"
  16. # load and process data
  17. label2id, id2label = data_cls.load_dict(label_path)
  18. train_ds = load_dataset(data_cls.read, data_path=train_path, lazy=False)
  19. dev_ds = load_dataset(data_cls.read, data_path=dev_path, lazy=False)
  20. test_ds = load_dataset(data_cls.read, data_path=test_path, lazy=False)
  21. # print examples
  22. print(train_ds[0])
  23. print(train_ds[1])
{'label': 1, 'target_text': '环境不错', 'text': '服务和环境都不错……'}
{'label': 1, 'target_text': '不错造型', 'text': '服务态度挺好的,还给吹了一个不错的造型'}

3.2.3 将数据转换成特征形式

在将数据加载完成后,接下来,我们将各项数据集转换成适合输入模型的特征形式,即将文本字符串数据转换成字典id的形式。这里我们要加载paddleNLP中的SkepTokenizer,其将帮助我们完成这个字符串到字典id的转换。

  1. model_name = "skep_ernie_1.0_large_ch"
  2. batch_size = 8
  3. max_seq_len = 512
  4. tokenizer = SkepTokenizer.from_pretrained(model_name)
  5. trans_func = partial(data_cls.convert_example_to_feature, tokenizer=tokenizer, label2id=label2id, max_seq_len=max_seq_len)
  6. train_ds = train_ds.map(trans_func, lazy=False)
  7. dev_ds = dev_ds.map(trans_func, lazy=False)
  8. test_ds = test_ds.map(trans_func, lazy=False)
  9. # print examples
  10. print(train_ds[0])
  11. print(train_ds[1])

3.2.4 构造DataLoader

接下来,我们需要根据加载至内存的数据构造DataLoader,该DataLoader将支持以batch的形式将数据进行划分,从而以batch的形式训练相应模型。

  1. batchify_fn = lambda samples, fn=Tuple(
  2. Pad(axis=0, pad_val=tokenizer.pad_token_id),
  3. Pad(axis=0, pad_val=tokenizer.pad_token_type_id),
  4. Stack(dtype="int64"),
  5. Stack(dtype="int64")
  6. ): fn(samples)
  7. train_batch_sampler = paddle.io.BatchSampler(train_ds, batch_size=batch_size, shuffle=True)
  8. dev_batch_sampler = paddle.io.BatchSampler(dev_ds, batch_size=batch_size, shuffle=False)
  9. test_batch_sampler = paddle.io.BatchSampler(test_ds, batch_size=batch_size, shuffle=False)
  10. train_loader = paddle.io.DataLoader(train_ds, batch_sampler=train_batch_sampler, collate_fn=batchify_fn)
  11. dev_loader = paddle.io.DataLoader(dev_ds, batch_sampler=dev_batch_sampler, collate_fn=batchify_fn)
  12. test_loader = paddle.io.DataLoader(test_ds, batch_sampler=test_batch_sampler, collate_fn=batchify_fn)

3.3 模型构建

本案例中,我们将基于SKEP模型实现图1所展示的评论观点抽取功能。具体来讲,我们将处理好的文本数据输入SKEP模型中,SKEP将会对文本的每个token进行编码,产生对应向量序列。我们使用CLS位置对应的输出向量进行情感分类。相应代码如下。

  1. class SkepForSequenceClassification(paddle.nn.Layer):
  2. def __init__(self, skep, num_classes=2, dropout=None):
  3. super(SkepForSequenceClassification, self).__init__()
  4. self.num_classes = num_classes
  5. self.skep = skep
  6. self.dropout = paddle.nn.Dropout(dropout if dropout is not None else self.skep.config["hidden_dropout_prob"])
  7. self.classifier = paddle.nn.Linear(self.skep.config["hidden_size"], num_classes)
  8. def forward(self, input_ids, token_type_ids=None, position_ids=None, attention_mask=None):
  9. _, pooled_output = self.skep(input_ids, token_type_ids=token_type_ids, position_ids=position_ids, attention_mask=attention_mask)
  10. pooled_output = self.dropout(pooled_output)
  11. logits = self.classifier(pooled_output)
  12. return logits

3.4 训练配置

接下来,定义情感分析模型训练时的环境,包括:配置训练参数、配置模型参数,定义模型的实例化对象,指定模型训练迭代的优化算法等,相关代码如下。

  1. # model hyperparameter setting
  2. num_epoch = 3
  3. learning_rate = 3e-5
  4. weight_decay = 0.01
  5. warmup_proportion = 0.1
  6. max_grad_norm = 1.0
  7. log_step = 20
  8. eval_step = 100
  9. seed = 1000
  10. checkpoint = "./checkpoint/"
  11. set_seed(seed)
  12. use_gpu = True if paddle.get_device().startswith("gpu") else False
  13. if use_gpu:
  14. paddle.set_device("gpu:0")
  15. if not os.path.exists(checkpoint):
  16. os.mkdir(checkpoint)
  17. skep = SkepModel.from_pretrained(model_name)
  18. model = SkepForSequenceClassification(skep, num_classes=len(label2id))
  19. num_training_steps = len(train_loader) * num_epoch
  20. lr_scheduler = LinearDecayWithWarmup(learning_rate=learning_rate, total_steps=num_training_steps, warmup=warmup_proportion)
  21. decay_params = [p.name for n, p in model.named_parameters() if not any(nd in n for nd in ["bias", "norm"])]
  22. grad_clip = paddle.nn.ClipGradByGlobalNorm(max_grad_norm)
  23. optimizer = paddle.optimizer.AdamW(learning_rate=lr_scheduler, parameters=model.parameters(), weight_decay=weight_decay, apply_decay_param_fun=lambda x: x in decay_params, grad_clip=grad_clip)
  24. metric = AccuracyAndF1()

3.5 模型训练与测试

本节我们将定义一个train函数和evaluate函数,其将分别进行训练和评估模型。在训练过程中,每隔log_steps步打印一次日志,每隔eval_steps步进行评估一次模型,并始终保存验证效果最好的模型。相关代码如下:

  1. def evaluate(model, data_loader, metric):
  2. model.eval()
  3. metric.reset()
  4. for batch_data in data_loader:
  5. input_ids, token_type_ids, _, labels = batch_data
  6. logits = model(input_ids, token_type_ids=token_type_ids)
  7. correct = metric.compute(logits, labels)
  8. metric.update(correct)
  9. accuracy, precision, recall, f1, _ = metric.accumulate()
  10. return accuracy, precision, recall, f1
  11. def train():
  12. # start to train model
  13. global_step, best_f1 = 1, 0.
  14. model.train()
  15. for epoch in range(1, num_epoch+1):
  16. for batch_data in train_loader():
  17. input_ids, token_type_ids, _, labels = batch_data
  18. # logits: batch_size, seql_len, num_tags
  19. logits = model(input_ids, token_type_ids=token_type_ids)
  20. loss = F.cross_entropy(logits, labels)
  21. loss.backward()
  22. lr_scheduler.step()
  23. optimizer.step()
  24. optimizer.clear_grad()
  25. if global_step > 0 and global_step % log_step == 0:
  26. print(f"epoch: {epoch} - global_step: {global_step}/{num_training_steps} - loss:{loss.numpy().item():.6f}")
  27. if (global_step > 0 and global_step % eval_step == 0) or global_step == num_training_steps:
  28. accuracy, precision, recall, f1 = evaluate(model, dev_loader, metric)
  29. model.train()
  30. if f1 > best_f1:
  31. print(f"best F1 performence has been updated: {best_f1:.5f} --> {f1:.5f}")
  32. best_f1 = f1
  33. paddle.save(model.state_dict(), f"{checkpoint}/best_cls.pdparams")
  34. print(f'evalution result: accuracy:{accuracy:.5f} precision: {precision:.5f}, recall: {recall:.5f}, F1: {f1:.5f}')
  35. global_step += 1
  36. paddle.save(model.state_dict(), f"{checkpoint}/final_cls.pdparams")
  37. train()
epoch: 1 - global_step: 20/300 - loss:0.901484
epoch: 1 - global_step: 40/300 - loss:0.154271
epoch: 1 - global_step: 60/300 - loss:0.000010
epoch: 1 - global_step: 80/300 - loss:0.413460
epoch: 1 - global_step: 100/300 - loss:0.000254
best F1 performence has been updated: 0.00000 --> 0.98734
evalution result: accuracy:0.98000 precision: 0.98734, recall: 0.98734,  F1: 0.98734
epoch: 2 - global_step: 120/300 - loss:0.000208
epoch: 2 - global_step: 140/300 - loss:0.000025
epoch: 2 - global_step: 160/300 - loss:0.003228
epoch: 2 - global_step: 180/300 - loss:0.000084
epoch: 2 - global_step: 200/300 - loss:0.783448
evalution result: accuracy:0.98000 precision: 1.00000, recall: 0.97468,  F1: 0.98718
epoch: 3 - global_step: 220/300 - loss:0.000134
epoch: 3 - global_step: 240/300 - loss:0.000813
epoch: 3 - global_step: 260/300 - loss:0.000276
epoch: 3 - global_step: 280/300 - loss:0.002265
epoch: 3 - global_step: 300/300 - loss:0.000753
best F1 performence has been updated: 0.98734 --> 0.99363
evalution result: accuracy:0.99000 precision: 1.00000, recall: 0.98734,  F1: 0.99363

接下来,我们将加载训练过程中评估效果最好的模型,并使用测试集进行测试。相关代码如下。

  1. # load model
  2. model_path = "./checkpoint/best_cls.pdparams"
  3. loaded_state_dict = paddle.load(model_path)
  4. skep = SkepModel.from_pretrained(model_name)
  5. model = SkepForSequenceClassification(skep, num_classes=len(label2id))
  6. model.load_dict(loaded_state_dict)
  1. accuracy, precision, recall, f1 = evaluate(model, test_loader, metric)
  2. print(f'evalution result: accuracy:{accuracy:.5f} precision: {precision:.5f}, recall: {recall:.5f}, F1: {f1:.5f}')
evalution result: accuracy:0.98000 precision: 0.98824, recall: 0.98824,  F1: 0.98824

4. 全流程模型推理

接下来,我们将加载上边训练完成的评论观点抽取模型和分类模型,进行模型推理,相应代码如下。

  1. label_ext_path = "./data/data121190/label_ext.dict"
  2. label_cls_path = "./data/data121242/label_cls.dict"
  3. ext_model_path = "./checkpoint/best_ext.pdparams"
  4. cls_model_path = "./checkpoint/best_cls.pdparams"
  5. # load dict
  6. model_name = "skep_ernie_1.0_large_ch"
  7. ext_label2id, ext_id2label = data_ext.load_dict(label_ext_path)
  8. cls_label2id, cls_id2label = data_cls.load_dict(label_cls_path)
  9. tokenizer = SkepTokenizer.from_pretrained(model_name)
  10. print("label dict loaded.")
  11. # load ext model
  12. ext_state_dict = paddle.load(ext_model_path)
  13. ext_skep = SkepModel.from_pretrained(model_name)
  14. ext_model = SkepForTokenClassification(ext_skep, num_classes=len(ext_label2id))
  15. ext_model.load_dict(ext_state_dict)
  16. print("extraction model loaded.")
  17. # load cls model
  18. cls_state_dict = paddle.load(cls_model_path)
  19. cls_skep = SkepModel.from_pretrained(model_name)
  20. cls_model = SkepForSequenceClassification(cls_skep, num_classes=len(cls_label2id))
  21. cls_model.load_dict(cls_state_dict)
  22. print("classification model loaded.")
  1. def predict(input_text, ext_model, cls_model, tokenizer, ext_id2label, cls_id2label, max_seq_len=512):
  2. ext_model.eval()
  3. cls_model.eval()
  4. # processing input text
  5. encoded_inputs = tokenizer(list(input_text), is_split_into_words=True, max_seq_len=max_seq_len,)
  6. input_ids = paddle.to_tensor([encoded_inputs["input_ids"]])
  7. token_type_ids = paddle.to_tensor([encoded_inputs["token_type_ids"]])
  8. # extract aspect and opinion words
  9. logits = ext_model(input_ids, token_type_ids=token_type_ids)
  10. predictions = logits.argmax(axis=2).numpy()[0]
  11. tag_seq = [ext_id2label[idx] for idx in predictions][1:-1]
  12. aps = decoding(input_text, tag_seq)
  13. # predict sentiment for aspect with cls_model
  14. results = []
  15. for ap in aps:
  16. aspect = ap[0]
  17. opinion_words = list(set(ap[1:]))
  18. aspect_text = concate_aspect_and_opinion(input_text, aspect, opinion_words)
  19. encoded_inputs = tokenizer(aspect_text, text_pair=input_text, max_seq_len=max_seq_len, return_length=True)
  20. input_ids = paddle.to_tensor([encoded_inputs["input_ids"]])
  21. token_type_ids = paddle.to_tensor([encoded_inputs["token_type_ids"]])
  22. logits = cls_model(input_ids, token_type_ids=token_type_ids)
  23. prediction = logits.argmax(axis=1).numpy()[0]
  24. result = {"aspect": aspect, "opinions": opinion_words, "sentiment": cls_id2label[prediction]}
  25. results.append(result)
  26. # print results
  27. format_print(results)
  28. max_seq_len = 512
  29. input_text = "环境装修不错,也很干净,前台服务非常好"
  30. predict(input_text, ext_model, cls_model, tokenizer, ext_id2label, cls_id2label, max_seq_len=max_seq_len)
aspect: 环境, opinions: [], sentiment: 正向
aspect: 装修, opinions: ['不错', '干净'], sentiment: 正向
aspect: 服务, opinions: ['好'], sentiment: 正向

参考文献: 

[1] H. Tian et al., “SKEP: Sentiment Knowledge Enhanced Pre-training for Sentiment Analysis,” arXiv:2005.05635 [cs], May 2020, Accessed: Nov. 11, 2021.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/661185
推荐阅读
相关标签
  

闽ICP备14008679号