use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class CLRWithHDP method gradientByFunc.
@Override
protected void gradientByFunc(_AdaptStruct u, _Doc r, double weight, double[] g) {
_Review review = (_Review) r;
// feature index
int n;
int cIndex = review.getHDPThetaStar().getIndex();
if (cIndex < 0 || cIndex >= m_kBar)
System.err.println("Error,cannot find the HDP theta star!");
int offset = m_dim * cIndex;
double delta = weight * (review.getYLabel() - logit(review.getSparse(), review));
// Bias term.
// x0=1
g[offset] -= delta;
// Traverse all the feature dimension to calculate the gradient.
for (_SparseFeature fv : review.getSparse()) {
n = fv.getIndex() + 1;
g[offset + n] -= delta * fv.getValue();
}
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class CLRWithHDP method calcLogLikelihoodX.
protected double calcLogLikelihoodX(_Review r) {
if (r.getHDPThetaStar().getLMStat() == null) {
return r.getL4NewCluster();
} else {
double[] Ns = r.getHDPThetaStar().getLMStat();
double N = Utils.sumOfArray(Ns);
double n = r.getLMSum();
_SparseFeature[] fvs = r.getLMSparse();
double L = Utils.lgamma(m_betaSum + N) - Utils.lgamma(m_betaSum + N + n);
for (_SparseFeature fv : fvs) {
L += logGammaDivision((int) fv.getValue(), m_betas[fv.getIndex()], Ns[fv.getIndex()]);
}
return L;
}
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class CLinAdaptWithHDP method logit.
@Override
protected double logit(_SparseFeature[] fvs, _Review r) {
int k, n;
double[] Au = r.getHDPThetaStar().getModel();
// Bias term: w_s0*a0+b0.
double sum = Au[0] * m_gWeights[0] + Au[m_dim];
for (_SparseFeature fv : fvs) {
n = fv.getIndex() + 1;
k = m_featureGroupMap[n];
sum += (Au[k] * m_gWeights[n] + Au[m_dim + k]) * fv.getValue();
}
return Utils.logistic(sum);
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class ACCTM_C method calculate_log_likelihood4Child.
@Override
protected double calculate_log_likelihood4Child(_Doc d) {
_ChildDoc4BaseWithPhi cDoc = (_ChildDoc4BaseWithPhi) d;
double docLogLikelihood = 0.0;
double gammaLen = Utils.sumOfArray(m_gamma);
double cDocXSum = Utils.sumOfArray(cDoc.m_xSstat);
// prepare compute the normalizers
_SparseFeature[] fv = cDoc.getSparse();
for (int i = 0; i < fv.length; i++) {
int wid = fv[i].getIndex();
double value = fv[i].getValue();
double wordLogLikelihood = 0;
for (int k = 0; k < number_of_topics; k++) {
double wordPerTopicLikelihood = childWordByTopicProb(k, wid) * childTopicInDocProb(k, cDoc) * childXInDocProb(0, cDoc) / (cDocXSum + gammaLen);
wordLogLikelihood += wordPerTopicLikelihood;
}
double wordPerTopicLikelihood = childLocalWordByTopicProb(wid, cDoc) * childXInDocProb(1, cDoc) / (cDocXSum + gammaLen);
wordLogLikelihood += wordPerTopicLikelihood;
if (Math.abs(wordLogLikelihood) < 1e-10) {
System.out.println("wordLoglikelihood\t" + wordLogLikelihood);
wordLogLikelihood += 1e-10;
}
wordLogLikelihood = Math.log(wordLogLikelihood);
docLogLikelihood += value * wordLogLikelihood;
}
return docLogLikelihood;
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class ACCTM_test method printParentPhi.
protected void printParentPhi(_Doc d, File phiFolder) {
_ParentDoc pDoc = (_ParentDoc) d;
String parentPhiFileName = pDoc.getName() + ".txt";
_SparseFeature[] fv = pDoc.getSparse();
try {
PrintWriter parentPW = new PrintWriter(new File(phiFolder, parentPhiFileName));
for (int n = 0; n < fv.length; n++) {
int index = fv[n].getIndex();
String featureName = m_corpus.getFeature(index);
parentPW.print(featureName + ":\t");
for (int k = 0; k < number_of_topics; k++) parentPW.print(pDoc.m_phi[n][k] + "\t");
parentPW.println();
}
parentPW.flush();
parentPW.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Aggregations