-
Notifications
You must be signed in to change notification settings - Fork 11
/
asynchronous.py
executable file
·220 lines (186 loc) · 7.73 KB
/
asynchronous.py
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
# asynchronous.py: Code for performing asynchronous computations
# This code suite is largely adapted from the online VB (aka stochastic
# variational Bayes) code of
# Matthew D. Hoffman, Copyright (C) 2010
# found here: http://www.cs.princeton.edu/~blei/downloads/onlineldavb.tar
# and also of
# Chong Wang, Copyright (C) 2011
# found here: http://www.cs.cmu.edu/~chongw/software/onlinehdp.tar.gz
#
# Adapted by: Nick Boyd, Tamara Broderick, Andre Wibisono, Ashia C. Wilson
#
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details. You should have received a copy of the GNU General
# Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
#filtering subroutine
from multiprocessing import Process, RawValue, Lock, RawArray, JoinableQueue
import numpy, batchvb, math, copy, ep_lda, ep2_lda
import ctypes as c
import multiprocessing as mp
def runBatchVB(workQueue, lockingPost):
for (W, K, docs, alpha, maxiters,thr,hbb) in iter(workQueue.get, "none"):
#read current parameter value (and copy)
lam = copy.deepcopy(lockingPost.value())
#calculate correction
batchVB = batchvb.BatchLDA(W, K, docs, alpha, lam, useHBBBound = hbb)
lam = batchVB.train( maxiters, thr)
ss = (lam - batchVB._eta)
#apply correction
lockingPost.increment(ss)
workQueue.task_done()
def runBatchEP(workQueue, lockingPost):
count = 0
for (W, K, docs, alpha, maxiters, thr, useNewton) in iter(workQueue.get, "none"):
#read current parameter value (and copy)
lam = copy.deepcopy(lockingPost.value())
#calculate correction
ep = ep_lda.EP_LDA(W, K, docs, alpha, lam, useNewton)
lam = ep.train(maxiters, thr)
ss = (lam - ep._eta)
#apply correction
lockingPost.increment(ss)
workQueue.task_done()
count += 1
print "\tdone " + str(count)
def runBatchEP2(workQueue, lockingPost):
count = 0
for (W, K, docs, alpha, maxiters, thr, useNewton) in iter(workQueue.get, "none"):
#read current parameter value (and copy)
lam = copy.deepcopy(lockingPost.value())
#calculate correction
ep2 = ep2_lda.EP2_LDA(W, K, docs, alpha, lam, useNewton)
lam = ep2.train(maxiters, thr)
ss = (lam - ep2._eta)
#apply correction
lockingPost.increment(ss)
workQueue.task_done()
count += 1
print "\tdone " + str(count)
def chunk(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
class LockingPosterior(object):
def __init__(self, eta):
(self._K, self._W) = eta.shape
self._lambda = RawArray('d', eta.reshape(self._K*self._W,))
self._lock = Lock()
def value(self):
with self._lock:
return numpy.frombuffer(self._lambda).reshape(self._K, self._W)
def increment(self, ss):
with self._lock:
self._lambda[:] = (numpy.frombuffer(self._lambda).reshape(self._K,self._W) + ss).reshape(self._K * self._W, )
class ParallelFiltering:
def __init__(self, W, K, alpha, eta, maxiters, threshold, useHBB, batchsize, numthreads):
"""
Arguments:
K: Number of topics
W: Number of words in the vocabulary
alpha: Hyperparameter for prior on weight vectors theta
eta: Hyperparameter for prior on topics beta
"""
self._str = "Async_%d_%r_%g_%d_%d" % (maxiters,useHBB,threshold,batchsize,numthreads)
self._thresh = threshold
self._maxiters = maxiters
self._hbb = useHBB
self._batchsize = batchsize
self._workQueue = JoinableQueue()
self._K = K
self._W = W
self._alpha = alpha
if numpy.isscalar(eta):
etaA = eta * numpy.ones((self._K, self._W))
else:
etaA = eta
self._posterior = LockingPosterior(etaA)
#start workers
self.workers = [Process(target=runBatchVB, args=(self._workQueue, self._posterior)) for i in range(numthreads)]
for worker in self.workers:
worker.start()
def __str__(self):
return self._str
def update_lambda(self,docs):
chunks = chunk(docs, self._batchsize)
for doc_set in chunks:
self._workQueue.put((self._W, self._K, doc_set, self._alpha, self._maxiters, self._thresh, self._hbb))
self._workQueue.join()
return (self._alpha, self._posterior.value())
def shutdown(self):
for worker in self.workers:
worker.terminate()
class ParallelFilteringEP:
def __init__(self, W, K, alpha, eta, maxiters, threshold, useNewton, batchsize, numthreads):
"""
Arguments:
K: Number of topics
W: Number of words in the vocabulary
alpha: Hyperparameter for prior on weight vectors theta
eta: Hyperparameter for prior on topics beta
"""
self._str = "AsyncEP_%d_%g_%r_%d_%d" % (maxiters, threshold, useNewton, batchsize, numthreads)
self._thresh = threshold
self._maxiters = maxiters
self._useNewton = useNewton
self._batchsize = batchsize
self._workQueue = JoinableQueue()
self._K = K
self._W = W
self._alpha = alpha
if numpy.isscalar(eta):
etaA = eta * numpy.ones((self._K, self._W))
else:
etaA = eta
self._posterior = LockingPosterior(etaA)
#start workers
workers = [Process(target=runBatchEP, args=(self._workQueue, self._posterior)) for i in range(numthreads)]
for worker in workers:
worker.start()
def __str__(self):
return self._str
def update_lambda(self,docs):
chunks = chunk(docs, self._batchsize)
for doc_set in chunks:
self._workQueue.put((self._W, self._K, doc_set, self._alpha, self._maxiters, self._thresh, self._useNewton))
self._workQueue.join()
return (self._alpha, self._posterior.value())
class ParallelFilteringEP2:
def __init__(self, W, K, alpha, eta, maxiters, threshold, useNewton, batchsize, numthreads):
"""
Arguments:
K: Number of topics
W: Number of words in the vocabulary
alpha: Hyperparameter for prior on weight vectors theta
eta: Hyperparameter for prior on topics beta
"""
self._str = "AsyncEP2_%d_%g_%r_%d_%d" % (maxiters, threshold, useNewton, batchsize, numthreads)
self._thresh = threshold
self._maxiters = maxiters
self._useNewton = useNewton
self._batchsize = batchsize
self._workQueue = JoinableQueue()
self._K = K
self._W = W
self._alpha = alpha
if numpy.isscalar(eta):
etaA = eta * numpy.ones((self._K, self._W))
else:
etaA = eta
self._posterior = LockingPosterior(etaA)
#start workers
workers = [Process(target=runBatchEP2, args=(self._workQueue, self._posterior)) for i in range(numthreads)]
for worker in workers:
worker.start()
def __str__(self):
return self._str
def update_lambda(self,docs):
chunks = chunk(docs, self._batchsize)
for doc_set in chunks:
self._workQueue.put((self._W, self._K, doc_set, self._alpha, self._maxiters, self._thresh, self._useNewton))
self._workQueue.join()
return (self._alpha, self._posterior.value())