[Paper Review] Detecting Gender Bias in Transformer-based Models: A Case Study on BERT
This paper proposes a novel attention map-based method to detect gender bias in BERT by analyzing attention scores between gender pronouns and occupations. It reveals that the query and key matrices ($\mathbf{W_q}$, $\mathbf{W_k}$) introduce significantly more gender bias than other modules, and bias propagates periodically through the model, being amplified in $\mathbf{Q}$, $\mathbf{K}$, $\mathbf{V}$, and the residual layer, but reduced by averaged attention.
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).
Motivation & Objective
- To investigate whether gender bias associated with occupations exists within the internal representations of BERT.
- To develop a method for detecting gender bias using attention maps that avoids position-based bias in evaluation.
- To trace the flow of gender bias through different components of the transformer architecture.
- To analyze bias distribution and enhancement patterns across layers using a large-scale Wikipedia-based dataset.
Proposed method
- The method uses gender-swapped sentence pairs to eliminate position effects in bias detection, comparing attention scores between male/female pronouns and occupations.
- It extracts and compares attention scores ($\mathbf{AS}$) between gendered pronouns and occupation tokens to compute gender tendency values.
- A gender bias detector is inserted at multiple positions in the model (embedding, $\mathbf{Q}$, $\mathbf{K}$, $\mathbf{V}$, $\mathbf{AvgAttention}$, and layer output) to trace internal bias flow.
- The attention score matrices ($\mathbf{AS}$) and averaged attention ($\mathbf{AvgAttention}$) are derived using standard transformer formulas: $\mathbf{AS} = \mathrm{Softmax}(\frac{\mathbf{Q}\mathbf{K}^T}{\sqrt{D_k}})$ and $\mathbf{AvgAttention} = \mathbf{AS} \cdot \mathbf{V}$.
- Bias degree is quantified using the difference in attention scores between male and female pronouns relative to the occupation, with consistency verified across the full Wikipedia pretraining dataset.
- Statistical analysis of bias distribution and enhancement probability is performed across 144 attention heads and 12 layers using box plots and difference curves.
Experimental results
Research questions
- RQ1Do gender biases associated with occupations emerge within the internal representations of BERT, particularly in attention mechanisms?
- RQ2How does the attention mechanism contribute to the amplification or reduction of gender bias across different layers and components?
- RQ3Which specific components of the self-attention module—$\mathbf{W_q}$, $\mathbf{W_k}$, $\mathbf{W_v}$, $\mathbf{Q}$, $\mathbf{K}$, $\mathbf{V}$, $\mathbf{AvgAttention}$, or the residual layer—most significantly influence gender bias?
- RQ4Is the gender bias consistent across the entire pretraining dataset, and does it exhibit periodic patterns of enhancement or mitigation?
Key findings
- The query ($\mathbf{W_q}$) and key ($\mathbf{W_k}$) matrices introduce significantly more gender bias than other components, including the embedding layer.
- The bias degree changes periodically: $\mathbf{Q}$, $\mathbf{K}$, $\mathbf{V}$, and the residual part of the attention layer enhance gender bias, while averaged attention ($\mathbf{AvgAttention}$) reduces it.
- The probability of bias enhancement is highest at $\mathbf{W_q}$ and $\mathbf{W_k}$, with $\mathbf{W_v}$ contributing to fewer biased heads.
- The distribution of gender bias values shows that $\mathbf{Q}$ and $\mathbf{K}$ have larger bias magnitudes than other components, including $\mathbf{W_v}$.
- The percentage of biased attention heads increases significantly at $\mathbf{W_q}$, $\mathbf{W_k}$, and the final layer output, indicating these components amplify bias.
- The consistency of gender bias conclusions across the entire Wikipedia pretraining dataset confirms that the observed bias patterns are robust and not artifacts of specific inputs.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.