Giter VIP home page Giter VIP logo

ear's Introduction

Entropy-based Attention Regularization ๐Ÿ‘‚

EAR is a regularization technique to mitigate uninteded bias while reducing lexical overfitting. It is based on attention entropy maximization. In practice, EAR adds a regularization term at training time to learn tokens with maximal self-attention entropy.

See the paper for additional details:

Attanasio, G., Nozza, D., Hovy, D., & Baralis, E. "Entropy-based Attention Regularization Frees Unintended Bias Mitigation from Lists". In Findings of the Association for Computational Linguistics: ACL2022. Association for Computational Linguistics, 2022.

Quick links

Project structure

The data used in this study is in data. Please note that we are not allowed to distribute all the data sets. For some of those, you will need to download it yourselves (instructions below). The code is organized in python scripts (training and evaluation of models), bash scripts to run experiments, and jupyter notebooks.

The main files are the following:

  • train_bert.py: use this script to train any bert-based model starting from HuggingFace checkpoints.
  • evaluate_model.py: use this script to evaluate a model either on a test set or a synthetic evaluation set.

Please find all the accepted parameters running python <script_name> --help.

Getting started

The following are the basic steps to setup our environment and replicate our results.

Getting the data sets

Please follow these instructions to retrive the presented dataset:

  • Misogyny (EN): the dataset is not publicly available. Please fill this form to submit a request to the authors.
  • Misogyny (IT): the dataset is not publicly available. Please fill this form to submit a request to the authors.
  • Multilingual and Multi-Aspect (MlMA): the dataset is available online. In data, we provide our splitfiles with the additional binary "hate" column used in our experiments.

For the sake of simplicty, we have assigned short names to each data set. Please find them and how to use them in dataset.py.

Dependencies

You'll need a working Python environment to run the code. The required dependencies are specified in the file environment.yml. We use conda virtual environments to manage the project dependencies in isolation.

Run the following command in the repository folder to create a separate environment and install all required dependencies in it:

conda create -n ear python==3.8
conda activate ear
pip install -r requirements.txt

Example

EAR can be plugged very easily to HuggingFace models.

from transformers import AutoTokenizer, AutoModel
import ear

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModel.from_pretrained("bert-base-uncased")

item = tokenizer("Today it's a good day!")
outputs = model(**item, output_attentions=True)

reg_strength = 0.01
neg_entropy = ear.compute_negative_entropy(
    inputs=outputs.attentions,
    attention_mask=item["attention_mask"]
)
reg_loss = reg_strength * neg_entropy
loss = reg_loss + output.loss

Reproducing Hate Speech Detection results

The bash folder contains some utility bash scripts useful to run multiple experiments sequentially. They cover the training and evaluation pipeline of all the models tested in the paper. To let everything work as expected, please run them from the parent directory.

Training

Please check out your disk size, these scripts will save two model checkpoints (best and the last one) for every seed.

Train BERT on the Misogyny (EN) dataset:

./bash/train_model_10_seeds.sh bert-base-uncased <output_dir> <training_dataset>

e.g., ./bash/train_model_10_seeds.sh bert-base-uncased . miso

Train BERT+EAR on the Multilingual and Multi-Aspect dataset:

./bash/train_model_EAR_10_seeds.sh bert-base-uncased <output_dir> <training_dataset>

e.g., ./bash/train_model_EAR_10_seeds.sh bert-base-uncased . mlma

Note that:

  • if you want to take into account class imbalance, you should add the --balanced_loss to the parameters passed as command line arguments to python;
  • for BERT+SOC (Kennedy et al. 2020), we re-use the authors's implementation. Therefore, no training scripts are provided here.

Testing

To evaluate a model, or a folder with several models (different seeds), you have to:

  1. run the evaluation on synthetic data.
  2. run the evaluation on test data

Evaluation of bias metrics on synthetic data

Here we provide an example to run the evaluation on Madlibs77K synthetic data using a specific checkpoint name (last.ckpt in this case).

./bash/evaluate_folder_madlibs_pattern.sh <in_dir> <out_dir> last.ckpt

Analogous script for the other synthetic sets are stored in the folder ./bash. Namely:

  • evaluate_folder_miso_synt.sh Run the evaluation of all the models within a specified parent directory on Misogyny (EN), synthetic set.
  • evaluate_folder_miso-ita_synt.sh Run the evaluation of all the models within a specified parent directory on Misogyny (IT), synthetic set.

Evaluation on test data

Here we provide an example to run the evaluation on the test set of MlMA.

./bash/test_folder.sh <in_dir> <out_dir> mlma <src_tokenizer> <ckpt_pattern>

Note that evaluation on Misogyny (IT) requires the parameter --src_tokenizer dbmdz/bert-base-italian-uncased

EAR for Biased Term Extraction

We provide a Jupyter Notebook where we show how to extract terms with the lowest contextualization, which may induce most of the bias in the model.

After having trained at least one model (i.e., you have a model checkpoint), the notebook term_extraction.ipynb will guide you through the discovery of biased terms.

References

Please use the following bibtex entry if you use this model in your project:

@inproceedings{attanasio-etal-2022-entropy,
    title = "Entropy-based Attention Regularization Frees Unintended Bias Mitigation from Lists",
    author = "Attanasio, Giuseppe  and
      Nozza, Debora  and
      Hovy, Dirk  and
      Baralis, Elena",
    booktitle = "Findings of the Association for Computational Linguistics: ACL 2022",
    month = may,
    year = "2022",
    address = "Dublin, Ireland",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2022.findings-acl.88",
    pages = "1105--1119",
    abstract = "Natural Language Processing (NLP) models risk overfitting to specific terms in the training data, thereby reducing their performance, fairness, and generalizability. E.g., neural hate speech detection models are strongly influenced by identity terms like gay, or women, resulting in false positives, severe unintended bias, and lower performance.Most mitigation techniques use lists of identity terms or samples from the target domain during training. However, this approach requires a-priori knowledge and introduces further bias if important terms are neglected.Instead, we propose a knowledge-free Entropy-based Attention Regularization (EAR) to discourage overfitting to training-specific terms. An additional objective function penalizes tokens with low self-attention entropy.We fine-tune BERT via EAR: the resulting model matches or exceeds state-of-the-art performance for hate speech classification and bias metrics on three benchmark corpora in English and Italian.EAR also reveals overfitting terms, i.e., terms most likely to induce bias, to help identify their effect on the model, task, and predictions.",
}

๐Ÿšจ Ethical considerations

The process of building the list remains a data-driven approach, which is strongly dependent on the task, collected corpus, term frequencies, and the chosen model. Therefore, the list might either lack specific terms that instead need to be attentioned, or include some that do not strictly perpetrate harm. Because of these twin issues, the resulting lists should not be read as complete or absolute. We would therefore discourage users from simply building and developing models based solely on the extracted terms. We want, instead, the terms to stand as a starting point for debugging and searching for potential bias issues in the task at hand.

License

All source code is made available under a MIT license. See LICENSE.md for the full license text.

The manuscript text is not open source. The authors reserve the rights to the article content, which is currently submitted for publication.

ear's People

Contributors

dnozza avatar g8a9 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

Watchers

 avatar

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.