Explaining Text Classifiers with Counterfactual Representations

Pirmin Lemberger, Antoine Saillenfest

Abstract

Counterfactual explanations are a powerful tool for interpretability, yet generating them for natural language remains a significant challenge due to the discrete and high-dimensional nature of text. This paper proposes a method for generating Counterfactual Representations (CFRs) by intervening directly in the latent embedding space of neural encoders. By utilizing linear concept erasure and orthogonal decomposition, the authors provide a computationally efficient, theoretically grounded framework for measuring causal effects and mitigating model bias without the need for text generation.

1. Introduction

Interpretability in machine learning often relies on counterfactual reasoning: "How would the model's prediction change if a specific attribute of the input were different?" In tabular data, such interventions are straightforward. However, in natural language processing (NLP), creating a counterfactual requires rewriting a sentence to alter a target attribute (e.g., sentiment or gender) while keeping all other factors—such as style, length, and factual content—constant.

Existing approaches to textual counterfactuals often rely on manual human editing, which is expensive and unscalable, or generative models, which can introduce unwanted artifacts or fail to preserve the original context. This research introduces a "minimally disruptive" alternative: intervening in the representation space rather than the text space. By manipulating the high-dimensional vectors produced by models like BERT, the authors demonstrate that one can achieve the benefits of counterfactual analysis with significantly lower computational overhead.

2. Methodology: Linear Counterfactual Representations

The core of the proposed method is the decomposition of a text representation $X$ into two distinct, orthogonal components based on a protected or target attribute $Z$. This process relies on Linear Concept Erasure:

  1. Decomposition: The representation $X$ is split into $X^\perp$ (the component containing no linear information about $Z$) and $X^\parallel$ (the component that encodes $Z$).
  2. Minimal Intervention: To simulate a counterfactual where the attribute $Z$ is changed to a new value $z$, the method leaves $X^\perp$ untouched. This ensures the "essence" of the original text is preserved. The component $X^\parallel$ is then replaced with a new value calculated via linear regression, conditioned on $X^\perp$ and the target value $z$.
  3. Causal Grounding: A key contribution of this work is the mathematical proof that this geometric operation aligns with Pearl’s Structural Causal Models (SCM). The resulting CFR is equivalent to the expectation of a counterfactual distribution under a specific Gaussian SCM, providing a rigorous theoretical foundation for the intervention.

Unlike adversarial training or deep generative approaches, this method uses closed-form projections and linear regressions, making it extremely fast and easy to integrate into existing workflows.

3. Experimental Setup

The authors evaluated the CFR method across four diverse settings to test its fidelity, causal estimation capabilities, and utility in bias mitigation:

  • EEEC+: An extension of the Equity Evaluation Corpus for mood classification, featuring ternary race and binary gender attributes. This synthetic dataset allows for direct comparison between representation-level interventions and "ground truth" text-level edits.
  • CEBaB: A restaurant review dataset where specific aspects (e.g., food, service) are manually edited, used to validate the estimation of Average Treatment Effects (ATE).
  • BiasInBios: A real-world dataset of professional biographies used to evaluate the method's ability to identify and reduce gender bias in occupation classification.
  • GloVe: Pre-trained word embeddings used for sanity checks to ensure that vector-level counterfactuals (e.g., transforming "bridesmaids" toward a male attribute) align with intuitive semantic shifts (e.g., moving toward "groomsmen").

4. Key Results and Insights

4.1 Fidelity and Causal Estimation

The experiments demonstrate that representation-level interventions are a highly effective proxy for text-level changes. On the EEEC+ dataset, predictions made on CFRs matched predictions made on actual human-written counterfactual text over 90% of the time. Furthermore, in the CEBaB dataset, the CFR-based estimates of causal effects closely aligned with empirical results derived from human-edited reviews, suggesting that the latent space captures the necessary causal structure.

4.2 The Linearity of Concepts

A significant finding is the effectiveness of the linearity assumption. While neural encoders are non-linear, the information for specific concepts like gender or race is often captured within linear subspaces. This allows simple projections to isolate and transform these concepts without disrupting the broader semantic meaning of the representation.

4.3 Bias Mitigation via Data Augmentation

CFRs are not merely diagnostic; they can be used to improve model fairness. By augmenting training sets with counterfactual representations, the authors reduced the "True Positive Rate Gap" (a common measure of gender bias) on the BiasInBios dataset from 0.070 to 0.060. This performance is comparable to complex "scrubbing" techniques but is more efficient and retains more of the original information in the embeddings.

5. Conclusion and Future Work

This paper demonstrates that counterfactual explanations for text classifiers do not require the generation of new text. By operating in the representation space, CFRs provide a theoretically grounded and computationally efficient path toward interpretable and fairer NLP models.

However, the reliance on linear erasure remains a primary limitation. While effective for BERT-style embeddings, non-linear concept encoding may require more sophisticated erasure techniques. Future research should explore non-linear regressions and the scalability of this method to more abstract or complex concepts that lack simple labels. Despite these challenges, CFRs represent a significant step toward making causal interpretability a standard component of the NLP pipeline.

Summary updated

BibTeX
@inproceedings{lemberger2024explaining,
  author = {Pirmin Lemberger and Antoine Saillenfest},
  title = {{Explaining Text Classifiers with Counterfactual Representations}},
  booktitle = {ECAI},
  year = {2024},
  doi = {10.3233/faia240576},
}
Figure