Site icon Towards AI

How Do Language Models Predict the NextĀ Word?šŸ¤”

How do language models predict the next word?

Author(s): Bala Priya CĀ 

N-gram language models ā€“ an introduction

Photo by Mick Haupt onĀ Unsplash

Have you ever guessed what the next sentence in the paragraph youā€™re reading would likely talk about?

Have you ever noticed that while reading, you almost always know the next word in the sentence?

Well, the answer to these questions is definitely Yes! As humans, weā€™re bestowed with the ability to read, understand languages and interpret contexts, and can almost always predict the next word in a text, based on what weā€™ve read so far.

Can we make a machine learning model do the same?

Oh yeah! We very well can!

And we already use such models everyday, here are some cool examples.

Autocomplete feature in Google Search (Image formatted byĀ author)
Autocomplete feature in messaging apps (Image formatted byĀ author)

In the context of Natural Language Processing, the task of predicting what word comes next is called Language Modeling.

Letā€™s take a simple example,

The students opened theirĀ _______.

What are the possible words that we can fill the blank with?

BooksšŸ“— šŸ“’šŸ“š

NotesšŸ“–

LaptopsšŸ‘©šŸ½ā€šŸ’»

MindsšŸ’”šŸ™‚

ExamsšŸ“‘ā”

Well, the list goesĀ on.šŸ˜Š

Waitā€¦why did we think of these words as the best choices, rather than ā€˜opened their Doors or Windowsā€™? šŸ™„

Itā€™s because we had the word students, and given the context ā€˜studentsā€™, the words such as books, notes and laptops seem more likely and therefore have a higher probability of occurrence than the words doors and windows.

Typically, this probability is what a language model aims at computing. Over the next few minutes, weā€™ll see the notion of n-grams, a very effective and popular traditional NLP technique, widely used before deep learning models became popular.

What does a language modelĀ do?

Describing in formal terms,

Probability distribution of the next word x(t+1) given x(1)ā€¦x(t) (ImageĀ Source)

A language model, thus, assigns a probability to a piece of text. The probability can be expressed using the chain rule as the product of the following probabilities.

The probability of the text according to the language model is:

Chain rule for the probability of a piece of text (ImageĀ Source)

How do we learn a languageĀ model?

Learn n-grams!Ā šŸ˜Š

An n-gram is a chunk of n consecutive words.

For our example, The students opened their _______, the following are the n-grams for n=1,2,3 and 4

In an n-gram language model, we make an assumption that the word x(t+1) depends only on the previous (n-1) words. The idea is to collect how frequently the n-grams occur in our corpus and use it to predict the next word.

Dependence on previous (n-1) words (ImageĀ Source)

This equation, on applying the definition of conditional probability yields,

Probabilities of n-grams and (n-1) grams (ImageĀ Source)

How do we compute these probabilities?

To compute the probabilities of these n-grams and n-1 grams, we just go ahead and start counting them in a large text corpus! The Probability of n-gram/Probability of (n-1) gram is given by:

Count occurrences of n-grams (ImageĀ Source)

Letā€™s learn a 4-gram language model for the example,

As the proctor started the clock, the students opened theirĀ _____

4-gram language model (ImageĀ Source)

In learning a 4-gram language model, the next word (the word that fills up the blank) depends only on the previous 3 words. If w is the word that goes into the blank, then we compute the conditional probability of the word w as follows:

Counting number of occurrences (ImageĀ Source)

In the above example, let us say we have the following:

"students opened their" occurred 1000 times
"students opened their books" occurred 400 times
-> P(books/students opened their) = 0.4
"students opened their exams" occurred 200 times
-> P(exams/students opened their) = 0.2

The language model would predict the word books;

But given the context, is books really the right choice? Wouldnā€™t the word exams be a better fit?

Recall that we have,

As the proctor started the clock, the students opened their _____

Should we really have discarded the context ā€˜proctorā€™?šŸ¤”

Looks like we shouldnā€™t have.

This leads us to understand some of the problems associated with n-grams.

Disadvantages of the n-gram languageĀ model

Problems ofĀ Sparsity

What if ā€œstudents opened theirā€ never occurred in theĀ corpus?

The count term in the denominator would go toĀ zero!

What if ā€œstudents opened their wā€ never occurred in theĀ corpus?

The count term in the numerator would beĀ zero!

Sparsity problem increases with increasing n. In practice, n cannot be greater than 5

Problem ofĀ Storage

As we need to store count for all possible n-grams in the corpus, increasing n or increasing the size of the corpus, both tend to become storage-inefficient.

However, n-gram language models can also be used for text generation; a tutorial on generating text using such n-grams can be found in reference[2] given below.

In the next blog post, we shall see how Recurrent Neural Networks (RNNs) can be used to address some of the disadvantages of the n-gram language model.

Happy new year everyone! āœØ

Wishing all of you a great year ahead! šŸŽ‰šŸŽŠšŸ„³

References

[1] CS224n: Natural Language Processing with Deep Learning

[2] NLP for Hackers


How do language models predict the next word?šŸ¤” was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Exit mobile version