SLIDE 1
Efficient ¡Inference ¡for Mul0nomial ¡Mixed ¡Membership Models
David ¡Mimno UMass, ¡Amherst
SLIDE 2 Summary
- How ¡fast ¡can ¡we ¡make ¡Gibbs ¡sampling?
- How ¡liGle ¡memory ¡can ¡we ¡use?
Goal: ¡small ¡collec0ons ¡should ¡be ¡fast, large ¡collec0ons ¡should ¡be ¡possible
SLIDE 3 Big ¡collec0ons! ¡ ¡Lots ¡of ¡topics!
SLIDE 4
Why ¡not ¡just ¡parallelize?
Big ¡computa0on Lots ¡of ¡smaller computa0ons
SLIDE 5
Why ¡not ¡just ¡parallelize?
Small ¡computa0on Lots ¡of ¡really ¡small computa0ons
Be ¡smart, ¡then ¡parallelize
SLIDE 6
Gibbs ¡Sampling ¡for ¡Topic ¡Models
dog cat feline hound dog dog horse equine 2 2 1 1 Doc ¡A Doc ¡B
SLIDE 7
Document-‑topic ¡sta0s0cs
dog cat feline hound dog dog horse equine
Nt|d
2 2 B 2 2 A 2 1
Doc ¡A Doc ¡B 2 2 1 1 docs topics
SLIDE 8
Topic-‑word ¡sta0s0cs
1 1 2 1 1 1 3 feline hound horse cat dog
Nw|t
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B 2 2 1 1 Word ¡types Topics
SLIDE 9
score(t ¡| ¡Nt|d, ¡Nw|t) ¡=
SLIDE 10
For ¡each ¡token,
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B
for ¡each ¡possible ¡topic ¡[0, ¡1, ¡2],
2 2 1 1
SLIDE 11
For ¡each ¡token,
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B
for ¡each ¡possible ¡topic ¡[0, ¡1, ¡2], ¡ ¡compute
2 2 1 1
score(0 ¡| ¡N0|A, ¡Ndog|0)
SLIDE 12
For ¡each ¡token,
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B
for ¡each ¡possible ¡topic ¡[0, ¡1, ¡2], ¡ ¡compute
2 2 1 1
score(0 ¡| ¡N0|A, ¡Ndog|0) score(1 ¡| ¡N1|A, ¡Ndog|1)
SLIDE 13
For ¡each ¡token,
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B
for ¡each ¡possible ¡topic ¡[0, ¡1, ¡2], ¡ ¡compute
2 2 1 1
score(0 ¡| ¡N0|A, ¡Ndog|0) score(1 ¡| ¡N1|A, ¡Ndog|1) score(2 ¡| ¡N2|A, ¡Ndog|2)
SLIDE 14
Add ¡up ¡the ¡scores
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B 2 2 1 1
score(0 ¡| ¡N0|A, ¡Ndog|0) + score(1 ¡| ¡N1|A, ¡Ndog|1) + score(2 ¡| ¡N2|A, ¡Ndog|2) Z ¡= ¡
SLIDE 15
Sample ¡a ¡new ¡topic
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B u ¡= ¡rand() ¡* ¡Z 2 2 1 1 Sample Return ¡t=0
SLIDE 16
For ¡each ¡token,
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B
for ¡each ¡possible ¡topic ¡[0, ¡1, ¡2],
2 1 1
SLIDE 17 Summary ¡so ¡far
– For ¡each ¡possible ¡topic ¡t ¡calculate ¡score(t ¡| ¡…)
- Add ¡up ¡scores ¡to ¡normalizing ¡constant
Z ¡= ¡∑t ¡score(t)
- Sample ¡u ¡~ ¡U(0, ¡Z) ¡and ¡return ¡the
corresponding ¡topic ¡t.
Performance ¡is ¡dominated ¡by ¡calcula0on ¡of ¡Z
SLIDE 18
The ¡normalizing ¡constant
SLIDE 19
The ¡normalizing ¡constant
SLIDE 20
The ¡normalizing ¡constant
SLIDE 21
The ¡normalizing ¡constant
SLIDE 22 The ¡normalizing ¡constant
Token-‑specific Document-‑specific Independent
document
SLIDE 23
The ¡normalizing ¡constant
SLIDE 24 Sta0s0cs ¡are ¡sparse
- Nt|d ¡: ¡10-‑20%
- mostly ¡zeros
- Nw|t ¡: ¡< ¡5%
- almost ¡all ¡zeros
SLIDE 25
Add ¡up ¡the ¡scores
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B 2 2 1 1
Z
SLIDE 26
Add ¡up ¡the ¡scores, ¡in ¡blocks
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B t ¡: ¡Nw|t ¡> ¡0 t ¡: ¡Nt|d ¡> ¡0 ∀ t 2 2 1 1
SLIDE 27
Add ¡up ¡the ¡scores, ¡in ¡blocks
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B Zword-‑specific Zdocument-‑specific Zsmoothing 2 2 1 1
SLIDE 28 Add ¡up ¡some ¡blocks, ¡cache ¡others
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B The ¡size ¡of ¡this ¡block ¡is ¡almost ¡constant Recalculate ¡the ¡word-‑specific block ¡for ¡every ¡token change ¡at ¡most ¡two from ¡each
2 2 1 1
SLIDE 29
Add ¡up ¡some ¡blocks, ¡cache ¡others
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B 2 2 1 1
SLIDE 30
Add ¡up ¡some ¡blocks, ¡cache ¡others
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B 2 2 1 1
SLIDE 31
Add ¡up ¡some ¡blocks, ¡cache ¡others
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B Worst ¡case: ¡we ¡might ¡have ¡to ¡loop ¡over ¡all ¡topics 2 2 1 1
SLIDE 32 Summary ¡so ¡far
- We ¡can ¡store ¡most ¡of ¡the ¡computa0on ¡to
calculate ¡Z ¡from ¡token ¡to ¡token.
- We ¡can ¡sample ¡exactly ¡from ¡the ¡same
distribu0on, ¡with ¡a ¡new ¡map ¡from ¡(0, ¡Z) ¡to topics. Fast ¡itera0on ¡over ¡{t ¡: ¡Nw|t ¡> ¡0} ¡is ¡cri.cal
SLIDE 33
Topic-‑word ¡sta0s0cs
1 1 2 1 1 1 3 feline hound horse cat dog
Nw|t
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B 2 2 1 1 Word ¡types Topics
SLIDE 34
Word-‑topic ¡sta0s0cs ¡transpose
Nw|t
dog cat feline hound dog dog horse equine Doc ¡A Doc ¡B
1 feline 1 hound 1 horse 1 cat 3 dog 2 1
2 2 1 1 Word ¡types Topics
SLIDE 35
Representa0ons ¡of ¡Nw|t: ¡arrays
int[] typeTopicCounts = new int[T]; Topics ¡index ¡array ¡posi0ons.
2 1 3 3 1
Most ¡entries ¡are ¡zero.
SLIDE 36 Integer ¡HashMaps
Faster, ¡but…
- Complicated
- Not ¡much ¡memory ¡improvement
- Adds ¡dependencies ¡on ¡external
libraries ¡(trove, ¡fastu0l). TIntIntHashMap typeTopicCounts = new TIntIntHashMap(); (giant ¡black ¡box)
1 3 3
SLIDE 37
Encoded ¡integer ¡arrays
int[] typeTopicCounts = new int[Nw]
(1, ¡3) (3, ¡0) 1
1 3 int32 count ¡bits topic ¡bits index ¡is ¡meaningless all ¡informa0on ¡is ¡in ¡array ¡values
Huge ¡speedup, ¡2x ¡faster ¡than ¡HashMaps
SLIDE 38
Example
1 96 … … … … 9 24 3 “dog” … 83 15 8
100 ¡topics, ¡so ¡4x100 ¡bytes ¡= ¡400
SLIDE 39
Example
1 96 … … … … 9 24 3 “dog” … 83 15 8 (1, ¡96) (3, ¡8) (9, ¡83) (24, ¡15) 3 2 1
4 ¡non-‑zero ¡topics, ¡4 ¡x ¡4 ¡bytes ¡= ¡16!
SLIDE 40
Example
1 96 … … … … 9 24 3 “dog” … 83 15 8 1<<7 ¡+ ¡96 3<<7 ¡+ ¡8 9<<7 ¡+ ¡83 24<<7 ¡+ ¡15 3 2 1
100 ¡topics, ¡27 ¡> ¡100
SLIDE 41
Basic ¡Opera0ons: ¡itera0on
(1, ¡96) (3, ¡8) (9, ¡83) (24, ¡15) 3 2 1
t = v & 0x1111111 Nw|t = v >> 7
SLIDE 42
Basic ¡Opera0ons: ¡itera0on
(1, ¡96) (3, ¡8) (9, ¡83) (24, ¡15) 3 2 1
Zword-‑specific ¡=
SLIDE 43
Basic ¡Opera0ons: ¡itera0on
(1, ¡96) (3, ¡8) (9, ¡83) (24, ¡15) 3 2 1
Zword-‑specific ¡=
SLIDE 44
Basic ¡Opera0ons: ¡itera0on
(1, ¡96) (3, ¡8) (9, ¡83) (24, ¡15) 3 2 1
Zword-‑specific ¡=
SLIDE 45
Basic ¡Opera0ons: ¡increment
(1, ¡96) (3, ¡8) (9, ¡83) (24, ¡15) 3 2 1
Nw|83++
SLIDE 46
Basic ¡Opera0ons: ¡increment
(1, ¡96) (3, ¡8) (9, ¡83) (24, ¡15) 3 2 1
Nw|83++
v = (9+1)<<7 + 83
SLIDE 47
Basic ¡Opera0ons: ¡increment
(1, ¡96) (3, ¡8) (10, ¡83) (24, ¡15) 3 2 1
Nw|83++
v = (9+1)<<7 + 83
SLIDE 48
Basic ¡Opera0ons: ¡increment
(1, ¡96) (1, ¡83) (1, ¡15) 3 2 1
Nw|83++
v = (1+1)<<7 + 83
SLIDE 49
Basic ¡Opera0ons: ¡increment
(1, ¡96) (1, ¡83) (1, ¡15) 3 2 1
Nw|83++
v = (1+1)<<7 + 83
SLIDE 50
Basic ¡Opera0ons: ¡increment
(1, ¡96) (2, ¡83) (1, ¡15) 3 2 1
Nw|83++
SLIDE 51
Basic ¡Opera0ons: ¡increment
(1, ¡96) (1, ¡15) (2, ¡83) 3 2 1
Nw|83++
SLIDE 52
Basic ¡Opera0ons: ¡decrement
(1, ¡96) (1, ¡83) (1, ¡15) 3 2 1
Nw|83-‑-‑
v = (1-1)<<7 + 83
SLIDE 53
Basic ¡Opera0ons: ¡decrement
(1, ¡96) (1, ¡15) 3 2 1
Nw|83-‑-‑
v = (1-1)<<7 + 83
SLIDE 54
Basic ¡Opera0ons: ¡decrement
(1, ¡96) (1, ¡15) 3 2 1
Nw|83-‑-‑
SLIDE 55 Basic ¡Opera0ons
– linear ¡in ¡number ¡of ¡non-‑zero ¡topics
- Increment ¡and ¡decrement:
– theore0cally ¡the ¡same ¡as ¡iterate, ¡but ¡usually constant
SLIDE 56 Memory ¡alloca0on
– Count ¡total ¡occurrences ¡of ¡each ¡word ¡type: ¡Nw – For ¡each ¡word ¡type, ¡allocate ¡int[min(T, Nw)]
– For ¡each ¡type ¡allocate ¡int[min(T, Nw)/k], increase ¡memory ¡as ¡needed ¡(or ¡not)
SLIDE 57
Standard ¡alloca0on
“dog” “the” “llama” … Topics
SLIDE 58
Conserva0ve ¡alloca0on
“dog” “the” “llama” … Topics “llama” ¡occurs ¡only ¡twice ¡in ¡corpus, ¡so at ¡most ¡two ¡topics
SLIDE 59
Huge ¡savings: ¡0me ¡and ¡memory
SLIDE 60
Huge ¡savings: ¡0me ¡and ¡memory
SLIDE 61 Large-‑scale ¡Polylingual ¡Topics
- Wikipedias ¡in ¡40 ¡languages ¡(AR ¡to ¡ZH)
(Ar0cles ¡linking ¡to ¡same ¡English ¡page ¡share ¡P(t|d) ¡)
- 1,200,000 ¡English ¡headwords
- 7,000,000 ¡ar0cles
- 300,000,000 ¡tokens
- 4,000,000 ¡dis0nct ¡word ¡types
– Drop ¡rare ¡words ¡(< ¡5 ¡tokens) – Drop ¡common ¡words ¡(> ¡5% ¡of ¡docs)
One ¡(1) ¡CPU
SLIDE 62 Thanks!
Efficient ¡Methods ¡for ¡Topic ¡Model ¡Inference ¡on ¡Streaming Document ¡Collec.ons. ¡Limin ¡Yao, ¡David ¡Mimno, Andrew ¡McCallum. ¡KDD ¡2009. ¡(See ¡sec0on ¡3.4)
hGp://mallet.cs.umass.edu (Implemented ¡in ¡cc.mallet.topics.WorkerRunnable)
- 40 ¡language ¡Wikipedia ¡model:
hGp://christo.cs.umass.edu/wiki40