예제 #1
0
        public static FloatFactorTable GetFloatFactorTable(float[][] weights, int[][] data, IList <IIndex <CRFLabel> > labelIndices, int numClasses)
        {
            FloatFactorTable factorTable = null;

            for (int j = 0; j < labelIndices.Count; j++)
            {
                IIndex <CRFLabel> labelIndex = labelIndices[j];
                FloatFactorTable  ft         = new FloatFactorTable(numClasses, j + 1);
                // ...and each possible labeling for that clique
                for (int k = 0; k < labelIndex.Size(); k++)
                {
                    int[] label  = labelIndex.Get(k).GetLabel();
                    float weight = 0.0f;
                    for (int m = 0; m < data[j].Length; m++)
                    {
                        //log.info("**"+weights[data[j][m]][k]);
                        weight += weights[data[j][m]][k];
                    }
                    ft.SetValue(label, weight);
                }
                //log.info(">>"+ft);
                //log.info("::"+ft);
                if (j > 0)
                {
                    ft.MultiplyInEnd(factorTable);
                }
                //log.info("::"+ft);
                factorTable = ft;
            }
            return(factorTable);
        }
예제 #2
0
 public static FloatFactorTable[] GetCalibratedCliqueTree(float[][] weights, int[][][] data, IList <IIndex <CRFLabel> > labelIndices, int numClasses)
 {
     //       for (int i = 0; i < weights.length; i++) {
     //         for (int j = 0; j < weights[i].length; j++) {
     //           log.info(i+" "+j+": "+weights[i][j]);
     //         }
     //       }
     //log.info("calibrating clique tree");
     FloatFactorTable[] factorTables = new FloatFactorTable[data.Length];
     FloatFactorTable[] messages     = new FloatFactorTable[data.Length - 1];
     for (int i = 0; i < data.Length; i++)
     {
         factorTables[i] = GetFloatFactorTable(weights, data[i], labelIndices, numClasses);
         if (Verbose)
         {
             log.Info(i + ": " + factorTables[i]);
         }
         if (i > 0)
         {
             messages[i - 1] = factorTables[i - 1].SumOutFront();
             if (Verbose)
             {
                 log.Info(messages[i - 1]);
             }
             factorTables[i].MultiplyInFront(messages[i - 1]);
             if (Verbose)
             {
                 log.Info(factorTables[i]);
                 if (i == data.Length - 1)
                 {
                     log.Info(i + ": " + factorTables[i].ToProbString());
                 }
             }
         }
     }
     for (int i_1 = factorTables.Length - 2; i_1 >= 0; i_1--)
     {
         FloatFactorTable summedOut = factorTables[i_1 + 1].SumOutEnd();
         if (Verbose)
         {
             log.Info((i_1 + 1) + "-->" + i_1 + ": " + summedOut);
         }
         summedOut.DivideBy(messages[i_1]);
         if (Verbose)
         {
             log.Info((i_1 + 1) + "-->" + i_1 + ": " + summedOut);
         }
         factorTables[i_1].MultiplyInEnd(summedOut);
         if (Verbose)
         {
             log.Info(i_1 + ": " + factorTables[i_1]);
             log.Info(i_1 + ": " + factorTables[i_1].ToProbString());
         }
     }
     return(factorTables);
 }