package phrase; import java.io.PrintStream; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import optimization.gradientBasedMethods.ProjectedGradientDescent; import optimization.gradientBasedMethods.ProjectedObjective; import optimization.gradientBasedMethods.stats.OptimizerStats; import optimization.linesearch.ArmijoLineSearchMinimizationAlongProjectionArc; import optimization.linesearch.InterpolationPickFirstStep; import optimization.linesearch.LineSearchMethod; import optimization.linesearch.WolfRuleLineSearch; import optimization.projections.SimplexProjection; import optimization.stopCriteria.CompositeStopingCriteria; import optimization.stopCriteria.ProjectedGradientL2Norm; import optimization.stopCriteria.StopingCriteria; import optimization.stopCriteria.ValueDifference; import optimization.util.MathUtils; import phrase.Corpus.Edge; public class PhraseContextObjective extends ProjectedObjective { private static final double GRAD_DIFF = 0.00002; private static double INIT_STEP_SIZE = 10; private static double VAL_DIFF = 1e-4; // FIXME needs to be tuned private static int ITERATIONS = 100; private PhraseCluster c; // un-regularized unnormalized posterior, p[edge][tag] // P(tag|edge) \propto P(tag|phrase)P(context|tag) private double p[][]; // regularized unnormalized posterior // q[edge][tag] propto p[edge][tag]*exp(-lambda) private double q[][]; private List data; // log likelihood under q private double loglikelihood; private SimplexProjection projectionPhrase; private SimplexProjection projectionContext; double[] newPoint; private int n_param; // likelihood under p public double llh; private Map edgeIndex; public PhraseContextObjective(PhraseCluster cluster, double[] startingParameters) { c=cluster; data=c.c.getEdges(); n_param=data.size()*c.K*2; parameters = startingParameters; if (parameters == null) parameters = new double[n_param]; newPoint = new double[n_param]; gradient = new double[n_param]; initP(); projectionPhrase = new SimplexProjection(c.scalePT); projectionContext = new SimplexProjection(c.scaleCT); q=new double [data.size()][c.K]; edgeIndex = new HashMap(); for (int e=0; e 0) { // first project using the phrase-tag constraints, // for all p,t: sum_c lambda_ptc < scaleP for (int p = 0; p < c.c.getNumPhrases(); ++p) { List edges = c.c.getEdgesForPhrase(p); double toProject[] = new double[edges.size()]; for(int tag=0;tag 1e-6) { // now project using the context-tag constraints, // for all c,t: sum_p omega_pct < scaleC for (int ctx = 0; ctx < c.c.getNumContexts(); ++ctx) { List edges = c.c.getEdgesForContext(ctx); double toProject[] = new double[edges.size()]; for(int tag=0;tag edges = c.c.getEdgesForPhrase(p); for(int tag=0;tag edges = c.c.getEdgesForContext(ctx); for(int tag=0; tag