<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Language Models | Haobin Tan</title><link>https://haobin-tan.netlify.app/tags/language-models/</link><atom:link href="https://haobin-tan.netlify.app/tags/language-models/index.xml" rel="self" type="application/rss+xml"/><description>Language Models</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Mon, 03 Aug 2020 00:00:00 +0000</lastBuildDate><image><url>https://haobin-tan.netlify.app/media/icon_hu7d15bc7db65c8eaf7a4f66f5447d0b42_15095_512x512_fill_lanczos_center_3.png</url><title>Language Models</title><link>https://haobin-tan.netlify.app/tags/language-models/</link></image><item><title>Languages Modeling (N-Gram)</title><link>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/</link><pubDate>Sun, 02 Aug 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/</guid><description/></item><item><title>N Gram</title><link>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/n-gram/</link><pubDate>Sun, 02 Aug 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/n-gram/</guid><description>&lt;p>&lt;strong>Language models (LMs)&lt;/strong>: Model that assign probabilities to sequence of words&lt;/p>
&lt;p>&lt;strong>N-gram&lt;/strong>: a sequence of N words&lt;/p>
&lt;p>​ E.g.: &lt;em>Please turn your homework &amp;hellip;&lt;/em>&lt;/p>
&lt;ul>
&lt;li>&lt;strong>bigram (2-gram)&lt;/strong>: &lt;strong>two&lt;/strong>-word sequence of word
&lt;ul>
&lt;li>&lt;em>“please turn”&lt;/em>, &lt;em>“turn your”&lt;/em>, or &lt;em>”your homework”&lt;/em>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>trigram (3-gram)&lt;/strong>: &lt;strong>three&lt;/strong>-word sequence of word
&lt;ul>
&lt;li>&lt;em>“please turn your”&lt;/em>, or &lt;em>“turn your homework”&lt;/em>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="motivation">Motivation&lt;/h2>
&lt;p>$P(w|h)$: probability of a word $w$ given some history $h$.&lt;/p>
&lt;p>Our task is to compute $P(w|h)$.&lt;/p>
&lt;p>Consider a simple example:&lt;/p>
&lt;p>Suppose the history $h$ is “&lt;em>its water is so transparent that&lt;/em>” and we want to know the probability that the next word is &lt;em>the&lt;/em>: $P(\text {the} | \text {its water is so transparent that})$&lt;/p>
&lt;h2 id="naive-way">&lt;strong>Naive way&lt;/strong>&lt;/h2>
&lt;p>Use &lt;strong>relative frequency counts&lt;/strong> (“Out of the times we saw the history $h$, how many times was it followed by the word $w$”?)&lt;/p>
&lt;ul>
&lt;li>Take a very large corpus, count the number of times we see &lt;code>its water is so transparent that&lt;/code>, and count the number of times this is followed by &lt;code>the&lt;/code>.&lt;/li>
&lt;/ul>
$$
P(\text {the} | \text {its water is so transparent that})=
\frac{C(\text {its water is so transparent that the})}{C(\text {its water is so transparent that})}
$$
&lt;ul>
&lt;li>
&lt;p>With a large enough corpus, such as the web, we can compute these counts and estimate the probability&lt;/p>
&lt;ul>
&lt;li>Works fine in many cases&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>🔴 Problems&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Even the web isn’t big enough to give us good estimates in most cases.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>This is because language is creative;&lt;/p>
&lt;ul>
&lt;li>
&lt;p>new sentences are created all the time,&lt;/p>
&lt;/li>
&lt;li>
&lt;p>and we won’t always be able to count entire sentences.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Similarly, if we wanted to know the joint probability of an entire sequence of words like &lt;code>its water is so transparent&lt;/code>, we could do it by asking “out of all possible sequences of five words, how many of them are &lt;code>its water is so transparent&lt;/code>?”&lt;/p>
&lt;ul>
&lt;li>We have to get the count of &lt;code>its water is so transparent&lt;/code> and divide by the sum of the counts of all possible five word sequences. That seems rather a lot to estimate!&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="cleverer-way">Cleverer way&lt;/h2>
&lt;p>Notation:&lt;/p>
&lt;ul>
&lt;li>$P(X\_i=\text{''the''})$: probability of a particular random variable $X\_i$ taking on the value “the”
&lt;ul>
&lt;li>Simplification: $P(the)$&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>$w\_1\dots w\_n$ or $w\_1^n$: a sequence of $n$ words
&lt;ul>
&lt;li>$w\_1^{n-1}$: the string $w\_1, w\_2, \dots w\_{n-1}$&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>$P(w\_1, w\_2, \dots, w\_n)$: joint probability of each word in a sequence having a particular value $P(X\_1=w\_1, X\_2=w\_2, \dots, X\_n=w\_n)$&lt;/li>
&lt;/ul>
&lt;p>Compute $P(w\_1, w\_2, \dots, w\_n)$: Use the &lt;strong>chain rule of probability&lt;/strong>
&lt;/p>
$$
\begin{aligned}
P\left(X\_{1} \ldots X\_{n}\right) &amp;=P\left(X\_{1}\right) P\left(X\_{2} | X\_{1}\right) P\left(X\_{3} | X\_{1}^{2}\right) \ldots P\left(X\_{n} | X\_{1}^{n-1}\right) \\\\
&amp;=\prod\_{k=1}^{n} P\left(X\_{k} | X\_{1}^{k-1}\right)
\end{aligned}
$$
&lt;p>
Apply to words:
&lt;/p>
$$
\begin{aligned}
P\left(w\_{1}^{n}\right) &amp;=P\left(w\_{1}\right) P\left(w\_{2} | w\_{1}\right) P\left(w\_{3} | w\_{1}^{2}\right) \ldots P\left(w\_{n} | w\_{1}^{n-1}\right) \\\\
&amp;=\prod\_{k=1}^{n} P\left(w\_{k} | w\_{1}^{k-1}\right)
\end{aligned}
$$
&lt;p>
🔴 Problem: We don’t know any way to compute the exact probability of a word given a long sequence of preceding words $P(w\_n|w\_1^{n-1})$&lt;/p>
&lt;ul>
&lt;li>we can’t just estimate by counting the number of times every word occurs following every long string, because language is creative and any particular context might have never occurred before! 🤪&lt;/li>
&lt;/ul>
&lt;p>🔧 Solution: &lt;strong>n-gram model&lt;/strong>&lt;/p>
&lt;h3 id="n-gram-model">n-gram model&lt;/h3>
&lt;p>💡 Idea of n-gram model: instead of computing the probability of a word given its entire history, we can &lt;strong>approximate the history by just the last few words&lt;/strong>.&lt;/p>
&lt;p>E.g.: the &lt;strong>bigram&lt;/strong> model, approximates the probability of a word given all the previous words $P(w\_n|w\_1^{n-1})$ by using only the conditional probability of the PRECEDING word $P(w\_n|w\_{n-1})$:
&lt;/p>
$$
P\left(w\_{n} | w\_{1}^{n-1}\right) \approx P\left(w\_{n} | w\_{n-1}\right)
$$
&lt;ul>
&lt;li>E.g.: $P(\text { the } | \text { Walden Pond's water is so transparent that }) \approx P(\text{the}|\text{that})$&lt;/li>
&lt;/ul>
&lt;p>👆 The assumption that the probability of a word depends only on the previous word is called a &lt;strong>Markov assumption&lt;/strong>. Markov models are the class of probabilistic models that assume we can predict the probability of some future unit &lt;em>without&lt;/em> looking too far into the past.&lt;/p>
&lt;p>Generalize the bigram (which looks one word into the past) to the trigram (which looks two words into the past) and thus to the n-gram (which looks $n − 1$ words into the past):
&lt;/p>
$$
P\left(w\_{n} | w\_{1}^{n-1}\right) \approx P\left(w\_{n} | w\_{n-N+1}^{n-1}\right)
$$
&lt;h2 id="estimate-n-gram-probabilities">Estimate n-gram probabilities&lt;/h2>
&lt;p>Intuitive way: &lt;strong>Maximum Likelihood Estimation (MLE)&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>Get counts from a corpus&lt;/li>
&lt;li>Normalize the counts so that they lie between 0 and 1&lt;/li>
&lt;/ul>
&lt;h3 id="bigram">Bigram&lt;/h3>
&lt;p>Let&amp;rsquo;s start from bigram. To compute a particular bigram probability of a word $y$ given a previous word $x$, we&amp;rsquo;ll compute the count of the bigram $C(xy)$ and normalize by the sum of all the bigrams that share the same first word $x$
&lt;/p>
$$
P\left(w\_{n} | w\_{n-1}\right)=\frac{C\left(w\_{n-1} w\_{n}\right)}{\sum\_{w} C\left(w\_{n-1} w\right)}
$$
&lt;p>
We can simplify this equation, since the sum of all bigram counts that start with a given word $w\_{n-1}$ must be equal to the unigram count for that word $w\_{n-1}$
&lt;/p>
$$
P\left(w\_{n} | w\_{n-1}\right)=\frac{C\left(w\_{n-1} w\_{n}\right)}{C\left(w\_{n-1}\right)}
$$
&lt;p>&lt;strong>Example&lt;/strong>:&lt;/p>
&lt;p>Given a mini-corpus of three sentences&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">&amp;lt;s&amp;gt; I am Sam &amp;lt;/s&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&amp;lt;s&amp;gt; Sam I am &amp;lt;/s&amp;gt;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&amp;lt;s&amp;gt; I do not like green eggs and ham &amp;lt;/s&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>We need to augment each sentence with a special symbol &lt;code>&amp;lt;s&amp;gt;&lt;/code> at the beginning of the sentence, to give us the bigram context of the first word.&lt;/li>
&lt;/ul>
&lt;p>The calculations for some of the bigram probabilities from this corpus:
&lt;/p>
$$
\begin{array}{lll}
P(\mathrm{I} |&lt;\mathrm{s}>)=\frac{2}{3}=0.67 &amp; P(\mathrm{Sam} |&lt;\mathrm{s}>)=\frac{1}{3}=0.33 &amp; P(\mathrm{am} | \mathrm{I})=\frac{2}{3}=0.67 \\\\
P(&lt;/ \mathrm{s}>| \mathrm{Sam})=\frac{1}{2}=0.5 &amp; P(\mathrm{Sam} | \mathrm{am})=\frac{1}{2}=0.5 &amp; P(\mathrm{do} | \mathrm{I})=\frac{1}{3}=0.33
\end{array}
$$
&lt;h3 id="n-gram">N-gram&lt;/h3>
&lt;p>For the general case of MLE n-gram parameter estimation:&lt;/p>
$$
P\left(w\_{n} | w\_{n-N+1}^{n-1}\right)=\frac{C\left(w\_{n-N+1}^{n-1} w\_{n}\right)}{C\left(w\_{n-N+1}^{n-1}\right)}
$$
&lt;p>It estimates the n-gram probability by dividing the observed frequency of a particular sequence by the observed frequency of a prefix. This ratio is called a &lt;strong>relative frequency&lt;/strong>.&lt;/p>
&lt;p>&lt;strong>Example&lt;/strong>:&lt;/p>
&lt;p>Use data from the now-defunct Berkeley Restaurant Project.&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-02%2016.56.21.png" alt="截屏2020-06-02 16.56.21" style="zoom:80%;" />
&lt;p>👆 This figure shows the bigram counts from a piece of a bigram grammar.&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-02%2016.58.56.png" alt="截屏2020-06-02 16.58.56" style="zoom:80%;" />
&lt;p>👆 This figure shows the bigram probabilities after normalization (dividing each cell in figure above (Figure 3.1) by the appropriate unigram for its row, taken from the following set of unigram probabilities)&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-02%2016.59.42.png" alt="截屏2020-06-02 16.59.42" style="zoom:80%;" />
&lt;p>Other useful probabilities:
&lt;/p>
$$
\begin{array}{ll}
P(\mathrm{i} |&lt;\mathrm{s}>)=0.25 &amp; P(\text { english } | \text { want })=0.0011 \\\\
P(\text { food } | \text { english })=0.5 &amp; P(&lt;/ \mathrm{s}>| \text { food })=0.68
\end{array}
$$
&lt;p>
Now we can compute the probability of sentences like &lt;em>I want English food&lt;/em> by simply multiplying the appropriate bigram probabilities together:
&lt;/p>
$$
\begin{aligned}
&amp;P(\langle s\rangle\text { i want english food}\langle / s\rangle) \\\\
=\quad &amp; P(\mathrm{i} |&lt;\mathrm{s}>) \cdot P(\text { want } | \mathrm{i}) \cdot P(\text { english } | \text { want }) \cdot P(\text { food } | \text { english }) \cdot P(&lt;/ \mathrm{s}>| \text { food }) \\\\
=\quad &amp; .25 \times .33 \times .0011 \times 0.5 \times 0.68 \\\\
=\quad &amp; .000031
\end{aligned}
$$
&lt;h3 id="pratical-issues">Pratical issues&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>In practice it’s more common to use &lt;strong>trigram&lt;/strong> models, which condition on the previous two words rather than the previous word, or &lt;strong>4-gram&lt;/strong> or even &lt;strong>5-gram&lt;/strong> models, when there is sufficient training data.&lt;/p>
&lt;ul>
&lt;li>Note that for these larger n- grams, we’ll need to assume extra context for the contexts to the left and right of the sentence end. For example, to compute trigram probabilities at the very beginning of the sentence, we can use two pseudo-words for the first trigram (i.e., $P(I|\text{\&lt;s\>\&lt;s\>})$).&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>We always represent and compute language model probabilities in log format as &lt;strong>log probabilities&lt;/strong>.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Multiplying enough n-grams together would easily result in &lt;strong>numerical underflow&lt;/strong> 🤪 (Since probability $\in (0, 1)$)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Adding in log space is equivalent to multiplying in linear space, so we combine log probabilities by adding them.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul></description></item><item><title>Evaluating Language Models</title><link>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/evaluating-lm/</link><pubDate>Mon, 03 Aug 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/evaluating-lm/</guid><description>&lt;h2 id="extrinsic-evaluation">&lt;strong>Extrinsic evaluation&lt;/strong>&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>Best way to evaluate the performance of a language model&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Embed LM in an application and measure how much the application improves&lt;/p>
&lt;/li>
&lt;li>
&lt;p>For speech recognition, we can compare the performance of two language models by running the speech recognizer twice, once with each language model, and seeing which gives the more accurate transcription.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>🔴 &lt;span style="color:red">Problem: running big NLP systems end-to-end is often very expensive&lt;/span>&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h2 id="intrinsic-evaluation">&lt;strong>Intrinsic evaluation&lt;/strong>&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>measures the quality of a model independent of any application.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Can be used to quickly evaluate potential improvements in a language model&lt;/p>
&lt;/li>
&lt;li>
&lt;p>We need&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Training set (Training corpus)&lt;/strong>&lt;/li>
&lt;li>&lt;strong>Development test set (devset)&lt;/strong>
&lt;ul>
&lt;li>Also called &lt;strong>Validation set&lt;/strong> (see &lt;a href="https://en.wikipedia.org/wiki/Training,_validation,_and_test_sets">wiki&lt;/a>)&lt;/li>
&lt;li>Particular test set
&lt;ul>
&lt;li>Implicitly tune to its characteristics&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Test set (Test corpus)&lt;/strong>
&lt;ul>
&lt;li>NOT to let the test sentences into the training set!&lt;/li>
&lt;li>Truely UNSEEN!&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>In practice: divide data into 80% training, 10% development, and 10% test.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>How it works?&lt;/p>
&lt;ul>
&lt;li>Given a corpus of text and we want to compare two different n-gram models
&lt;ol>
&lt;li>we divide the data into training and test sets,&lt;/li>
&lt;li>train the parameters of both models on the training set, and&lt;/li>
&lt;li>then compare how well the two trained models fit the test set.
&lt;ul>
&lt;li>&amp;ldquo;Fit the test set&amp;rdquo; means: whichever model assigns a &lt;strong>higher probability&lt;/strong> to the test set—meaning it more accurately predicts the test set—is a &lt;strong>better&lt;/strong> model.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ol>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="perplexity">Perplexity&lt;/h2>
&lt;p>Instead of raw probability as our metric for evaluating language models, in practice we use &lt;strong>perplexity&lt;/strong>.&lt;/p>
&lt;p>The &lt;strong>perplexity&lt;/strong> (sometimes called &lt;em>&lt;strong>PP&lt;/strong>&lt;/em> for short) of a language model on a test set is the inverse probability of the test set, normalized by the number of words.&lt;/p>
&lt;p>For a test set $W=w_{1} w_{2} \ldots w_{N}$:
&lt;/p>
$$
\begin{array}{ll}
\operatorname{PP}(W) &amp;=P\left(w_{1} w_{2} \ldots w_{N}\right)^{-\frac{1}{N}} \\\\
&amp;=\sqrt[N]{\frac{1}{P\left(w_{1} w_{2} \ldots w_{N}\right)}} \\\\
&amp;\overset{\text{chain rule}}{=} \sqrt[N]{\displaystyle\prod_{i=1}^{N} \frac{1}{P\left(w_{i} | w_{1} \ldots w_{i-1}\right)}}
\end{array}
$$
&lt;p>
Thus, perplexity of &lt;em>W&lt;/em> with a bigram language model is
&lt;/p>
$$
\operatorname{PP}(W)=\sqrt[N]{\prod_{i=1}^{N} \frac{1}{P\left(w_{i} | w_{i-1}\right)}}
$$
&lt;p>
The higher the conditional probabil- ity of the word sequence, the lower the perplexity. Thus, minimizing perplexity is equivalent to maximizing the test set probability according to the language model.&lt;/p>
&lt;p>What we generally use for word sequence in perplexity computation is the ENTIRE sequence of words in test test. Since this sequence will cross many sentence boundaries, we need to include&lt;/p>
&lt;ul>
&lt;li>the begin- and end-sentence markers &lt;code>&amp;lt;s&amp;gt;&lt;/code> and &lt;code>&amp;lt;/s&amp;gt;&lt;/code> in the probability computation.&lt;/li>
&lt;li>the end-of-sentence marker &lt;code>&amp;lt;/s&amp;gt;&lt;/code> (but not the beginning-of-sentence marker &lt;code>&amp;lt;s&amp;gt;&lt;/code>) in the total count of word tokens &lt;em>N&lt;/em>.&lt;/li>
&lt;/ul>
&lt;h3 id="another-aspect">Another aspect&lt;/h3>
&lt;p>We can also think about perpleixty as the &lt;strong>weighted average branching factor&lt;/strong> of a language.&lt;/p>
&lt;ul>
&lt;li>branching factor of a language: the number of possible next words that can follow any word.&lt;/li>
&lt;/ul>
&lt;p>Example&lt;/p>
&lt;p>Consider the task of recognizing the digits in English (zero, one, two,&amp;hellip;, nine), given that (both in some training set and in some test set) each of the 10 digits occurs with equal probability $P=\frac{1}{10}$. The perplexity of this mini-language is in fact 10.
&lt;/p>
$$
\begin{aligned}
\operatorname{PP}(W) &amp;=P\left(w_{1} w_{2} \ldots w_{N}\right)^{-\frac{1}{N}} \\\\
&amp;=\left(\frac{1}{10}^{N}\right)^{-\frac{1}{N}} \\\\
&amp;=\frac{1}{10}^{-1} \\\\
&amp;=10
\end{aligned}
$$
&lt;p>
Now suppose that the number zero is really frequent and occurs far more often than other numbers.&lt;/p>
&lt;ul>
&lt;li>0 occur 91 times in the training set, and&lt;/li>
&lt;li>each of the other digits occurred 1 time each.&lt;/li>
&lt;/ul>
&lt;p>Now we see the following test set: &lt;code>0 0 0 0 0 3 0 0 0 0&lt;/code>. We should expect the perplexity of this test set to be lower since most of the time the next number will be zero, which is very predictable (i.e. has a high probability). Thus, although the branching factor is still 10, the perplexity or &lt;em>weighted&lt;/em> branching factor is smaller.&lt;/p></description></item><item><title>Generalization and Zeros</title><link>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/generalization-and-zeros/</link><pubDate>Mon, 03 Aug 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/generalization-and-zeros/</guid><description>&lt;p>The n-gram model is dependent on the training corpus (like many statistical models).&lt;/p>
&lt;p>Implication:&lt;/p>
&lt;ul>
&lt;li>The probabilities often encode specific facts about a given training corpus.&lt;/li>
&lt;li>n-grams do a better and better job of modeling the training corpus as we increase the value of $N$.&lt;/li>
&lt;/ul>
&lt;p>Notice when building n-gram models:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>use a training corpus that has a similar &lt;strong>genre&lt;/strong> to whatever task we are trying to accomplish.&lt;/p>
&lt;ul>
&lt;li>&lt;em>To build a language model for translating legal documents, we need a training corpus of legal documents.&lt;/em>&lt;/li>
&lt;li>&lt;em>To build a language model for a question-answering system, we need a training corpus of questions.&lt;/em>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Get training data in the appropriate dialect (especially when processing social media posts or spoken transcripts)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Handle &lt;strong>sparsity&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>When the corpus is limited, some perfectly acceptable English word sequences are bound to be missing from it.&lt;/p>
&lt;p>$\rightarrow$ &lt;span style="color:red">We’ll have many cases of putative “zero probability n-grams” that should really have some non-zero probability! &lt;/span>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Example:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Consider the words that follow the bigram &lt;em>denied the&lt;/em> in the WSJ Treebank3 corpus, together with their counts:&lt;/p>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%e6%88%aa%e5%b1%8f2020-06-03%2012.03.38-20200803105913667.png" alt="截屏2020-06-03 12.03.38" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;/li>
&lt;li>
&lt;p>But suppose our test set has phrases like:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">denied the offer
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">denied the loan
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Our model will incorrectly estimate that the $P(\text{offer}|\text{denied the})$ is 0! 🤪&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Zeros&lt;/strong>: things that don’t ever occur in the training set but do occur in the test set&lt;/p>
&lt;ul>
&lt;li>
&lt;p>🔴 Problems&lt;/p>
&lt;ul>
&lt;li>
&lt;p>We are &lt;strong>underestimating&lt;/strong> the probability of all sorts of words that might occur, which will hurt the performance of any application we want to run on this data.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>If the probability of any word in the test set is 0, the entire probability of the test set is 0.&lt;/p>
&lt;p>$\rightarrow$ Based on the definition of perplexity, we can’t compute perplexity at all, since we can’t divide by 0!&lt;/p>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>​&lt;/p>
&lt;h3 id="unknow-words">Unknow words&lt;/h3>
&lt;p>&lt;strong>Closed vocabulary&lt;/strong> system:&lt;/p>
&lt;ul>
&lt;li>All the words can occur&lt;/li>
&lt;li>the test set can only contain words from this lexicon, and there will be NO unknown words.&lt;/li>
&lt;li>Reasonable assumption in some domains
&lt;ul>
&lt;li>speech recognition (we have pronunciation dictionary in advance)&lt;/li>
&lt;li>machine translation (we have phrase table in advance)&lt;/li>
&lt;li>The language model can only use the words in that dictionary or phrase table.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Unknown words&lt;/strong>: words we simply have NEVER seen before.&lt;/p>
&lt;ul>
&lt;li>sometimes called &lt;strong>out of vocabulary (OOV)&lt;/strong> words.&lt;/li>
&lt;li>&lt;strong>OOV rate&lt;/strong>: percentage of OOV words that appear in the test set&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Open vocabulary&lt;/strong> system:&lt;/p>
&lt;ul>
&lt;li>we model these potential unknown words in the test set by adding a pseudo-word called &lt;code>&amp;lt;UNK&amp;gt;&lt;/code>.&lt;/li>
&lt;/ul>
&lt;p>Two common ways to to train the probabilities of the unknown word model &lt;code>&amp;lt;UNK&amp;gt;&lt;/code>&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Turn the problem back into a closed vocabulary one by choosing a fixed vocabulary in advance&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Choose a vocabulary&lt;/strong> (word list) that is fixed in advance.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Convert&lt;/strong> in the training set any word that is not in this set (any OOV word) to&lt;/p>
&lt;p>the unknown word token &lt;code>&amp;lt;UNK&amp;gt;&lt;/code> in a text normalization step.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Estimate&lt;/strong> the probabilities for &lt;code>&amp;lt;UNK&amp;gt;&lt;/code> from its counts just like any other regular&lt;/p>
&lt;p>word in the training set.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/li>
&lt;li>
&lt;p>We don’t have a prior vocabulary in advance&lt;/p>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Create&lt;/strong> such a vocabulary implicitly&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Replace&lt;/strong> words in the training data by &lt;code>&amp;lt;UNK&amp;gt;&lt;/code> based on their frequency.&lt;/p>
&lt;ul>
&lt;li>we can replace by &lt;code>&amp;lt;UNK&amp;gt;&lt;/code> all words that occur fewer than $n$ times in the training set, where $n$ is some small number, or&lt;/li>
&lt;li>equivalently select a vocabulary size $V$ in advance (say 50,000) and choose the top $V$ words by frequency and replace the rest by &lt;code>&amp;lt;UNK&amp;gt;&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>In either case we then proceed to train the language model as before, treating &lt;code>&amp;lt;UNK&amp;gt;&lt;/code> like a regular word.&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/li>
&lt;/ul></description></item><item><title>Smoothing</title><link>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/smoothing/</link><pubDate>Mon, 03 Aug 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/smoothing/</guid><description>&lt;p>To keep a language model from assigning zero probability to these unseen events, we’ll have to shave off a bit of probability mass from some more frequent events and give it to the events we’ve never seen.&lt;/p>
&lt;h2 id="laplace-smoothing-add-1-smoothing">Laplace smoothing (Add-1 smoothing)&lt;/h2>
&lt;p>💡 Idea: add one to all the bigram counts, before we normalize them into probabilities.&lt;/p>
&lt;ul>
&lt;li>does not perform well enough to be used in modern n-gram models 🤪, but&lt;/li>
&lt;li>usefully introduces many of the concepts&lt;/li>
&lt;li>gives a useful baseline&lt;/li>
&lt;li>a practical smoothing algorithm for other tasks like text classification&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Unsmoothed&lt;/strong> maximum likelihood estimate of the unigram probability of the word $w_i$: its count $c_i$ normalized by the total number of word tokens $N$
&lt;/p>
$$
P\left(w\_{i}\right)=\frac{c\_{i}}{N}
$$
&lt;p>
&lt;strong>Laplace smoothed&lt;/strong>:&lt;/p>
&lt;ul>
&lt;li>Merely adds one to each count&lt;/li>
&lt;li>Since there are $V$ words in the vocabulary and each one was incremented, we also need to adjust the denominator to take into account the extra $V$ observations.&lt;/li>
&lt;/ul>
$$
P_{\text {Laplace}}\left(w_{i}\right)=\frac{c_{i}+1}{N+V}
$$
&lt;p>&lt;strong>Adjust count&lt;/strong> $c^*$
&lt;/p>
$$
c_{i}^{*}=\left(c_{i}+1\right) \frac{N}{N+V}
$$
&lt;ul>
&lt;li>
&lt;p>easier to compare directly with the MLE counts and can be turned into a probability like an MLE count by normalizing by $N$&lt;/p>
&lt;blockquote>
&lt;p>$\frac{c_I^*}{N} = \left(c_{i}+1\right) \frac{N}{N+V} \cdot \frac{1}{N} =\frac{c_{i}+1}{N+V} = P_{\text {Laplace}}\left(w_{i}\right)$&lt;/p>
&lt;/blockquote>
&lt;/li>
&lt;/ul>
&lt;h3 id="another-aspect-of-smoothing">Another aspect of smoothing&lt;/h3>
&lt;p>A related way to view smoothing is as &lt;strong>discounting&lt;/strong> (lowering) some non-zero counts in order to get the probability mass that will be assigned to the zero counts.&lt;/p>
&lt;p>Thus, instead of referring to the discounted counts $c^{\*}$, we might describe a smoothing algorithm in terms of a relative &lt;strong>discount&lt;/strong> $d_c$
&lt;/p>
$$
d_c = \frac{c^*}{c}
$$
&lt;p>
(the ratio of the discounted counts to the original counts)&lt;/p>
&lt;h3 id="example">Example&lt;/h3>
&lt;p>Smooth the Berkeley Restaurant Project bigrams&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Original(unsmoothed) bigram counts and probabilities&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-02%2016.56.21.png" alt="截屏2020-06-02 16.56.21" style="zoom:80%;" />
&lt;/li>
&lt;/ul>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-02%2016.58.56.png" alt="截屏2020-06-02 16.58.56" style="zoom:80%;" />
&lt;ul>
&lt;li>
&lt;p>Add-one smoothed counts and probabilities&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-03%2016.33.50.png" alt="截屏2020-06-03 16.33.50" style="zoom:80%;" />
&lt;/li>
&lt;/ul>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-03%2016.36.08.png" alt="截屏2020-06-03 16.36.08" style="zoom:80%;" />
&lt;p>Computation:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Recall: normal bigram probabilities are computed by normalizing each row of counts by the unigram count
&lt;/p>
$$
P\left(w_{n} | w_{n-1}\right)=\frac{C\left(w_{n-1} w_{n}\right)}{C\left(w_{n-1}\right)}
$$
&lt;/li>
&lt;li>
&lt;p>add-one smoothed bigram: augment the unigram count by the number of total word types in the vocabulary&lt;/p>
$$
P_{\text {Laplace }}^{*}\left(w_{n} | w_{n-1}\right)=\frac{C\left(w_{n-1} w_{n}\right)+1}{\sum_{w}\left(C\left(w_{n-1} w\right)+1\right)}=\frac{C\left(w_{n-1} w_{n}\right)+1}{C\left(w_{n-1}\right)+V}
$$
&lt;/li>
&lt;li>
&lt;p>It is often convenient to reconstruct the count matrix so we can see how much a smoothing algorithm has changed the original counts.
&lt;/p>
$$
c^{*}\left(w_{n-1} w_{n}\right)=\frac{\left[C\left(w_{n-1} w_{n}\right)+1\right] \times C\left(w_{n-1}\right)}{C\left(w_{n-1}\right)+V}
$$
&lt;p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-03%2017.19.19.png" alt="截屏2020-06-03 17.19.19" style="zoom:80%;" />&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>Add-one smoothing has made a very big change to the counts.&lt;/p>
&lt;ul>
&lt;li>$C(\text{want to})$ changed from 609 to 238&lt;/li>
&lt;li>$P(to|want)$ decreases from .66 in the unsmoothed case to .26 in the smoothed case&lt;/li>
&lt;li>The discount $d$ (the ratio between new and old counts) shows us how strikingly the counts for each prefix word have been reduced
&lt;ul>
&lt;li>the discount for the bigram &lt;em>want to&lt;/em> is .39&lt;/li>
&lt;li>the discount for &lt;em>Chinese food&lt;/em> is .10&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>The sharp change in counts and probabilities occurs because too much probability mass is moved to all the zeros.&lt;/p>
&lt;h2 id="add-k-smoothing">Add-k smoothing&lt;/h2>
&lt;p>Instead of adding 1 to each count, we add a fractional count $k$
&lt;/p>
$$
P_{\mathrm{Add}-\mathrm{k}}^{*}\left(w_{n} | w_{n-1}\right)=\frac{C\left(w_{n-1} w_{n}\right)+k}{C\left(w_{n-1}\right)+k V}
$$
&lt;ul>
&lt;li>$k$: can be chosen by optimizing on a devset (validation set)&lt;/li>
&lt;/ul>
&lt;p>Add-k smoothing&lt;/p>
&lt;ul>
&lt;li>useful for some tasks (including text classification)&lt;/li>
&lt;li>still doesn’t work well for language modeling, generating counts with poor variances and often inappropriate discounts 🤪&lt;/li>
&lt;/ul>
&lt;h2 id="backoff-and-interpolation">Backoff and interpolation&lt;/h2>
&lt;p>Sometimes using less context is a good thing, helping to generalize more for contexts that the model hasn’t learned much about.&lt;/p>
&lt;h3 id="backoff">Backoff&lt;/h3>
&lt;p>&lt;strong>💡 “Back off” to a lower-order n-gram if we have zero evidence for a higher-order n-gram&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>If the n-gram we need has zero counts, we approximate it by backing off to the (n-1)-gram. We continue backing off until we reach a history that has some counts.
&lt;ul>
&lt;li>&lt;em>we use the trigram if the evidence is sufficient, otherwise we use the bigram, otherwise the unigram.&lt;/em>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h4 id="katz-backoff">Katz backoff&lt;/h4>
&lt;ul>
&lt;li>
&lt;p>Rely on a discounted probability $P^*$ if we’ve seen this n-gram before (i.e., if we have non-zero counts)&lt;/p>
&lt;ul>
&lt;li>
&lt;p>We have to discount the higher-order n-grams to save some probability mass for the lower order n-grams&lt;/p>
&lt;blockquote>
&lt;p>if the higher-order n-grams aren’t discounted and we just used the undiscounted MLE probability, then as soon as we replaced an n-gram which has zero probability with a lower-order n-gram, we would be adding probability mass, and the total probability assigned to all possible strings by the language model would be greater than 1!&lt;/p>
&lt;/blockquote>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Otherwise, we recursively back off to the Katz probability for the shorter-history (n-1)-gram.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>$\Rightarrow$ The probability for a backoff n-gram $P_{\text{BO}}$ is &lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/image-20200803110631566.png" alt="image-20200803110631566" style="zoom:18%;" />&lt;/p>
&lt;ul>
&lt;li>$P^*$: discounted probability&lt;/li>
&lt;li>$\alpha$: a function to distribute the discounted probability mass to the lower order n-grams&lt;/li>
&lt;/ul>
&lt;h3 id="interpolation">Interpolation&lt;/h3>
&lt;p>💡 Mix the probability estimates from all the n-gram estimators, weighing and combining the trigram, bigram, and unigram counts.&lt;/p>
&lt;p>In &lt;em>simple&lt;/em> &lt;strong>linear&lt;/strong> interpolation, we combine different order n-grams by linearly interpolating all the models. I.e., we estimate the trigram probability $P\left(w_{n} | w_{n-2} w_{n-1}\right)$ by mixing together the unigram, bigram, and trigram probabilities, each weighted by a $\lambda$
&lt;/p>
$$
\begin{array}{ll}
\hat{P}\left(w_{n} | w_{n-2} w_{n-1}\right) = &amp; \lambda_{1} P\left(w_{n} | w_{n-2} w_{n-1}\right) \\\\
&amp;+ \lambda_{2} P\left(w_{n} | w_{n-1}\right) \\\\
&amp;+ \lambda_{3} P\left(w_{n}\right)
\end{array}
$$
&lt;p>
s.t.
&lt;/p>
$$
\sum_{i} \lambda_{i}=1
$$
&lt;p>
In a &lt;em>slightly more sophisticated&lt;/em> version of linear interpolation, each $\lambda$ weight is computed by conditioning on the context.&lt;/p>
&lt;ul>
&lt;li>If we have particularly accurate counts for a particular bigram, we assume that the counts of the trigrams based on this bigram will be more trustworthy, so we can make the $λ$s for those trigrams higher and thus give that trigram more weight in the interpolation.&lt;/li>
&lt;/ul>
$$
\begin{array}{ll}
\hat{P}\left(w_{n} | w_{n-2} w_{n-1}\right)=&amp; \lambda_{1}\left(w_{n-2}^{n-1}\right) P\left(w_{n} | w_{n-2} w_{n-1}\right) \\\\
&amp;+\lambda_{2}\left(w_{n-2}^{n-1}\right) P\left(w_{n} | w_{n-1}\right) \\\\
&amp;+\lambda_{3}\left(w_{n-2}^{n-1}\right) P\left(w_{n}\right)
\end{array}
$$
&lt;h4 id="how-to-set-lambdas">How to set $\lambda$s?&lt;/h4>
&lt;p>Learn from a &lt;strong>held-out&lt;/strong> corpus&lt;/p>
&lt;ul>
&lt;li>Held-out corpus: an additional training corpus that we use to set hyperparameters like these $λ$ values, by choosing the $λ$ values that maximize the likelihood of the held-out corpus.&lt;/li>
&lt;li>We fix the n-gram probabilities and then search for the $λ$ values that give us the highest probability of the held-out set
&lt;ul>
&lt;li>Common method: &lt;strong>EM&lt;/strong> algorithm&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="kneser-ney-smoothing-">Kneser-Ney Smoothing 👍&lt;/h2>
&lt;p>One of the most commonly used and best performing n-gram smoothing methods 👏&lt;/p>
&lt;p>Based on &lt;strong>absolute discounting&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>subtracting a fixed (absolute) discount $d$ from each count.&lt;/li>
&lt;li>💡 Intuition:
&lt;ul>
&lt;li>since we have good estimates already for the very high counts, a small discount &lt;em>d&lt;/em> won’t affect them much&lt;/li>
&lt;li>It will mainly modify the smaller counts, for which we don’t necessarily trust the estimate anyway&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>
&lt;figure >
&lt;div class="flex justify-center ">
&lt;div class="w-100" >&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%e6%88%aa%e5%b1%8f2020-06-03%2022.51.39-20200803110820400.png" alt="截屏2020-06-03 22.51.39" loading="lazy" data-zoomable />&lt;/div>
&lt;/div>&lt;/figure>
&lt;/p>
&lt;p>Except for the held-out counts for 0 and 1, all the other bigram counts in the held-out set could be estimated pretty well by just subtracting 0.75 from the count in the training set! In practice this discount is actually a good one for bigrams with counts 2 through 9.&lt;/p>
&lt;p>The equation for interpolated absolute discounting applied to bigrams:
&lt;/p>
$$
P_{\text {AbsoluteDiscounting }}\left(w_{i} | w_{i-1}\right)=\frac{C\left(w_{i-1} w_{i}\right)-d}{\sum_{v} C\left(w_{i-1} v\right)}+\lambda\left(w_{i-1}\right) P\left(w_{i}\right)
$$
&lt;ul>
&lt;li>First term: discounted bigram
&lt;ul>
&lt;li>We could just set all the $d$ values to .75, or we could keep a separate discount value of 0.5 for the bigrams with counts of 1.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Second term: unigram with an interpolation weight $\lambda$&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Kneser-Ney discounting&lt;/strong> augments absolute discounting with a more sophisticated way to handle the lower-order unigram distribution.&lt;/p>
&lt;p>Sophisticated means: Instead of $P(w)$ which answers the question “How likely is $w$?”, we’d like to create a unigram model that we might call $P\_{\text{CONTINUATION}}$, which answers the question “How likely is &lt;em>w&lt;/em> to appear as a novel continuation?”&lt;/p>
&lt;p>How can we estimate this probability of seeing the word $w$ as a novel continuation, in a new unseen context?&lt;/p>
&lt;p>💡 The Kneser-Ney intuition: base our $P\_{\text{CONTINUATION}}$ on the &lt;em>number of different contexts word&lt;/em> $w$ &lt;em>has appeared in&lt;/em> (the number of bigram types it completes).&lt;/p>
&lt;ul>
&lt;li>Every bigram type was a novel continuation the first time it was seen.&lt;/li>
&lt;li>We hypothesize that words that have appeared in more contexts in the past are more likely to appear in some new context as well.&lt;/li>
&lt;/ul>
&lt;p>The number of times a word $w$ appears as a novel continuation can be expressed as:
&lt;/p>
$$
P\_{\mathrm{CONTINUATION}}(w) \propto|\{v: C(v w)>0\}|
$$
&lt;p>
To turn this count into a probability, we normalize by the total number of word bigram types:
&lt;/p>
$$
P\_{\text{CONTINUATION}}(w)=\frac{|\{v: C(v w)>0\}|}{|\{(u^{\prime}, w^{\prime}): Cu^{\prime} w^{\prime})>0\}|}
$$
&lt;p>
An equivalent formulation based on a different metaphor is to use the number of word types seen to precede $w$, normalized by the number of words preceding all words,
&lt;/p>
$$
P\_{\mathrm{CONTINUATION}}(w)=\frac{|\{v: C(v w)>0\}|}{\sum_{w^{\prime}}|\{v: C(v w^{\prime})>0\}|}
$$
&lt;p>
The final equation for &lt;strong>Interpolated Kneser-Ney smoothing&lt;/strong> for bigrams is:
&lt;/p>
$$
P_{\mathrm{KN}}\left(w\_{i} | w\_{i-1}\right)=\frac{\max \left(C\left(w\_{i-1} w\_{i}\right)-d, 0\right)}{C\left(w\_{i-1}\right)}+\lambda\left(w\_{i-1}\right) P\_{\mathrm{CONTINUATION}}\left(w\_{i}\right)
$$
&lt;ul>
&lt;li>
&lt;p>$λ$: normalizing constant that is used to distribute the probability mass
&lt;/p>
$$
\lambda(w_{i-1})=\frac{d}{\sum_{v} C(w_{i-1} v)}|\{w: C(w_{i-1} w)>0\}|
$$
&lt;ul>
&lt;li>First term: normalized discount&lt;/li>
&lt;li>Second term: the number of word types that can follow $w_{i-1}$. or, equivalently, the number of word types that we discounted (i.e., the number of times we applied the normalized discount.)&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>The general recursive formulation is
&lt;/p>
$$
P_{\mathrm{KN}}\left(w_{i} | w_{i-n+1}^{i-1}\right)=\frac{\max \left(c_{K N}\left(w_{i-n+1}^{i}\right)-d, 0\right)}{\sum_{v} c_{K N}\left(w_{i-n+1}^{i-1} v\right)}+\lambda\left(w_{i-n+1}^{i-1}\right) P_{K N}\left(w_{i} | w_{i-n+2}^{i-1}\right)
$$
&lt;ul>
&lt;li>
&lt;p>$C_{KN}$: depends on whether we are counting the highest-order n-gram being interpolated or one of the lower-order n-grams &lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/image-20200803111708917.png" alt="image-20200803111708917" style="zoom:18%;" />&lt;/p>
&lt;ul>
&lt;li>$\operatorname{continuationcount}(\cdot)$: the number of unique single word contexts for $\cdot$&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>At the termination of the recursion, unigrams are interpolated with the uniform distribution
&lt;/p>
$$
P_{\mathrm{KN}}(w)=\frac{\max \left(c_{K N}(w)-d, 0\right)}{\sum_{w^{\prime}} c_{K N}\left(w^{\prime}\right)}+\lambda(\varepsilon) \frac{1}{V}
$$
&lt;ul>
&lt;li>$\varepsilon$: empty string&lt;/li>
&lt;/ul></description></item><item><title>Perplexity’s Relation to Entropy</title><link>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/perplexity-to-entropy/</link><pubDate>Mon, 03 Aug 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/perplexity-to-entropy/</guid><description>&lt;div class="flex px-4 py-3 mb-6 rounded-md bg-primary-100 dark:bg-primary-900">
&lt;span class="pr-3 pt-1 text-primary-600 dark:text-primary-300">
&lt;svg height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">&lt;path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="m11.25 11.25l.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0a9 9 0 0 1 18 0m-9-3.75h.008v.008H12z"/>&lt;/svg>
&lt;/span>
&lt;span class="dark:text-neutral-300">&lt;p>&lt;strong>Recall&lt;/strong>&lt;/p>
&lt;p>A better n-gram model is one that assigns a higher probability to the test data, and perplexity is a normalized version of the probability of the test set.&lt;/p>
&lt;/span>
&lt;/div>
&lt;p>&lt;strong>Entropy&lt;/strong>: a measure of information&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Given:&lt;/p>
&lt;ul>
&lt;li>A random variable $X$ ranging over whatever we are predicting (words, letters, parts of speech, the set of which we’ll call $χ$)&lt;/li>
&lt;li>with a particular probability function $p(x)$&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>The entropy of the random variable $X$ is
&lt;/p>
$$
H(X)=-\sum\_{x \in \chi} p(x) \log\_{2} p(x)
$$
&lt;ul>
&lt;li>If we use log base 2, the resulting value of entropy will be measured in &lt;strong>bits&lt;/strong>.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>💡 Intuitive way to think about entropy: a &lt;strong>lower bound&lt;/strong> on the number of bits it would take to encode a certain decision or piece of information in the optimal coding scheme.&lt;/p>
&lt;p>&lt;strong>Example&lt;/strong>&lt;/p>
&lt;p>Imagine that we want to place a bet on a horse race but it is too far to go all the way to Yonkers Racetrack, so we’d like to send a short message to the bookie to tell him which of the eight horses to bet on.&lt;/p>
&lt;p>One way to encode this message is just to use the binary representation of the horse’s number as the code: horse 1 would be &lt;code>001&lt;/code>, horse 2 &lt;code>010&lt;/code>, horse 3 &lt;code>011&lt;/code>, and so on, with horse 8 coded as &lt;code>000&lt;/code>. On average we would be sending 3 bits per race.&lt;/p>
&lt;p>Suppose that the spread is the actual distribution of the bets placed and that we represent it as the prior probability of each horse as follows:&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2020-06-04%2010.23.19.png" alt="截屏2020-06-04 10.23.19" style="zoom:80%;" />
&lt;p>The entropy of the random variable &lt;em>X&lt;/em> that ranges over horses gives us a lower bound on the number of bits and is
&lt;/p>
$$
\begin{aligned}
H(X) &amp;=-\sum\_{i=1}^{i=8} p(i) \log p(i) \\\\
&amp;=-\frac{1}{2} \log \frac{1}{2}-\frac{1}{4} \log \frac{1}{4}-\frac{1}{8} \log \frac{1}{8}-\frac{1}{16} \log \frac{1}{16}-4\left(\frac{1}{64} \log \frac{1}{64}\right) \\\\
&amp;=2 \text { bits }
\end{aligned}
$$
&lt;p>
A code that averages 2 bits per race can be built with &lt;em>short&lt;/em> encodings for &lt;em>more probable&lt;/em> horses, and &lt;em>longer&lt;/em> encodings for &lt;em>less probable&lt;/em> horses. E.g. we could encode the most likely horse with the code &lt;code>0&lt;/code>, and the remaining horses as &lt;code>10&lt;/code>, then &lt;code>110&lt;/code>, &lt;code>1110&lt;/code>, &lt;code>111100&lt;/code>, &lt;code>111101&lt;/code>, &lt;code>111110&lt;/code>, and &lt;code>111111&lt;/code>.&lt;/p>
&lt;p>Suppose horses are equally likely. In this case each horse would have a probability of $\frac{1}{8}$. The entropy is then
&lt;/p>
$$
H(X)=-\sum\_{i=1}^{i=8} \frac{1}{8} \log \frac{1}{8}=-\log \frac{1}{8}=3 \mathrm{bits}
$$
&lt;hr>
&lt;p>Most of what we will use entropy for involves &lt;em>&lt;strong>sequences&lt;/strong>&lt;/em>.&lt;/p>
&lt;p>For a grammar, for example, we will be computing the entropy of some sequence of words $W=\{w\_0, w\_1, w\_2, \dots, w\_n\}$. One way to do this is to have a variable that ranges over sequences of words. For example we can compute the entropy of a random variable that ranges over all &lt;em>&lt;strong>finite&lt;/strong>&lt;/em> sequences of words of length $n$ in some language $L$
&lt;/p>
$$
H\left(w\_{1}, w\_{2}, \ldots, w\_{n}\right)=-\sum_{W\_{1}^{n} \in L} p\left(W\_{1}^{n}\right) \log p\left(W\_{1}^{n}\right)
$$
&lt;p>
&lt;strong>Entropy rate&lt;/strong> (&lt;strong>per-word entropy&lt;/strong>): entropy of this sequence divided by the number of word
&lt;/p>
$$
\frac{1}{n} H\left(W\_{1}^{n}\right)=-\frac{1}{n} \sum_{W\_{1}^{n} \in L} p\left(W\_{1}^{n}\right) \log p\left(W\_{1}^{n}\right)
$$
&lt;p>
For sequence $L$ of &lt;em>&lt;strong>infinite&lt;/strong>&lt;/em> length, the entropy rate $H(L)$ is
&lt;/p>
$$
\begin{aligned}
H(L) &amp;=\lim \_{n \rightarrow \infty} \frac{1}{n} H\left(w\_{1}, w\_{2}, \ldots, w\_{n}\right) \\\\
&amp;=-\lim \_{n \rightarrow \infty} \frac{1}{n} \sum_{W \in L} p\left(w\_{1}, \ldots, w_{n}\right) \log p\left(w\_{1}, \ldots, w\_{n}\right)
\end{aligned}
$$
&lt;h2 id="the-shannon-mcmillan-breiman-theorem">The Shannon-McMillan-Breiman theorem&lt;/h2>
&lt;p>If the language is regular in certain ways (to be exact, if it is both &lt;strong>stationary&lt;/strong> and &lt;strong>ergodic&lt;/strong>), then
&lt;/p>
$$
H(L)=\lim \_{n \rightarrow \infty}-\frac{1}{n} \log p\left(w\_{1} w\_{2} \ldots w\_{n}\right)
$$
&lt;p>
I.e., we can take a single sequence that is long enough instead of summing over all possible sequences.&lt;/p>
&lt;ul>
&lt;li>💡 Intuition: a long-enough sequence of words will contain in it many other shorter sequences and that each of these shorter sequences will reoccur in the longer sequence according to their probabilities.&lt;/li>
&lt;/ul>
&lt;p>&lt;strong>Stationary&lt;/strong>&lt;/p>
&lt;p>A stochastic process is said to be &lt;strong>stationary&lt;/strong> if the probabilities it assigns to a sequence are &lt;em>invariant&lt;/em> with respect to shifts in the time index.&lt;/p>
&lt;ul>
&lt;li>
&lt;p>I.e., the probability distribution for words at time $t$ is the same as the probability distribution at time $t+1$.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Markov models, and hence n-grams, are stationary.&lt;/p>
&lt;ul>
&lt;li>E.g., in bigram, $P_i$ is dependent only on $P_{i-1}$. If we shift our time index by $x$, $P_{i+x}$ is still dependent on $P_{i+x-1}$&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Natural language is NOT stationary&lt;/p>
&lt;ul>
&lt;li>the probability of upcoming words can be dependent on events that were arbitrarily distant and time dependent.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;p>To summarize, by making some incorrect but convenient simplifying assumptions, &lt;strong>we can compute the entropy of some stochastic process by taking a very long sample of the output and computing its average log probability.&lt;/strong>&lt;/p>
&lt;h2 id="cross-entropy">Cross-entropy&lt;/h2>
&lt;p>Useful when we don’t know the actual probability distribution $p$ that generated some data&lt;/p>
&lt;p>It allows us to use some $m$, which is a model of $p$ (i.e., an approximation to $p$). The&lt;/p>
&lt;p>cross-entropy of $m$ on $p$ is defined by&lt;/p>
$$
H(p, m)=\lim\_{n \rightarrow \infty}-\frac{1}{n} \sum\_{W \in L} p\left(w\_{1}, \ldots, w\_{n}\right) \log m\left(w\_{1}, \ldots, w\_{n}\right)
$$
&lt;p>(we draw sequences according to the probability distribution $p$, but sum the log of their probabilities according to $m$)&lt;/p>
&lt;p>Following the Shannon-McMillan-Breiman theorem, for a stationary ergodic process:
&lt;/p>
$$
H(p, m)=\lim\_{n \rightarrow \infty}-\frac{1}{n} \log m\left(w\_{1} w\_{2} \ldots w\_{n}\right)
$$
&lt;p>
(as for entropy, we can estimate the cross-entropy of a model $m$ on some distribution $p$ by taking a single sequence that is long enough instead of summing over all possible sequences)&lt;/p>
&lt;p>The cross-entropy $H(p. m)$ is an &lt;strong>upper bound&lt;/strong> on the entropy $H(p)$:
&lt;/p>
$$
H(p)\leq H(p, m)
$$
&lt;p>
This means that we can use some simplified model $m$ to help estimate the true entropy of a sequence of symbols drawn according to probability $p$&lt;/p>
&lt;ul>
&lt;li>The more accurate $m$ is, the closer the cross-entropy $H(p, m)$ will be to the true entropy $H(p)$
&lt;ul>
&lt;li>Difference between $H(p, m)$ and $H(p)$ is a measure of how accurate a model is&lt;/li>
&lt;li>The more accurate model will be the one with the lower cross-entropy.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="relationship-between-perplexity-and-cross-entropy">Relationship between perplexity and cross-entropy&lt;/h3>
&lt;p>Cross-entropy is defined in the limit, as the length of the observed word sequence goes to infinity. We will need an approximation to cross-entropy, relying on a (sufficiently long) sequence of fixed length.&lt;/p>
&lt;p>This approximation to the cross-entropy of a model $M=P\left(w_{i} | w_{i-N+1} \dots w_{i-1}\right)$ on a sequence of words $W$ is
&lt;/p>
$$
H(W)=-\frac{1}{N} \log P\left(w_{1} w_{2} \ldots w_{N}\right)
$$
&lt;p>
The &lt;strong>perplexity&lt;/strong> of a model $P$ on a sequence of words $W$ is now formally defined as&lt;/p>
&lt;p>the exp of this cross-entropy:
&lt;/p>
$$
\begin{aligned}
\operatorname{Perplexity}(W) &amp;=2^{H(W)} \\\\
&amp;=P\left(w_{1} w_{2} \ldots w_{N}\right)^{-\frac{1}{N}} \\\\
&amp;=\sqrt[N]{\frac{1}{P\left(w_{1} w_{2} \ldots w_{N}\right)}} \\\\
&amp;=\sqrt[N]{\prod_{i=1}^{N} \frac{1}{P\left(w_{i} | w_{1} \ldots w_{i-1}\right)}}
\end{aligned}
$$</description></item><item><title>Summary (TL;DR)</title><link>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/n-gram-tldr/</link><pubDate>Mon, 03 Aug 2020 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/ai/natural-language-processing/lm-n-grams/n-gram-tldr/</guid><description>&lt;h2 id="language-models">&lt;strong>Language models&lt;/strong>&lt;/h2>
&lt;ul>
&lt;li>offer a way to assign a probability to a sentence or other sequence of words, and to predict a word from preceding words.
&lt;ul>
&lt;li>$P(w|h)$: Probability of word $w$ given history $h$&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="n-gram-model">&lt;strong>n-gram model&lt;/strong>&lt;/h2>
&lt;ul>
&lt;li>
&lt;p>estimate words from a fixed window of previous words
&lt;/p>
$$
P\left(w_{n} | w_{1}^{n-1}\right) \approx P\left(w_{n} | w_{n-N+1}^{n-1}\right)
$$
&lt;/li>
&lt;li>
&lt;p>n-gram probabilities can be estimated by counting in a corpus and normalizing (&lt;strong>MLE&lt;/strong>)
&lt;/p>
$$
P\left(w_{n} | w_{n-N+1}^{n-1}\right)=\frac{C\left(w_{n-N+1}^{n-1} w_{n}\right)}{C\left(w_{n-N+1}^{n-1}\right)}
$$
&lt;/li>
&lt;li>
&lt;p>Evaluation&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Extrinsically in some task (expensive!)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Instrinsically using &lt;strong>perplexity&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>perplexity of a test set according to a language model: the geometric mean of the inverse test set probability computed by the model.
$$
\begin{array}{ll}
\operatorname{PP}(W) &amp;=P\left(w_{1} w_{2} \ldots w_{N}\right)^{-\frac{1}{N}} \\\\
&amp;=\sqrt[N]{\frac{1}{P\left(w_{1} w_{2} \ldots w_{N}\right)}} \\\\
&amp;\overset{\text{chain rule}}{=} \sqrt[N]{\displaystyle\prod_{i=1}^{N} \frac{1}{P\left(w_{i} | w_{1} \ldots w_{i-1}\right)}}
\end{array}
$$&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="smoothing">&lt;strong>Smoothing&lt;/strong>&lt;/h2>
&lt;p>provide a more sophisticated way to estimate the probability of n-grams&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Laplace (Add-one) smmothing
&lt;/p>
$$
P_{\text {Laplace}}\left(w_{i}\right)=\frac{c_{i}+1}{N+V}
$$
&lt;ul>
&lt;li>$V$: Number of words in the vocabulary&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Add-k smoothing
&lt;/p>
$$
P_{\mathrm{Add}-\mathrm{k}}^{*}\left(w_{n} | w_{n-1}\right)=\frac{C\left(w_{n-1} w_{n}\right)+k}{C\left(w_{n-1}\right)+k V}
$$
&lt;/li>
&lt;li>
&lt;p>Backoff or interpolation&lt;/p>
&lt;ul>
&lt;li>Rely on lower-order n-gram counts&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Kneser-Ney smoothing&lt;/p>
&lt;ul>
&lt;li>makes use of the probability of a word being a novel continuation&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul></description></item></channel></rss>