Exemple #1
0
 //
 // PROB OF TAG AT SINGLE POSITION CONDITIONED ON PREVIOUS AND FOLLOWING
 // SEQUENCE OF LABELS
 //
 // public double condProbGivenPreviousAndNext(int position, int label, int[]
 // prevLabels, int[] nextLabels) {
 // }
 //
 // JOINT CONDITIONAL PROBS
 //
 /// <returns>a new CRFCliqueTree for the weights on the data</returns>
 public static Edu.Stanford.Nlp.IE.Crf.CRFCliqueTree <E> GetCalibratedCliqueTree <E>(int[][][] data, IList <IIndex <CRFLabel> > labelIndices, int numClasses, IIndex <E> classIndex, E backgroundSymbol, ICliquePotentialFunction 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_1 = factorTables.Length - 2; i_1 >= 0; i_1--)
     {
         FactorTable summedOut = factorTables[i_1 + 1].SumOutEnd();
         summedOut.DivideBy(messages[i_1]);
         // log.info("backward summedOut, summedOut= " + summedOut.toProbString());
         factorTables[i_1].MultiplyInEnd(summedOut);
     }
     // log.info("after backward calibration, FT["+i+"] = " + factorTables[i].toProbString());
     return(new Edu.Stanford.Nlp.IE.Crf.CRFCliqueTree <E>(factorTables, classIndex, backgroundSymbol));
 }
Exemple #2
0
 //
 // PROB OF TAG AT SINGLE POSITION CONDITIONED ON FOLLOWING SEQUENCE OF LABELS
 //
 public virtual 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.Array.Copy(nextLabels, 0, p, 0, p.Length);
             return(factorTables[position].ConditionalLogProbGivenPrevious(p, label));
         }
     }
 }
Exemple #3
0
        // static FactorTable getFactorTable(double[][] weights, int[][] data, List<Index<CRFLabel>> labelIndices, int numClasses, int posInSent) {
        //   CliquePotentialFunction cliquePotentialFunc = new LinearCliquePotentialFunction(weights);
        //   return getFactorTable(data, labelIndices, numClasses, cliquePotentialFunc, null, posInSent);
        // }
        internal static FactorTable GetFactorTable(int[][] data, IList <IIndex <CRFLabel> > labelIndices, int numClasses, ICliquePotentialFunction cliquePotentialFunc, double[][] featureValByCliqueSize, int posInSent)
        {
            FactorTable factorTable = null;

            for (int j = 0; j < sz; j++)
            {
                IIndex <CRFLabel> labelIndex = labelIndices[j];
                FactorTable       ft         = new FactorTable(numClasses, j + 1);
                double[]          featureVal = null;
                if (featureValByCliqueSize != null)
                {
                    featureVal = featureValByCliqueSize[j];
                }
                // ... and each possible labeling for that clique
                for (int k = 0; k < liSize; k++)
                {
                    int[]  label           = labelIndex.Get(k).GetLabel();
                    double cliquePotential = cliquePotentialFunc.ComputeCliquePotential(j + 1, k, data[j], featureVal, posInSent);
                    // for (int m = 0; m < data[j].length; m++) {
                    //   weight += weights[data[j][m]][k];
                    // }
                    // try{
                    ft.SetValue(label, cliquePotential);
                }
                // } catch (Exception e) {
                // System.out.println("CRFCliqueTree::getFactorTable");
                // System.out.println("NumClasses: " + numClasses + " j+1: " + (j+1));
                // System.out.println("k: " + k+" label: " +label+" labelIndexSize: " +
                // labelIndex.size());
                // throw new RunTimeException(e.toString());
                // }
                if (j > 0)
                {
                    ft.MultiplyInEnd(factorTable);
                }
                factorTable = ft;
            }
            return(factorTable);
        }
Exemple #4
0
 /// <summary>This function assumes a LinearCliquePotentialFunction is used for wrapping the weights</summary>
 /// <returns>a new CRFCliqueTree for the weights on the data</returns>
 public static Edu.Stanford.Nlp.IE.Crf.CRFCliqueTree <E> GetCalibratedCliqueTree <E>(double[] weights, double wscale, int[][] weightIndices, int[][][] data, IList <IIndex <CRFLabel> > labelIndices, int numClasses, IIndex <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_1 = factorTables.Length - 2; i_1 >= 0; i_1--)
     {
         FactorTable summedOut = factorTables[i_1 + 1].SumOutEnd();
         summedOut.DivideBy(messages[i_1]);
         factorTables[i_1].MultiplyInEnd(summedOut);
     }
     return(new Edu.Stanford.Nlp.IE.Crf.CRFCliqueTree <E>(factorTables, classIndex, backgroundSymbol));
 }
Exemple #5
0
        private static FactorTable GetFactorTable(double[] weights, double wScale, int[][] weightIndices, int[][] data, IList <IIndex <CRFLabel> > labelIndices, int numClasses)
        {
            FactorTable factorTable = null;

            for (int j = 0; j < sz; j++)
            {
                IIndex <CRFLabel> labelIndex = labelIndices[j];
                FactorTable       ft         = new FactorTable(numClasses, j + 1);
                // ... and each possible labeling for that clique
                for (int k = 0; k < liSize; k++)
                {
                    int[]  label  = labelIndex.Get(k).GetLabel();
                    double weight = 0.0;
                    for (int m = 0; m < data[j].Length; m++)
                    {
                        int wi = weightIndices[data[j][m]][k];
                        weight += wScale * weights[wi];
                    }
                    // try{
                    ft.SetValue(label, weight);
                }
                // } catch (Exception e) {
                // System.out.println("CRFCliqueTree::getFactorTable");
                // System.out.println("NumClasses: " + numClasses + " j+1: " + (j+1));
                // System.out.println("k: " + k+" label: " +label+" labelIndexSize: " +
                // labelIndex.size());
                // throw new RunTimeException(e.toString());
                // }
                if (j > 0)
                {
                    ft.MultiplyInEnd(factorTable);
                }
                factorTable = ft;
            }
            return(factorTable);
        }
Exemple #6
0
        /// <summary>
        /// Computes the unnormalized log conditional distribution over values of the
        /// element at position pos in the sequence, conditioned on the values of the
        /// elements in all other positions of the provided sequence.
        /// </summary>
        /// <param name="sequence">the sequence containing the rest of the values to condition on</param>
        /// <param name="position">the position of the element to give a distribution for</param>
        /// <returns>
        /// an array of type double, representing a probability distribution;
        /// sums to 1.0
        /// </returns>
        public virtual double[] ScoresOf(int[] sequence, int position)
        {
            if (position >= factorTables.Length)
            {
                throw new Exception("Index out of bounds: " + position);
            }
            // DecimalFormat nf = new DecimalFormat("#0.000");
            // if (position>0 && position<sequence.length-1) System.out.println(position
            // + ": asking about " +sequence[position-1] + "(" + sequence[position] +
            // ")" + sequence[position+1]);
            double[] probThisGivenPrev = new double[numClasses];
            double[] probNextGivenThis = new double[numClasses];
            // double[] marginal = new double[numClasses]; // for debugging only
            // compute prob of this tag given the window-1 previous tags, normalized
            // extract the window-1 previous tags, pad left with background if necessary
            int prevLength = windowSize - 1;

            int[] prev = new int[prevLength + 1];
            // leave an extra element for the
            // label at this position
            int i = 0;

            for (; i < prevLength - position; i++)
            {
                // will only happen if
                // position-prevLength < 0
                prev[i] = classIndex.IndexOf(backgroundSymbol);
            }
            for (; i < prevLength; i++)
            {
                prev[i] = sequence[position - prevLength + i];
            }
            for (int label = 0; label < numClasses; label++)
            {
                prev[prev.Length - 1]    = label;
                probThisGivenPrev[label] = factorTables[position].UnnormalizedLogProb(prev);
            }
            // marginal[label] = factorTables[position].logProbEnd(label); // remove:
            // for debugging only
            // ArrayMath.logNormalize(probThisGivenPrev);
            // compute the prob of the window-1 next tags given this tag
            // extract the window-1 next tags
            int nextLength = windowSize - 1;

            if (position + nextLength >= Length())
            {
                nextLength = Length() - position - 1;
            }
            FactorTable nextFactorTable = factorTables[position + nextLength];

            if (nextLength != windowSize - 1)
            {
                for (int j = 0; j < windowSize - 1 - nextLength; j++)
                {
                    nextFactorTable = nextFactorTable.SumOutFront();
                }
            }
            if (nextLength == 0)
            {
                // we are asking about the prob of no sequence
                Arrays.Fill(probNextGivenThis, 1.0);
            }
            else
            {
                int[] next = new int[nextLength];
                System.Array.Copy(sequence, position + 1, next, 0, nextLength);
                for (int label_1 = 0; label_1 < numClasses; label_1++)
                {
                    // ask the factor table such that pos is the first position in the
                    // window
                    // probNextGivenThis[label] =
                    // factorTables[position+nextLength].conditionalLogProbGivenFirst(label,
                    // next);
                    // probNextGivenThis[label] =
                    // nextFactorTable.conditionalLogProbGivenFirst(label, next);
                    probNextGivenThis[label_1] = nextFactorTable.UnnormalizedConditionalLogProbGivenFirst(label_1, next);
                }
            }
            // pointwise multiply
            return(ArrayMath.PairwiseAdd(probThisGivenPrev, probNextGivenThis));
        }