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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
package phrase;
import io.FileUtil;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class PhraseCluster {
public int K;
public double scale;
private int n_phrase;
private int n_words;
public PhraseCorpus c;
private ExecutorService pool;
/**@brief
* emit[tag][position][word]
*/
private double emit[][][];
private double pi[][];
public static void main(String[] args) {
String input_fname = args[0];
int tags = Integer.parseInt(args[1]);
String outputDir = args[2];
int iterations = Integer.parseInt(args[3]);
double scale = Double.parseDouble(args[4]);
int threads = Integer.parseInt(args[5]);
PhraseCorpus corpus = new PhraseCorpus(input_fname);
PhraseCluster cluster = new PhraseCluster(tags, corpus, scale, threads);
PhraseObjective.ps = FileUtil.openOutFile(outputDir + "/phrase_stat.out");
for(int i=0;i<iterations;i++){
double o = cluster.PREM();
//double o = cluster.EM();
PhraseObjective.ps.println("ITER: "+i+" objective: " + o);
}
PrintStream ps=io.FileUtil.openOutFile(outputDir + "/posterior.out");
cluster.displayPosterior(ps);
ps.println();
cluster.displayModelParam(ps);
ps.close();
PhraseObjective.ps.close();
cluster.finish();
}
public PhraseCluster(int numCluster, PhraseCorpus corpus, double scale, int threads){
K=numCluster;
c=corpus;
n_words=c.wordLex.size();
n_phrase=c.data.length;
this.scale = scale;
if (threads > 0)
pool = Executors.newFixedThreadPool(threads);
emit=new double [K][PhraseCorpus.NUM_CONTEXT][n_words];
pi=new double[n_phrase][K];
for(double [][]i:emit){
for(double []j:i){
arr.F.randomise(j);
}
}
for(double []j:pi){
arr.F.randomise(j);
}
}
public void finish()
{
if (pool != null)
pool.shutdown();
}
public double EM(){
double [][][]exp_emit=new double [K][PhraseCorpus.NUM_CONTEXT][n_words];
double [][]exp_pi=new double[n_phrase][K];
double loglikelihood=0;
//E
for(int phrase=0;phrase<c.data.length;phrase++){
int [][] data=c.data[phrase];
for(int ctx=0;ctx<data.length;ctx++){
int context[]=data[ctx];
double p[]=posterior(phrase,context);
loglikelihood+=Math.log(arr.F.l1norm(p));
arr.F.l1normalize(p);
int contextCnt=context[context.length-1];
//increment expected count
for(int tag=0;tag<K;tag++){
for(int pos=0;pos<context.length-1;pos++){
exp_emit[tag][pos][context[pos]]+=p[tag]*contextCnt;
}
exp_pi[phrase][tag]+=p[tag]*contextCnt;
}
}
}
System.out.println("Log likelihood: "+loglikelihood);
//M
for(double [][]i:exp_emit){
for(double []j:i){
arr.F.l1normalize(j);
}
}
emit=exp_emit;
for(double []j:exp_pi){
arr.F.l1normalize(j);
}
pi=exp_pi;
return loglikelihood;
}
public double PREM(){
if (pool != null)
return PREMParallel();
double [][][]exp_emit=new double [K][PhraseCorpus.NUM_CONTEXT][n_words];
double [][]exp_pi=new double[n_phrase][K];
double loglikelihood=0;
double primal=0;
//E
for(int phrase=0;phrase<c.data.length;phrase++){
PhraseObjective po=new PhraseObjective(this,phrase);
po.optimizeWithProjectedGradientDescent();
double [][] q=po.posterior();
loglikelihood+=po.getValue();
primal+=po.primal();
for(int edge=0;edge<q.length;edge++){
int []context=c.data[phrase][edge];
int contextCnt=context[context.length-1];
//increment expected count
for(int tag=0;tag<K;tag++){
for(int pos=0;pos<context.length-1;pos++){
exp_emit[tag][pos][context[pos]]+=q[edge][tag]*contextCnt;
}
exp_pi[phrase][tag]+=q[edge][tag]*contextCnt;
}
}
}
System.out.println("Log likelihood: "+loglikelihood);
System.out.println("Primal Objective: "+primal);
//M
for(double [][]i:exp_emit){
for(double []j:i){
arr.F.l1normalize(j);
}
}
emit=exp_emit;
for(double []j:exp_pi){
arr.F.l1normalize(j);
}
pi=exp_pi;
return primal;
}
public double PREMParallel(){
assert(pool != null);
final LinkedBlockingQueue<PhraseObjective> expectations
= new LinkedBlockingQueue<PhraseObjective>();
double [][][]exp_emit=new double [K][PhraseCorpus.NUM_CONTEXT][n_words];
double [][]exp_pi=new double[n_phrase][K];
double loglikelihood=0;
double primal=0;
//E
for(int phrase=0;phrase<c.data.length;phrase++){
final int p=phrase;
pool.execute(new Runnable() {
public void run() {
try {
//System.out.println("" + Thread.currentThread().getId() + " optimising lambda for " + p);
PhraseObjective po = new PhraseObjective(PhraseCluster.this, p);
po.optimizeWithProjectedGradientDescent();
//System.out.println("" + Thread.currentThread().getId() + " done optimising lambda for " + p);
expectations.put(po);
//System.out.println("" + Thread.currentThread().getId() + " added to queue " + p);
} catch (InterruptedException e) {
System.err.println(Thread.currentThread().getId() + " Local e-step thread interrupted; will cause deadlock.");
e.printStackTrace();
}
}
});
}
// aggregate the expectations as they become available
for(int count=0;count<c.data.length;count++) {
try {
//System.out.println("" + Thread.currentThread().getId() + " reading queue #" + count);
// wait (blocking) until something is ready
PhraseObjective po = expectations.take();
// process
int phrase = po.phrase;
//System.out.println("" + Thread.currentThread().getId() + " taken phrase " + phrase);
double [][] q=po.posterior();
loglikelihood+=po.getValue();
primal+=po.primal();
for(int edge=0;edge<q.length;edge++){
int []context=c.data[phrase][edge];
int contextCnt=context[context.length-1];
//increment expected count
for(int tag=0;tag<K;tag++){
for(int pos=0;pos<context.length-1;pos++){
exp_emit[tag][pos][context[pos]]+=q[edge][tag]*contextCnt;
}
exp_pi[phrase][tag]+=q[edge][tag]*contextCnt;
}
}
} catch (InterruptedException e){
System.err.println("M-step thread interrupted. Probably fatal!");
e.printStackTrace();
}
}
System.out.println("Log likelihood: "+loglikelihood);
System.out.println("Primal Objective: "+primal);
//M
for(double [][]i:exp_emit){
for(double []j:i){
arr.F.l1normalize(j);
}
}
emit=exp_emit;
for(double []j:exp_pi){
arr.F.l1normalize(j);
}
pi=exp_pi;
return primal;
}
/**
*
* @param phrase index of phrase
* @param ctx array of context
* @return unnormalized posterior
*/
public double[]posterior(int phrase, int[]ctx){
double[] prob=Arrays.copyOf(pi[phrase], K);
for(int tag=0;tag<K;tag++){
for(int c=0;c<ctx.length-1;c++){
int word=ctx[c];
prob[tag]*=emit[tag][c][word];
}
}
return prob;
}
public void displayPosterior(PrintStream ps)
{
c.buildList();
for (int i = 0; i < n_phrase; ++i)
{
int [][]data=c.data[i];
for (int[] e: data)
{
double probs[] = posterior(i, e);
arr.F.l1normalize(probs);
// emit phrase
ps.print(c.phraseList[i]);
ps.print("\t");
ps.print(c.getContextString(e));
ps.print("||| C=" + e[e.length-1] + " |||");
int t=arr.F.argmax(probs);
ps.print(t+"||| [");
for(t=0;t<K;t++){
ps.print(probs[t]+", ");
}
// for (int t = 0; t < numTags; ++t)
// System.out.print(" " + probs[t]);
ps.println("]");
}
}
}
public void displayModelParam(PrintStream ps)
{
c.buildList();
ps.println("P(tag|phrase)");
for (int i = 0; i < n_phrase; ++i)
{
ps.print(c.phraseList[i]);
for(int j=0;j<pi[i].length;j++){
ps.print("\t"+pi[i][j]);
}
ps.println();
}
ps.println("P(word|tag,position)");
for (int i = 0; i < K; ++i)
{
ps.println(i);
for(int position=0;position<PhraseCorpus.NUM_CONTEXT;position++){
ps.println(position);
for(int word=0;word<emit[i][position].length;word++){
if((word+1)%100==0){
ps.println();
}
ps.print(c.wordList[word]+"="+emit[i][position][word]+"\t");
}
ps.println();
}
ps.println();
}
}
}
|