Ejemplo n.º 1
0
        public virtual Edu.Stanford.Nlp.IE.Crf.FactorTable SumOutFront()
        {
            Edu.Stanford.Nlp.IE.Crf.FactorTable ft = new Edu.Stanford.Nlp.IE.Crf.FactorTable(numClasses, windowSize - 1);
            int stride = ft.Size();

            for (int i = 0; i < stride; i++)
            {
                ft.SetValue(i, ArrayMath.LogSum(table, i, table.Length, stride));
            }
            return(ft);
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            int numClasses = 6;
            int cliqueSize = 3;

            System.Console.Error.Printf("Creating factor table with %d classes and window (clique) size %d%n", numClasses, cliqueSize);
            Edu.Stanford.Nlp.IE.Crf.FactorTable ft = new Edu.Stanford.Nlp.IE.Crf.FactorTable(numClasses, cliqueSize);
            for (int i = 0; i < numClasses; i++)
            {
                for (int j = 0; j < numClasses; j++)
                {
                    for (int k = 0; k < numClasses; k++)
                    {
                        int[] b = new int[] { i, j, k };
                        ft.SetValue(b, (i * 4) + (j * 2) + k);
                    }
                }
            }
            log.Info(ft);
            double normalization = 0.0;

            for (int i_1 = 0; i_1 < numClasses; i_1++)
            {
                for (int j = 0; j < numClasses; j++)
                {
                    for (int k = 0; k < numClasses; k++)
                    {
                        normalization += ft.UnnormalizedLogProb(new int[] { i_1, j, k });
                    }
                }
            }
            log.Info("Normalization Z = " + normalization);
            log.Info(ft.SumOutFront());
            Edu.Stanford.Nlp.IE.Crf.FactorTable ft2 = new Edu.Stanford.Nlp.IE.Crf.FactorTable(numClasses, 2);
            for (int i_2 = 0; i_2 < numClasses; i_2++)
            {
                for (int j = 0; j < numClasses; j++)
                {
                    int[] b = new int[] { i_2, j };
                    ft2.SetValue(b, i_2 * numClasses + j);
                }
            }
            log.Info(ft2);
            // FactorTable ft3 = ft2.sumOutFront();
            // log.info(ft3);
            for (int i_3 = 0; i_3 < numClasses; i_3++)
            {
                for (int j = 0; j < numClasses; j++)
                {
                    int[]  b = new int[] { i_3, j };
                    double t = 0;
                    for (int k = 0; k < numClasses; k++)
                    {
                        t += System.Math.Exp(ft.ConditionalLogProbGivenPrevious(b, k));
                        System.Console.Error.WriteLine(k + "|" + i_3 + ',' + j + " : " + System.Math.Exp(ft.ConditionalLogProbGivenPrevious(b, k)));
                    }
                    log.Info(t);
                }
            }
            log.Info("conditionalLogProbGivenFirst");
            for (int j_1 = 0; j_1 < numClasses; j_1++)
            {
                for (int k = 0; k < numClasses; k++)
                {
                    int[]  b = new int[] { j_1, k };
                    double t = 0.0;
                    for (int i_4 = 0; i_4 < numClasses; i_4++)
                    {
                        t += ft.UnnormalizedConditionalLogProbGivenFirst(i_4, b);
                        System.Console.Error.WriteLine(i_4 + "|" + j_1 + ',' + k + " : " + ft.UnnormalizedConditionalLogProbGivenFirst(i_4, b));
                    }
                    log.Info(t);
                }
            }
            log.Info("conditionalLogProbGivenFirst");
            for (int i_5 = 0; i_5 < numClasses; i_5++)
            {
                for (int j = 0; j_1 < numClasses; j_1++)
                {
                    int[]  b = new int[] { i_5, j_1 };
                    double t = 0.0;
                    for (int k = 0; k < numClasses; k++)
                    {
                        t += ft.ConditionalLogProbGivenNext(b, k);
                        System.Console.Error.WriteLine(i_5 + "," + j_1 + '|' + k + " : " + ft.ConditionalLogProbGivenNext(b, k));
                    }
                    log.Info(t);
                }
            }
            numClasses = 2;
            Edu.Stanford.Nlp.IE.Crf.FactorTable ft3 = new Edu.Stanford.Nlp.IE.Crf.FactorTable(numClasses, cliqueSize);
            ft3.SetValue(new int[] { 0, 0, 0 }, System.Math.Log(0.25));
            ft3.SetValue(new int[] { 0, 0, 1 }, System.Math.Log(0.35));
            ft3.SetValue(new int[] { 0, 1, 0 }, System.Math.Log(0.05));
            ft3.SetValue(new int[] { 0, 1, 1 }, System.Math.Log(0.07));
            ft3.SetValue(new int[] { 1, 0, 0 }, System.Math.Log(0.08));
            ft3.SetValue(new int[] { 1, 0, 1 }, System.Math.Log(0.16));
            ft3.SetValue(new int[] { 1, 1, 0 }, System.Math.Log(1e-50));
            ft3.SetValue(new int[] { 1, 1, 1 }, System.Math.Log(1e-50));
            Edu.Stanford.Nlp.IE.Crf.FactorTable ft4 = ft3.SumOutFront();
            log.Info(ft4.ToNonLogString());
            Edu.Stanford.Nlp.IE.Crf.FactorTable ft5 = ft3.SumOutEnd();
            log.Info(ft5.ToNonLogString());
        }