Ejemplo n.º 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];
                }
            }
        }
Ejemplo n.º 2
0
        //the scalar version
        virtual public void getLogYY(double scalar, model m, dataSeq x, int i, ref dMatrix YY, ref List <double> Y, bool takeExp, bool mask)
        {
            YY.set(0);
            listTool.listSet(ref Y, 0);

            List <double>      w     = m.W;
            List <featureTemp> fList = _fGene.getFeatureTemp(x, i);
            int nTag = m.NTag;

            for (int j = 0; j < fList.Count; j++)
            {
                featureTemp ptr = fList[j];
                int         id  = ptr.id;
                double      v   = ptr.val;
                for (int s = 0; s < nTag; s++)
                {
                    int f = _fGene.getNodeFeatID(id, s);
                    Y[s] += w[f] * scalar * v;
                }
            }
            if (i > 0)
            {
                for (int s = 0; s < nTag; s++)
                {
                    for (int sPre = 0; sPre < nTag; sPre++)
                    {
                        int f = _fGene.getEdgeFeatID(sPre, s);
                        YY[sPre, s] += w[f] * scalar;
                    }
                }
            }
            double maskValue = double.MinValue;

            if (takeExp)
            {
                listTool.listExp(ref Y);
                YY.eltExp();
                maskValue = 0;
            }
            if (mask)
            {
                List <int> tagList = x.getTags();
                for (int s = 0; s < Y.Count; s++)
                {
                    if (tagList[i] != s)
                    {
                        Y[s] = maskValue;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void getYYandY(model m, dataSeq x, List <dMatrix> YYlist, List <List <double> > Ylist, List <dMatrix> maskYYlist, List <List <double> > maskYlist)
        {
            int nNodes = x.Count;
            //int nTag = m.NTag;
            int nState = m.NState;

            double[] dAry = new double[nState];
            bool     mask = false;

            try
            {
                //Global.rwlock.AcquireReaderLock(Global.readWaitTime);

                for (int i = 0; i < nNodes; i++)
                {
                    dMatrix       YYi = new dMatrix(nState, nState);
                    List <double> Yi  = new List <double>(dAry);
                    //compute the Mi matrix
                    getLogYY(m, x, i, ref YYi, ref Yi, false, mask);
                    YYlist.Add(YYi);
                    Ylist.Add(Yi);

                    maskYYlist.Add(new dMatrix(YYi));
                    maskYlist.Add(new List <double>(Yi));
                }

                //Global.rwlock.ReleaseReaderLock();
            }
            catch (ApplicationException)
            {
                Console.WriteLine("read out time!");
            }

            //get the masked YY and Y
            double  maskValue      = double.MinValue;
            dMatrix statesPerNodes = m.getStatesPerNode(x);

            for (int i = 0; i < nNodes; i++)
            {
                List <double> Y       = maskYlist[i];
                List <int>    tagList = x.getTags();
                for (int s = 0; s < Y.Count; s++)
                {
                    if (statesPerNodes[i, s] == 0)
                    {
                        Y[s] = maskValue;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        virtual public void getLogYY(model m, dataSeq x, int i, ref dMatrix YY, ref List <double> Y, bool takeExp, bool mask)
        {
            YY.set(0);
            listTool.listSet(ref Y, 0);

            float[]            w     = m.W;
            List <featureTemp> fList = _fGene.getFeatureTemp(x, i);
            int nTag = m.NTag;

            foreach (featureTemp ft in fList)
            {
                for (int s = 0; s < nTag; s++)
                {
                    int f = _fGene.getNodeFeatID(ft.id, s);
                    Y[s] += w[f] * ft.val;
                }
            }
            if (i > 0)
            {
                for (int s = 0; s < nTag; s++)
                {
                    for (int sPre = 0; sPre < nTag; sPre++)
                    {
                        int f = _fGene.getEdgeFeatID(sPre, s);
                        YY[sPre, s] += w[f];
                    }
                }
            }
            double maskValue = double.MinValue;

            if (takeExp)
            {
                listTool.listExp(ref Y);
                YY.eltExp();
                maskValue = 0;
            }
            if (mask)
            {
                List <int> tagList = x.getTags();
                for (int s = 0; s < Y.Count; s++)
                {
                    if (tagList[i] != s)
                    {
                        Y[s] = maskValue;
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public dMatrix getStatesPerNode(dataSeq x)
        {
            int     n   = x.Count;
            dMatrix spn = x.GoldStatesPerNode;

            if (spn == null || spn.R == 0)
            {
                List <int> tList = x.getTags();
                spn = new dMatrix(tList.Count, _nTag);
                for (int i = 0; i < tList.Count; i++)
                {
                    int tag = tList[i];
                    spn[i, tag] = 1;
                }
                x.GoldStatesPerNode = spn;
            }
            return(spn);
        }
Ejemplo n.º 6
0
        public dMatrix getStatesPerNode(dataSeq x)
        {
            int     n   = x.Count;
            dMatrix spn = x.GoldStatesPerNode;

            if (spn == null || spn.R == 0)
            {
                List <int> tList = x.getTags();
                spn = new dMatrix(tList.Count, _nHiddenState);
                for (int i = 0; i < tList.Count; i++)
                {
                    int        tag    = tList[i];
                    List <int> states = tagToHiddenStates(tag);
                    foreach (int s in states)
                    {
                        spn[i, s] = 1;
                    }
                }
                x.GoldStatesPerNode = spn;
            }
            return(spn);
        }
Ejemplo n.º 7
0
        //the scalar version
        override public void getLogYY(double scalar, model m, dataSeq x, int i, ref dMatrix YY, ref List <double> Y, bool takeExp, bool mask)
        {
            YY.set(0);
            listTool.listSet(ref Y, 0);

            float[]            w     = m.W;
            List <featureTemp> fList = _fGene.getFeatureTemp(x, i);
            int nTag = m.NTag;

            foreach (featureTemp ft in fList)
            {
                for (int s = 0; s < nTag; s++)
                {
                    int f = _fGene.getNodeFeatID(ft.id, s);
                    Y[s] += w[f] * scalar * ft.val;
                }
            }
            if (i > 0)
            {
                //non-rich
                if (Global.useTraditionalEdge)
                {
                    for (int s = 0; s < nTag; s++)
                    {
                        for (int sPre = 0; sPre < nTag; sPre++)
                        {
                            int f = _fGene.getEdgeFeatID(sPre, s);
                            YY[sPre, s] += w[f] * scalar;
                        }
                    }
                }

                //rich
                foreach (featureTemp im in fList)
                {
                    int id = im.id;
                    if (id < _fGene.getNRichFeatTemp())
                    {
                        for (int s = 0; s < nTag; s++)
                        {
                            for (int sPre = 0; sPre < nTag; sPre++)
                            {
                                int f = _fGene.getEdgeFeatID(id, sPre, s);
                                YY[sPre, s] += w[f] * scalar * im.val;
                            }
                        }
                    }
                }
            }
            double maskValue = double.MinValue;

            if (takeExp)
            {
                listTool.listExp(ref Y);
                YY.eltExp();
                maskValue = 0;
            }
            if (mask)
            {
                List <int> tagList = x.getTags();
                for (int s = 0; s < Y.Count; s++)
                {
                    if (tagList[i] != s)
                    {
                        Y[s] = maskValue;
                    }
                }
            }
        }