Detecting Gender Bias in Transformer-based Models: A Case Study on BERT

Bingbing Li, Hongwu Peng, Rajat Sainju, Junhuan Yang, Lei Yang, Yueying Liang, Weiwen Jiang, Binghui Wang, Hang Liu, Caiwen DingarXiv 2021

Abstract

Large Language Models (LLMs) like BERT often inherit societal biases from their training data. While previous research has focused on input embeddings or output predictions, the internal mechanisms of bias propagation remain under-explored. This study introduces a novel "bias detector" to trace gender bias flow within the Transformer architecture. By analyzing attention maps across 60,548 Wikipedia sentences, we find that the Query ($W_q$) and Key ($W_k$) matrices are the primary drivers of bias amplification, whereas the attention averaging process serves as a temporary dampener.

Introduction

LLMs are frequently treated as "black boxes" regarding bias. While it is well-documented that word embeddings contain stereotypical associations, the role of internal sub-modules—such as the Query, Key, and Value matrices—in processing these associations is less clear. This paper addresses this gap by investigating the "internal gender bias flow" within a pretrained BERT model to identify specific components responsible for bias generation.

Methods

The researchers developed a modular bias detector based on Attention Maps. The process involves:

  1. Gender Swapping: Creating pairs of sentences where gendered pronouns are swapped (e.g., "She accompanied him..." becomes "He accompanied her...").
  2. Attention Score (AS) Analysis: Measuring the "relation degree" between pronouns and occupations by summing the attention scores between them.
  3. Quantifying Bias: Defining a metric, $degree_{biased}$, which measures if the relative strength of the connection between a gender and an occupation remains consistent even when genders are swapped. If the model consistently links "nurse" more strongly to "she" than "he" regardless of sentence structure, bias is detected.
The proposed bias detection framework, illustrating gender swapping, normalization, and the calculation of the bias degree metric.

The detector was applied to 60,548 sentences from Wikipedia containing at least two opposite-gender pronouns and one occupation (e.g., "nurse," "programmer").

Results

The study reveals a "periodic" flow of bias throughout the model. The Query ($W_q$) and Key ($W_k$) matrices consistently introduce the most bias across all 12 layers of BERT. These components are responsible for determining how words relate to one another, making them the primary site where stereotypical associations are forged.

Mean gender bias degree across BERT layers. The bars highlight how Q and K matrices amplify bias, while AvgAttention (cyan bars) often shows negative values, indicating a dampening effect.

In contrast, the Value ($W_v$) matrix—which carries the actual content of the words—introduces much less bias. Interestingly, the "AvgAttention" stage (where the model averages information from different words) actually reduces the bias degree. It acts as a temporary dampener before bias is re-enhanced by subsequent residual connections and layer normalization at the end of the block.

Conclusion

This research pinpoints the $W_q$ and $W_k$ matrices as the primary sites of gender bias amplification in BERT. While limited to binary gender and the BERT-base architecture, these findings provide a roadmap for targeted debiasing strategies. By focusing on specific internal modules rather than the entire model, researchers can develop more efficient and explainable fairness interventions for future AI systems.

Summary updated

Abstract

In this paper, we propose a novel gender bias detection method by utilizing attention map for transformer-based models. We 1) give an intuitive gender bias judgement method by comparing the different relation degree between the genders and the occupation according to the attention scores, 2) design a gender bias detector by modifying the attention module, 3) insert the gender bias detector into different positions of the model to present the internal gender bias flow, and 4) draw the consistent gender bias conclusion by scanning the entire Wikipedia, a BERT pretraining dataset. We observe that 1) the attention matrices, Wq and Wk introduce much more gender bias than other modules (including the embedding layer) and 2) the bias degree changes periodically inside of the model (attention matrix Q, K, V, and the remaining part of the attention layer (including the fully-connected layer, the residual connection, and the layer normalization module) enhance the gender bias while the averaged attentions reduces the bias).
BibTeX
@misc{li2021detecting,
  author = {Bingbing Li and Hongwu Peng and Rajat Sainju and Junhuan Yang and Lei Yang and Yueying Liang and Weiwen Jiang and Binghui Wang and Hang Liu and Caiwen Ding},
  title = {{Detecting Gender Bias in Transformer-based Models: A Case Study on BERT}},
  year = {2021},
  eprint = {2110.15733},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2110.15733},
}
Figure