Hidden Markov Language Model
This post is a summary of the 5th assignment for the Fall 2018 Computational Linguistics course, with the task of using a Hidden Markov Model to calculate sentence probabilities.
Experimental Setup
- Split sentences by line.
- When computing the state transition matrix and the observation probability matrix, consider the part-of-speech tags of words in the training set.
- Ignore square brackets and phrase-level part-of-speech tags; after deduplication, 43 distinct part-of-speech tags are obtained as the hidden states.
- Ignore part-of-speech tags and square brackets, and count the words in the training, validation, and test sets, yielding 55,416 distinct words.
- Computation precision is set to 1000 decimal places.
Process of Computing Sentence Probability with a Hidden Markov Model
Using the example 迈向/v 充满/v 希望/n 的/u 新/a 世纪/n, the following processing is performed:
- Add
<bos>and<eos>to the beginning and end of the sentence, yielding the new sentence<bos>/<bos> 迈向/v 充满/v 希望/n 的/u 新/a 世纪/n <eos>/<eos> - State set
- Observation set
From the new sentence, the state transition matrix is obtained as follows:
| 0 | 1 | 0 | 0 | 0 | 0 | |
| 0 | 0.5 | 0 | 0.5 | 0 | 0 | |
| 0 | 0 | 0 | 0 | 1.0 | 0 | |
| 0 | 0 | 0.5 | 0 | 0 | 0.5 | |
| 0 | 0 | 0 | 1.0 | 0 | 0 | |
| 0.166 | 0.166 | 0.166 | 0.166 | 0.166 | 0.166 |
Similarly, the observation probability matrix is obtained as follows:
| 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 0.5 | 0.5 | 0 | 0 | 0 | 0 | 0 | |
| 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | |
| 0 | 0 | 0 | 0.5 | 0 | 0 | 0.5 | 0 | |
| 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
Since the beginning of a sentence is always , the initial state distribution .
This yields a Hidden Markov Model
Suppose the observation sequence after removing part-of-speech tags (which can be regarded as sentences in the validation and test sets) is <bos> 迈向 充满 希望 的 新 世纪 <eos>, which can be converted to the vector
Forward Algorithm
The forward algorithm can yield the probability of the observation sequence . Define the probability that at time the partial observation sequence is and the state is as the forward probability, denoted
The computation process is as follows: (1) Initialization
(2) Recursion, for
(3) Termination
After computation, the forward probability of the sequence is obtained as
Backward Algorithm
Similarly, the backward algorithm can also yield the probability of the observation sequence . The difference is that the initial state distribution is not needed at the beginning of the computation, but is instead introduced at the termination step.
Define the probability that, given the state is at time , the partial observation sequence from to is as the backward probability, denoted
(1) Initialization
(2) Recursion, for
(3) Termination
After computation, the backward probability of the sequence is obtained as , which matches the forward probability, verifying that the two probabilities are equal.
Based on the example computed above, one can construct the state transition matrix of part-of-speech tags from all training corpus, as well as the observation probability matrix from part-of-speech tags to words in the training, (stripped of part-of-speech tags) validation, and test sets (word frequencies in the training set are accumulated normally, while frequencies of newly appeared words in the validation and test sets are 0). The forward algorithm and the backward algorithm are then used to compute sentence probabilities.
Comparison and Analysis of N-gram Language Model Results
Hidden Markov Model Results
In this experiment, the forward algorithm and the backward algorithm were implemented and tested. Since the results produced by these two methods are identical, a single 1.txt was submitted. The following observations can be drawn from the submitted 1.txt:
- Overall, the sentence probabilities split by line are quite small, with average sentence probabilities on the order of to . Extremely small probabilities sometimes occur, for example on the order of .
- There are 2456 sentences with a probability of 0. Considering the computation precision is already very high (1000 decimal places places retained), the reason may lie in the fact that the observation probability matrix and the state transition matrix of the Hidden Markov Model are excessively sparse, with most elements being 0. The model has poor capability to handle state transitions that appear in the validation and test sets but not in the training set. Therefore, the cases where the final computed probability is 0 account for roughly half of the results.
In contrast, the n-gram language model experiment, which employed various smoothing methods, handles new words and low-frequency words in the validation and test sets well, and no sentence with a probability of 0 occurred.
Comparison of Hidden Markov Model Results and N-gram Language Model Results
Average Sentence Probability
|Method|Corpus|Average Sentence Probability|Sentence Probability Ranking| |:---:|:---:|:---:|:---:|:---:| |add_one_bigram|valid|4.23E-11|5| |add_one_unigram|valid|2.79E-07|3| |back_off_trigram|valid|1.06E-05|1| |hmm|valid|2.84E-09|4| |good_turing_bigram|valid|1.53E-13|6| |good_turing_unigram|valid|3.89E-07|2|
|Method|Corpus|Average Sentence Probability|Sentence Probability Ranking| |:---:|:---:|:---:|:---:|:---:| |add_one_bigram|test|1.03E-10|6| |add_one_unigram|test|8.36E-07|4| |back_off_trigram|test|1.01E-05|2| |hmm|test|1.16E-08|5| |good_turing_bigram|test|7.07E-03|1| |good_turing_unigram|test|1.36E-06|3|
If most words in a sentence have a relatively high probability, then the sentence probability obtained via (the n-gram approach) will also be high, and the differences in probability between different sentences will be more pronounced.
Therefore, from the table above, it can be seen that the average sentence probability computed by the Hidden Markov Model is at a medium-to-low level, ranking 4th (validation corpus) and 5th (test corpus) respectively. This reflects, from a side perspective, that the Hidden Markov Model has average adaptability to the validation and test corpus, or in other words, the Hidden Markov Model built from the training corpus does not have high confidence when predicting sentences in the validation and test corpus that did not appear in the training set.
Average Rank of Probability Difference
For each sentence in the set, 5 n-gram model approaches compute probabilities respectively. Each is subtracted from the Hidden Markov Model probability and the absolute value is taken (). These differences are then ranked to obtain the ranks . The ranks for all sentences are then averaged for each n-gram model as to obtain the average rank of probability difference compared to the Hidden Markov Model, as shown in the two tables below:
| Method | Corpus | Average Rank of Probability Difference |
|---|---|---|
| add_one_bigram | valid | 1.722009569 |
| add_one_unigram | valid | 1.340669856 |
| back_off_trigram | valid | 1.758851675 |
| good_turing_bigram | valid | 2.864114833 |
| good_turing_unigram | valid | 2.314354067 |
| Method | Corpus | Average Rank of Probability Difference |
|---|---|---|
| add_one_bigram | test | 1.667934783 |
| add_one_unigram | test | 1.43423913 |
| back_off_trigram | test | 1.842934783 |
| good_turing_bigram | test | 2.773369565 |
| good_turing_unigram | test | 2.281521739 |
In summary, the probabilities obtained by the add_one_unigram method are closest to those obtained by the Hidden Markov Model, followed by the add_one_bigram method, then the back_off_trigram method, then the good_turing_unigram method, while the probabilities obtained by good_turing_bigram are the most distant from those obtained by the Hidden Markov Model.
References
[1] Li Hang (2012). Statistical Learning Methods. Tsinghua University Press, Beijing.