GA ttention: Gated Attention for the Detection of Abusive Language (2025)

Abstract: Abusive language online creates toxic environments and exacerbates social tensions, underscoring the need for robust NLP models to interpret nuanced linguistic cues. This paper introduces GAttention, a novel Gated Attention mechanism that combines the strengths of Contextual attention and Self-attention mechanisms to address the limitations of existing attention models within the text classification task. GAttention capitalizes on local and global query vectors by integrating the internal relationships within a sequence (Self-attention) and the global relationships among distinct sequences (Contextual attention). This combination allows for a more nuanced understanding and processing of sequence elements, which is particularly beneficial in context-sensitive text classification tasks such as the case of abusive language detection. By applying this mechanism to transformer-based encoder models, we showcase how it enhances the model’s ability to discern subtle nuances and contextual clues essential for identifying abusive language, a challenging and increasingly relevant NLP task.

## introduction abusive language (al) has become an increasingly urgent concern in today's digitally connected world, where social media and online platforms serve as primary venues for communication @xcite . offensive or hateful content can foster a toxic atmosphere, harm vulnerable communities, and exacerbate social tensions @xcite . consequently, detecting al has emerged as a pivotal challenge within natural language processing (nlp), while numerous challenges exist in the detection of al, one of the most critical lies in the need for models capable of interpreting both offensive and non-offensive words, along with the underlying context in which such language is used for the accurate identification of al @xcite . in parallel with this societal need, current encoder-decoder transformer models have gained widespread acceptance in the nlp community, offering a versatile framework for various tasks @xcite . their adaptability allows for finetuning in a wide range of tasks, including question answering, machine translation, text classification, text generation, text summarization, sentiment analysis, and named entity recognition @xcite . a key component of these models is the attention mechanism, which is crucial for capturing dependencies within a sequence and conditioning the model's outputs on these learned relationships @xcite . over recent years, numerous attention mechanisms have been proposed to address diverse tasks in nlp and computer vision @xcite . these mechanisms focus on modeling local and global dependencies @xcite , unifying different modalities by projecting features from one modality to another @xcite , integrating various abstraction levels within an input sequence @xcite , or combining the outputs of distinct attention modules @xcite . particularly relevant are contextual attention (ca) @xcite and self-attention (sa) @xcite , which approach the modeling of local and global dependencies in distinct ways. while sa captures local dependencies by deriving its query vector directly from elements within the same sequence, ca models global dependencies by emphasizing specific tokens based on an external query vector, which is updated according to task-specific requirements @xcite . both sa and ca have demonstrated strong performance in many applications @xcite ; however, each also presents limitations. ca overlooks the local relationships among tokens within a se-quence, whereas sa does not explicitly account for the global relationships across distinct sequences. this can result in the loss of crucial information, potentially weakening a model's ability to interpret context, a critical element for tasks such as al detection, where meaning can shift dramatically based on linguistic nuance. for the sake of clarity, we use the term local relationships to refer to the connections between the words present within a single message, and the term global relationships to denote their interpretation within a broader domain or a task-specific setting. in our application domain, their use in other texts related to al. to address these limitations, we introduce gattention, a novel gated attention mechanism that combines the strengths of both sa and ca to capture local and global dependencies better. by weighting and merging the local (sa) query vectors and the global (ca) query vectors, gattention preserves the crucial contextual cues needed for complex classification problems. additionally, we introduce the multi-head gattention mechanism to enhance the model's ability to attend to different aspects of the input simultaneously, improving its capacity to capture intricate relationships within the data. although both the gattention and multi-head gattention variants can be applied to any sequence of word/token encoding features (or multimodal feature sequences), we focus on evaluating their efficacy in context-sensitive text classification tasks, particularly in various tasks related to the detection of al. these tasks are ideally suited for testing our gattention mechanism variants since text classified as abusive hinges on the context in which words are used, distinguishing between offensive and non-offensive usage @xcite . the results obtained from the different gattention configurations were encouraging, achieving performance improvements in five out of six al detection subtasks when compared to three replicated stateof-the-art approaches, including techniques based on large language models (llms). furthermore, the proposed mechanisms outperformed the best results reported in the shared tasks, while maintaining a favorable trade-off between performance and model size. in summary, the main contributions of this paper are as follows: 1) the proposal of the gattention mechanism, which leverages the relevance of the local (instance-dependent) and global (taskdependent) features of any encoding sequence by combining the sa and ca representations. 2) the multi-headed adaptation of the gattention mechanism and its integration in contemporary transformer encoding models for the al detection task. 3) the evaluation and analysis of the gattention and multi-head gattention mechanisms across three different datasets dedicated to detecting al on social media platforms. ## proposed gated attention mechanism this section is organized into two subsections. the first subsection details the gattention mechanism, while the second presents its extension into a multihead perspective. the gattention mechanism processes inputs from two distinct representations derived from the sa and ca mechanisms, respectively. figure 1 illustrates the generation of these representations, starting from a sequence of encoded features x @xmath0, where d represents the number of encoding features, and n is the number of elements in the sequence. as depicted in figure 1 , the process begins with the encoded features x e sequence. the representations q, k, and @xmath1. this is achieved by multiplying the matrix x e with the weight matrices w q , w k , and w @xmath2, respectively. the sa and ca representations are calculated using these matrices, denoted by the matrices x s and x @xmath3. for the sa representation, the scaled dot-product attention mechanism, as proposed in @xcite , is employed; equation 1 details the process for computing the matrix x s . concerning the ca representation, we use the ca mechanism proposed in @xcite . this mechanism receives the matrix q as input and utilizes a context vector u @xmath4, randomly initialized and subsequently learned during training. the vector u h is a query vector to compute the attention values α @xmath5. this is achieved by measuring the similarity between the elements of the sequence q and the application domain represented by u h . such similarity is calculated as detailed in equation 2, involving the scalar dot product of u t h and q; the resulting values are then normalized using a softmax function. in contrast to the ca mechanism proposed by @xcite , which employs a weighted sum between each attention value and its corresponding encoded features for the final sequence representation, our representation x c , as shown in equation 3, captures all the information from the attention values. this is done by performing an element-wise multiplication ⊙ between each scalar of α c and its corresponding encoding features in q. ## main results to compare the performance of our gattention mechanism against a similar approach that combines sa and ca representations, the second section (rows 6 -9) presents the results of incorporating the sca mechanism @xcite into the final encoding layer of the transformer models. integrating the sca mechanism yields a uniform enhancement across the three evaluation datasets. overall, the best results of this integration were obtained with the ernie model. the third section (rows 10 -13) shows the perforthe results present the mean from three random training runs. the "am" column stands for "attention mechanism", while "bstr" and "cb" refer to the best shared-task result and contemporary benchmark, respectively. sections 4 and 5 of when comparing the results obtained from the contemporary benchmarks (rows 23 -25), it can be observed that the cb rhb approach consistently outperforms the llm-based benchmarks (row 23 vs. rows 24 and 25) across all subtasks of the three evaluation datasets. moreover, it achieves this superior performance with significantly fewer parameters, highlighting the advantages of leveraging pre-trained encoding models for al classification, particularly in fine-grained tasks such as subtask b of the ami 2018 dataset. when compar-ing our best configuration with the best contemporary benchmark (row 21 vs. row 23), our approach again achieves superior performance in five out of six subtasks. the only exception is subtask a of the hasoc 2019 dataset, where performance is slightly lower. upon examining the instances in this dataset, we hypothesize that this may be due to the presence of code-switching, which poses additional challenges. it is worth noting that the topranked systems and the contemporary benchmarks typically rely on llms, ensemble strategies, pretraining of transformer models on large-scale corpora using pre-training tasks specifically designed for al detection, and extensive data augmentation techniques @xcite @xcite @xcite . in contrast, our approach solely integrates variants of the gattention mechanism into existing pretrained encoding models, without requiring additional pre-training or ensemble methods. finally, ## analysis as a first step, we calculated the z and 1z values for all tokens in the test instances posttraining. next, we averaged these values for each token and extracted the top 25 tokens with the highest averages. this process aimed to identify the most relevant words or expressions for the sa and ca representations within the datasets. figure 4 presents word clouds for these top 25 tokens, showcasing both representations. the word clouds of the ca representation include offensive and harsh terms like 'f**k', 'b**ch', 'h**e', 's**t', etc. additionally, words frequently associated with targets and stereotypes in hate speech, such as 't**ror**t', 'woman', 'drive', 'liberals', and 'conservatives', are also included. conversely, the sa representation word clouds predominantly feature reflexive pronouns, verbs, conjunctions, and adjectives. this distinction underscores the ca mechanism's significant role in differentiating contextual and semantic elements in the al detection task. to further support this analysis, appendix a.4 contrasts the sa values produced by the gattention mechanism with the attention weights from the ernie model. additionally, appendix a.5 provides an instancelevel analysis of the sa and ca representation values. ## conclusions and future work one of the main challenges in using current attention mechanisms is the loss of contextual or internal information among the tokens in a text sequence. to address this, we proposed the gattention and the multi-head gattention mechanisms. these mechanisms combine the sa and ca representations through a weighted approach based on their contributions to the text classification tasks. specifically, we integrated the gattention mechanism into four pre-trained transformer models and evaluated its performance in the al detection task. this task was chosen due to the highly contextdependent interpretation of words, making it an ideal testbed for assessing the efficacy of the proposed mechanism. the results were encouraging, demonstrating improvements across all models and evaluation datasets, which included both binary and multi-class classification tasks targeting different types and targets of al. notably, the best performance was achieved using the multi-head gattention mechanism with 12 heads integrated into the ernie model. furthermore, our analysis revealed a dynamic adaptation in the relevance of the sa and ca representations, highlighting the mechanism's effectiveness in detecting al. as future work, we consider 1) evaluating the gattention mechanism in tasks where context interpretation is critical for classification, such as in detecting depression, deception, and fake news; 2) extending the application of the gattention mechanism to al detection in memes, by integrating attention representations from different modalities; and 3) developing novel loss functions that incorporate the context vector of the ca mechanism into the classification process. on the other hand,