Rule By Example: Harnessing Logical Rules for Explainable Hate Speech Detection (2023)

Abstract: Classic approaches to content moderation typically apply a rule-based heuristic approach to flag content. While rules are easily customizable and intuitive for humans to interpret, they are inherently fragile and lack the flexibility or robustness needed to moderate the vast amount of undesirable content found online today. Recent advances in deep learning have demonstrated the promise of using highly effective deep neural models to overcome these challenges. However, despite the improved performance, these data-driven models lack transparency and explainability, often leading to mistrust from everyday users and a lack of adoption by many platforms. In this paper, we present Rule By Example (RBE): a novel exemplar-based contrastive learning approach for learning from logical rules for the task of textual content moderation. RBE is capable of providing rule-grounded predictions, allowing for more explainable and customizable predictions compared to typical deep learning-based approaches. We demonstrate that our approach is capable of learning rich rule embedding representations using only a few data examples. Experimental results on 3 popular hate speech classification datasets show that RBE is able to outperform state-of-the-art deep learning classifiers as well as the use of rules in both supervised and unsupervised settings while providing explainable model predictions via rule-grounding.

## introduction content moderation is a major challenge confronting the safety of online social platforms such as facebook, twitter, youtube, twitch, etc. @xcite . major technology corporations are increasingly allocating valuable resources towards the development of automated systems for the detection and moderation of harmful content in addition to hiring and training expert human moderators to combat the growing menace of negativity and toxicity online @xcite . despite the popularity of deep learning approaches, many practical solutions used in products today are comprised of rule-based techniques based on expertly curated signals such as block lists, key phrases, and regular expressions @xcite @xcite . such methods are widely used due to their transparency, ease of customization, and interpretability. however, they have the disadvantage of being difficult to maintain and scale, in addition to being inherently fragile and noisy @xcite @xcite . figure 1 shows an example where logical rules, while explainable in nature, face the problem of being inflexible to their context of use in natural language. while a given rule may be too specific and fail to capture different variations of usage commonly found in content online, rules can also be too broad and incorrectly block lexically similar content. in contrast to the challenges faced by rule-based methods, data-driven deep learning approaches have shown great promise across a wide range of content moderation tasks and modalities @xcite @xcite . fueled by large amounts of data and deep neural networks, these complex models are capable of learning richer representations that better generalize to unseen data. the impressive performances of these models have resulted in significant industry investment in content moderation as-a-service. several technology companies such as google foot_0 , openai foot_1 , and microsoft foot_2 use these models to offer services to aid in content moderation. however, despite their significant investment, they face adoption challenges due to the inability of customers to understand how these complex models reason about their decisions @xcite @xcite . additionally, with the increasing attention around online content moderation and distrust amongst consumers, explainability and transparency are at the forefront of demands @xcite . this presents the challenging open question of how we can leverage the robustness and predictive performance of complex deep-learning models whilst allowing the transparency, customizability, and interpretability that rule-based approaches provide. prior works such as @xcite ; @xcite ; @xcite have explored learning from rules for tasks such as controlling neural network learning, assisting in human annotation, and improving self-supervised learning in low data scenarios. @xcite propose a rule-exemplar training method for noisy supervision using rules. while performant in denoising over-generalized rules in the network via a soft implication loss, similar to other ml approaches, this method lacks the ability to interpret model predictions at inference time. @xcite propose a general-purpose framework for the automatic discovery and integration of symbolic rules into pre-trained models. however, these symbolic rules are derived from low-capacity ml models on a reduced feature space. while less complex than large deep neural networks, these low-capacity models are still not easily interpretable by humans. therefore, the task of combining the explainability of rules and the predictive power of deep learning models remains an open problem. in order to tackle this problem, we introduce rule by example (rbe): a novel exemplar-based contrastive learning approach for learning from logical rules for the task of textual content moderation. rbe is comprised of two neural networks, a rule encoder, and a text encoder, which jointly learn rich embedding representations for hateful content and the logical rules that govern them. through the use of contrastive learning, our framework uses a semantic similarity objective that pairs hateful examples with clusters of rule exemplars that govern it. through this approach, rbe is able to provide more explainable predictions by allowing for what we define as rule-grounding. this means that our model is able to ground its predictions by showing the corresponding explainable logical rule and the exemplars that constitute that rule. we evaluate rbe in both supervised and unsupervised settings using a suite of rulesets. our results show that with as little as one exemplar per rule, rbe is capable of outperforming state-of-theart hateful text classifiers across three benchmark content moderation datasets in both settings. in summary, the contributions of this paper are: ## rule by example framework in this section, we outline the rule by example framework, define its operational terms, and describe its end-to-end architecture. we first formally describe the two main operational terms used in our framework: 1) ruleset -a ruleset is comprised of a series of executable functions that when given text as input "fire" if and only if all conditions defined in the rule are met by the input. figure 1 shows an example of a simple rule that is triggered if a given text contains the keywords "hate" or "loathe" and contains "women". rules can be any programmable function that acts on text such as regular expressions, blocklists, keywords, etc. in the scope of this work, we only consider simple rules that humans can easily interpret. as such an ml model cannot be considered a rule, given their black-box nature. 2) exemplar -an exemplar is a given textual example that well-defines the type of content governed by a rule. for example, x 1 and x 2 in figure 1 can be considered exemplars of rule r 1 since they correctly match the conditions of r 1 . consider a ruleset of rule-exemplar pairs r"tpr 1 , e 1 q, pr 2 , e 2 q, ..., pr n , e n qu where r i denotes a defined rule and e i denotes an exemplar for which r i correctly fires. for a given corpus x comprising labeled examples x"tpx 1 , y 1 q, px 2 , y 2 q, ..., px m , y m qu, each rule r i can be used as a black-box function r i : x ñ ty i , hu to noisily label each instance x such that it assigns a label y or no label at all. an instance may be covered by more than one rule or no rule at all. additionally, the cover set c denotes the set of instances in x where a rule r i fires. the generalization problem that arises when rules are applied noisily is two-fold. when rules are too broad the cover set c is large and incorrectly labels a large amount of non-hateful content. likewise, when rules are too strict and fragile, the cover set c is too small, and lexically and semantically similar content that is hateful ends up being ignored. our goal is to leverage these rules and their exemplars to facilitate explainable model learning. algorithm 1 supervised dual encoder training require: rule encoder θr text encoder θt input: training data x"px1, y1q...pxn, ynq, ruleset r"pr1, e1q, ..., prn, enq output: updated parameters θr, θt 1: initialize θr and θt 2: while not converged do 3: get mini-batch x b 4: for each instance xi in x b do 5: get exemplars ei " dorulesetpr, xiq 6: concatenate exemplars ei 7: end for 8: get θrpe b q and θtpx b q 9: compute l" 1 2 py b d 2 `p1 ´yb qmaxpmargin d, 0q 2 q 10: update parameters of θr and θt 11: end while ## experimental setup training we train all models with adamw optimizer and weight decay of 0.01 on all data. we employ early stopping with a ceiling of 10 epochs, a learning rate of 2e-5, batch size of 8, and linear learning rate warmup over the first 10% steps with a cosine schedule. our models are trained with nvidia tesla v100 32gb gpus using azure machine learning studio. we pre-process data and train all models with different random seeds over multiple runs. our implementation of rbe is based on huggingface transformers @xcite and sentence transformers @xcite . rbe utilizes two bert-based networks consisting of 110 million parameters each. approximately 2,000 gpu hours were required to train all hyperparameter variations of rbe plus the bert baseline across all 3 test sets. baselines we evaluate our training algorithms in both supervised and unsupervised settings. we compare against the baselines of applying logical rules as is and the current sota approach of training transformer-based sequence classifiers @xcite . ## results and discussion we analyze the results of our experiments, detail our insights, and discuss the implications of applying rbe for explainable hate speech detection. evaluation metrics: the precision, recall, and f1 score for each dataset in a supervised setting are reported in ## related work there has been active work on detecting hate speech in language @xcite @xcite . hate speech detection has proven to be a nuanced and difficult task, leading to the development of approaches and datasets targeted at various aspects of the problem @xcite @xcite . however, few attempts have been made to focus on the explainability of these models, which is an increasing area of concern surrounding their use online @xcite , thus leading to the continued utilization of less powerful but more explainable methods such as rules. prior works have explored incorporating logical rules into model learning. @xcite proposed to weakly learn from rules by pairing them with exemplars and training a denoising model. however, this requires defining rules for all output classes, making it inapplicable to the task of hate speech detection. additionally, this method only focuses on decreasing rule scope to solve the overgeneralization problem. it does not simultaneously tackle the over-specificity problem demonstrated in figure 1 . finally, this method does not provide a way for interpreting model predictions during inference. @xcite proposes a way to control neural network training and inference via rules, however, their framework represents rules as differentiable functions requiring complex perturbations to incorporate, making it more suitable to numerical rules such as those defined in healthcare and finance as opposed to the complex nuances of language. @xcite proposes a framework for the automatic induction of symbolic rules from a small set of labeled data. however, these rules are derived from low-capacity ml models and are as a result not human-readable or explainable. ## conclusion we introduce rule by example, an exemplar-based contrastive learning framework that enables learning from logical rules for accurate and explainable hate speech detection. specifically, we propose a novel dual-encoder model architecture designed to produce meaningful rule and text representations. rbe leverages a novel exemplar-based contrastive learning objective that converges the representations of rules and text inputs of similar classes. we share results on three public datasets for hate speech detection that validate the rule by example framework can not only vastly outperform the initial ruleset but also outperform baseline sota classification methods in both supervised and unsupervised settings. moreover, rbe enables rule-grounding which allows for more explainable model prediction benefits not available in sota classification methods alongside additional flexibility via ruleset adaptation.