package phrase; import java.io.PrintStream; import java.util.Arrays; import java.util.List; 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; public class PhraseObjective extends ProjectedObjective { static final double GRAD_DIFF = 0.00002; static double INIT_STEP_SIZE = 300; static double VAL_DIFF = 1e-6; // FIXME needs to be tuned - and this might be too weak static int ITERATIONS = 100; //private double c1=0.0001; // wolf stuff //private double c2=0.9; private static double lambda[][]; private PhraseCluster c; /**@brief * for debugging purposes */ //public static PrintStream ps; /**@brief current phrase being optimzed*/ public int phrase; /**@brief un-regularized posterior * unnormalized * p[edge][tag] * P(tag|edge) \propto P(tag|phrase)P(context|tag) */ private double[][]p; /**@brief regularized posterior * q[edge][tag] propto p[edge][tag]*exp(-lambda) */ private double q[][]; private List data; /**@brief log likelihood of the associated phrase * */ private double loglikelihood; private SimplexProjection projection; double[] newPoint ; private int n_param; /**@brief likelihood under p * */ public double llh; public PhraseObjective(PhraseCluster cluster, int phraseIdx){ phrase=phraseIdx; c=cluster; data=c.c.getEdgesForPhrase(phrase); n_param=data.size()*c.K; if (lambda==null){ lambda=new double[c.c.getNumPhrases()][]; } if (lambda[phrase]==null){ lambda[phrase]=new double[n_param]; } parameters=lambda[phrase]; newPoint = new double[n_param]; gradient = new double[n_param]; initP(); projection=new SimplexProjection(c.scalePT); q=new double [data.size()][c.K]; setParameters(parameters); } private void initP(){ p=new double[data.size()][]; for(int edge=0;edgemax) max=q[edge][tag]; } sum+=max; } return sum; } public double primal() { return loglikelihood() - KL_divergence() - c.scalePT * l1lmax(); } }