-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdmmclust.go
327 lines (277 loc) · 7.62 KB
/
dmmclust.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package dmmclust
import (
"math/rand"
"sync"
"github.com/pkg/errors"
randomkit "gorgonia.org/randomkit"
)
// ScoringFn is any function that can take a document and return the probabilities of it existing in those clusters
type ScoringFn func(doc Document, docs []Document, clusters []Cluster, conf Config) []float64
// Sampler is anything that can generate a index based on the given probability
type Sampler interface {
Sample([]float64) int
}
// Config is a struct that configures the running of the algorithm.
type Config struct {
// Maximum number of clusters expected
K int
// Vocabulary is the size of the vocabulary
Vocabulary int
// Number of iterations
Iter int
// Probability that controls whether a student will join an empty table
Alpha float64
// Betweenness affinity
Beta float64
// Score is a scoring function that will be used
Score ScoringFn
// Sampler is the sampler function
Sampler Sampler
}
// valid checks that the config for errors
func (c *Config) valid() error {
if c.Score == nil {
return errors.New("Expected Score to not be nil")
}
if c.Sampler == nil {
return errors.New("Expected Sampler to not be nil")
}
return nil
}
// TokenSet is a vector of word IDs for a document.
// Depending on algorithm, it may be uniquified using a set function that does not preserve order
type TokenSet []int
func (ts TokenSet) IDs() []int { return []int(ts) }
func (ts TokenSet) Len() int { return len(ts) }
// Document is anything that can return a TokenSet
type Document interface {
// IDs returns a list of word IDs. It's preferable to return an ordered list, rather than a uniquified set.
IDs() []int
// Len returns the number of words in the document
Len() int
}
// Cluster is a representation of a cluster. It doesn't actually store any of the data, only the metadata of the cluster
type Cluster struct {
id int
docs, words int
dist distro
}
func (c *Cluster) addDoc(doc Document) {
c.docs++
c.words += doc.Len()
if c.dist == nil {
c.dist = make(distro)
}
for _, tok := range doc.IDs() {
c.dist[tok]++
}
}
func (c *Cluster) removeDoc(doc Document) {
c.docs--
c.words -= doc.Len()
for _, tok := range doc.IDs() {
c.dist[tok]--
}
}
// ID returns the ID of the cluster. This is not set or used until the results are returned
func (c *Cluster) ID() int { return c.id }
// Docs returns the number of documents in the cluster
func (c *Cluster) Docs() int { return c.docs }
// Freq returns the frequency of the ith word
func (c *Cluster) Freq(i int) float64 { return c.dist[i] }
// Wordcount returns the number of words in the cluster
func (c *Cluster) Wordcount() int { return c.words }
// Words returns the word IDs in the cluster
func (c *Cluster) Words() []int {
retVal := make([]int, 0, len(c.dist))
for k := range c.dist {
retVal = append(retVal, k)
}
return retVal
}
// FindClusters is the main function to find clusters.
func FindClusters(docs []Document, conf Config) ([]Cluster, error) {
if err := conf.valid(); err != nil {
return nil, err
}
state := make([]Cluster, conf.K)
probs := make([]float64, conf.K)
for i := range probs {
probs[i] = 1.0 / float64(conf.K)
}
// initialize the clusters
dz := make([]int, len(docs))
for i, doc := range docs {
// randomly chuck docs into a cluster
z := conf.Sampler.Sample(probs)
dz[i] = z
clust := &state[z]
clust.addDoc(doc)
}
clusterCount := conf.K
for i := 0; i < conf.Iter; i++ {
var transfers int
for j, doc := range docs {
// remove from old cluster
old := dz[j]
clust := &state[old]
clust.removeDoc(doc)
// draw sample from distro to find new cluster
p := conf.Score(doc, docs, state, conf)
z2 := conf.Sampler.Sample(p)
// transfer doc to new clusetr
if z2 != old {
transfers++
}
dz[j] = z2
newClust := &state[z2]
newClust.addDoc(doc)
}
// TODO: count new clusters
var clusterCount2 int
for i := range state {
if state[i].docs > 0 {
clusterCount2++
}
}
if transfers == 0 && clusterCount2 == clusterCount && i > 25 {
break // convergence achieved. Time to GTFO
}
clusterCount = clusterCount2
}
// return the clusters. As an additional niceness, we'll relabel the cluster IDs
retVal := make([]Cluster, len(dz))
reindex := make([]int, conf.K)
for i := range reindex {
reindex[i] = -1
}
var maxID int
for i, clusterID := range dz {
retVal[i] = state[clusterID]
var cid int
if cid = reindex[clusterID]; cid < 0 {
cid = maxID
maxID++
reindex[clusterID] = cid
}
retVal[i].id = cid
}
return retVal, nil
}
/* Scoring Functions */
// Algorithm3 is the implementation of Equation 3 in the original paper.
// This assumes that a word can only occur once in a string. If the requirement is that words can appear multiple times,
// use Algorithm4.
func Algorithm3(doc Document, docs []Document, clusters []Cluster, conf Config) []float64 {
docCount := float64(len(docs))
k := float64(conf.K)
vocab := float64(conf.Vocabulary)
retVal := make([]float64, len(clusters))
var wg sync.WaitGroup
ts := doc.IDs()
for i := range clusters {
clust := clusters[i]
wg.Add(1)
go func(clust Cluster, i int, wg *sync.WaitGroup) {
p := float64(clust.Docs()) + conf.Alpha/(docCount-1.0+k*conf.Alpha)
num := algo3Numerator(clust, ts, conf.Beta)
denom := algoDenominator(clust, ts, conf.Beta, vocab)
retVal[i] = p * num / denom
wg.Done()
}(clust, i, &wg)
}
wg.Wait()
norm := sum(retVal)
if norm <= 0 {
norm = 1
}
for i := range retVal {
retVal[i] = retVal[i] / norm
}
return retVal
}
// Algorithm4 is the implementation of Equation 4 in the original paper.
// It allows for multiple words to be used in a document.
func Algorithm4(doc Document, docs []Document, clusters []Cluster, conf Config) []float64 {
docCount := float64(len(docs))
k := float64(conf.K)
vocab := float64(conf.Vocabulary)
retVal := make([]float64, len(clusters))
var wg sync.WaitGroup
ts := doc.IDs()
for i := range clusters {
clust := clusters[i]
wg.Add(1)
go func(clust Cluster, i int, wg *sync.WaitGroup) {
p := float64(clust.Docs()) + conf.Alpha/(docCount-1.0+k*conf.Alpha)
num := algo4Numerator(clust, ts, conf.Beta)
denom := algoDenominator(clust, ts, conf.Beta, vocab)
retVal[i] = p * num / denom
wg.Done()
}(clust, i, &wg)
}
wg.Wait()
norm := sum(retVal)
if norm <= 0 {
norm = 1
}
for i := range retVal {
retVal[i] = retVal[i] / norm
}
return retVal
}
func algoDenominator(clust Cluster, ts TokenSet, beta float64, vocab float64) float64 {
retVal := 1.0
wc := float64(clust.Wordcount())
for i := 0; i < len(ts); i++ {
retVal *= wc + vocab*beta + float64(i)
}
return retVal
}
func algo3Numerator(clust Cluster, ts TokenSet, beta float64) float64 {
retVal := 1.0
for _, tok := range ts {
retVal *= (clust.Freq(tok) + beta)
}
return retVal
}
func algo4Numerator(clust Cluster, ts TokenSet, beta float64) float64 {
d := make(kvs, 0, len(ts))
for _, tok := range ts {
d = d.add(tok)
d.incr(tok)
}
retVal := 1.0
for _, tok := range ts {
prod := 1.0
freq := d.val(tok)
clustFreq := clust.Freq(tok)
for j := 0.0; j < freq; j++ {
prod *= clustFreq + beta + j
}
retVal *= prod
}
return retVal
}
/* Sampling Functions */
// Gibbs is the standard sampling function, as per the paper.
type Gibbs struct {
randomkit.BinomialGenerator
}
func NewGibbs(rand *rand.Rand) *Gibbs {
return &Gibbs{
BinomialGenerator: randomkit.BinomialGenerator{
Rand: rand,
},
}
}
// Gibbs returns the index sampled
func (s *Gibbs) Sample(p []float64) int {
ret := s.BinomialGenerator.Multinomial(1, p, len(p))
for i, v := range ret {
if v != 0 {
return i
}
}
panic("Unreachable") // practically this part is unreachable
}