public void read(string a, string b) { //features string[] lineAry = a.Split(Global.lineEndAry, StringSplitOptions.RemoveEmptyEntries); foreach (string im in lineAry) { List <featureTemp> nodeList = new List <featureTemp>(); string[] imAry = im.Split(Global.commaAry, StringSplitOptions.RemoveEmptyEntries); foreach (string imm in imAry) { if (imm.Contains("/")) { string[] biAry = imm.Split(Global.slashAry, StringSplitOptions.RemoveEmptyEntries); featureTemp ft = new featureTemp(int.Parse(biAry[0]), double.Parse(biAry[1])); nodeList.Add(ft); } else { featureTemp ft = new featureTemp(int.Parse(imm), 1); nodeList.Add(ft); } } featureTemps.Add(nodeList); } //yGold lineAry = b.Split(Global.commaAry, StringSplitOptions.RemoveEmptyEntries); foreach (string im in lineAry) { yGold.Add(int.Parse(im)); } }
//return the gradient of -log{P(y*|x,w)} as follows: E_{P(y|x)}(F(x,y)) - F(x,y*) virtual public double getGrad(List <double> vecGrad, model m, dataSeq x, baseHashSet <int> idSet) { if (idSet != null) { idSet.Clear(); } int nTag = m.NTag; //compute beliefs belief bel = new belief(x.Count, nTag); belief belMasked = new belief(x.Count, nTag); _inf.getBeliefs(bel, m, x, false); _inf.getBeliefs(belMasked, m, x, true); double ZGold = belMasked.Z; double Z = bel.Z; List <featureTemp> fList; for (int i = 0; i < x.Count; i++) { fList = _fGene.getFeatureTemp(x, i); for (int j = 0; j < fList.Count; j++) { featureTemp im = fList[j]; int id = im.id; double v = im.val; for (int s = 0; s < nTag; s++) { int f = _fGene.getNodeFeatID(id, s); if (idSet != null) { idSet.Add(f); } vecGrad[f] += bel.belState[i][s] * v; vecGrad[f] -= belMasked.belState[i][s] * v; } } } for (int i = 1; i < x.Count; i++) { for (int s = 0; s < nTag; s++) { for (int sPre = 0; sPre < nTag; sPre++) { int f = _fGene.getEdgeFeatID(sPre, s); if (idSet != null) { idSet.Add(f); } vecGrad[f] += bel.belEdge[i][sPre, s]; vecGrad[f] -= belMasked.belEdge[i][sPre, s]; } } } return(Z - ZGold); }
//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; } } } }