avatarPreston Lim

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6009

Abstract

s if you want to learn more about BERT.</p><div id="cede" class="link-block"> <a href="https://towardsdatascience.com/bert-explained-state-of-the-art-language-model-for-nlp-f8b21a9b6270"> <div> <div> <h2>BERT Explained: State of the art language model for NLP</h2> <div><h3>An approachable and understandable explanation of BERT, a recent paper by Google that achieved SOTA results in wide…</h3></div> <div><p>towardsdatascience.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*ViwaI3Vvbnd-CJSQ.png)"></div> </div> </div> </a> </div><h2 id="a07f">Fine-tuning BERT for sentiment analysis</h2><p id="13f9">I fine-tuned BERT, specifically, <i>BertForSequenceClassification</i> using the<code>bert-base-uncased</code> model from the <a href="https://huggingface.co/transformers/">huggingface transformers library</a>.</p><p id="b1e0">The fine-tuning process was conducted on the <a href="https://www.kaggle.com/crowdflower/twitter-airline-sentiment">Twitter airline sentiment dataset</a> — my training code can be found in this repo:<b> <a href="https://github.com/prestonlimlianjie/bert-sentiment-analysis-straits-times">bert-sentiment-analysis-straits-times</a></b></p><figure id="b888"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*qsaUKnnr8Lb8GJBf0kKDKA.png"><figcaption>Sample records from the Twitter airline sentiment dataset</figcaption></figure><p id="94c8">The dataset contains <b>14,640</b> tweets that were manually classified as negative, positive, or neutral. I created a training set with 80% of the dataset and kept the other 20% for validation.</p><p id="c0c2">I ran the training on a Google Colab notebook with a GPU and obtained the following validation results. The entire training process took approximately 20 minutes to complete.</p><figure id="e1f9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1sd4Ayp_joMWrKLEfx94Kg.png"><figcaption>Screenshot of validation results</figcaption></figure><p id="7fc6">The results show that the model has an <b>83% </b>accuracy — that is, it is able to accurately predict the correct sentiment given an input sentence 83% of the time.</p><p id="e5a1">It also appears that the model is better at predicting comments that are <b>positive</b> than <b>negative </b>or<b> neutral.</b></p><p id="a8c9">Great, now that we have a decent model that can predict sentiments, let’s test it out on a bunch of comments made by Singaporeans!</p><p id="5c1a">A random Sesame Street aside. I’ve been obsessively binging the incredible podcast <i>The Weirdest Thing I Learned This Week</i> and discovered that according to folklore, vampires are obsessive counters.</p><p id="5fe0">That is, they feel compelled to count every single object around them — which is why people threw poppy seeds on the burial grounds of suspected vampires so that they would be so preoccupied with counting them that they wouldn’t attack humans. So perhaps Sesame Street was actually on point with the characterization of Count Dracula. Anyway, on to the results.</p><h1 id="e805">Results: Sentiment analysis of comments on The Straits Times articles</h1><p id="168c">The Straits Times is my favorite national newspaper. And judging from how fervently Singaporeans from all walks of life comment on the news articles, I’m sure it is also the beloved source of <i>#realnews </i>for many Singaporeans.</p><p id="d56e">And so, to answer the two questions I posed earlier about (i) exactly how negative are Singaporeans, and (ii) do we dislike all topics equally, I collected a bunch of Facebook comments from <b>5 randomly selected articles </b>on The Straits Times to run the analysis on.</p><p id="cec8">The 5 articles I chose to study were:</p><ul><li><i>Husband runs off when wife is pregnant and defaults on paying maintenance</i></li><li><i>China launches gigantic telescope in hunt for life beyond earth</i></li><li><i>Tsai Ing-wen re-elected Taiwan President; KMT’s Han Kuo-yu concedes defeat</i></li><li><i>Forum: Promote plant-based diet to cut Singaporean’s carbon footprint</i></li><li><i>Menacing mynas, pigeons and crows: Complaints about nuisance birds rising</i></li></ul><p id="6262">There were <b>257 comments </b>in total across the 5 articles.</p><h2 id="4dd6">How good is BERT at analyzing the sentiments of Straits Times comments?</h2><p id="641d">In the previous step, we fine-tuned the BERT model on the Twitter airline sentiments dataset, and verified that it works well. Typically, a good computer scientist would verify that this model also works well on a test dataset of interest — in our case the Straits Times comments.</p><p id="8e44">What this means is that we should collect a bunch of comments from The Straits Times’ posts and manually label them as <b>positive</b>, <b>negative</b>, or <b>neutral</b>. However, I’m doing this in my spare time on a Sunday, so ain’t nobody got time for that.</p><figure id="4c6c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*YxkoF2R0W92uLdgh.jpeg"><figcaption></figcaption></figure><p id="d199">Instead, I’m going to take the short-cut of using the model to predict the sentiments in The Straits Times’ comments and then looking through the predictions to see if they make sense.</p><figure id="f598"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Oox8o5-2iyavpniuIliu0g.png"><figcaption></figcaption></figure><p id="5303">Let’s take a look at the comments on any article — for example this one: <b>Forum: Promote plant-based diet to cut Singaporeans’ carbon footprint</b>. The article is an opinion letter that asks the government to intervene or “nudge” Singaporeans towards plant-based diets.</p><h2 id="67ef">Correctly classified comments</h2><p id="9051">The model does a pretty good job identifying the correc

Options

t classes of many comments, despite some comments having spelling and grammar issues.</p><figure id="0be7"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*p89A5lkHHrfx3tuqqCpGmg.png"><figcaption></figcaption></figure><figure id="9b60"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5WJKG9zC9XI655CAdbRreQ.png"><figcaption>Comments from articles on The Straits Times — the sentiment for each comment as predicted by the model is displayed on the right.</figcaption></figure><h2 id="3b2c">Misclassified comments</h2><p id="ee1d">However, there were a few comments that were misclassified.</p><p id="761e">For example the first comment below was classified as <b>neutral</b>, when the user actually had a negative sentiment in the response. The way I interpreted the comment was this: the user demands for political leaders and news organizations to first adopt a vegan diet before shoving veganism down his/her throat. However, this required some <b>inference</b>, which existing NLP models like BERT aren’t very well-versed at doing.</p><p id="cd3d">Additionally, in the second comment below, the user was being <b>sarcastic</b> — and BERT, like most state-of-the-art NLP models, struggle to understand sarcasm.</p><figure id="2567"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*fTwQ1oONsxR4QpN-BRocQg.png"><figcaption></figcaption></figure><h2 id="96ae">Special shoutout to these comments that are uninterpretable even to humans</h2><figure id="ee53"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*0z80q1T9oQe4Iq3stVy0dg.png"><figcaption>WHAT IS HAPPENING?? I am surprised that BERT was even able to give these comments a label.</figcaption></figure><p id="5664">Cool, so BERT works pretty well at classifying The Straits Times’ comments! Let’s use it to study all the comments on the 5 articles that we’ve selected above.</p><h2 id="e550">Good news! Not all of our comments are negative!</h2><p id="de30">Maybe Singaporeans aren’t negative Nancies after all. As it turns out, only <b>52.9%</b> of our comments were negative.</p><figure id="0e44"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Hdf_XeFzmDhl-Fde698_EA.png"><figcaption>Prediction results on the Straits Times comments</figcaption></figure><p id="f654">And if I were to be optimistic about the situation, what this means is at <b>48.1% </b>of the comments were positive or neutral!</p><h2 id="776a">Which articles get Singaporeans the most riled up?</h2><figure id="5d6e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Akch1XCk4PoimLCiSdO0kQ.png"><figcaption></figcaption></figure><p id="f83a"><i>Husband runs off when wife is pregnant and defaults on paying maintenance” </i>made Singaporeans most upset — with <b>77.8%</b> negative and <b>2.8% </b>positive comments.</p><p id="6e4c">On the other hand, “<i>Tsai Ing-wen re-elected Taiwan President; KMT’s Han Kuo-yu concedes defeat</i>” made Singaporeans the most happy — with only <b>45.3% </b>negative and <b>34.8%</b> positive comments!</p><h2 id="8c11">My feelings/sentiments on this entire process</h2><p id="f699">BERT is a pretty powerful language model — but while the field of NLP has made significant advancements in recent years, it still has a long way to go before machines can fully comprehend humans.</p><p id="55d0">But then again, I too find it hard to understand humans sometimes.</p><figure id="ea34"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*SDBNIpiyy59OjSUW"><figcaption></figcaption></figure><figure id="d37e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*kcSpNPWXYYv2xu9n"><figcaption>Taken from the meta-group The Straits Times Comment Section — a Facebook group dedicated to studying why people comment the way they do on The Straits Times and other news platforms</figcaption></figure><p id="216b"><i>The views in this article are my own and do not reflect the views of the organizations that I am a part of.</i></p><p id="f353">I hope you enjoyed reading this article. If you would like to read some of my other fun and frivolous™ projects, check these out:</p><div id="0c56" class="link-block"> <a href="https://towardsdatascience.com/i-wrote-a-program-that-speaks-like-the-collective-hive-mind-of-the-straits-times-forum-41c96e7d8d71"> <div> <div> <h2>I wrote a program that speaks like the collective hive-mind of The Straits Times Forum</h2> <div><h3>The forum section of The Straits Times — one of the major news papers in Singapore — is a national treasure. The forum…</h3></div> <div><p>towardsdatascience.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*akflhL6zx6OHAcSg)"></div> </div> </div> </a> </div><div id="f1ee" class="link-block"> <a href="https://blog.usejournal.com/i-made-a-machine-learning-chicken-rice-classifier-in-4-hours-to-tell-me-what-type-of-chicken-rice-e9b1af4aa069"> <div> <div> <h2>I made a machine learning chicken rice classifier in ~4 hours to tell me what type of chicken rice…</h2> <div><h3>This entire frivolous episode started when my colleague got chicken rice for lunch. The lunchtime conversation then…</h3></div> <div><p>blog.usejournal.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*0rMCL6BIBhX8_9kR1fOfng.png)"></div> </div> </div> </a> </div><h2 id="9bae">Acknowledgements</h2><p id="b9ce">Special thanks to <i>sebsk</i> for open-sourcing their <a href="https://github.com/sebsk/CS224N-Project">CS224N project</a> from which I referenced much of the BERT fine-tuning process.</p></article></body>

Are Singaporeans negative Nancies? A sentiment analysis of social media comments using BERT

Complaining is an undisputed national past time for many Singaporeans. But exactly how negative are we? And do we loathe all topic matters equally?

An excerpt written by the founder of the World Toilet Organization. Published in my favorite national newspaper, The Straits Times.

Wow, I get that this was maybe written from the perspective of the World Toilet Organization but what a load of — oh crap, am I perhaps also complicit in this complaining culture?

In order to prevent my innately-Singaporean negativity from clouding my judgement, I guess I’ll have to use some good old data science to conduct this study.

In the next section, I’ll be explaining the deep learning technique used to perform sentiment analysis — skip ahead to the end if you want to find out what the results are.

An assortment of negative comments from posts on the Straits Times Facebook group

What is sentiment analysis?

Sentiment analysis is a technique to study the opinions or emotions expressed in a given text.

Many companies use sentiment analysis to track how their brands are being perceived by people through online platforms like the app store or Amazon reviews.

For our use case, we want to use sentiment analysis to measure how Singaporeans perceive specific news articles.

Computers struggle at sentiment analysis

Sentiment analysis is difficult for machines because human languages are highly complex. And so machines find it really hard to understand if the emotion behind a sentence is positive, negative, or neutral.

For example, the English language has many words that while seemingly identical, can have drastically different meanings when used in different contexts.

Take the use of the word wound in the following sentences:

John wound the clock. The arrow inflicted a wound onto her arm.

Traditional NLP techniques like static word embeddings are only able to ascribe one single meaning to each word. So in traditional techniques, the word wound would have the same meaning to a computer regardless of the way it was being used in a sentence.

But thankfully, the field of NLP has started seeing more advanced language modeling techniques such as BERT, which we will use in this study.

BERT from Sesame Street saves the day

BERT, or Bidirectional Encoder Representations with Transformers, is a state-of-the-art natural language understanding model. When Google released BERT in 2018, it broke all existing records for gold-standard natural language understanding tasks.

The reason BERT outperforms previous NLP techniques at understanding language is because it tries to learn the context in which a word is used.

BERT was trained by studying billions of sentences from Wikipedia and other text sources, and then made to predict a bunch of words that was hidden from these sentences. In doing so, BERT gets a better understanding of the context in which words appear compared to traditional static word embedding techniques!

Sorry did I sound too excited? What I really meant was, oh BERT’s just alright. Like, what a dumb name. I mean they only named it BERT because its predecessor was named ELMO.

What BERT looks like to Sesame street fans (left). What BERT looks like to Computer Scientists (right).

For our specific sentiment analysis use case, we fine-tune a BERT model to perform a classification step of 3 possible classes: positive, negative, and neutral. What this means is that we need to take the pre-trained BERT model from Google and teach it how to analyze sentiments from sentences.

In short, the model needs to do the following steps:

  • take in a sentence — in our case an English sentence — and produce a contextual word embeddings representation for the sentence (specifically for the [CLS] token)
  • pass the [CLS] contextual word embedding vector into a 3-dimension output vector (each dimension corresponds to the 3 classes)
  • perform a softmax on the 3-dimension output vector to determine whether the sentiment is positive, negative, or neutral.

What this means is that BERT takes a sentence and converts it to a mathematical representation that machines can understand and work with.

Here’s one way to think about it

BERT is an English-as-a-second-language student trying to understand English. He could read a sentence in English but it would be way easier if he translates it into a language that BERT is fluent in — let’s say, BERT-lish.

So BERT does just that — he translates the input sentence to BERT-lish. And finally, BERT determines if the BERT-lish sentence is positive, negative, or neutral.

This is of course a gross simplification of the BERT model, but hey, I don’t want to get more complaints about how long the article would have been if I tried to fully explain it. Here are some good resources if you want to learn more about BERT.

Fine-tuning BERT for sentiment analysis

I fine-tuned BERT, specifically, BertForSequenceClassification using thebert-base-uncased model from the huggingface transformers library.

The fine-tuning process was conducted on the Twitter airline sentiment dataset — my training code can be found in this repo: bert-sentiment-analysis-straits-times

Sample records from the Twitter airline sentiment dataset

The dataset contains 14,640 tweets that were manually classified as negative, positive, or neutral. I created a training set with 80% of the dataset and kept the other 20% for validation.

I ran the training on a Google Colab notebook with a GPU and obtained the following validation results. The entire training process took approximately 20 minutes to complete.

Screenshot of validation results

The results show that the model has an 83% accuracy — that is, it is able to accurately predict the correct sentiment given an input sentence 83% of the time.

It also appears that the model is better at predicting comments that are positive than negative or neutral.

Great, now that we have a decent model that can predict sentiments, let’s test it out on a bunch of comments made by Singaporeans!

A random Sesame Street aside. I’ve been obsessively binging the incredible podcast The Weirdest Thing I Learned This Week and discovered that according to folklore, vampires are obsessive counters.

That is, they feel compelled to count every single object around them — which is why people threw poppy seeds on the burial grounds of suspected vampires so that they would be so preoccupied with counting them that they wouldn’t attack humans. So perhaps Sesame Street was actually on point with the characterization of Count Dracula. Anyway, on to the results.

Results: Sentiment analysis of comments on The Straits Times articles

The Straits Times is my favorite national newspaper. And judging from how fervently Singaporeans from all walks of life comment on the news articles, I’m sure it is also the beloved source of #realnews for many Singaporeans.

And so, to answer the two questions I posed earlier about (i) exactly how negative are Singaporeans, and (ii) do we dislike all topics equally, I collected a bunch of Facebook comments from 5 randomly selected articles on The Straits Times to run the analysis on.

The 5 articles I chose to study were:

  • Husband runs off when wife is pregnant and defaults on paying maintenance
  • China launches gigantic telescope in hunt for life beyond earth
  • Tsai Ing-wen re-elected Taiwan President; KMT’s Han Kuo-yu concedes defeat
  • Forum: Promote plant-based diet to cut Singaporean’s carbon footprint
  • Menacing mynas, pigeons and crows: Complaints about nuisance birds rising

There were 257 comments in total across the 5 articles.

How good is BERT at analyzing the sentiments of Straits Times comments?

In the previous step, we fine-tuned the BERT model on the Twitter airline sentiments dataset, and verified that it works well. Typically, a good computer scientist would verify that this model also works well on a test dataset of interest — in our case the Straits Times comments.

What this means is that we should collect a bunch of comments from The Straits Times’ posts and manually label them as positive, negative, or neutral. However, I’m doing this in my spare time on a Sunday, so ain’t nobody got time for that.

Instead, I’m going to take the short-cut of using the model to predict the sentiments in The Straits Times’ comments and then looking through the predictions to see if they make sense.

Let’s take a look at the comments on any article — for example this one: Forum: Promote plant-based diet to cut Singaporeans’ carbon footprint. The article is an opinion letter that asks the government to intervene or “nudge” Singaporeans towards plant-based diets.

Correctly classified comments

The model does a pretty good job identifying the correct classes of many comments, despite some comments having spelling and grammar issues.

Comments from articles on The Straits Times — the sentiment for each comment as predicted by the model is displayed on the right.

Misclassified comments

However, there were a few comments that were misclassified.

For example the first comment below was classified as neutral, when the user actually had a negative sentiment in the response. The way I interpreted the comment was this: the user demands for political leaders and news organizations to first adopt a vegan diet before shoving veganism down his/her throat. However, this required some inference, which existing NLP models like BERT aren’t very well-versed at doing.

Additionally, in the second comment below, the user was being sarcastic — and BERT, like most state-of-the-art NLP models, struggle to understand sarcasm.

Special shoutout to these comments that are uninterpretable even to humans

WHAT IS HAPPENING?? I am surprised that BERT was even able to give these comments a label.

Cool, so BERT works pretty well at classifying The Straits Times’ comments! Let’s use it to study all the comments on the 5 articles that we’ve selected above.

Good news! Not all of our comments are negative!

Maybe Singaporeans aren’t negative Nancies after all. As it turns out, only 52.9% of our comments were negative.

Prediction results on the Straits Times comments

And if I were to be optimistic about the situation, what this means is at 48.1% of the comments were positive or neutral!

Which articles get Singaporeans the most riled up?

Husband runs off when wife is pregnant and defaults on paying maintenance” made Singaporeans most upset — with 77.8% negative and 2.8% positive comments.

On the other hand, “Tsai Ing-wen re-elected Taiwan President; KMT’s Han Kuo-yu concedes defeat” made Singaporeans the most happy — with only 45.3% negative and 34.8% positive comments!

My feelings/sentiments on this entire process

BERT is a pretty powerful language model — but while the field of NLP has made significant advancements in recent years, it still has a long way to go before machines can fully comprehend humans.

But then again, I too find it hard to understand humans sometimes.

Taken from the meta-group The Straits Times Comment Section — a Facebook group dedicated to studying why people comment the way they do on The Straits Times and other news platforms

The views in this article are my own and do not reflect the views of the organizations that I am a part of.

I hope you enjoyed reading this article. If you would like to read some of my other fun and frivolous™ projects, check these out:

Acknowledgements

Special thanks to sebsk for open-sourcing their CS224N project from which I referenced much of the BERT fine-tuning process.

Machine Learning
Bert
Deep Learning
NLP
Social Media
Recommended from ReadMedium