U o R at S em E val-2021 Task 7: Utilizing Pre-trained D istil BERT Model and Multi-scale CNN for Humor Detection (2021)

Abstract: Humour detection is an interesting but difficult task in NLP. Because humorous might not be obvious in text, it can be embedded into context, hide behind the literal meaning and require prior knowledge to understand. We explored different shallow and deep methods to create a humour detection classifier for task 7-1a. Models like Logistic Regression, LSTM, MLP, CNN were used, and pre-trained models like DistilBert were introduced to generate accurate vector representation for textual data. We focused on applying multi-scale strategy on modelling, and compared different models. Our best model is the DistilBERT+MultiScale CNN, it used different sizes of CNN kernel to get multiple scales of features, which achieved 93.7% F1-score and 92.1% accuracy on the test set.

## introduction humor detection is an interesting but difficult task in natural language processing (nlp) and requires various techniques to understand the meaning of a sentence and identify humor. for example, humour by sarcasm can mean that a piece of text can have two very different meanings and the nlp algorithm needs to be able to understand which meaning is intended. the aim of task 7-1a of semeval 2021 @xcite was to address the challenge of classifying humour in text. provided for this task was a dataset constructed of short phrases in english along with a label classifying whether or not each phrase is intended to be humorous. the labels have been obtained by surveying a group of people that represent a variety of genders, political stances and income levels. the given dataset includes training set with 8000 labeled sentences, a development set of 1000 sentences and a test set of 1000 sentences. the dataset was made up of english phrases that were labeled by their intent to be humorous. this means the label annotators were not saying whether or not they found the text funny but whether the writer of the text intended it to be funny. the texts were predominantly one sentence long with a small proportion being two or three sentences. each phrase was labeled for humour intent and, if humour was intended, then a rating was given for how funny it is. also of the texts intended to be humorous, a label was given for whether it is offensive, and if so, how offensive it is. the texts covered a range of types of jokes such as puns, sarcasm, dark jokes and "dad" jokes. one example is "i never finish anything. i have a black belt in partial arts." which contains a pun and is labeled as humorous. our best approach is distilbert+multiscale cnn. it introduced a pre-trained distilbert model to extract textual features, and created a multi-scale cnn model for humour classification. first, the distilbert tokenizer generated a word token vector and an attention mask vector for each sentence. then, we fed these vectors into a pretrained distilbert model to get hidden features. after that, five cnn layers with different kernel sizes were used to get features of different scales. each feature vector was subsequently concatenated together. finally, the fused features were fed into dense layers in order to classify text into humorous or not humorous classes. it achieved 93.66% of f1 score and 92.10% of accuracy in using test set. ## related work we used four shallow models as a benchmark for our main approach. the first method was k-nearest neightbors (knn) @xcite in which an unlabeled query point is given the label of the majority of the k neighboring points. the second method is naive bayes (sammut c., 2011a) which utilizes bayes rule together with a strong assumption that the attributes are conditionally independent. the third is a random forest (sammut c., 2011b) which is an ensemble of decision trees trained on a bootstrap sample from the original dataset. the fourth method is the support vector machine (svm) (burges, 1998) which transforms the input data to a higher dimensional space and seeks to define a hyper-plane separating the classes. we also implemented voting to combine these methods where the prediction of the voting is simply the majority label predicted by the shallow models. we also tried deep models. long short-term memory (lstm) is a very effective recurrent neural networks for processing sequence data, which considers long and short term memory over time @xcite . convolutional neural network (cnn) uses a convolution kernel to extract hidden features from input, which is widely used in the field of computer vision @xcite . 1-dimension cnn layer can fit with sequence data, so it can be used to extract textual features. transfer learning methods are widely used in the field of nlp. pre-trained word embeddings are usually trained on unlabelled dataset and maps each word token into a fixed vector representing the meaning of the word in a hidden space. for example, global vectors for word representation (glove) is trained on a 6 billion word corpus using an unsupervised learning method in order to find word co-occurrence @xcite . unlike conventional embeddings, contextualized embeddings dynamically map a word token into vectors based on the context using a pre-trained encoder. by extracting the features and adapting new data to the model, we can implement general down-stream tasks based on the pre-trained model. bert uses a deep transformer as its encoder, and trains on language modelling tasks and next sentence prediction, which is often used in nlp with excellent performance @xcite . distilbert uses knowledge distillation method to compress the model, which retains 97% of the performance of original bert but is 60% faster @xcite . ## system overview in our bert-based models, we used distilbert which is a light version of bert to extract textual features. the bert model is a transformer-based model, which is pretrained on vast amounts of textual data in language modelling tasks @xcite . therefore, the weights in the bert model, which contains semantic information, can be used as contextualized embedding for general purposes. it can better represent textual data than a random tokenizer. due to hardware limits, we only used the distilbert uncased base model in our system. it retains 97% performance of the original bert but is 60% faster @xcite . for comparison, we also used glove pretrained embedding to represent text and created a 2-layer lstm model as our baseline. shallow machine learning models were also explored for comparison. figure 1 shows the pipeline of our experimental process. regarding feature extraction, we first used the distilbert tokenizer to vectorize the textual data then padded them into the same size. an attention mask was built to use binary vectors to differentiate padded zeros and word tokens. the vectors and mask were fed into the bert model, and we stored the output of the last layer as the representation of text. because the task was a text classification problem, we only used the "[cls]" value in the text representation for logistic regression (lr) and lstm model, which was the first vector of each row. the length of each text representation was 768. for example, the sentence "told my mom i hit 1200 twitter..." was firstly tokenized into vectors " @xcite ..]", and the [cls] vector of distilbert output of this sentence was "[6.7492e-02, -1.6599e-01, 1.0417e-01, ...]", which had length of 768. for the fully connected (fc) model and all cnn models, we used the full output of the distilbert model as features, which were in shape of 136 x 768. after extracting the features, we applied different models to predict the humor class. first, we implemented the lr model with default parameters. the package scikit-learn was used to build and train linear shallow models, such as lr, knn, naive bayes, random forest, and svm. and then, we created a lstm model. it consisted of two 32node lstm layers and a 32-node fully-connected layer. also, we built a fully connected network, which had a 128-node dense layer and 64-node dense layer. in addition, we tried a cnn model which contained 2 64-node cnn-1d layer and a 128-node dense layer. for each network, we took the extracted features as input, and used a 1-node dense layer with sigmoid activation to collect output. in addition, dropout layers were used in each model to handle over-fitting problems. for comparison, we also implemented a glove based lstm. nltk module was used to tokenize sentences and remove stop words and special characters. glove @xcite is pretrained word vectors representation, which was used as the initial weights of the embedding layer. the glove+lstm model used 50-dim embedding which connected to two 32-node lstm layers. the output of lstm was flattened and then fed into 16node fully connected layer. and a 1-node dense layer with sigmoid activation was used to output probability of humorous class. it used the same model compiling parameters as the distilbertbased models. moreover, previous research proved that multiple scale of cnn layers can capture hidden features in different granularity, which improves performance @xcite . therefore, we build two multi-scale cnn models. the first model is distilbert+multiscale cnn, figure 2 shows its structure. it used five 64-node cnn-1d layers with different kernel size of [1, 2, 3, 4, 5]. these 5 layers could extract hidden information from the features in various granularity. each cnn layer was then connected to a globalmaxpool1d layer to get a down-sampled representation in the shape of (batch size, 64). also, dropout layers were applied on each output, and the 5 outputs were concatenated to a single vector in the shape of (batch size, 320). subsequently, the combined vector was fed into a 512-node fully-connected layer. rectify linear unit (relu) activation functions were applied to all cnn and fully-connected layers. finally, a 1-node fully-connected layer with sigmoid activation function was added at the end of the network to collect the output value. we used 0.5 as a threshold to categorise the probability value into 0 (not humorous) and 1 (humorous) categories. in addition, we used "rmsprop" optimizer and binary cross entropy loss for stochastic gradient descent optimization. in addition, we tried other multi-scale strategies, inspired by the deep multi-scale fusion hashing model @xcite . to differentiate two multiscale models, we called this one multipool cnn. it used 5 different kernel size of maxpooling layers to scale input features into different resolution. for each maxpooling layer we set strides to 1 and used "valid" padding methods, and it connected to a 64-node cnn-1d layer with a kernel size of 1 and relu activation. therefore, 5 cnn layers can extract features from different resolution of input. similar to the multi-scale cnn, each output subsequently connected to a globalmaxpool1d layer and dropout layer. finally, 5 different outputs were concatenated together and fed into a 512-node fully-connected layer. the output layer and compiling method are the same as the multi-scale cnn model. ## experimental setup we implemented our experiments on the university's virtual machine, which has a python environment with a shared 16 gb tesla v100 gpu. tensorflow 2.0 and keras were used to build neural networks. python "transformers" module was used to import the distilbert model. "pandas" and "numpy" modules were utilised to manipulate data, and we used "scikit-learn" to import basic machine learning models and evaluation metrics. moreover, we used the training set to train our models and took 20% split of training set as validation set to tune hyper-parameter in development stage. the dev set was used to assess performance of models in evaluation stage, and we applied our models on test set in order to submit results. in data preprocessing, we load the data into a pandas data frame, and tried to remove all the tags and special characters in the text. after that, we used the dis-tilbert tokenizer to vectorise and encode the text into the format that the distilbert model required. in all the models, we set 1 and 2 as random seeds for numpy and tensorflow respectively. also, we used early stopping strategy when training models. all distilbert based nerual network models were stopped at 23 epochs. also, the batch size was set to 64. regarding evaluation, we evaluated our model on dev set and test set (gold data). f1 score and accuracy were used to evaluate performance. the accuracy score shows the ratio of correct prediction. since the f1 score considers both precision and recall, which would be more informative metric, we selected our model based on the f1 score. ## results we found that the features extracted by distilbert boosts the model performance significantly and it is difficult to generate a good results without using a pre-trained embedding or model. due to hardware limits, we only tried the light version of bert, and only implemented shallow layer models. our official submission used dis-tilbert+fc model, which achieved 92.41% f1score and ranked 47th in task 7-1a. we modified our model in post-evaluation stage, and our best model is distilbert+multi-scale cnn, which had 93.66% of f1 score and 92.10% of accuracy in using test set. the given official baseline is 85.7% of f1 score and 88.4% of accuracy. all of our dis-tilbert based models and one glove embedding based model had better performance than the baseline. detailed evaluation scores on dev set and test set were included in in comparison, the svm model achieved an f1score of 85.04% and an accuracy 81.70% on the test set, which is our best shallow model. however, all of these model generated scores lower than the given baseline. also, our glove-based lstm model achieved 88.5% f1-score, and distilbert-based lstm achieved 89.6%. it makes sense that transformerbased pretrained representation is better than traditional pretrained embedding. due to efficiency considerations, we only used 32 nodes in the lstm layer. so, these two models have poorer performance than other models using more nodes. even the distilbert+lr model has higher f1 score (90.4%) and accuracy (88.0%). surprisingly, a not humorous humorous not humorous 337 48 humorous 31 584 however, our best model still made mistakes on predicting humorous classes of a few sentences. an error analysis is helpful to understand the wrong predictions. in the prediction results of the dis-tilbert+multiscale cnn model, 79 out of 1000 sentences are incorrectly predicted. the ## conclusions to conclude, we explored various methods to build humour detection classifiers for task 7-1a. models like logistic regression, lstm, fc, cnn were used, and pre-trained models like distilbert were introduced to generate an accurate vector representation for textual data. our best model is the dis-tilbert+multiscale cnn, which achieved 93.7% f1-score and 92.1% accuracy on the test set. we focused on applying multi-scale strategy on modelling, and compared different models. and our results shows that cnn are more suitable for this task than lstm fc and other shallow models. also, we found that pre-trained embeddings, weights or representations are crucial for our model performance. we only explored multi-scale from the wide dimension, this strategy can also be used in deep dimension. in the future, a deeper network with more nodes can be explored and the full version of bert model can be exploited.