📄 sparsetrainer.java
字号:
package iitb.CRF;import cern.colt.function.*;import cern.colt.matrix.*;import cern.colt.matrix.impl.*;/** * * @author Sunita Sarawagi * */ public class SparseTrainer extends Trainer { boolean logTrainer; static class ExpFunc implements DoubleFunction { public double apply(double a) {return Math.exp(a);} }; static class ExpFunc2D implements IntIntDoubleFunction { public double apply(int first, int second, double third) { return Math.exp(third); } }; static class ExpFunc1D implements IntDoubleFunction { public double apply(int first, double third) { return Math.exp(third); } }; static ExpFunc expFunc = new ExpFunc(); static IntDoubleFunction expFunc1D = new ExpFunc1D(); static IntIntDoubleFunction expFunc2D = new ExpFunc2D(); /** * @param numY * @return */ protected DoubleMatrix1D newLogDoubleMatrix1D(int numY) { if ((Boolean.valueOf(params.miscOptions.getProperty("sparse", "false"))).booleanValue()) return new LogSparseDoubleMatrix1D(numY); return new LogDenseDoubleMatrix1D(numY); } protected DoubleMatrix2D newLogDoubleMatrix2D(int numR, int numC) { if ((Boolean.valueOf(params.miscOptions.getProperty("sparse", "false"))).booleanValue()) return new LogSparseDoubleMatrix2D(numR, numC); return new LogDenseDoubleMatrix2D(numR, numC); } public SparseTrainer(CrfParams p) { super(p); params = p; logTrainer = params.trainerType.equals("ll"); } public void train(CRF model, DataIter data, double[] l, Evaluator eval) { init(model,data,l); evaluator = eval; if (params.debugLvl > 0) { Util.printDbg("Number of features :" + lambda.length); } doTrain(); } void initMatrices() { if (!logTrainer) { Mi_YY = new SparseDoubleMatrix2D(numY,numY); Ri_Y = new SparseDoubleMatrix1D(numY); alpha_Y = new SparseDoubleMatrix1D(numY); newAlpha_Y = new SparseDoubleMatrix1D(numY); tmp_Y = new SparseDoubleMatrix1D(numY); } else { Mi_YY = newLogDoubleMatrix2D(numY,numY); Ri_Y = newLogDoubleMatrix1D(numY); alpha_Y = newLogDoubleMatrix1D(numY); newAlpha_Y = newLogDoubleMatrix1D(numY); tmp_Y = newLogDoubleMatrix1D(numY); } } protected double computeFunctionGradient(double lambda[], double grad[]) { if (params.trainerType.equals("ll")) return computeFunctionGradientLL(lambda, grad); double logli = 0; try { for (int f = 0; f < lambda.length; f++) { grad[f] = -1*lambda[f]*params.invSigmaSquare; logli -= ((lambda[f]*lambda[f])*params.invSigmaSquare)/2; } boolean doScaling = params.doScaling; diter.startScan(); if (featureGenCache != null) featureGenCache.startDataScan(); for (int numRecord = 0; diter.hasNext(); numRecord++) { DataSequence dataSeq = (DataSequence)diter.next(); if (featureGenCache != null) featureGenCache.nextDataIndex(); if (params.debugLvl > 1) { Util.printDbg("Read next seq: " + numRecord + " logli " + logli); } alpha_Y.assign(1); for (int f = 0; f < lambda.length; f++) ExpF[f] = 0; if ((beta_Y == null) || (beta_Y.length < dataSeq.length())) { beta_Y = new DoubleMatrix1D[2*dataSeq.length()]; for (int i = 0; i < beta_Y.length; i++) beta_Y[i] = new SparseDoubleMatrix1D(numY); scale = new double[2*dataSeq.length()]; } // compute beta values in a backward scan. // also scale beta-values to 1 to avoid numerical problems. scale[dataSeq.length()-1] = (doScaling)?numY:1; beta_Y[dataSeq.length()-1].assign(1.0/scale[dataSeq.length()-1]); for (int i = dataSeq.length()-1; i > 0; i--) { if (params.debugLvl > 2) { Util.printDbg("Features fired"); //featureGenerator.startScanFeaturesAt(dataSeq, i); //while (featureGenerator.hasNext()) { //Feature feature = featureGenerator.next(); //Util.printDbg(feature.toString()); //} } // compute the Mi matrix computeMi(featureGenerator,lambda,dataSeq,i,Mi_YY,Ri_Y); tmp_Y.assign(beta_Y[i]); tmp_Y.assign(Ri_Y,multFunc); // RobustMath.Mult(Mi_YY, tmp_Y, beta_Y[i-1],1,0,false,edgeGen); Mi_YY.zMult(tmp_Y, beta_Y[i-1]); // need to scale the beta-s to avoid overflow scale[i-1] = doScaling?beta_Y[i-1].zSum():1; if ((scale[i-1] < 1) && (scale[i-1] > -1)) scale[i-1] = 1; constMultiplier.multiplicator = 1.0/scale[i-1]; beta_Y[i-1].assign(constMultiplier); } double thisSeqLogli = 0; for (int i = 0; i < dataSeq.length(); i++) { // compute the Mi matrix computeMi(featureGenerator,lambda,dataSeq,i,Mi_YY,Ri_Y); // find features that fire at this position.. featureGenerator.startScanFeaturesAt(dataSeq, i); if (i > 0) { // tmp_Y.assign(alpha_Y); // RobustMath.Mult(Mi_YY, tmp_Y, newAlpha_Y,1,0,true,edgeGen); Mi_YY.zMult(alpha_Y, newAlpha_Y,1,0,true); newAlpha_Y.assign(Ri_Y,multFunc); } else { newAlpha_Y.assign(Ri_Y); } while (featureGenerator.hasNext()) { Feature feature = featureGenerator.next(); int f = feature.index(); int yp = feature.y(); int yprev = feature.yprev(); float val = feature.value(); if ((dataSeq.y(i) == yp) && (((i-1 >= 0) && (yprev == dataSeq.y(i-1))) || (yprev < 0))) { grad[f] += val; thisSeqLogli += val*lambda[f]; } if (yprev < 0) { ExpF[f] += newAlpha_Y.get(yp)*val*beta_Y[i].get(yp); } else { ExpF[f] += alpha_Y.get(yprev)*Ri_Y.get(yp)*Mi_YY.get(yprev,yp)*val*beta_Y[i].get(yp); } } alpha_Y.assign(newAlpha_Y); // now scale the alpha-s to avoid overflow problems. constMultiplier.multiplicator = 1.0/scale[i]; alpha_Y.assign(constMultiplier); if (params.debugLvl > 2) { System.out.println("Alpha-i " + alpha_Y.toString()); System.out.println("Ri " + Ri_Y.toString()); System.out.println("Mi " + Mi_YY.toString()); System.out.println("Beta-i " + beta_Y[i].toString()); } //badVector(alpha_Y); } double Zx = alpha_Y.zSum(); //if (Zx == 0) { //Zx = (Double.MIN_VALUE*100000000); //} thisSeqLogli -= log(Zx); // correct for the fact that alpha-s were scaled. for (int i = 0; i < dataSeq.length(); i++) { thisSeqLogli -= log(scale[i]); } if (thisSeqLogli > 0) { System.out.println("This is shady: something is wrong Pr(y|x) > 1!"); } logli += thisSeqLogli; // update grad. for (int f = 0; f < grad.length; f++) grad[f] -= ExpF[f]/Zx; if (params.debugLvl > 1) { System.out.println("Sequence " + thisSeqLogli + " " + logli); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -