Contrastive Learning for Fair Representations
Aili Shen, Xudong Han, Trevor Cohn, Timothy Baldwin, Lea Frermann
Introduction
Neural methods have achieved great success for classification tasks in NLP and computer vision. However, datasets which neural models are trained on embody cultural and societal stereotypes from the real world. Models trained on such datasets often capture spurious correlations between target labels and protected attributes, leading to biased predictions (i.e., models perform unequally for different sub-groups) and leakage of authorship-related sensitive information from learned representations (i.e., attackers can recover the demographic information from learned representations). This kind of unfairness has been identified in various tasks, such as twitter sentiment analysis (Blodgett, Green, and O’Connor 2016; Han, Baldwin, and Cohn 2021b), part-of-speech tagging (Hovy and Søgaard 2015; Li, Baldwin, and Cohn 2018; Han, Baldwin, and Cohn 2021a), and image activity recognition (Wang et al. 2019; Zhao et al. 2017).
To mitigate bias associated with protected attributes, various kinds of methods have been proposed (Zhao et al. 2018, 2017; Li, Baldwin, and Cohn 2018). Data manipulation, such as balancing the dataset with respect to the protected attribute (Wang et al. 2019) and augmenting a gender-biased dataset with gender-swapped sentences (Zhao et al. 2018), can reduce bias at the input level, however it can be costly in terms of time and compute resources. And it has been demonstrated that it is not an effective way to reduce bias. Adversarial training is a popular method for mitigating bias by preventing a discriminator from reverse engineering protected attribute information from learned representations (Elazar and Goldberg 2018; Resheff et al. 2019; Han, Baldwin, and Cohn 2021b, a; Li, Baldwin, and Cohn 2018). However, it is often difficult to optimise and increases model complexity and, consequently, computational cost.
We propose a novel debiasing method based on contrastive learning (Oord, Li, and Vinyals 2018; Li et al. 2021a; Tian, Krishnan, and Isola 2020; Henaff 2020; Bui et al. 2021; Li et al. 2021b; Chen et al. 2020b). Driven by the intuition that good and fair representations for classification should pull instances together only if they belong to the same class but not based on shared protected attributes (such as gender or race), we present an effective debiasing method based on contrastive learning. Specifically, our proposed method combines two contrastive loss components with a cross-entropy loss, thereby maximising the similarities of instance pairs which share a main task label and minimising the similarities of such pairs from the protected attribute perspective. To the best of our knowledge, our work is the first to integrate contrastive loss components to obtain fairer representations. We demonstrate the effectiveness of our method across four tasks, spanning NLP and computer vision. Our contributions in this work are:
We present a debiasing method based on contrastive learning, combining cross-entropy loss with two contrastive loss components;
Experimental results over four NLP and computer vision tasks show that our proposed method achieves the best accuracy–fairness tradeoff in each case;
Our method is simple to implement and agnostic to model architectures, and incurs minimal additional computing cost.
Related Work
We briefly review research in the two most related areas: debiasing methods and contrastive learning.
Prior debiasing methods fall into three categories. First, data manipulation aims to balance the input, followed by re-training the model on a fairer dataset (Wang et al. 2019; Badjatiya, Gupta, and Varma 2019; De-Arteaga et al. 2019; Elazar and Goldberg 2018). However, it has been shown to be both computationally prohibitive for large datasets or models, and ineffective in ensuring fair models (De-Arteaga et al. 2019; Wang et al. 2019). Second, post-processing methods “bleach” sensitive information from learnt representations after main task training. For example, Iterative Null Space Projection (INLP; Ravfogel et al. (2020)) iteratively trains a linear discriminator over a protected attribute given pre-computed fixed representations. The representations are then projected onto the linear discriminator’s null-space, thereby making it difficult for a linear classifier to identify the protected attribute. In the third category, approaches augment the original training objective, to encourage the model to learn representations that are oblivious to protected attributes. Adversarial models are the prime example (Li, Baldwin, and Cohn 2018; Zhang, Lemoine, and Mitchell 2018; Resheff et al. 2019; Wang et al. 2019; Barrett et al. 2019; Han, Baldwin, and Cohn 2021b), in leveraging one (Li, Baldwin, and Cohn 2018; Elazar and Goldberg 2018) or more (Han, Baldwin, and Cohn 2021b) discriminators to encourage the main model to learn representations that do not reveal protected information. Our method also introduces an augmented objective, however, unlike adversarial methods, it does not add additional model parameters, and hence is computationally much lighter weight. We compare our method against INLP and adversarial baselines.
Contrastive Learning
The basic idea behind contrastive learning (CL) is to pull similar instances together and push dissimilar instances apart by maximising the similarities of similar instances and minimising those of dissimilar pairs within the unit feature space (Oord, Li, and Vinyals 2018; Tian, Krishnan, and Isola 2020; Li et al. 2021a; Grill et al. 2020; Chen et al. 2020a; Henaff 2020). CL has been particularly successful in computer vision, where positive (similar) instance pairs can be generated via data augmentation (i.e., systematic, meaning-invariant manipulation of an input image such as cropping or blurring (Chen et al. 2020a; Fang et al. 2020; Cubuk et al. 2019)), and negative (dissimilar) instance pairs correspond to different items in the original data. More recently, supervised contrastive learning (SCL) was proposed in the context of classification, where positive instances belong to the same class, and negative instances belong to different classes (Khosla et al. 2020). SCL, when combined with a cross entropy loss, has been shown to improve model robustness to noise and data sparsity (Gunel et al. 2021) as well as adversarial attacks (Bui et al. 2021). We adapt SCL to fair supervised learning, and present evidence of its effectiveness in learning debiased representations and fair classifiers.
Fair & Supervised Contrastive Learning
Our proposed method equips supervised contrastive learning with an improved loss function which simultaneously encourages data separation in terms of the main class labels, and discourages the differentiation of data points on the basis of their protected attributes. Fair contrastive learning is illustrated in Figure 1, and is compatible with different classifier architectures and data modalities, such as language and vision. Our architecture consists of three components:
An embedding module, Embed(), which maps an input instance (e.g., a document or an image) to a vector representation , which is in turn used as input to the encoder network;
An encoder network, Enc(), which maps the input representation to the final hidden representation;
An aggregated objective (), which is a weighted combination of a cross-entropy loss, contrastive loss based on main task labels, and contrastive loss based on protected attribute labels.
where is the number of main task classes; denotes that the th instance belongs to the main task class ; denotes the predicted probability of the th instance belonging to the main task class ; and is the softmax output from the classifier, whose input is . However, cross-entropy loss focuses only on maximising the predicted probability of the th instance belonging to the gold-standard class, and not on how similar versus dissimilar instances are located in representation space. In this work, we explicitly model the similarity of instances in the representation space via supervised contrastive learning.
Contrastive Losses
Given a mini-batch with a set of randomly sampled instances, positive instance pairs (those which are truly equivalent) and negative instance pairs (those representing distinct concepts) are formed. We use two different criteria for creating these pairs: their main task label, and their protected attribute, as described below. Assuming a batch of positive and negative pairs, the contrastive loss is computed as,
is computed on positive and negative samples constructed based on main task labels (e.g., pos vs neg sentiment), where instances in the mini-batch belonging to the same main task class are used to construct positive samples; otherwise, they are used to form negative samples. The intuition behind this loss component is that representations that are well-separated for the main task are more desirable, as illustrated in the top quadrant of Figure 1, where the main task labels are indicated in blue and orange, and are separated into distinct clusters.
is based on positive and negative samples with respect to protected attribute labels (e.g., male vs female), where instances belonging to the same protected attribute class form positive samples; otherwise, they are used to construct negative samples. Our goal is to infer latent representations which are oblivious to the protected attribute of an instance. We enforce representations of instances with different protected attribute values to mix together by discouraging the model from effectively contrasting those instances, with the goal of reducing the correlation between the main task and protected attribute. This intuition is illustrated in the bottom quadrant of Figure 1.
Objective Function
Our final objective incorporates both contrastive learning methods, to produce task-indicative and protected-attribute-agnostic representations, as illustrated in the right quadrant of Figure 1. It is formulated as a weighted average of , , and ,
The second term, , pulls instances from the same main task label closer together, and pushes instances from different classes further apart, while the third term, , encourages instances with the same protected attribute to stay apart and instances from different classes to mix together. and are hyperparameters that control the relative importance of the cross entropy and contrastive learning terms.
Our method differs from existing debiasing methods in that fairer representations and predictions are: (1) achieved via contrastive learning rather than data manipulation; (2) jointly trained with the base classifier, rather than removing protected attribute information through post-processing, such as with INLP (Ravfogel et al. 2020); and (3) obtained without the need to train an additional network, as necessary for adversarial methods (Li, Baldwin, and Cohn 2018). We show in extensive experiments that our model is superior to adversarial and post-processing methods in terms of the performance–fairness tradeoff, and more efficient to train than adversarial debiasing.
Experiments
We vary the architecture of Embed() across different tasks, and do not finetune it during training.For hate speech detection and activity recognition, Embed() is first pretrained or finetuned to obtain task-specific representations, and then fixed in later stages of training. The architecture of Enc() consists of two fully-connected layers with a hidden size of 300. All models are trained and evaluated on the same dataset splits, and models are selected based on their performance on the development set. For fair comparisons, we finetune the learning rate, batch size, and extra hyperparameters introduced by the corresponding debiasing methods for each model on each dataset. Details of the hyperparameters for each model and dataset, such as the number of layers and activation functions, are included in Supplementary Material. For all experiments, we use the Adam optimiser (Kingma and Ba 2015) and early stopping with a patience of 5. In the absence of a standardised method for performing model selection in fairness research (noting the complexity of model selection given the multi-objective accuracy–fairness tradeoff), we determine the best-achievable accuracy for a given model, and select the hyperparameter settings that minimise GAP while maintaining accuracy as close as possible to the best-achievable value (all based on the dev set). The development of a robust, reproducible, standardised model selection method is desperately needed in fairness research, and something that we plan to investigate in future work.
We compare our method with various baselines:
CE: train Enc() with cross-entropy loss and no explicit bias mitigation.
INLP: train Enc() with cross-entropy loss, and apply iterative null-space projection (“INLP”: Ravfogel et al. (2020)) to the learned representations. Specifically, a linear discriminator is iteratively trained over the protected attribute to project the representation onto the discriminator’s null-space, thereby reducing protected attribute information from the representation.
Adv: jointly train Enc() with cross-entropy loss and an ensemble of 3 adversarial discriminators over the protected attribute, with an orthogonality constraint applied to each pair of sub-discriminators to encourage them to learn different aspects of the representations (Han, Baldwin, and Cohn 2021b).
Evaluation Metrics
To evaluate the performance of models on the main task, we adopt Accuracy for all four datasets. We measure model bias in a number of different ways, via bias in the model predictions or linear leakage over hidden or logit representations.
measures the ability of a linear classifier to recover the protected attribute from a model’s output hidden representations or logits.
Leakage@h: based on the final hidden representation before the classifier layer.
Leakage@: based on the main task output (logits).
In each case, we train a linear-kernel SVM on outputs generated for the training instances, and measure leakage over the test instances. Lower values indicate a fairer model.
is a single aggregate measure comprising model performance as well as the three fairness metrics (GAP and leakage at and ). Before aggregation, we scale each metric to the unit interval by dividing the model-specific values by their respective maximum (), so that normalized values reflect the performance of each model relative to the best result. Next we assign predictive performance and overall fairness equal weights. Between fairness measures, we weigh prediction bias equal to overall leakage, leading to: \text{Tradeoff}{=}\frac{1}{2}{N}(\text{Accuracy})+\frac{1}{4}{N}(1{-}\text{GAP})+\frac{1}{8}{N}(1{-}\text{Leakage@{h}}){+}\frac{1}{8}{N}(1{-}\text{Leakage@\hat{\textbf{y}}}). The best achievable Tradeoff is 1, which indicates that a model outperformed all other models with respect to all metrics.
measures the GPU time required to train a model to achieve the reported results, averaged over 10 runs.
We apply our models across 4 datasets, covering NLP and vision tasks, in the form of both binary and multi-class main task classification problems. We report results in terms of accuracy, fairness (GAP and linear leakage), and efficiency across all tasks. We additionally explore the accuracy–fairness tradeoff in detail for one binary NLP task (Moji) and one multi-class computer vision task (imSitu).
Experiment 1: Twitter Sentiment Analysis
The task is to predict the binary sentiment for a given English tweet, based on the dataset of Blodgett, Green, and O’Connor (2016) (Moji hereafter), where each tweet is also annotated with a binary private attribute indirectly capturing the race of the tweet author as either African American English (AAE) or Standard American English (SAE). Following previous studies (Ravfogel et al. 2020; Han, Baldwin, and Cohn 2021b), the training dataset is balanced with respect to both sentiment and race but skewed in terms of sentiment–race combinations (40% happy-AAE, 10% happy-SAE, 10% sad-AAE, and 40% sad-SAE, respectively).Note that the dev and test set are balanced in terms of sentiment–race combinations. The number of instances in the training, dev, and test sets are 100k, 8k, and 8k, respectively.
Implementation Details
Following previous work (Elazar and Goldberg 2018; Ravfogel et al. 2020; Han, Baldwin, and Cohn 2021b), we use DeepMoji (Felbo et al. 2017), a model pretrained over 1.2 billion English tweets, as Embed() to obtain text representations. The parameters of DeepMoji are fixed in our experiments.
Results
We plot the tradeoff between Accuracy and Leakage@h for INLP, Adv, and Con∗ on the test set in Figure 2, where points in red circles are Pareto frontiers for each model.Given two predictions whose Accuracy and Leakage@h are (, ) and (, ), if and , we say the prediction (, ) is dominated by the prediction (, ); otherwise, they are non-dominated predictions, and form part of the Pareto frontier. The results are obtained by varying the most-sensitive hyperparameter for each model: the number of iterations for INLP, the weight for adversarial loss for Adv, and for our method Con∗. We can see that our proposed method achieves the best performance in terms of both Accuracy and Leakage@h, while INLP and Adv achieve better Leakage@h at the cost of Accuracy.
Experiment 2: Hate Speech Detection
The task is to predict whether a tweet contains hate speech, based on the English Twitter dataset of Huang et al. (2020) (Hate speech), where each instance is labelled with several binary protected author attributes: binary gender (m/f), race (white/non-white), and age (29/29). Following the work of Huang et al. (2020) and Han, Baldwin, and Cohn (2021a), we debias models for age, which was shown to lead to the highest model bias in prior work. We use the standard data split of 31K/7K/7K train/dev/test instances, respectively.
Implementation Details
Following Huang et al. (2020) and Han, Baldwin, and Cohn (2021a), we train a bidirectional Gated Recurrent Unit (Cho et al. 2014) (bi-GRU) with a cell size of 200 to predict whether a tweet contains abusive language, which is later used as Embed() to obtain text representations (400d). We opt for a bi-GRU architecture as it has been shown to achieve the best bias reduction on the dataset (Huang et al. 2020).
Results
Experiment 3: Profession Classification
The task is to predict a person’s profession given their biography, based on the dataset of De-Arteaga et al. (2019), consisting of short online biographies which have been labelled with one of 28 professions (main task label) and binary gender (protected attribute). We use the dataset split of (De-Arteaga et al. 2019; Ravfogel et al. 2020), consisting of 257K/40K/99K train/dev/test instances.There are slight differences between our dataset and that used by De-Arteaga et al. (2019) and Ravfogel et al. (2020) as a small number of biographies were no longer available on the web when we scraped them.
Implementation Details
Following the work of Ravfogel et al. (2020), we use the “CLS” token representation of the pretrained uncased BERT-base (Devlin et al. 2019) as Embed(), without any further finetuning.
Results
Experiment 4: Activity Recognition
Given an image, the task is to predict the activity depicted in the image based on the imSitu dataset (Zhao et al. 2017; Wang et al. 2019), which contains 211 activity classes and binary gender labels. The dataset contains roughly 110 instances for each activity, making it difficult to obtain decent performance without finetuning the backbone model, and also making debiasing impractical. Therefore, we group these fine-grained labels according to their corresponding coarse-grained labels, where similar verbs are grouped into one class according to the FrameNet label hierarchy (Baker, Fillmore, and Lowe 1998). The resulting dataset contains 12 main task labels, and 12K/3K/2K train/dev/test instances.
Implementation Details
Following Wang et al. (2019), we use standard ResNet-50 encoder (He et al. 2016) pretrained on ImageNet, and replace the classifier layer. To extract activity-capturing representations, following the work of Wang et al. (2019), the classifier layer is first trained with a learning rate of 0.0001 and a batch size of 128 for at most 60 epochs. Then ResNet-50 is finetuned with a learning rate of 1e-5 and a batch size of 64 for at most 60 epochs. The best-performing snapshot evaluated on the dev set is used as the Embed() to obtain image representations (2,048d).
Results
Figure 2 shows the tradeoff plot between Accuracy and Leakage@h on the test set. Different models perform differently in terms of the tradeoff, with neither Adv nor Con∗ reducing bias substantially over INLP at higher levels of accuracy.
Analysis
To explore the impact of and , we conduct ablation studies on the Moji and Bios datasets by ablating one of the two contrastive loss components. We denote the model trained with \alpha\mathcal{L}_{\text{ce}}+\beta\text{\mathcal{L}_{\text{scl}}} as Con, and the model trained with \alpha\mathcal{L}_{\text{ce}}-\beta\text{\mathcal{L}_{\text{fcl}}} as Con.
The results are shown in Figure 3. We can see that Con∗ achieves the best performance across all evaluation metrics on the Moji dataset. On the imSitu dataset, Con∗ also achieves the best accuracy, while roughly equalling the best bias results. This illustrates the advantage of incorporating both contrastive loss components.
Visualising Representations
In Figure 4, we show t-SNE plots of the learned representations of CE and Con∗ on the Moji training set from the perspectives of the main task labels and protected attribute values. We can clearly see that for CE, the positive (happy) instances are mostly on the left of the figure and negative (sad) instances are mostly on the right of Figure 4 (upper left figure). From the race perspective, AAE instances are more likely towards the left and instances with SAE are most likely to be towards the right of Figure 4 (bottom left figure). For Con∗, the resulting representations show that instances belonging to the same class cluster together in terms of sentiment, and instances belonging to the different classes mix together in terms of race, affirming our motivation.
Binary Classification vs. Multi-class Classification
Based on experimental results from Table 1, we observed that Con∗ is effective in reducing bias in terms of GAP, Leakage@, and Leakage@h on the Moji and Hate speech datasets, which are both binary classification tasks. The relatively less impressive results of Con∗ on Bios and imSitu can be explained by the fact that it is difficult to find the sweet spot for representing instances in multi-class classification settings. That is, the more classes there are, the harder it is for the optimiser to separate the classes from one another, leading to scattered representations at the main task class level, and making contrastive learning less effective in bias reduction.
To verify this hypothesis, we focus on the professions of nurse and surgeon in the Bios dataset, two professions with well-documented gender stereotypes. As shown in Table 2, the row of CEm presents the results of the instances belonging to the class “nurse” and “surgeon” in the multi-class classification setting and the row of CEb shows the results of the same set data in the binary classification setting. We can see that moving from the multi-class classification setting into the binary classification setting, there is a big increase in Accuracy and a small decrease in GAP and Leakage@. In the binary classification setting, neither INLP nor Adv can effectively reduce GAP, while Con∗ is much more effective at reducing gender bias across all bias evaluation metrics in the binary classification setting, with a small decrease in Accuracy.
Limitations
A limitation of our proposed approach is that the method is designed to remove information related to protected attributes based on the assumption that the attacker model will be a linear classifier. We leave the investigation of protecting against attacks by non-linear classifiers to future work. In our work, the Embed() is not learned or fine-tuned together with Enc() and the classification layer in an end-to-end fashion. However, finetuning the Embed() has the potential for better task-specific or semantic-preserving representations of text and images, which may further remove biases encoded in the the pretrained models. Exploring how to construct informative negative samples in the multi-class classification setting, which has not been studied in the literature, is also an interesting direction for future work.
Conclusion
Biased representations and predictions can reinforce existing societal biases and stereotypes. Based on the intuition that similar instances belonging to the same main task class should be pulled together and similar instances belonging to the same protected attribute class should be pushed apart in the representation space, we proposed to combine cross-entropy loss with two contrastive loss components in optimising neural networks. Experimental results over four NLP and vision datasets demonstrate the effectiveness of our proposed method. Further analysis and ablation studies indicate the necessity of incorporating both contrastive loss components in bias reduction, to maintain main task accuracy.