← 学习库 Speech and Language Processing 本册目录

4.5.1 Laplace Smoothing

One simple way to do smoothing might be just to take our matrix of bigram counts, before we normalize them into probabilities, and add one to all the counts. This algorithm is called Laplace smoothing, or Laplace's Law (Lidstone, 1920; Johnson, 1932; Jeffreys, 1948). Laplace smoothing does not perform well enough to be used in modern N-gram models, but we begin with it because it introduces many of the concepts that we will see in other smoothing algorithms and also gives us a useful baseline.

Let's start with the application of Laplace smoothing to unigram probabilities. Recall that the unsmoothed maximum likelihood estimate of the unigram probability of the word $ w_{i} $ is its count $ c_{i} $ normalized by the total number of word tokens N:

$$ P(w_{i})=\frac{c_{i}}{N} $$

Laplace smoothing merely adds one to each count (hence its alternate name add-

原书第 109 页

one smoothing). Since there are V words in the vocabulary, and each one got incremented, we also need to adjust the the denominator to take into account the extra V observations. $ ^{6} $

$$ P_{Laplace}(w_{i})=\frac{c_{i}+1}{N+V} $$

Instead of changing both the numerator and denominator it is convenient to describe how a smoothing algorithm affects the numerator, by defining an \textit{adjusted count} $ c^* $. This adjusted count is easier to compare directly with the MLE counts, and can be turned into a probability like an MLE count by normalizing by $ N $. To define this count, since we are only changing the numerator, in addition to adding one we'll also need to multiply by a normalization factor $ \frac{N}{N+V} $:

$$ c_{i}^{*}=(c_{i}+1)\frac{N}{N+V} $$

We can now turn $c^{*}$ into a probability $p_{i}^{*}$ by normalizing by $N$.

A related way to view smoothing is as \textit{discounting} (lowering) some non-zero counts in order to get the probability mass that will be assigned to the zero counts. Thus instead of referring to the discounted counts $ c^{*} $, we might describe a smoothing algorithm in terms of a relative \textit{discount} $ d_{c} $, the ratio of the discounted counts to the original counts:

$$ d_{c}=\frac{c^{*}}{c} $$

Now that we have the intuition for the unigram case, let's smooth our Berkeley Restaurant Project bigrams. Fig. 4.5 shows the add-one smoothed counts for the bigrams in Fig. 4.1.

iwanttoeatchinesefoodlunchspend
i68281101113
want3160927762
to315687317212
eat1131173431
chinese211118321
food1611612511
lunch31111211
spend21211111
Figure 4.5 Add-one smoothed bigram counts for eight of the words (out of V = 1446) in the Berkeley Restaurant Project corpus of 9332 sentences.

Fig. 4.6 shows the add-one smoothed probabilities for the bigrams in Fig. 4.2. Recall that normal bigram probabilities are computed by normalizing each row of counts by the unigram count:

原书第 110 页

$$ P(w_{n}|w_{n-1})=\frac{C(w_{n-1}w_{n})}{C(w_{n-1})} $$

For add-one smoothed bigram counts we need to augment the unigram count by the number of total word types in the vocabulary V:

$$ P_{Laplace}^{*}(w_{n}|w_{n-1})=\frac{C(w_{n-1}w_{n})+1}{C(w_{n-1})+V} $$

Thus each of the unigram counts given in the previous section will need to be augmented by V = 1446. The result is the smoothed bigram probabilities in Fig. 4.6.

iwanttoeatchinesefoodlunchspend
i0.00150.210.000250.00250.000250.000250.000250.00075
want0.00130.000420.260.000840.00290.00290.00250.00084
to0.000780.000260.00130.180.000780.000260.00180.055
eat0.000460.000460.00140.000460.00780.00140.020.00046
chinese0.00120.000620.000620.000620.000620.0520.00120.00062
food0.00630.000390.00630.000390.000790.0020.000390.00039
lunch0.00170.000560.000560.000560.000560.00110.000560.00056
spend0.00120.000580.00120.000580.000580.000580.000580.00058
Figure 4.6 Add-one smoothed bigram probabilities for eight of the words (out of V = 1446) in the BeRP corpus of 9332 sentences.

It is often convenient to reconstruct the count matrix so we can see how much a smoothing algorithm has changed the original counts. These adjusted counts can be computed by Equation (4.24). Fig. 4.7 shows the reconstructed counts.

$$ c^{*}(w_{n-1}w_{n})=\frac{[C(w_{n-1}w_{n})+1]\times C(w_{n-1})}{C(w_{n-1})+V} $$

iwanttoeatchinesefoodlunchspend
i3.85270.646.40.640.640.641.9
want1.20.392380.782.72.72.30.78
to1.90.633.14301.90.634.4133
eat0.340.3410.345.81150.34
chinese0.20.0980.0980.0980.0988.20.20.098
food6.90.436.90.430.862.20.430.43
lunch0.570.190.190.190.190.380.190.19
spend0.320.160.320.160.160.160.160.16
Figure 4.7 Add-one reconstituted counts for eight words (of V = 1446) in the BeRP corpus of 9332 sentences.

Note that add-one smoothing has made a very big change to the counts. $ C(\text{want to}) $ changed from 608 to 238! We can see this in probability space as well: $ P(to|\text{want}) $

原书第 111 页

decreases from .66 in the unsmoothed case to .26 in the smoothed case. Looking at the discount $ d $ (the ratio between new and old counts) shows us how strikingly the counts for each prefix word have been reduced; the discount for the bigram want to is .39, while the discount for Chinese food is .10, a factor of 10!

One snarp change in counts and probabilities occurs because too much probability mass is moved to all the zeros. We could move a bit less mass by adding a fractional count rather than 1 (add-δ smoothing; (Lidstone, 1920; Johnson, 1932; Jeffreys, 1948)), but this method requires a method for choosing δ dynamically, results in an inappropriate discount for many counts, and turns out to give counts with poor variances. For these and other reasons (Gale and Church, 1994), we'll need better smoothing methods for N-grams like the ones we'll see in the next section.

← 4.3.2 Unknown Words: Open versus closed vocabulary tasks4.5.2 Good-Turing Discounting →