Language Models

Language models are mathematical structures that map a sequence of tokens to a probability. The probability assigned to a sequence of tokens depends on how often the tokens are observed together in order. This mapping between the sequence of tokens and their associated probability is derived from vast quantities of textual data using a suitable learning rule.

We define a sequence of tokens, , and the joint probability distribution that the language model learns from empirical data,

The language model is also a generative model, as it models the joint probability distribution of the given data. We can simplify the above distribution,

Consider the following example, where , the probability assigned by the language model to this sequence will be computed as,

As we observed, the probability associated with each token is dependent or conditioned on tokens that occur prior in the sequence. This gives us a hint, that language models are contextually rich models considering different parts of the sequence when assigning probability to a token.

N-grams Model

Instead of considering the entire previous sequence to compute the probability used in the language model above, we only consider the past tokens from the sequence.

With , we obtain the bigram model, where the probability of a token is dependent only on its previous token,

The assumption that the probability of a token depends only on the previous token in the sequence is called a Markov assumption.

Improving the N-grams Model: Using Log Probabilities

As observed in the expression of the language model, the desired probability is a product of many probabilities, which might be very small if the text corpus under consideration is very large. Multiplying many small numbers together makes the product smaller making it ‘underflow’ the computer’s memory. The product is so small that it is represented as a zero even though it is not.

Using logarithm solves the problem, converting a huge product expression to a summation over individual log probabilities,

Improving the N-grams models: Using Smoothing Techniques

Just as we avoided smaller products by introducing log probabilities, we also need to avoid zero-ing out the entire product if only one of the individual probabilities was zero. If the probability of a token is calculated by computing its relative frequency in the corpus,

where is the count of token in the corpus and is the total number of tokens in the corpus. We can add to the numerator and denominator, the process known as Laplace Smoothing,

Evaluating Language Models: Perplexity

To evaluate how accurately a language model performs on a test-dataset (unobserved data), we can compute the perplexity of the model. It is defined as,

Lower the perplexity of the test sequence, the better the model. Perplexity also represents the uncertainty in the value of a sample being derived from a given distribution. Thus, a lower perplexity suggests that the chances of a sequence being derived or sampled by the probability distribution constructed by the language model are high.


References