Giter Site home page Giter Site logo

frankaging / quasi-attention-absa Goto Github PK

View Code? Open in Web Editor NEW
44.0 2.0 12.0 1.2 MB

The Codebase for Quasi-Attention BERT Model for TABSA Tasks (AAAI '21)

Home Page: https://arxiv.org/abs/2010.07523

License: MIT License

Python 92.85% Shell 0.57% Jupyter Notebook 6.58%
bert sentiment-analysis aspect-based-sentiment-analysis sentihood-dataset semeval quasi-attention

quasi-attention-absa's Introduction

Quasi-Attention-ABSA

Codebase for Context-Guided BERT for Targeted Aspect-Based Sentiment Analysis (AAAI2021)

Contents

Citation

@inproceedings{wu2020context,
  title={Context-Guided BERT for Targeted Aspect-Based Sentiment Analysis},
  author={Wu, Zhengxuan and Ong, Desmond C},
  booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
  year={2021}
}

Quick start

Download Pretrained BERT Model

You will have to download pretrained BERT model in order to execute the fine-tune pipeline. We recommand to use models provided by the official release on BERT from BERT-Base (Google's pre-trained models). Note that their model is in tensorflow format. To convert tensorflow model to pytorch model, you can use the helper script to do that. For example,

cd code/
python convert_tf_checkpoint_to_pytorch.py \
--tf_checkpoint_path uncased_L-12_H-768_A-12/bert_model.ckpt \
--bert_config_file uncased_L-12_H-768_A-12/bert_config.json \
--pytorch_dump_path uncased_L-12_H-768_A-12/pytorch_model.bin

Datasets

We already preprocess the datasets for you. To be able to compare with the SOTA models, we adapt the preprocess pipeline right from this previous repo where SOTA models are trained. To regenerate the dataset, please refer to their paper and generate. Please also consider to cite their paper for this process.

Train CG-BERT Model and QACG-BERT Models

Our (T)ABSA BERT models are adapted from huggingface BERT model for text classification. If you want to take a look at the original model please search for BertForSequenceClassification. To train QACG-BERT model with semeval2014 dataset on GPU 0 and 1, you can do something like this,

cd code/
CUDA_VISIBLE_DEVICES=0,1,2,3 python run_classifier.py \
--task_name sentihood_NLI_M \
--data_dir ../datasets/sentihood/ \
--output_dir ../results/sentihood/QACGBERT-reproduce/ \
--model_type QACGBERT \
--do_lower_case \
--max_seq_length 128 \
--train_batch_size 64 \
--eval_batch_size 256 \
--learning_rate 2e-5 \
--num_train_epochs 30 \
--vocab_file ../models/BERT-Google/vocab.txt \
--bert_config_file ../models/BERT-Google/bert_config.json \
--init_checkpoint ../models/BERT-Google/pytorch_model.bin \
--seed 123 \
--evaluate_interval 25

Please take a look at code/util/args_parser.py to find our different arguments you can pass with. And you can alsp take a look at code/util/processor.py to see how we process different datasets. We currently supports almost 10 different dataset loadings. You can create your own within 1 minute for loading data. You can specify your directories info above in the command.

Analyze Attention Weights, Relevance and More

Once you have your model ready, save it to a location that you know (e.g., ../results/semeval2014/QACGBERT/checkpoint.bin). Our example code how to get relevance scores is in a jupyter notebook format, which is much easier to read. This is how you will open it,

cd code/notebook/
jupyter notebook

Inside visualization, we provide an example on how to extract attention scores, gradient sensitivity scores!

License

This repo has a Creative Commons Attribution 4.0 International License.

quasi-attention-absa's People

Contributors

frankaging avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

quasi-attention-absa's Issues

Can't train CG-BERT model

Hi, I am unable to train the CG-BERT model for any dataset. Below is the running command I've used and the result log. I have changed just the training batch size and evaluation interval. This error occurs for any dataset when setting the model type to CGBERT. I have successfully trained the QACGBERT model.

$ CUDA_VISIBLE_DEVICES=0 python run_classifier.py --task_name semeval_NLI_M 
--data_dir ../datasets/semeval2014/ 
--output_dir ../results/semeval2014/CGBERT-reproduce/ 
--model_type CGBERT 
--do_lower_case --max_seq_length 128 
--train_batch_size 32 --eval_batch_size 256
--learning_rate 2e-5 --num_train_epochs 30 
--vocab_file uncased_L-12_H-768_A-12/vocab.txt 
--bert_config_file uncased_L-12_H-768_A-12/bert_config.json 
--init_checkpoint uncased_L-12_H-768_A-12/pytorch_model.bin 
--seed 123 --evaluate_interval 500 --context_standalone

05/17/2021 12:53:53 - INFO - util.train_helper -   device cuda n_gpu 1 distributed training False
output_log_file= ../results/semeval2014/CGBERT-reproduce/log.txt
05/17/2021 12:53:53 - INFO - util.train_helper -   *** Model Config ***
05/17/2021 12:53:53 - INFO - util.train_helper -   {
  "attention_probs_dropout_prob": 0.1,
  "full_pooler": false,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 768,
  "initializer_range": 0.02,
  "intermediate_size": 3072,
  "max_position_embeddings": 512,
  "num_attention_heads": 12,
  "num_hidden_layers": 12,
  "type_vocab_size": 2,
  "vocab_size": 30522
}

05/17/2021 12:53:53 - INFO - util.train_helper -   model = CGBERT
init_weight = True
05/17/2021 12:53:54 - INFO - util.train_helper -   retraining with saved model.
100%|████████████████████████████████████████████████████████████████████████████████████████████| 15220/15220 [00:02<00:00, 5133.30it/s]
05/17/2021 12:53:58 - INFO - util.train_helper -   ***** Running training *****
05/17/2021 12:53:58 - INFO - util.train_helper -     Num examples = 15220
05/17/2021 12:53:58 - INFO - util.train_helper -     Batch size = 32
05/17/2021 12:53:58 - INFO - util.train_helper -     Num steps = 14268
100%|██████████████████████████████████████████████████████████████████████████████████████████████| 4000/4000 [00:00<00:00, 4770.35it/s]
Iteration:   0%|                                                                                                 | 0/476 [00:00<?, ?it/s]
Epoch:   0%|                                                                                                      | 0/30 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "run_classifier.py", line 39, in <module>
    run(args)
  File "run_classifier.py", line 30, in run
    output_log_file, epoch, global_best_acc, args)
  File "~/Quasi-Attention-ABSA/code/util/train_helper.py", line 729, in step_train
    context_ids=context_ids)
ValueError: not enough values to unpack (expected 6, got 2)
```

代码结果不好

您好,我复现了您的代码,为什么和论文中的结果差距很大,甚至说模型就是无效的,我没有改动您的代码,直接运行的
以下是 CGBERT模型epoch = 29的结果

`05/07/2021 01:46:20 - INFO - util.train_helper - epoch = 29████████████████████████████▌| 312/314 [00:05<00:00, 48.88it/s] 01:46:20 - INFO - util.train_helper - global_step = 18750

05/07/2021 01:46:20 - INFO - util.train_helper - loss = 1.0986090652494622

05/07/2021 01:46:20 - INFO - util.train_helper - test_loss = 1.0970154359082507

05/07/2021 01:46:20 - INFO - util.train_helper - test_accuracy = 0.8382118147951038

05/07/2021 01:46:20 - INFO - util.train_helper - aspect_strict_Acc = 0.47897817988291647

05/07/2021 01:46:20 - INFO - util.train_helper - aspect_Macro_F1 = 0

05/07/2021 01:46:20 - INFO - util.train_helper - aspect_Macro_AUC = 0.47944606445295795

05/07/2021 01:46:20 - INFO - util.train_helper - sentiment_Acc = 0.6661184210526315

05/07/2021 01:46:20

  • INFO - util.train_helper - sentiment_Macro_AUC = 0.48320620795185415`

以下是 QACGBERT模型epoch = 24的结果,以及最后的结果
05/08/2021 00:45:03 - INFO - util.train_helper - ***** Evaluation Interval Hit *****8<00:24, 5.05it/s, train_loss=1.35]
Iteration: 100%|████████████████████████████████████████████████████████████████████████| 167/167 [00:03<00:00, 49.49it/s]
05/08/2021 00:45:07 - INFO - util.train_helper - ***** Evaluation results *****
05/08/2021 00:45:07 - INFO - util.train_helper - epoch = 24████████████████████████▌| 166/167 [00:03<00:00, 42.66it/s]

05/08/2021 00:45:07 - INFO - util.train_helper - global_step = 15750

05/08/2021 00:45:07 - INFO - util.train_helper - loss = 1.4141508170202666

05/08/2021 00:45:07 - INFO - util.train_helper - test_loss = 1.4643629932118034

05/08/2021 00:45:07 - INFO - util.train_helper - test_accuracy = 0.40375

05/08/2021 00:45:07 - INFO - util.train_helper - aspect_P = 0.3350454365863295

05/08/2021 00:45:07 - INFO - util.train_helper - aspect_R = 0.8273170731707317

05/08/2021 00:45:07 - INFO - util.train_helper - aspect_F = 0.47694038245219356

05/08/2021 00:45:07 - INFO - util.train_helper - sentiment_Acc_4_classes = 0.36390243902439023

05/08/2021 00:45:07 - INFO - util.train_helper - sentiment_Acc_3_classes = 0.5220966084275437

05/08/2021 00:45:07 - INFO - util.train_helper - sentiment_Acc_2_classes = 0.6234357224118316

Iteration: 100%|███████████████████████████████████████████████████████| 635/635 [02:29<00:00, 4.25it/s, train_loss=2.04]
Epoch: 100%|███████████████████████████████████████████████████████████████████████████| 25/25 [1:01:38<00:00, 147.94s/it]
05/08/2021 00:45:34 - INFO - util.train_helper - ***** Global best performance *****
05/08/2021 00:45:34 - INFO - util.train_helper - accuracy on dev set: 0.4942233632862644

bad results when use models downloaded from huggingface

Hi,
I try to reproduct your work with pytorch BERT model download from huggingface, only to get a very bad result, the training loss keeps around 1.0 in the first 10 epochs. But when I follow your instruction, download google BERT model and converte it with the helper script, then the training process seems to go well.
I wonder why this is happening? Is this because these two models are very different?
Huggingface download link here: https://huggingface.co/bert-base-uncased/tree/main

where package BertConfig and BertModel

when i excute the file "convert_tf_checkpoint_to_pytorch.py"
i got the error: ModuleNotFoundError: No module named 'model.BERT'
where can i get the module "model.BERT" or package BertConfig and BertModel ?
thx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.