package phrase; import gnu.trove.TIntArrayList; import io.FileUtil; import java.io.IOException; import java.io.PrintStream; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; import phrase.Corpus.Edge; public class PhraseCluster { public int K; public double scalePT, scaleCT; private int n_phrases, n_words, n_contexts, n_positions; public Corpus c; public ExecutorService pool; // emit[tag][position][word] = p(word | tag, position in context) private double emit[][][]; // pi[phrase][tag] = p(tag | phrase) private double pi[][]; public PhraseCluster(int numCluster, Corpus corpus, double scalep, double scalec, int threads){ K=numCluster; c=corpus; n_words=c.getNumWords(); n_phrases=c.getNumPhrases(); n_contexts=c.getNumContexts(); n_positions=c.getNumContextPositions(); this.scalePT = scalep; this.scaleCT = scalec; if (threads > 0) pool = Executors.newFixedThreadPool(threads); emit=new double [K][n_positions][n_words]; pi=new double[n_phrases][K]; for(double [][]i:emit){ for(double []j:i){ arr.F.randomise(j); } } for(double []j:pi){ arr.F.randomise(j); } } public double EM(){ double [][][]exp_emit=new double [K][n_positions][n_words]; double [][]exp_pi=new double[n_phrases][K]; double loglikelihood=0; //E for(int phrase=0; phrase < n_phrases; phrase++){ List contexts = c.getEdgesForPhrase(phrase); for (int ctx=0; ctx 0; loglikelihood += edge.getCount() * Math.log(z); arr.F.l1normalize(p); int count = edge.getCount(); //increment expected count TIntArrayList context = edge.getContext(); for(int tag=0;tag edges = c.getEdgesForPhrase(phrase); for(int edge=0;edge 0) System.out.println("WARNING: failed to converge in " + failures + "/" + n_phrases + " cases"); System.out.println("\tmean iters: " + iterations/(double)n_phrases); System.out.println("\tllh: " + loglikelihood); System.out.println("\tKL: " + kl); System.out.println("\tphrase l1lmax: " + l1lmax); //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 PREM_phrase_constraints_parallel() { assert(pool != null); assert(scaleCT <= 0); final LinkedBlockingQueue expectations = new LinkedBlockingQueue(); double [][][]exp_emit=new double [K][n_positions][n_words]; double [][]exp_pi=new double[n_phrases][K]; double loglikelihood=0, kl=0, l1lmax=0, primal=0; final AtomicInteger failures = new AtomicInteger(0); int iterations=0; //E for(int phrase=0;phrase edges = c.getEdgesForPhrase(phrase); for(int edge=0;edge 0) System.out.println("WARNING: failed to converge in " + failures.get() + "/" + n_phrases + " cases"); System.out.println("\tmean iters: " + iterations/(double)n_phrases); System.out.println("\tllh: " + loglikelihood); System.out.println("\tKL: " + kl); System.out.println("\tphrase l1lmax: " + l1lmax); //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 PREM_phrase_context_constraints(){ assert (scaleCT > 0); double[][][] exp_emit = new double [K][n_positions][n_words]; double[][] exp_pi = new double[n_phrases][K]; double[] lambda = null; //E step PhraseContextObjective pco = new PhraseContextObjective(this, lambda, pool); lambda = pco.optimizeWithProjectedGradientDescent(); //now extract expectations List edges = c.getEdges(); for(int e = 0; e < edges.size(); ++e) { double [] q = pco.posterior(e); Corpus.Edge edge = edges.get(e); TIntArrayList context = edge.getContext(); int contextCnt = edge.getCount(); //increment expected count for(int tag=0;tag EPS) ps.print("\t" + j + ": " + pi[i][j]); } ps.println(); } ps.println("P(word|tag,position)"); for (int i = 0; i < K; ++i) { for(int position=0;position EPS) ps.print(c.getWord(word)+"="+emit[i][position][word]+"\t"); } ps.println(); } ps.println(); } } double phrase_l1lmax() { double sum=0; for(int phrase=0; phrase