Example #1
0
        public HyperVertex(bool isTerminal, int tag, int subtagCounts, int subtagCapacity = -1)
        {
            this.tag = tag;
            this.TYPE = isTerminal ? VTYPE.TERMINAL : VTYPE.NONTERMINAL;
            this.posteriorScore = isTerminal ? 0.0f : double.NegativeInfinity;
            this.posteriorViterbiAlpha = isTerminal ? 0.0f : double.NegativeInfinity;
            this.subtagCount = subtagCounts;
            if (subtagCapacity < 0) {
                subtagCapacity = subtagCounts;
            }
            _alpha = new double[subtagCapacity];
            _beta = new double[subtagCapacity];
            pruned = new bool[subtagCapacity];

            alpha = new RiggedArray { v = _alpha, pruned = this.pruned, Length = subtagCount };
            beta = new RiggedArray { v = _beta, pruned = this.pruned, Length = subtagCount };

            if (!isTerminal) {
                for (int i = 0; i < _alpha.Length; ++i) {
                    _alpha [i] = double.NegativeInfinity;
                }
            }

            for (int i = 0; i < _beta.Length; ++i) {
                _beta [i] = double.NegativeInfinity;
            }
        }
Example #2
0
        public HyperVertex(int subtagCapacity)
        {
            _alpha = new double[subtagCapacity];
            _beta = new double[subtagCapacity];
            pruned = new bool[subtagCapacity];

            alpha = new RiggedArray { v = _alpha, pruned = this.pruned, Length = subtagCount };
            beta = new RiggedArray { v = _beta, pruned = this.pruned, Length = subtagCount };
        }
Example #3
0
        public static void SumBackward(RiggedArray pbeta, double[] terminalScore, RiggedArray tbeta)
        {
            if (tbeta.Length != 1 || tbeta.pruned [0]) {
                throw new Exception ("wrong terminal rules!");
            }

            //tbeta[0] += MathHelper.InnerProduct(pbeta, terminalScore);
            tbeta.v [0] = MathHelper.LogAdd (tbeta.v [0], MathHelper.LogInnerProduct (pbeta.v, terminalScore, pbeta.pruned, pbeta.Length));
        }
Example #4
0
        public static void SumBackward(RiggedArray pbeta, double[][] unaryScores, RiggedArray cbeta)
        {
            for (int c = 0; c < cbeta.Length; ++c) {
                if (cbeta.pruned [c] || unaryScores [c] == null) {
                    continue;
                }

                //cbeta[c] += MathHelper.InnerProduct(unaryScores[c], pbeta);
                //cbeta[c] += MathHelper.InnerProduct(unaryScores[c], pbeta);
                cbeta.v [c] = MathHelper.LogAdd (cbeta.v [c], MathHelper.LogInnerProduct (unaryScores [c], pbeta.v, pbeta.pruned, pbeta.Length));
            }
        }
Example #5
0
 public void MaxForwardDUMMY(ViterbiTrace[] traces, RiggedArray palpha, RiggedArray calpha)
 {
     for (int i = 0; i < palpha.Length; ++i)
     {
         if (palpha.pruned[i] || calpha.pruned[i])
         {
             continue;
         }
         double s = calpha.v[i];
         if (s > palpha.v[i])
         {
             traces[i].edge = this;
             traces[i].subtag0 = i;
             traces[i].subtag1 = -1;
             palpha.v[i] = s;
         }
     }
 }
Example #6
0
        public void MaxForward(ViterbiTrace[] traces, RiggedArray palpha, double[][] unaryScores, RiggedArray calpha)
        {
            for (int c = 0; c < calpha.Length; ++c)
            {
                if (calpha.pruned[c] || double.IsNegativeInfinity(calpha.v[c]) || unaryScores[c] == null)
                {
                    continue;
                }

                //MathHelper.ax_addto_y(calpha[c], unaryScores[c], palpha);

                for (int p = 0; p < palpha.Length; ++p)
                {
                    if (palpha.pruned[p])
                    {
                        continue;
                    }

                    double s = calpha.v[c] + unaryScores[c][p];
                    if (s > palpha.v[p])
                    {
                        traces[p].edge = this;
                        traces[p].subtag0 = c;
                        traces[p].subtag1 = -1;
                        palpha.v[p] = s;
                    }
                }
            }
        }
Example #7
0
        public void MaxForward(ViterbiTrace[] traces, RiggedArray palpha, double[][][] binaryScores, RiggedArray lalpha, RiggedArray ralpha)
        {
            for (int l = 0; l < lalpha.Length; ++l)
            {
                if (lalpha.pruned[l] || double.IsNegativeInfinity(lalpha.v[l]) || binaryScores[l] == null)
                {
                    continue;
                }

                for (int r = 0; r < ralpha.Length; ++r)
                {
                    if (ralpha.pruned[r] || double.IsNegativeInfinity(ralpha.v[r]) || binaryScores[l][r] == null)
                    {
                        continue;
                    }

                    //MathHelper.ax_addto_y(lalpha[l] * ralpha[r], binaryScores[l][r], palpha);
                    double x = lalpha.v[l] + ralpha.v[r];

                    for (int p = 0; p < palpha.Length; ++p)
                    {
                        if (palpha.pruned[p])
                        {
                            continue;
                        }
                        double s = x + binaryScores[l][r][p];

                        if (s > palpha.v[p])
                        {
                            traces[p].edge = this;
                            traces[p].subtag0 = l;
                            traces[p].subtag1 = r;
                            palpha.v[p] = s;
                        }
                    }

                }
            }
        }
Example #8
0
 public static void SumForwardDUMMY(RiggedArray palpha, RiggedArray calpha)
 {
     for (int i = 0; i < palpha.Length; ++i) {
         if (palpha.pruned [i] || calpha.pruned [i]) {
             continue;
         }
         palpha.v [i] = MathHelper.LogAdd (palpha.v [i], calpha.v [i]);
     }
 }
Example #9
0
        public void MaxForward(ViterbiTrace[] traces, RiggedArray palpha, double[] terminalScores)
        {
            //MathHelper.ax_addto_y(1.0f, terminalScores, palpha);

            for (int i = 0; i < terminalScores.Length; ++i)
            {
                if (palpha.pruned[i])
                {
                    continue;
                }
                if (terminalScores[i] > palpha.v[i])
                {
                    traces[i].edge = this;
                    traces[i].subtag0 = 0;
                    traces[i].subtag1 = -1;
                    palpha.v[i] = terminalScores[i];
                }
                //palpha.v[i] = MathHelper.LogAdd(palpha.v[i], terminalScores[i]);
            }
        }
Example #10
0
        public static void SumForward(RiggedArray palpha, double[][][] binaryScores, RiggedArray lalpha, RiggedArray ralpha)
        {
            for (int l = 0; l < lalpha.Length; ++l) {
                if (lalpha.pruned [l] || double.IsNegativeInfinity (lalpha.v [l]) || binaryScores [l] == null) {
                    continue;
                }

                for (int r = 0; r < ralpha.Length; ++r) {
                    if (ralpha.pruned [r] || double.IsNegativeInfinity (ralpha.v [r]) || binaryScores [l] [r] == null) {
                        continue;
                    }

                    double x = lalpha.v [l] + ralpha.v [r];

                    for (int p = 0; p < palpha.Length; ++p) {
                        if (palpha.pruned [p]) {
                            continue;
                        }
                        palpha.v [p] = MathHelper.LogAdd (x + binaryScores [l] [r] [p], palpha.v [p]);
                    }
                }
            }
        }
Example #11
0
        public static void SumForward(RiggedArray palpha, double[][] unaryScores, RiggedArray calpha)
        {
            for (int c = 0; c < calpha.Length; ++c) {
                if (calpha.pruned [c] || double.IsNegativeInfinity (calpha.v [c]) || unaryScores [c] == null) {
                    continue;
                }

                //MathHelper.ax_addto_y(calpha[c], unaryScores[c], palpha);

                for (int p = 0; p < palpha.Length; ++p) {
                    if (palpha.pruned [p]) {
                        continue;
                    }
                    palpha.v [p] = MathHelper.LogAdd (calpha.v [c] + unaryScores [c] [p], palpha.v [p]);
                }
            }
        }
Example #12
0
        public static void SumForward(RiggedArray palpha, double[] terminalScores)
        {
            //MathHelper.ax_addto_y(1.0f, terminalScores, palpha);

            for (int i = 0; i < terminalScores.Length; ++i) {
                if (palpha.pruned [i]) {
                    continue;
                }
                palpha.v [i] = MathHelper.LogAdd (palpha.v [i], terminalScores [i]);
            }
        }
Example #13
0
 public static void SumBackwardDUMMY(RiggedArray pbeta, RiggedArray cbeta)
 {
     for (int i = 0; i < pbeta.Length; ++i) {
         if (pbeta.pruned [i] || cbeta.pruned [i]) {
             continue;
         }
         cbeta.v [i] = MathHelper.LogAdd (cbeta.v [i], pbeta.v [i]);
     }
 }
Example #14
0
        public static void SumBackward(
            RiggedArray pbeta,
            double[][][] binaryScores,
            RiggedArray lalpha,
            RiggedArray ralpha,
            RiggedArray lbeta,
            RiggedArray rbeta,
            double[] lbuf)
        {
            //double[] lbuf = new double[ralpha.Length + 1];

            for (int l = 0; l < lalpha.Length; ++l) {
                if (binaryScores [l] == null || lalpha.pruned [l]) {
                    continue;
                }

                lbuf [0] = lbeta.v [l];
                int next = 1;

                for (int r = 0; r < ralpha.Length; ++r) {
                    if (binaryScores [l] [r] == null || ralpha.pruned [r]) {
                        continue;
                    }

                    //lbeta[l] += ralpha[r] * MathHelper.InnerProduct(binaryScores[l][r], pbeta);

                    //rbeta[r] += lalpha[l] * MathHelper.InnerProduct(binaryScores[l][r], pbeta);

                    double x = MathHelper.LogInnerProduct (binaryScores [l] [r], pbeta.v, pbeta.pruned, pbeta.Length);

                    //if (!sb.Get(l, r, out x))
                    //{
                    //    x = MathHelper.LogInnerProduct(binaryScores[l][r], pbeta.v, pbeta.pruned, pbeta.Length);
                    //    sb.Set(l, r, x);
                    //}

                    //lbeta.v[l] = MathHelper.LogAdd(lbeta.v[l], ralpha.v[r] + x);
                    lbuf [next] = ralpha.v [r] + x;
                    next += 1;
                    rbeta.v [r] = MathHelper.LogAdd (rbeta.v [r], lalpha.v [l] + x);
                }

                if (next > 1) {
                    lbeta.v [l] = MathHelper.LogAdd (lbuf, next);
                }
            }
        }