11/08/1999 JHU CS 600.465/Jan Hajic 1
Introduction to Natural Language Processing (600.465)
Maximum Entropy
Dr. Jan HajičCS Dept., Johns Hopkins Univ.
[email protected]/~hajic
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 2
Maximum Entropy??• Why maximum entropy??• Recall: so far, we always “liked”
– minimum entropy... = minimum uncertainty = maximum predictive power
.... distributions– always: relative to some “real world” data– always: clear relation between the data, model and
parameters: e.g., n-gram language model
• This is still the case! But...
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 3
The Maximum Entropy Principle
• Given some set of constraints (“relations”, “facts”), which must hold (i.e., we believe they correspond to the real world we model):What is the best distribution among those available?
• Answer: the one with maximum entropy(of such distributions satisfying the constraints)
• Why? ...philosophical answer:– Occam’s razor; Jaynes, ...:
• make things as simple as possible, but not simpler;• do not pretend you know something you don’t
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 4
Example
• Throwing the “unknown” die– do not know anything ® we should assume a fair die
(uniform distribution ~ max. entropy distribution)
• Throwing unfair die– we know: p(4) = 0.4, p(6) = 0.2, nothing else– best distribution? – do not assume anything
about the rest:
• What if we use instead:
1 2 3 4 5 60.1 0.1 0.1 0.4 0.1 0.2
1 2 3 4 5 60.25 0.05 0.05 0.4 0.05 0.2 ?
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 5
Using Non-Maximum Entropy Distribution
• ME distribution: p:
• Using instead: q:
• Result depends on the real world:– real world ~ our constraints (p(4) = 0.4, p(6) = 0.2), everything
else no specific constraints:• our average error: D(q||p) [recall: Kullback-Leibler distance]
– real world ~ orig. constraints + p(1) = 0.25:• q is best (but hey, then we should have started with all 3 constraints!)
1 2 3 4 5 60.1 0.1 0.1 0.4 0.1 0.2
1 2 3 4 5 60.25 0.05 0.05 0.4 0.05 0.2
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 6
Things in Perspective: n-gram LM
• Is an n-gram model a ME model?– yes if we believe that trigrams are the all and only
constraints • trigram model constraints: p(z|x,y) = c(x,y,z)/c(x,y)
– no room for any “adjustments”• like if we say p(2) = 0.7, p(6) = 0.3 for a throwing die
• Accounting for the apparent inadequacy:– smoothing– ME solution: (sort of) smoothing “built in”
• constraints from training, maximize entropy on training + heldout
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 7
Features and Constraints• Introducing...
– binary valued selector functions (“features”):• fi(y,x) Î {0,1}, where
– y Î Y (sample space of the event being predicted, e.g. words, tags, ...), – x Î X (space of contexts, e.g. word/tag bigrams, unigrams, weather
conditions, of - in general - unspecified nature/length/size)
– constraints:• Ep(fi(y,x)) = E’(fi(y,x)) (= empirical expectation)
• recall: expectation relative to distribution p: Ep(fi) = Σy,xp(x,y)fi(y,x)
• empirical expectation: E’(fi) = Σy,xp’(x,y)fi(y,x) = 1/|T| Σt=1..Tfi(yt,xt)• notation: E’(fi(y,x)) = di: constraints of the form Ep(fi(y,x)) = di
unusual!
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 8
Additional Constraint (Ensuring Probability Distribution)
• The model’s p(y|x) should be probability distribution:– add an “omnipresent” feature f0(y,x) = 1 for all y,x– constraint: Ep(f0(y,x)) = 1
• Now, assume:– We know the set S = {fi(y,x), i=0..N} (|S| = N+1)– We know all the constraints
• i.e. a vector di, one for each feature, i=0..N
• Where are the parameters?– ...we do not even know the form of the model yet
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 9
The Model
• Given the constraints, what is the form of the model which maximizes the entropy of p?
• Use Lagrangian Multipliers:– minimizing some function φ(z) in the presence of N
constraints gi(z) = di means to minimize
φ(x) - Σi=1..Nλi(gi(x) - di) (w.r.t. all λi and x)– our case, minimize
A(p) = -H(p) - Σi=1..Nλi(Ep(fi(y,x)) - di) (w.r.t. all λi and p!)– i.e. φ(z) = -H(p), gi(z)= Ep(fi(y,x)) (variable z ~ distribution p)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 10
Loglinear (Exponential) Model
• Maximize: for p, derive (partial derivation) and solve A’(p) = 0:δ[−H(p) − Σi=0..Nλi(Ep(fi(y,x)) - di)]/δp = 0
δ[ Σ p log(p) − Σi=0..Nλi((Σ p fi) - di)]/δp = 0...
1 + log(p) − Σi=0..Nλi fi = 0
1 + log(p) = Σi=1..Nλi fi + λ0
p = eΣi=1..Nλi fi + λ0 - 1
• p(y,x) = (1/Z) eΣi=1..Nλifi(y,x) (Z = e 1-λ0, the normalization factor)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 11
Maximizing the Lambdas: Setup• Model: p(y,x) = (1/Z) eΣi=1..Nλifi(y,x)
• Generalized Iterative Scaling (G.I.S.)– obeys form of model & constraints:
• Ep(fi(y,x)) = di
– G.I.S. needs, in order to work, ∀y,x Σi=1..N fi(y,x) = C• to fulfill, define additional constraint:
• fN+1(y,x) = Cmax - Σi=1..N fi(y,x), where Cmax = maxx,y Σi=1..N fi(y,x)
– also, approximate (because ΣxÎAll contexts is not (never) feasible)
• Ep(fi) = Σy,xp(x,y)fi(y,x) ≅ 1/|T| Σt=1..TΣyÎYp(y|xt)fi(y,xt) (use (y,x)=p(y|x)p’(x), where p’(x) is empirical i.e. from data T)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 12
Generalized Iterative Scaling
• 1. Initialize λi(1) (any values, e.g. 0), compute di, i=1..N+1
• 2. Set iteration number n to 1.• 3. Compute current model distribution expected values
of all the constraint expectations Ep(n)(fi) (based on p(n)(y|xt))
– [pass through data, see previous slide; at each data position t, compute p(n)(y,xt), normalize]
• 4. Update λi(n+1) = λi
(n) + (1/C) log(di/Ep(n)(fi))• 5. Repeat 3.,4. until convergence.
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 13
Comments on Features
• Advantage of “variable” (~ not fixed) context in f(y,x):– any feature o.k. (examples mostly for tagging):
• previous word’s part of speech is VBZ or VB or VBP, y is DT• next word: capitalized, current: “.”, and y is a sentence break (SB detect)• y is MD, and the current sentence is a question (last word: question mark)• tag assigned by a different tagger is VBP, and y is VB• it is before Thanksgiving and y is “turkey” (Language modeling)• even (God forbid!) manually written rules, e.g. y is VBZ and there is ...
– remember, the predicted event plays a role in a feature:• also, a set of events: f(y,x) is true if y is NNS or NN, and x is ...• x can be ignored as well (“unigram” features)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 14
Feature Selection• Advantage:
– throw in many features • typical case: specify templates manually (pool of features P), fill in
from data, possibly add some specific manually written features• let the machine select• Maximum Likelihood ~ Minimum Entropy on training data• after, of course, computing the λi’s using the MaxEnt algorithm
• Naive (greedy of course) algorithm:– start with empty S, add feature at a time (MLE after ME)– too costly for full computation (|S| x |P| x |ME-time|)– Solution: see Berger & DellaPietras
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 15
References• Manning-Schuetze:
– Section 16.2
• Jelinek:– Chapter 13 (includes application to LM)– Chapter 14 (other applications)
• Berger & DellaPietras in CL, 1996, 1997– Improved Iterative Scaling (does not need Σi=1..N fi(y,x) = C)– “Fast” Feature Selection!
• Hildebrand, F.B.: Methods of Applied Math., 1952
11/08/1999 JHU CS 600.465/Jan Hajic 16
Introduction to Natural Language Processing (600.465)
Maximum Entropy Tagging
Dr. Jan HajièCS Dept., Johns Hopkins Univ.
[email protected]/~hajic
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 17
The Task, Again
• Recall:– tagging ~ morphological disambiguation– tagset VT ⊂ (C1,C2,...Cn)
• Ci - morphological categories, such as POS, NUMBER, CASE, PERSON, TENSE, GENDER, ...
– mapping w ® {t ÎVT} exists• restriction of Morphological Analysis: A+ ® 2(L,C1,C2,...,Cn)
where A is the language alphabet, L is the set of lemmas
– extension to punctuation, sentence boundaries (treated as words)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 18
Maximum Entropy Tagging Model
• Generalp(y,x) = (1/Z) eΣi=1..Nλifi(y,x)
Task: find λi satisfying the model and constraints • Ep(fi(y,x)) = di
where • di = E’(fi(y,x)) (empirical expectation i.e. feature frequency)
• Taggingp(t,x) = (1/Z) eΣi=1..Nλifi(t,x) (λ0 might be extra: cf. μ in AR)
• t Î Tagset,• x ~ context (words and tags alike; say, up to three positions R/L)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 19
Features for Tagging
• Context definition– two words back and ahead, two tags back, current word:
• xi = (wi-2,ti-2,wi-1,ti-1,wi,wi+1,wi+2)
– features may ask any information from this window• e.g.:
– previous tag is DT– previous two tags are PRP$ and MD, and the following word is “be”– current word is “an”– suffix of current word is “ing”
• do not forget: feature also contains ti, the current tag:– feature #45: suffix of current word is “ing” & the tag is VBG Û f45 = 1
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 20
Feature Selection• The PC1 way (see also yesterday’s class):
– (try to) test all possible feature combinations• features may overlap, or be redundant; also, general or specific
- impossible to select manually
– greedy selection:• add one feature at a time, test if (good) improvement:
– keep if yes, return to the pool of features if not
– even this is costly, unless some shortcuts are made• see Berger & DPs for details
• The other way: – use some heuristic to limit the number of features
• 1Politically (or, Probabilistically-stochastically) Correct
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 21
Limiting the Number of Features
• Always do (regardless whether you’re PC or not):– use contexts which appear in the training data (lossless
selection)
• More or less PC, but entails huge savings (in the number of features to estimate λi weights for):– use features appearing only L-times in the data (L ~ 10)– use wi-derived features which appear with rare words only– do not use all combinations of context (this is even “LC1”) – but then, use all of them, and compute the λi only once
using the Generalized Iterative Scaling algorithm• 1Linguistically Correct
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 22
Feature Examples (Context)
• From A. Ratnaparkhi (EMNLP, 1996, UPenn)– ti = T, wi = X (frequency c > 4):
• ti = VBG, wi = selling
– ti = T, wi contains uppercase char (rare): • ti = NNP, tolower(wi) ¹ wi
– ti = T, ti-1 = Y, ti-2 = X:• ti = VBP, ti-2 = PRP, ti-1 = RB
• Other examples of possible features:– ti = T, tj is X, where j is the closest left position where Y
• ti = VBZ, tj = NN, Y Û tj Î {NNP, NNS, NN}
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 23
Feature Examples (Lexical/Unknown)
• From AR:– ti = T, suffix(wi)= X (length X < 5):
• ti = JJ, suffix(wi) = eled (traveled, leveled, ....)
– ti = T, prefix(wi)= X (length X < 5): • ti = JJ, prefix(wi) = well (well-done, well-received,...)
– ti = T, wi contains hyphen: • ti = JJ, ‘-’ in wi (open-minded, short-sighted,...)
• Other possibility, for example:– ti = T, wi contains X:
• ti = NounPl, wi contains umlaut (ä,ö,ü) (Wörter, Länge,...)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 24
“Specialized” Word-based Features
• List of words with most errors (WSJ, Penn Treebank):– about, that, more, up, ...
• Add “specialized”, detailed features:– ti = T, wi = X, ti-1 = Y, ti-2 = Z:
• ti = IN, wi = about, ti-1 = NNS, ti-2 = DT
– possible only for relatively high-frequency words
• Slightly better results (also, inconsistent [test] data)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 25
Maximum Entropy Tagging: Results
• For details, see AR• Base experiment (133k words, < 3% unknown):
– 96.31% word accuracy
• Specialized features added:– 96.49% word accuracy
• Consistent subset (training + test)– 97.04% word accuracy (97.13% w/specialized features)
• This is the best result on WSJ so far.
11/08/1999 JHU CS 600.465/Jan Hajic 26
Introduction to Natural Language Processing (600.465)
Feature-Based Tagging
Dr. Jan HajièCS Dept., Johns Hopkins Univ.
[email protected]/~hajic
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 27
The Task, Again
• Recall:– tagging ~ morphological disambiguation– tagset VT ⊂ (C1,C2,...Cn)
• Ci - morphological categories, such as POS, NUMBER, CASE, PERSON, TENSE, GENDER, ...
– mapping w ® {t ÎVT} exists• restriction of Morphological Analysis: A+ ® 2(L,C1,C2,...,Cn)
where A is the language alphabet, L is the set of lemmas
– extension to punctuation, sentence boundaries (treated as words)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 28
Feature Selection Problems
• Main problem with Maximum Entropy [tagging]:– Feature Selection (if number of possible features is in
the hundreds of thousands or millions)– No good way
• best so far: Berger & DP’s greedy algorithm• heuristics (cutoff based: ignore low-count features)
• Goal:– few but “good” features (“good” ~ high predictive power
~ leading to low final cross entropy)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 29
Feature-based Tagging
• Idea:– save on computing the weights (λi)
• are they really so important?
– concentrate on feature selection
• Criterion (training):– error rate (~ accuracy; borrows from Brill’s tagger)
• Model form (probabilistic - same as for Maximum Entropy):
p(y|x) = (1/Z(x)) eΣi=1..Nλifi(y,x)
® Exponential (or Loglinear) Model
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 30
Feature Weight (Lambda) Approximation
• Let Y be the sample space from which we predict (tags in our case), and fi(y,x) a b.v. feature
• Define a “batch of features” and a “context feature”:B(x) = {fi; all fi’s share the same context x} fB(x)(x’) = 1 Ûdf x ⊂ x’ (x is part of x’)
• in other words, holds wherever a context x is found
• Example: f1(y,x) = 1 Ûdf y=JJ, left tag = JJ f2(y,x) = 1 Ûdf y=NN, left tag = JJ
B(left tag = JJ) = {f1, f2} (but not, say, [y=JJ, left tag = DT])
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 31
Estimation• Compute:
p(y|B(x)) = (1/Z(B(x))) Σd=1..|T|δ(yd,y)fB(x)(xd)• frequency of y relative to all places where any of B(x) features holds
for some y; Z(B(x)) is the natural normalization factor • Z(B(x)) = Σd=1..|T| fB(x)(xd)
“compare” to uniform distribution:• α(y,B(x)) = p(y|B(X)) / (1 / |Y|)
α(y,B(x)) > 1 for p(y|B(x)) better than uniform; and vice versa
• If fi(y,x) holds for exactly one y (in a given context x),then we have 1:1 relation between α(y,B(x)) and fi(y,x) from B(x) and λi = log (α(y,B(x))) NB: works in constant time
independent of λj, j¹ i
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 32
What we got
• Substitute:p(y|x) = (1/Z(x)) eΣi=1..Nλifi(y,x) =
= (1/Z(x)) Πi=1..Nα(y,B(x))fi(y,x)
= (1/Z(x)) Πi=1..N (|Y| p(y|B(x)))fi(y,x)
= (1/Z’(x)) Πi=1..N (p(y|B(x)))fi(y,x)
= (1/Z’(x)) ΠB(x’); x’ ⊂ x p(y|B(x’))... Naive Bayes (independence assumption)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 33
The Reality
• take advantage of the exponential form of the model (do not reduce it completely to naive Bayes):– vary α(y,B(x)) up and down a bit (quickly)
• captures dependence among features
– recompute using “true” Maximum Entropy• the ultimate solution
– combine feature batches into one, with new α(y,B(x’))• getting very specific features
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 34
Search for Features• Essentially, a way to get rid of unimportant features:
– start with a pool of features extracted from full data– remove infrequent features (small threshold, < 2)– organize the pool into batches of features
• Selection from the pool P:– start with empty S (set of selected features)– try all features from the pool, compute α(y,B(x)), compute
error rate over training data.– add the best feature batch permanently; stop when no
correction made [complexity: |P| x |S| x |T|]
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 35
Adding Features in Blocks, Avoiding the Search for the Best
• Still slow; solution: add ten (5,20) best features at a time, assuming they are independent (i.e., the next best feature would change the error rate the same way as if no intervening addition of a feature is made).
• Still slow [(|P| x |S| x |T|)/10, or 5, or 20]; solution:• Add all features improving the error rate by a certain
threshold; then gradually lower the threshold down to the desired value; complexity [|P| x log|S| x |T|] if
• threshold(n+1) = threshold(n) / k, k > 1 (e.g. k = 2)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 36
Types of Features
• Position:– current– previous, next– defined by the closest word with certain major POS
• Content:– word (w), tag(t) - left only, “Ambiguity Class” (AC) of a
subtag (POS, NUMBER, GENDER, CASE, ...)
• Any combination of position and content• Up to three combinations of (position,content)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 37
Ambiguity Classes (AC)
• Also called “pseudowords” (MS, for word sense disambiguationi task), here: “pseudotags”
• AC (for tagging) is a set of tags (used as an indivisible token).– Typically, these are the tags assigned by a morphology to a
given word:• MA(books) [restricted to tags] = { NNS, VBZ }:
AC = NNS_VBZ
• Advantage: deterministic® looking at the ACs (and words, as before) to the right allowed
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 38
Subtags
• Inflective languages: too many tags ® data sparseness• Make use of separate categories (remember morphology):
– tagset VT ⊂ (C1,C2,...Cn)• Ci - morphological categories, such as POS, NUMBER, CASE,
PERSON, TENSE, GENDER, ...
• Predict (and use for context) the individual categories• Example feature:
– previous word is a noun, and current CASE subtag is genitive
• Use separate ACs for subtags, too (ACPOS = N_V)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 39
Combining Subtags
• Apply the separate prediction (POS, NUMBER) to – MA(books) = { (Noun, Pl), (VerbPres, Sg)}
• Now what if the best subtags are– Noun for POS– Sg for NUMBER
• (Noun, Sg) is not possible for books
• Allow only possible combinations (based on MA)• Use independence assumption (Tag = (C1, C2, ..., Cn)):
(best) Tag = argmaxTag Î MA(w) Πi=1..|Categories| p(Ci|w,x)
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 40
Smoothing
• Not needed in general (as usual for exponential models)– however, some basic smoothing has an advantage of
not learning unnecessary features at the beginning– very coarse: based on ambiguity classes
• assign the most probable tag for each AC, using MLE• e.g. NNS for AC = NNS_VBZ
– last resort smoothing: unigram tag probability– can be even parametrized from the outside– also, needed during training
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 41
Overtraining
• Does not appear in general– usual for exponential models– does appear in relation to the training curve:
– but does not go down until very late in the training (singletons do cause overtraining)
training accuracy
test accuracy
11/08/1999 JHU CS 600.465/ Intro to NLP/Jan Hajic 42
Results• Training: Orwell 1984 (EU, MULTEXT-EAST)
– translation into many languages, manually annotated by tags and lexical information; ~100k training words
• Test: 20k words from the end of Orwell’s 1984• Results:
Language Ambiguity W+Punct Acc. W only Acc. Features LearntEnglish 38.7 98.39 98.03 1088Romanian 40.0 97.35 96.71 3055Czech 46.0 92.96 90.23 2398Estonian 40.2 95.95 94.19 2412Hungarian 26.3 97.71 96.94 1853Slovene 38.0 93.18 91.29 3426