use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class PRLogisticRegression method calcFuncGradient.
// This function is used to calculate the value and gradient with the new beta.
@Override
protected double calcFuncGradient(Collection<_Doc> trainSet) {
double gValue = 0, fValue = 0;
double Pij = 0, logPij = 0;
// Add the L2 regularization.
double L2 = 0, b;
for (int i = 0; i < m_beta.length; i++) {
b = m_beta[i];
m_g[i] = 2 * m_lambda * b;
L2 += b * b;
}
int Yi;
_SparseFeature[] fv;
int doc_index = 0;
for (_Doc doc : trainSet) {
fv = doc.getSparse();
Yi = doc.getYLabel();
// compute posterior regularized q(y=j|xi)\propto p(y=j|xi)exp(\lambda\phi(y=j, y)
calcPosterior(fv, m_doc_pr[doc_index], m_cache);
for (int j = 0; j < m_classNo; j++) {
Pij = m_cache[j];
logPij = Math.log(Pij);
if (Yi == j) {
gValue = Pij - 1;
fValue += logPij;
} else
gValue = Pij;
int offset = j * (m_featureSize + 1);
m_g[offset] += gValue;
// (Yij - Pij) * Xi
for (_SparseFeature sf : fv) m_g[offset + sf.getIndex() + 1] += gValue * sf.getValue();
}
doc_index++;
}
return m_lambda * L2 - fValue;
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class LDAGibbs4AC_test method rankChild4ParentByLikelihood.
protected double rankChild4ParentByLikelihood(_ChildDoc cDoc, _ParentDoc pDoc) {
int cDocLen = cDoc.getTotalDocLength();
_SparseFeature[] fv = pDoc.getSparse();
double docLogLikelihood = 0;
for (_SparseFeature i : fv) {
int wid = i.getIndex();
double value = i.getValue();
double wordLogLikelihood = 0;
for (int k = 0; k < number_of_topics; k++) {
double wordPerTopicLikelihood = (word_topic_sstat[k][wid] / m_sstat[k]) * ((cDoc.m_sstat[k] + d_alpha) / (d_alpha * number_of_topics + cDocLen));
wordLogLikelihood += wordPerTopicLikelihood;
}
docLogLikelihood += value * Math.log(wordLogLikelihood);
}
return docLogLikelihood;
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class LDAGibbs4AC_test method rankChild4StnByLanguageModel.
protected HashMap<String, Double> rankChild4StnByLanguageModel(_Stn stnObj, _ParentDoc pDoc) {
HashMap<String, Double> childLikelihoodMap = new HashMap<String, Double>();
double smoothingMu = m_LM.m_smoothingMu;
for (_ChildDoc cDoc : pDoc.m_childDocs) {
int cDocLen = cDoc.getTotalDocLength();
_SparseFeature[] fv = cDoc.getSparse();
double stnLogLikelihood = 0;
double alphaDoc = smoothingMu / (smoothingMu + cDocLen);
_SparseFeature[] sv = stnObj.getFv();
for (_SparseFeature svWord : sv) {
double featureLikelihood = 0;
int wid = svWord.getIndex();
double stnVal = svWord.getValue();
int featureIndex = Utils.indexOf(fv, wid);
double docVal = 0;
if (featureIndex != -1) {
docVal = fv[featureIndex].getValue();
}
double smoothingProb = (1 - alphaDoc) * docVal / (cDocLen);
smoothingProb += alphaDoc * m_LM.getReferenceProb(wid);
featureLikelihood = Math.log(smoothingProb);
stnLogLikelihood += stnVal * featureLikelihood;
}
childLikelihoodMap.put(cDoc.getName(), stnLogLikelihood);
}
return childLikelihoodMap;
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class LDAGibbs4AC_test method rankChild4StnByHybrid.
protected HashMap<String, Double> rankChild4StnByHybrid(_Stn stnObj, _ParentDoc pDoc) {
HashMap<String, Double> childLikelihoodMap = new HashMap<String, Double>();
double smoothingMu = m_LM.m_smoothingMu;
for (_ChildDoc cDoc : pDoc.m_childDocs) {
double cDocLen = cDoc.getTotalDocLength();
_SparseFeature[] fv = cDoc.getSparse();
double stnLogLikelihood = 0;
double alphaDoc = smoothingMu / (smoothingMu + cDocLen);
_SparseFeature[] sv = stnObj.getFv();
for (_SparseFeature svWord : sv) {
double featureLikelihood = 0;
int wid = svWord.getIndex();
double stnVal = svWord.getValue();
int featureIndex = Utils.indexOf(fv, wid);
double docVal = 0;
if (featureIndex != -1) {
docVal = fv[featureIndex].getValue();
}
double LMLikelihood = (1 - alphaDoc) * docVal / (cDocLen);
LMLikelihood += alphaDoc * m_LM.getReferenceProb(wid);
double TMLikelihood = 0;
for (int k = 0; k < number_of_topics; k++) {
// double likelihoodPerTopic =
// topic_term_probabilty[k][wid];
// System.out.println("likelihoodPerTopic1-----\t"+likelihoodPerTopic);
//
// likelihoodPerTopic *= cDoc.m_topics[k];
// System.out.println("likelihoodPerTopic2-----\t"+likelihoodPerTopic);
TMLikelihood += (word_topic_sstat[k][wid] / m_sstat[k]) * (topicInDocProb(k, cDoc) / (d_alpha * number_of_topics + cDocLen));
// TMLikelihood +=
// topic_term_probabilty[k][wid]*cDoc.m_topics[k];
// System.out.println("TMLikelihood\t"+TMLikelihood);
}
featureLikelihood = m_tau * LMLikelihood + (1 - m_tau) * TMLikelihood;
// featureLikelihood = TMLikelihood;
featureLikelihood = Math.log(featureLikelihood);
stnLogLikelihood += stnVal * featureLikelihood;
}
childLikelihoodMap.put(cDoc.getName(), stnLogLikelihood);
}
return childLikelihoodMap;
}
use of structures._SparseFeature in project IR_Base by Linda-sunshine.
the class corrLDA_Gibbs method calculate_log_likelihood4Parent.
protected double calculate_log_likelihood4Parent(_Doc d) {
_ParentDoc pDoc = (_ParentDoc) d;
double docLogLikelihood = 0;
_SparseFeature[] fv = pDoc.getSparse();
double docTopicSum = Utils.sumOfArray(pDoc.m_sstat);
double alphaSum = d_alpha * number_of_topics;
for (int j = 0; j < fv.length; j++) {
int wid = fv[j].getIndex();
double value = fv[j].getValue();
double wordLogLikelihood = 0;
for (int k = 0; k < number_of_topics; k++) {
double wordPerTopicLikelihood = parentWordByTopicProb(k, wid) * parentTopicInDocProb(k, pDoc) / (alphaSum + docTopicSum);
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;
}
Aggregations