Using Transfer-based Language Models to Detect Hateful and Offensive Language Online (2020)

Abstract: Distinguishing hate speech from non-hate offensive language is challenging, as hate speech not always includes offensive slurs and offensive language not always express hate. Here, four deep learners based on the Bidirectional Encoder Representations from Transformers (BERT), with either general or domain-specific language models, were tested against two datasets containing tweets labelled as either ‘Hateful’, ‘Normal’ or ‘Offensive’. The results indicate that the attention-based models profoundly confuse hate speech with offensive and normal language. However, the pre-trained models outperform state-of-the-art results in terms of accurately predicting the hateful instances.

## introduction the majority of the tweets on twitter or posts on facebook are harmless and often posted purposefully, but some express hatred towards a targeted individual or minority group and members. these posts are intended to be derogatory, humiliating or insulting and are defined as hate speech by @xcite . different from offensive language, hate speech is usually expressed towards group attributes such as religion, ethnic origin, sexual orientation, disability or gender @xcite . some of the biggest firms invest heavily in tracking abusive language, e.g., automatic detection of offensive language in comments @xcite or giving a percentage of how likely a text is to be perceived as toxic. 1 however, these and other existing tools share a common flaw of not distinguishing between offensive and hateful language. one important reason to keep these two separate is that hate speech is considered a felony in many countries. the task of separating offensive and hateful language has shown to be demanding; however, with the recent scientific breakthroughs and the concept of transfer learning, we can take huge steps in the right direction. the paper investigates the effects of transferring knowledge from the bidirectional encoder representations from transformers (bert; @xcite language model on distinguishing hateful, offensive and normal language, by fine-tuning the pre-trained bert language model with data containing hateful and offensive language, and comparing its performance to the state-of-the-art on two widely used hate speech detection datasets. those datasets are presented section 2. section 3 then gives an overview of related work in the field of hate speech detection. section 4 describes the implemented system architecture. section 5 presents the experiments, including setup and results, while section 6 evaluates and discusses those results. section 7 concludes and suggests future work. ## data many existing datasets containing hate speech are publicly available for use and consist of data from several sources online, mainly twitter @xcite @xcite @xcite @xcite , while some cover other sources such as fox news comments @xcite and sentences from posts on the white supremacist online forum @xcite . almost all available datasets are labelled by humans, foot_1 which results in different approaches taken when creating and annotating the datasets. some researchers use expert annotators @xcite , others use majority voting among several the dataset created by founta et al. (2018b) contains almost 100k annotated tweets with four labels, "normal", "spam", "hateful" and "abusive". as the authors only provide tweet ids for researches to retrieve tweets through the twitter application programming interface (api), some tweets may for several reasons not be retrievable, e.g., a tweet or the user account behind a tweet may have been deleted; thus, of the 99,799 provided tweet ids, only 68,299 tweets were retrieved. the label distribution of those compared to the original label distribution for dataset f is shown in transferring knowledge from word embeddings to be used as input to neural networks has been a common technique. @xcite experimented with character n-grams in combination with word embeddings from word2vec @xcite in various convolutional neural network (cnn) setups, with the best performing model using transferred knowledge from word2vec. adding character n-grams boosted precision, but lowered recall. @xcite experimented with several machine learners and neural networks, with the best performer being an long short-term memory (lstm) with random word vectors where the network's output was used as input to a gradient boosted decision tree. however, their results have shown questionable and difficult to reproduce @xcite . @xcite al. (2017a park and fung (2017) created a hybrid system that tried to capture features from two input levels, using two cnns, one character-based and one word-based. meyer and @xcite proposed an optimised architecture combining components with cnns and lstms into one system. one part of the system used character n-grams as input while the other part used word embeddings. they used the dataset from @xcite , obtaining better results than previous solutions. most of the research discussed above used that dataset (with labels 'sexist', 'racist' or 'neither') or a slightly modified version @xcite . the dataset by @xcite in contrast separates hateful language from offensive and normal language, making the task harder. @xcite used this dataset and six other, but merged the offensive class with the normal class. on the 2-class hate vs normal language task, they outperformed the state-of-the-art on 6 out of 7 datasets with a system feeding word embeddings from word2vec into a cnn to produce input vectors for an lstm network with gru cells performing the final classification. founta et al. (2018a) used the same dataset, but kept the offensive samples separate from the normal ones, thus taking on the challenge of separating hateful and offensive language. they ran two networks in parallel, one rnn with text input and one feed-forward network with metadata input, followed by a concatenation layer and a classification layer, performing slightly below the f 1 -score 0.900 @xcite achieved with a baseline lr model. however, @xcite surpassed the baseline using pre-trained word embeddings as input to multiple multilayer perceptron (mlp) layers, achieving a total f 1 -score of 0.924. still, the fscore increase is due to better performance on the 'normal' and 'offensive' classes, with the model actually performing worse on the 'hate' class. this agrees with malmasi and zampieri (2018) who tested several supervised learners and ensemble classifiers on the dataset, reporting a noticeable difficulty of distinguishing hateful language from profanity. their extensive results analysis showed that tweets with the highest probability of being tagged as hate usually are targeted at a specific social group, so that contextual and semantic document features may be required to improve performance. @xcite in contrast claimed near-perfect performance, misclassifying only 0.035% of true hate speech samples on a combination of datasets from @xcite using n-grams as features and feeding the tf-idf values of these into classifiers such as support vector machine, naïve bayes and logistic regression. however, analysing their training and test data foot_2 shows that 74% of the test data is either duplicate or in the training data, giving a highly biased test set and questionable results. basile et al. (2019) and zampieri et al. (2019, 2020 ) present findings from semeval-2019 task 5 and 6 resp. -2020 task 12, observing that pretrained attention-based deep learning models were used by the top teams in all subtasks. @xcite and @xcite were the top teams in semeval-2019 task 5, using elmo together with lstm networks. elmo (embeddings from language model; @xcite uses a bidirectional language model to create deeply contextualised word representations, with unsupervised pre-training. gpt (generative pre-training transformer; @xcite expanded the amount of text the language model can be trained on by combining the ideas of unsupervised pre-training @xcite and transformers @xcite with attention. bert @xcite is a direct descendant of gpt, although instead of using a stack of transformer decoders, bert uses a stack of transformer encoders, and while gpt only trains a forward language model, bert is bidirectional. with the release of two pre-trained language models, bert base and bert large , bert can be used as a language model for tasks such as hate speech detection. @xcite used bert base to deliver some of the best results in semeval-2019 task 6, while several semeval-2020 tasks saw continuous transformer multitask pre-training (ernie 2.0; @xcite outperforming other solutions. ## architecture word embedding techniques based on bag-ofwords contexts, such as word2vec @xcite , only capture the semantic relations among words @xcite , whereas language models are more complex and can capture the meaning of a word in a sentence, i.e., its context. this work focuses on such language models and explores the effect of transferring knowledge from a substantial pre-trained language model to a classifier predicting hateful and offensive expressions. ## experiments and results the two original pre-trained language models bert base and bert large from @xcite were tested together with the two language models (bert base* and bert large*) further trained with domain-specific data. each system's performance was tested with the two datasets d @xcite . dataset f annotates tweets as 'hateful', 'offensive' 'spam' or 'normal'. when identifying hateful and offensive language, the 'spam' class is redundant and was omitted. however, to compare to previous research, experiments with the original 4-class dataset f were also carried out. all text data in the experiments were lowercased. both datasets were split into a training set containing 80% of the total samples and a held-out test set containing the remaining 20%, with scikitlearn's stratified splitting function used to ensure equal class balance between the sets. the order of the training samples was shuffled before each run. cross-validation with multiple folds was not implemented due to framework limitations. all experiments were run on devices with at least 64gb ram, the amount recommended by the creators of bert. the two original language models were pre-trained with a sequence length of 512 and batch size 256. the fine-tuned models had a sequence length of 128 and batch size 32. all four language models were trained with the adam optimiser, with the optimal learning rates found to be 3e-5 for the fine-tuning process and 2e-5 for the classification process after an exhaustive search with parameters suggested by @xcite . other parameters shared by the four systems are a dropout probability of 10% on all layers, the number of training epochs which was 3, and an evaluation batch size of 8. the fine-tuning of the language models took around 3 hours on two nvidia v100 gpus with 32gb ram each, while classification with bert base and large took on average around 1 and 2 hours, respectively. system performance will be measured by micro averaged precision, recall, and f 1 -score, as this is more suitable for unbalanced datasets and gives detailed insights into how the models classify each sample. the macro averaged total for each metric will also be presented for comparison reasons. ## evaluation and discussion the main difference between the two datasets used in the experiments is the size and label distribution. the size of dataset f allows for more training samples than dataset d although systems transferring knowledge from pre-trained language models have shown that even small datasets can achieve similar performance @xcite . the four models' overall performance on datasets d and f are the same despite the fact that the latter dataset allows for more language model fine-tuning. the label distribution in dataset f is more realistic than dataset d, where a large portion of the samples is labelled as 'offensive'. however, this unbalance of dataset d does not seem to affect the models' performance noticeably. the reason is probably that dataset d contains a sufficient amount of class samples for the models to learn the other two classes. this ability to learn with a few training examples is one of the main advantages of using language models instead of traditional word embeddings. ## conclusion and future work to explore the effects of applying language models to the downstream task of hate speech detection, four systems based on the bert language models were implemented and tested on two datasets annotated both for hateful and offensive language. two of the systems were further pre-trained with unlabelled domain-specific data. however, the results did not reflect any notable improvement with the extended language models. all four models achieved f 1 -scores close to or above state-of-the-art solutions on both datasets, and their ability to correctly distinguish hate speech from offensive and ordinary language was considerably better than the compared solutions, but the scores on the 'hateful' class are not sufficient enough to bring the systems into practical use, as hateful expressions would pass through the system or more benign cases would be incorrectly censored. still, language models bring a considerable potential to understanding all the nuances of hateful utterances, and further exploration of how to most effectively train and transfer knowledge from them is necessary. the models used in the experiments were all pre-trained on the english wikipedia and book-corpus to obtain general language understanding. typically, the language that appears in wikipedia articles and books are somewhat domain neutral and formal. this language may be too different from the hate speech domain in terms of words and sentences. therefore, it may be beneficial to collect documents from hate speech datasets and create one large corpus, which can be used as input data to pre-train bert's encoders from scratch. a problem with bert is the vast number of parameters that need to be set, leading to memory problems and long training times. however, the usage of transformers for language processing is a fast-moving field, so several ideas and strategies have lately been introduced to improve on the original bert setup. one of those -such as al-bert, 'a lite bert' @xcite ; gpt-3, 'generative pre-trained transformer' @xcite ; continuous pre-training ('ernie 2.0'; @xcite ; transformers for longer sequences ('bigbird'; @xcite ; or layerwise adaptive large batch optimisation ('lamb'; @xcite -could be tested on the task. @xcite 's albert can drastically reduce the number of parameters and help solve memory problems and reduce training times. zaheer et al. (2020)'s 'bigbird', with its sparse attention mechanism, allows for longer input sequences than bert and is suitable for tasks where the datasets include longer documents. @xcite utilised large batch stochastic optimisation methods to reduce the training time of bert remarkably. as describe in section 4.3, each tweet in the training set was split into two for the next sentence prediction task bert is performing during pre-training. this was done because tweets rarely contain two full sentences. however, this strategy can lead to some loss of linguistic information and it may be better to just skip next sentence prediction during training and only perform the masked language model task.