yangheng95 / PyABSA

Sentiment Analysis, Text Classification, Text Augmentation, Text Adversarial defense, etc.;
https://pyabsa.readthedocs.io
MIT License
937 stars 159 forks source link

[Question] Why all then sentiment predict Positive #78

Closed brightgems closed 3 years ago

brightgems commented 3 years ago

Question Hi, it's great works you'd been made on this project.

I used this project for training on custom dataset, it has around 2000 examples. Label count is a little imbalance.Finally, I trained a model with 100 apoach and achieved apc_acc around 90 score. But the predict resullt is always Positive on all the aspect.

thanks very much you any advice?

yangheng95 commented 3 years ago

Hi, I sorry for that, but I need some information to locate the error. Which model (example script) do your use? Have you tried the provided datasets and do they work well?

yangheng95 commented 3 years ago

I test the sentiment_inference.py and find no error, can you show me a part of texts in your inference dataset?

brightgems commented 3 years ago

Hi yangheng95, the example script can output results. then I train on custom dataset, below is example data: the dataset has totol of 2000 examples. I know the demo dataset almost only annotate aspect on noun, and my dataset doesn't follow this rule.

香 B-ASP 2
味 I-ASP 2
不 I-ASP 2
错 I-ASP 2
, O -999
洗 O -999
完 O -999
之 O -999
后 O -999
头 B-ASP 2
发 I-ASP 2
非 I-ASP 2
常 I-ASP 2
柔 I-ASP 2
顺 I-ASP 2
, O -999
本 O -999
人 O -999
漂 O -999
染 O -999
大 O -999
户 O -999
, O -999
大 O -999
家 O -999
真 O -999
的 O -999
可 O -999
以 O -999
冲 O -999
这 O -999
个 O -999
! O -999
! O -999

适 O -999
合 O -999
发 O -999
质 O -999
: O -999
所 O -999
以 O -999
发 O -999
质 O -999
泡 B-ASP 0
沫 I-ASP 0
数 O -999
量 O -999
: O -999
非 O -999
常 O -999
丰 O -999
富 O -999
产 O -999
品 O -999
香 O -999
味 O -999
: O -999
持 B-ASP 2
久 I-ASP 2
留 O -999
香 B-ASP 2
控 B-ASP 2
油 I-ASP 2
效 O -999
果 O -999
: O -999
非 O -999
常 O -999
好 O -999
柔 B-ASP 2
顺 I-ASP 2
效 O -999
果 O -999
: O -999
一 O -999
梳 O -999
理 O -999

this is my training script:

# -*- coding: utf-8 -*-
# file: train_atepc.py
# time: 2021/5/21 0021
# author: yangheng <yangheng@m.scnu.edu.cn>
# github: https://github.com/yangheng95
# Copyright (C) 2021. All Rights Reserved.

########################################################################################################################
#                                               ATEPC training_tutorials script                                        #
########################################################################################################################

from pyabsa.functional import ATEPCModelList
from pyabsa.functional import Trainer, ATEPCTrainer
from pyabsa.functional import ABSADatasetList
from pyabsa.functional import ATEPCConfigManager
# from pyabsa.core.atepc.dataset_utils.atepc_utils import load_apc_datasets

chinese_sets = 'corpus'
atepc_config_chinese = ATEPCConfigManager.get_atepc_config_chinese()
atepc_config_chinese.log_step = 50
atepc_config_chinese.model = ATEPCModelList.LCF_ATEPC
atepc_config_chinese.evaluate_begin = 4
atepc_config_chinese.batch_size = 32
atepc_config_chinese.pretrained_bert = "/pretrain-models/bert/chinese_wwm_ext_pytorch"
# atepc_config_chinese.pretrained_bert = "d:/model_zoo/bert/chinese_wwm_ext_pytorch"
atepc_config_chinese.gradient_accumulation_steps = 1
atepc_config_chinese.polarities_dim = 3
atepc_config_chinese.num_epoch = 100
atepc_config_chinese.SDR = 3
atepc_config_chinese.lcf = 'fusion'

print(atepc_config_chinese.args)

aspect_extractor = Trainer(config=atepc_config_chinese,
                           dataset=chinese_sets,
                           checkpoint_save_mode=1,
                           auto_device=True
                           )
yangheng95 commented 3 years ago

Thank you for your reponse, I notice that each review in your dataset contains multiple polarity labels, which may casue this problem. The acceptable format of ATEPC dataset is like: image That is, each review has all the aspect labels, but only has the polarity label of the target aspect. This is because current models can not predict sentiment polarities in parallel, however, there may be a model can do this in the future.

yangheng95 commented 3 years ago

Please reformat your dataset and let me know if it works. D:)

yangheng95 commented 3 years ago

Important As I received many similar reports, I add a new feature then to prepare the reviews containing multiple polarities. You can update to version 1.1.15 to use your current datasets. Please let me know any problem. Thanks.

brightgems commented 3 years ago

I noticed that the changes in v1.1.15 has refactor readfile in order to seperate many aspects in a document into many pair of aspect/document/sentiment.

Just adopted latest version and retrained model, it achieved poor performance on ate even through apc is a little better. this is final checkpoint: lcf_atepc_fusion_apcacc_92.79_apcf1_63.67_atef1_3.53.

Bad thing is above model cannot etract any term and polirtiy in prediction stage. output is:

{'extraction_res': [], 'polarity_res': []}

Feel like thing is more complicate if a document has many aspects. Thanks for your advice.

yangheng95 commented 3 years ago

As I dont have your dataset, please try 1.1.16a0 and help me test and report. Many thanks! pip install pyabsa==1.1.16a0

brightgems commented 3 years ago

thanks for advice! is there a way share my shampoo dataset to ABSADatasets project?

BTW, Looks like v1.1.16a0 hasn't publish yet

yangheng95 commented 3 years ago

I can see the prerelease at https://pypi.org/project/pyabsa/1.1.16a0/

I recommend you prepare the apc format dataset, and PR the dataset in yangheng95/ABSADatasets. Then I will help you prepare it into atepc datasets and inference datasets, and register it in PyABSA

yangheng95 commented 3 years ago

Please declare your copyrights in PR

yangheng95 commented 3 years ago

@all-contributors bot please add @brightgems dataset

allcontributors[bot] commented 3 years ago

@yangheng95

I've put up a pull request to add @brightgems! :tada:

brightgems commented 3 years ago

@yangheng95 thanks for guidance. I had udpated the library version to be v1.1.17. After train model again from scratch, it achieved better f1 perfomance than before, lcf_atepc_fusion_apcacc_91.71_apcf1_71.39_atef1_44.65.

Still remain one thing try to resolve, sentiment prediction cannot well distinguished for document composed of positive and negative aspects.

Beacuse the aspect in polarity_res is concatenation of all aspects. I'm suspecting whether aspect sentiment has degraded to sentence sentiment classification. Thanks for your advice.

Take an example as below:

包装很丑,总休效果一般,去屑效果非常好,能够深层滋养头皮,控油、去屑、止痒效果非常好。

'polarity_res': [{'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 
屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑
', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [0, 1, 2, 3], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 
效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般
', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [9], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑  
, 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', 
'一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [12, 13, 14, 15, 16, 17, 18], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑
', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [22, 23, 24, 25, 26, 27], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', ' 
控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [29, 30], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 
常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', 
'皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 
果 非 常 好', 'positions': [32, 33], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止
 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去
 屑 止 痒 效 果 非 常 好', 'positions': [35, 36, 37, 38, 39, 40, 41], 'sentiment': 'Positive'}]}
yangheng95 commented 3 years ago

@yangheng95 thanks for guidance. I had udpated the library version to be v1.1.17. After train model again from scratch, it achieved better f1 perfomance than before, lcf_atepc_fusion_apcacc_91.71_apcf1_71.39_atef1_44.65.

Still remain one thing try to resolve, sentiment prediction cannot well distinguished for document composed of positive and negative aspects.

Beacuse the aspect in polarity_res is concatenation of all aspects. I'm suspecting whether aspect sentiment has degraded to sentence sentiment classification. Thanks for your advice.

Take an example as below:

包装很丑,总休效果一般,去屑效果非常好,能够深层滋养头皮,控油、去屑、止痒效果非常好。

'polarity_res': [{'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 
屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑
', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [0, 1, 2, 3], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 
效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般
', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [9], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑  
, 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', 
'一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [12, 13, 14, 15, 16, 17, 18], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑
', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [22, 23, 24, 25, 26, 27], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', ' 
控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 果 非 常 好', 'positions': [29, 30], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止 痒 效 果 非 
常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', 
'皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去 屑 止 痒 效 
果 非 常 好', 'positions': [32, 33], 'sentiment': 'Positive'}, {'sentence': '包 装 很 丑 , 总 休 效 果 一 般 , 去 屑 效 果 非 常 好 , 能 够 深 层 滋 养 头 皮 , 控 油 、 去 屑 、 止
 痒 效 果 非 常 好 。', 'tokens': ['包', '装', '很', '丑', ',', '总', '休', '效', '果', '一', '般', ',', '去', '屑', '效', '果', '非', '常', '好', ',', '能', '够', '深', '层', '滋', '养', '头', '皮', ',', '控', '油', '、', '去', '屑', '、', '止', '痒', '效', '果', '非', '常', '好', '。'], 'aspect': '包 装 很 丑 一 去 屑 效 果 非 常 好 深 层 滋 养 头 皮 控 油 去
 屑 止 痒 效 果 非 常 好', 'positions': [35, 36, 37, 38, 39, 40, 41], 'sentiment': 'Positive'}]}

I suppose this is caused by the imbalanced distribution in this dataset. Besides, although the annotation strategy does not influence the performance of the ATEPC model, the annotation of aspect terms and aspects' sentiment should obey the same strategy.

BTW, the current prediction script does not filter the same sentence and aspects. I may fix it soon.

brightgems commented 3 years ago

thanks for the response. I had found that prediction result can has negative/posivtive aspects at some sentence. And improved imbalanced distribution in this dataset should be helpful for this issue.

yangheng95 commented 2 years ago

This PR may be helpful: https://github.com/yangheng95/PyABSA/pull/89