コード例 #1
0
        public Lattice(model m, inference inf, dataSeq x)
        {
            _w = x.Count;
            _h = m.NTag;

            _logBel = new belief(_w, _h);

            List <dMatrix>        YYlist = new List <dMatrix>();
            List <List <double> > Ylist  = new List <List <double> >();

            inf.getYYandY(m, x, YYlist, Ylist);

            for (int i = 0; i < _w; i++)
            {
                _logBel.belState[i] = new List <double>(Ylist[i]);

                if (i > 0)
                {
                    _logBel.belEdge[i] = new dMatrix(YYlist[i]);
                }
            }

            _heuListList = new List <List <double> >();
            for (int i = 0; i < _w; i++)
            {
                _heuListList.Add(new List <double>(new double[_h]));
            }

            Viterbi _bwdViterbi = new Viterbi(_w, _h);

            for (int i = 0; i < _w; i++)
            {
                _bwdViterbi.setScores(i, Ylist[i], YYlist[i]);
            }
            List <int> tags = new List <int>();

            _bwdViterbi.runViterbi(ref tags);
            //update the viterbiHeuristicMap
            for (int i = 0; i < _w; i++)
            {
                for (int j = 0; j < _h; j++)
                {
                    double h = _bwdViterbi.getPathScore(i, j);
                    setHeuMap(i, j, h);
                }
            }

            //get zGold
            ZGold = 0;
            for (int i = 0; i < x.Count; i++)
            {
                int s = x.getTags(i);
                ZGold += Ylist[i][s];
                if (i > 0)
                {
                    int sPre = x.getTags(i - 1);
                    ZGold += YYlist[i][sPre, s];
                }
            }
        }
コード例 #2
0
        //fast viterbi decode without probability
        public void decodeViterbi_test(model m, dataSeq x, List <int> tags)
        {
            tags.Clear();

            int     nNode  = x.Count;
            int     nState = m.NState;
            dMatrix YY     = new dMatrix(nState, nState);

            double[]      dAry  = new double[nState];
            List <double> Y     = new List <double>(dAry);
            Viterbi       viter = new Viterbi(nNode, nState);

            for (int i = 0; i < nNode; i++)
            {
                getLogYY(m, x, i, ref YY, ref Y, false, false);
                viter.setScores(i, Y, YY);
            }

            List <int> states = new List <int>();
            double     numer  = viter.runViterbi(ref states, false);

            for (int i = 0; i < states.Count; i++)
            {
                int tag = m.hStateToTag(states[i]);
                tags.Add(tag);
            }
        }
コード例 #3
0
        public double decodeViterbi(model m, dataSeq x, List <int> tags)
        {
            tags.Clear();
            int     nNode = x.Count;
            int     nTag  = m.NTag;
            dMatrix YY    = new dMatrix(nTag, nTag);

            double[]      dAry  = new double[nTag];
            List <double> Y     = new List <double>(dAry);
            Viterbi       viter = new Viterbi(nNode, nTag);

            for (int i = 0; i < nNode; i++)
            {
                getLogYY(m, x, i, ref YY, ref Y, false, false);
                viter.setScores(i, Y, YY);
            }

            List <int> states = new List <int>();
            double     numer  = viter.runViterbi(ref states, false);

            for (int i = 0; i < states.Count; i++)
            {
                int tag = states[i];
                tags.Add(tag);
            }
            double Z = getZ(m, x, false);

            return(Math.Exp(numer - Z));
        }
コード例 #4
0
        //fast viterbi decode without probability
        public void decodeViterbi_train(model m, dataSeq x, List <dMatrix> YYlist, List <List <double> > Ylist, List <int> tags)
        {
            int     nNode  = x.Count;
            int     nState = m.NState;
            Viterbi viter  = new Viterbi(nNode, nState);

            for (int i = 0; i < nNode; i++)
            {
                viter.setScores(i, Ylist[i], YYlist[i]);
            }

            double numer = viter.runViterbi(ref tags, false);
        }
コード例 #5
0
ファイル: Inference.cs プロジェクト: xusun26/Decode-CRF
        //fast viterbi decode without probability
        public void decodeViterbi_test(model m, dataSeq x, List <int> tags)
        {
            tags.Clear();

            int     nNode = x.Count;
            int     nTag  = m.NTag;
            dMatrix YY    = new dMatrix(nTag, nTag);

            double[]      dAry  = new double[nTag];
            List <double> Y     = new List <double>(dAry);
            Viterbi       viter = new Viterbi(nNode, nTag);

            for (int i = 0; i < nNode; i++)
            {
                getLogYY(m, x, i, ref YY, ref Y, false, false);
                viter.setScores(i, Y, YY);
            }

            viter.runViterbi(ref tags);
        }