public double condLogProbGivenNext(int position, int label, int[] nextLabels) { position = position + nextLabels.length; if (nextLabels.length + 1 == windowSize) { return factorTables[position].conditionalLogProbGivenNext(nextLabels, label); } else if (nextLabels.length + 1 < windowSize) { FactorTable ft = factorTables[position].sumOutFront(); while (ft.windowSize() > nextLabels.length + 1) { ft = ft.sumOutFront(); } return ft.conditionalLogProbGivenPrevious(nextLabels, label); } else { int[] p = new int[windowSize - 1]; System.arraycopy(nextLabels, 0, p, 0, p.length); return factorTables[position].conditionalLogProbGivenPrevious(p, label); } }
/** * Gives the probability of a tag at a single position conditioned on a * sequence of previous labels. * * @param position Index in sequence * @param label Label of item at index * @param prevLabels Indices of labels in previous positions * @return conditional log probability */ public double condLogProbGivenPrevious(int position, int label, int[] prevLabels) { if (prevLabels.length + 1 == windowSize) { return factorTables[position].conditionalLogProbGivenPrevious(prevLabels, label); } else if (prevLabels.length + 1 < windowSize) { FactorTable ft = factorTables[position].sumOutFront(); while (ft.windowSize() > prevLabels.length + 1) { ft = ft.sumOutFront(); } return ft.conditionalLogProbGivenPrevious(prevLabels, label); } else { int[] p = new int[windowSize - 1]; System.arraycopy(prevLabels, prevLabels.length - p.length, p, 0, p.length); return factorTables[position].conditionalLogProbGivenPrevious(p, label); } }
if (nextLength != windowSize - 1) { for (int j = 0; j < windowSize - 1 - nextLength; j++) { nextFactorTable = nextFactorTable.sumOutFront();
/** * This function assumes a LinearCliquePotentialFunction is used for wrapping the weights * @return a new CRFCliqueTree for the weights on the data */ public static <E> CRFCliqueTree<E> getCalibratedCliqueTree(double[] weights, double wscale, int[][] weightIndices, int[][][] data, List<Index<CRFLabel>> labelIndices, int numClasses, Index<E> classIndex, E backgroundSymbol) { FactorTable[] factorTables = new FactorTable[data.length]; FactorTable[] messages = new FactorTable[data.length - 1]; for (int i = 0; i < data.length; i++) { factorTables[i] = getFactorTable(weights, wscale, weightIndices, data[i], labelIndices, numClasses); if (i > 0) { messages[i - 1] = factorTables[i - 1].sumOutFront(); factorTables[i].multiplyInFront(messages[i - 1]); } } for (int i = factorTables.length - 2; i >= 0; i--) { FactorTable summedOut = factorTables[i + 1].sumOutEnd(); summedOut.divideBy(messages[i]); factorTables[i].multiplyInEnd(summedOut); } return new CRFCliqueTree<>(factorTables, classIndex, backgroundSymbol); }
/** * @return a new CRFCliqueTree for the weights on the data */ public static <E> CRFCliqueTree<E> getCalibratedCliqueTree(int[][][] data, List<Index<CRFLabel>> labelIndices, int numClasses, Index<E> classIndex, E backgroundSymbol, CliquePotentialFunction cliquePotentialFunc, double[][][] featureVals) { FactorTable[] factorTables = new FactorTable[data.length]; FactorTable[] messages = new FactorTable[data.length - 1]; for (int i = 0; i < data.length; i++) { double[][] featureValByCliqueSize = null; if (featureVals != null) featureValByCliqueSize = featureVals[i]; factorTables[i] = getFactorTable(data[i], labelIndices, numClasses, cliquePotentialFunc, featureValByCliqueSize, i); // log.info("before calibration,FT["+i+"] = " + factorTables[i].toProbString()); if (i > 0) { messages[i - 1] = factorTables[i - 1].sumOutFront(); // log.info("forward message, message["+(i-1)+"] = " + messages[i-1].toProbString()); factorTables[i].multiplyInFront(messages[i - 1]); // log.info("after forward calibration, FT["+i+"] = " + factorTables[i].toProbString()); } } for (int i = factorTables.length - 2; i >= 0; i--) { FactorTable summedOut = factorTables[i + 1].sumOutEnd(); summedOut.divideBy(messages[i]); // log.info("backward summedOut, summedOut= " + summedOut.toProbString()); factorTables[i].multiplyInEnd(summedOut); // log.info("after backward calibration, FT["+i+"] = " + factorTables[i].toProbString()); } return new CRFCliqueTree<>(factorTables, classIndex, backgroundSymbol); }
log.info(ft.sumOutFront()); ft3.setValue(new int[] {1, 1, 1}, Math.log(1e-50)); FactorTable ft4 = ft3.sumOutFront(); log.info(ft4.toNonLogString()); FactorTable ft5 = ft3.sumOutEnd();
public double condLogProbGivenNext(int position, int label, int[] nextLabels) { position = position + nextLabels.length; if (nextLabels.length + 1 == windowSize) { return factorTables[position].conditionalLogProbGivenNext(nextLabels, label); } else if (nextLabels.length + 1 < windowSize) { FactorTable ft = factorTables[position].sumOutFront(); while (ft.windowSize() > nextLabels.length + 1) { ft = ft.sumOutFront(); } return ft.conditionalLogProbGivenPrevious(nextLabels, label); } else { int[] p = new int[windowSize - 1]; System.arraycopy(nextLabels, 0, p, 0, p.length); return factorTables[position].conditionalLogProbGivenPrevious(p, label); } }
public double condLogProbGivenNext(int position, int label, int[] nextLabels) { position = position + nextLabels.length; if (nextLabels.length + 1 == windowSize) { return factorTables[position].conditionalLogProbGivenNext(nextLabels, label); } else if (nextLabels.length + 1 < windowSize) { FactorTable ft = factorTables[position].sumOutFront(); while (ft.windowSize() > nextLabels.length + 1) { ft = ft.sumOutFront(); } return ft.conditionalLogProbGivenPrevious(nextLabels, label); } else { int[] p = new int[windowSize - 1]; System.arraycopy(nextLabels, 0, p, 0, p.length); return factorTables[position].conditionalLogProbGivenPrevious(p, label); } }
public double condLogProbGivenNext(int position, int label, int[] nextLabels) { position = position+nextLabels.length; if (nextLabels.length + 1 == windowSize) { return factorTables[position].conditionalLogProbGivenNext(nextLabels, label); } else if (nextLabels.length + 1 < windowSize) { FactorTable ft = factorTables[position].sumOutFront(); while (ft.windowSize() > nextLabels.length + 1) { ft = ft.sumOutFront(); } return ft.conditionalLogProbGivenPrevious(nextLabels, label); } else { int[] p = new int[windowSize-1]; System.arraycopy(nextLabels, 0, p, 0, p.length); return factorTables[position].conditionalLogProbGivenPrevious(p, label); } }
/** * Gives the probability of a tag at a single position conditioned on * a sequence of previous labels. * * @param position Index in sequence * @param label Label of item at index * @param prevLabels * @return conditional log probability */ public double condLogProbGivenPrevious(int position, int label, int[] prevLabels) { if (prevLabels.length + 1 == windowSize) { return factorTables[position].conditionalLogProbGivenPrevious(prevLabels, label); } else if (prevLabels.length + 1 < windowSize) { FactorTable ft = factorTables[position].sumOutFront(); while (ft.windowSize() > prevLabels.length + 1) { ft = ft.sumOutFront(); } return ft.conditionalLogProbGivenPrevious(prevLabels, label); } else { int[] p = new int[windowSize-1]; System.arraycopy(prevLabels, prevLabels.length - p.length, p, 0, p.length); return factorTables[position].conditionalLogProbGivenPrevious(p, label); } }
/** * Gives the probability of a tag at a single position conditioned on a * sequence of previous labels. * * @param position Index in sequence * @param label Label of item at index * @param prevLabels Indices of labels in previous positions * @return conditional log probability */ public double condLogProbGivenPrevious(int position, int label, int[] prevLabels) { if (prevLabels.length + 1 == windowSize) { return factorTables[position].conditionalLogProbGivenPrevious(prevLabels, label); } else if (prevLabels.length + 1 < windowSize) { FactorTable ft = factorTables[position].sumOutFront(); while (ft.windowSize() > prevLabels.length + 1) { ft = ft.sumOutFront(); } return ft.conditionalLogProbGivenPrevious(prevLabels, label); } else { int[] p = new int[windowSize - 1]; System.arraycopy(prevLabels, prevLabels.length - p.length, p, 0, p.length); return factorTables[position].conditionalLogProbGivenPrevious(p, label); } }
/** * Gives the probability of a tag at a single position conditioned on a * sequence of previous labels. * * @param position Index in sequence * @param label Label of item at index * @param prevLabels Indices of labels in previous positions * @return conditional log probability */ public double condLogProbGivenPrevious(int position, int label, int[] prevLabels) { if (prevLabels.length + 1 == windowSize) { return factorTables[position].conditionalLogProbGivenPrevious(prevLabels, label); } else if (prevLabels.length + 1 < windowSize) { FactorTable ft = factorTables[position].sumOutFront(); while (ft.windowSize() > prevLabels.length + 1) { ft = ft.sumOutFront(); } return ft.conditionalLogProbGivenPrevious(prevLabels, label); } else { int[] p = new int[windowSize - 1]; System.arraycopy(prevLabels, prevLabels.length - p.length, p, 0, p.length); return factorTables[position].conditionalLogProbGivenPrevious(p, label); } }
if (nextLength != windowSize - 1) { for (int j = 0; j < windowSize - 1 - nextLength; j++) { nextFactorTable = nextFactorTable.sumOutFront();
if (nextLength != windowSize - 1) { for (int j = 0; j < windowSize - 1 - nextLength; j++) { nextFactorTable = nextFactorTable.sumOutFront();
if (nextLength != windowSize - 1) { for (int j = 0; j < windowSize - 1 - nextLength; j++) { nextFactorTable = nextFactorTable.sumOutFront();
/** * This function assumes a LinearCliquePotentialFunction is used for wrapping the weights * @return a new CRFCliqueTree for the weights on the data */ public static <E> CRFCliqueTree<E> getCalibratedCliqueTree(double[] weights, double wscale, int[][] weightIndices, int[][][] data, List<Index<CRFLabel>> labelIndices, int numClasses, Index<E> classIndex, E backgroundSymbol) { FactorTable[] factorTables = new FactorTable[data.length]; FactorTable[] messages = new FactorTable[data.length - 1]; for (int i = 0; i < data.length; i++) { factorTables[i] = getFactorTable(weights, wscale, weightIndices, data[i], labelIndices, numClasses); if (i > 0) { messages[i - 1] = factorTables[i - 1].sumOutFront(); factorTables[i].multiplyInFront(messages[i - 1]); } } for (int i = factorTables.length - 2; i >= 0; i--) { FactorTable summedOut = factorTables[i + 1].sumOutEnd(); summedOut.divideBy(messages[i]); factorTables[i].multiplyInEnd(summedOut); } return new CRFCliqueTree<E>(factorTables, classIndex, backgroundSymbol); }
/** * This function assumes a LinearCliquePotentialFunction is used for wrapping the weights * @return a new CRFCliqueTree for the weights on the data */ public static <E> CRFCliqueTree<E> getCalibratedCliqueTree(double[] weights, double wscale, int[][] weightIndices, int[][][] data, List<Index<CRFLabel>> labelIndices, int numClasses, Index<E> classIndex, E backgroundSymbol) { FactorTable[] factorTables = new FactorTable[data.length]; FactorTable[] messages = new FactorTable[data.length - 1]; for (int i = 0; i < data.length; i++) { factorTables[i] = getFactorTable(weights, wscale, weightIndices, data[i], labelIndices, numClasses); if (i > 0) { messages[i - 1] = factorTables[i - 1].sumOutFront(); factorTables[i].multiplyInFront(messages[i - 1]); } } for (int i = factorTables.length - 2; i >= 0; i--) { FactorTable summedOut = factorTables[i + 1].sumOutEnd(); summedOut.divideBy(messages[i]); factorTables[i].multiplyInEnd(summedOut); } return new CRFCliqueTree<>(factorTables, classIndex, backgroundSymbol); }
/** * @return a new CRFCliqueTree for the weights on the data */ public static CRFCliqueTree getCalibratedCliqueTree(double[][] weights, int[][][] data, Index[] labelIndices, int numClasses, Index classIndex, String backgroundSymbol) { FactorTable[] factorTables = new FactorTable[data.length]; FactorTable[] messages = new FactorTable[data.length - 1]; for (int i = 0; i < data.length; i++) { factorTables[i] = getFactorTable(weights, data[i], labelIndices, numClasses); if (i > 0) { messages[i - 1] = factorTables[i - 1].sumOutFront(); factorTables[i].multiplyInFront(messages[i - 1]); } } for (int i = factorTables.length - 2; i >= 0; i--) { FactorTable summedOut = factorTables[i + 1].sumOutEnd(); summedOut.divideBy(messages[i]); factorTables[i].multiplyInEnd(summedOut); } return new CRFCliqueTree(factorTables, classIndex, backgroundSymbol); }
/** * @return a new CRFCliqueTree for the weights on the data */ public static <E> CRFCliqueTree<E> getCalibratedCliqueTree(int[][][] data, List<Index<CRFLabel>> labelIndices, int numClasses, Index<E> classIndex, E backgroundSymbol, CliquePotentialFunction cliquePotentialFunc, double[][][] featureVals) { FactorTable[] factorTables = new FactorTable[data.length]; FactorTable[] messages = new FactorTable[data.length - 1]; for (int i = 0; i < data.length; i++) { double[][] featureValByCliqueSize = null; if (featureVals != null) featureValByCliqueSize = featureVals[i]; factorTables[i] = getFactorTable(data[i], labelIndices, numClasses, cliquePotentialFunc, featureValByCliqueSize, i); // log.info("before calibration,FT["+i+"] = " + factorTables[i].toProbString()); if (i > 0) { messages[i - 1] = factorTables[i - 1].sumOutFront(); // log.info("forward message, message["+(i-1)+"] = " + messages[i-1].toProbString()); factorTables[i].multiplyInFront(messages[i - 1]); // log.info("after forward calibration, FT["+i+"] = " + factorTables[i].toProbString()); } } for (int i = factorTables.length - 2; i >= 0; i--) { FactorTable summedOut = factorTables[i + 1].sumOutEnd(); summedOut.divideBy(messages[i]); // log.info("backward summedOut, summedOut= " + summedOut.toProbString()); factorTables[i].multiplyInEnd(summedOut); // log.info("after backward calibration, FT["+i+"] = " + factorTables[i].toProbString()); } return new CRFCliqueTree<>(factorTables, classIndex, backgroundSymbol); }
/** * @return a new CRFCliqueTree for the weights on the data */ public static <E> CRFCliqueTree<E> getCalibratedCliqueTree(int[][][] data, List<Index<CRFLabel>> labelIndices, int numClasses, Index<E> classIndex, E backgroundSymbol, CliquePotentialFunction cliquePotentialFunc, double[][][] featureVals) { FactorTable[] factorTables = new FactorTable[data.length]; FactorTable[] messages = new FactorTable[data.length - 1]; for (int i = 0; i < data.length; i++) { double[][] featureValByCliqueSize = null; if (featureVals != null) featureValByCliqueSize = featureVals[i]; factorTables[i] = getFactorTable(data[i], labelIndices, numClasses, cliquePotentialFunc, featureValByCliqueSize, i); // System.err.println("before calibration,FT["+i+"] = " + factorTables[i].toProbString()); if (i > 0) { messages[i - 1] = factorTables[i - 1].sumOutFront(); // System.err.println("forward message, message["+(i-1)+"] = " + messages[i-1].toProbString()); factorTables[i].multiplyInFront(messages[i - 1]); // System.err.println("after forward calibration, FT["+i+"] = " + factorTables[i].toProbString()); } } for (int i = factorTables.length - 2; i >= 0; i--) { FactorTable summedOut = factorTables[i + 1].sumOutEnd(); summedOut.divideBy(messages[i]); // System.err.println("backward summedOut, summedOut= " + summedOut.toProbString()); factorTables[i].multiplyInEnd(summedOut); // System.err.println("after backward calibration, FT["+i+"] = " + factorTables[i].toProbString()); } return new CRFCliqueTree<E>(factorTables, classIndex, backgroundSymbol); }