use of structures._Review in project IR_Base by Linda-sunshine.
the class CLRWithMMB method updateDocMembership.
@Override
public // Override this function since we have different conditions for removing clusters.
void updateDocMembership(_HDPAdaptStruct user, _Review r) {
int index = -1;
_HDPThetaStar curThetaStar = r.getHDPThetaStar();
// remove the current review from the user side.
user.incHDPThetaStarMemSize(r.getHDPThetaStar(), -1);
// remove the current review from the theta side.
// remove the lm stat first before decrease the document count
curThetaStar.rmLMStat(r.getLMSparse());
curThetaStar.updateMemCount(-1);
// No data associated with the cluster
if (curThetaStar.getMemSize() == 0 && curThetaStar.getTotalEdgeSize() == 0) {
System.out.println("[Debug]Zero cluster detected in updating doc!");
// check if every dim gets 0 count in language mode
LMStatSanityCheck(curThetaStar);
// recycle the gamma
m_gamma_e += curThetaStar.getGamma();
// curThetaStar.resetGamma();
// swap the disabled theta to the last for later use
index = findHDPThetaStar(curThetaStar);
// move it back to \theta*
swapTheta(m_kBar - 1, index);
curThetaStar.reset();
m_kBar--;
}
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class MTCLRWithMMB method gradientByFunc.
@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight, double[] g) {
_Review r = (_Review) review;
// feature index
int n;
int cIndex = r.getHDPThetaStar().getIndex();
if (cIndex < 0 || cIndex >= m_kBar)
System.err.println("Error,cannot find the theta star!");
int offset = m_dim * cIndex;
int offsetSup = m_dim * m_kBar;
double delta = weight * (r.getYLabel() - logit(r.getSparse(), r));
// Bias term.
// x0=1, each cluster.
g[offset] -= delta;
// super model.
g[offsetSup] -= m_q * delta;
// Traverse all the feature dimension to calculate the gradient.
for (_SparseFeature fv : review.getSparse()) {
n = fv.getIndex() + 1;
// cluster model.
g[offset + n] -= delta * fv.getValue();
// super model.
g[offsetSup + n] -= delta * fv.getValue() * m_q;
}
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class MTCLinAdaptWithMMB method logit.
// Logit function is different from the father class.
@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] * getSupWeights(0) + Au[m_dim];
for (_SparseFeature fv : fvs) {
n = fv.getIndex() + 1;
k = m_featureGroupMap[n];
sum += (Au[k] * getSupWeights(n) + Au[m_dim + k]) * fv.getValue();
}
return Utils.logistic(sum);
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class _MMBAdaptStruct method evaluate.
@Override
public double evaluate(_Doc doc) {
_Review r = (_Review) doc;
double prob = 0, sum = 0;
double[] probs = r.getCluPosterior();
int n, m, k;
// not adaptation based
if (m_dim == 0) {
for (k = 0; k < probs.length; k++) {
// need to be fixed: here we assumed binary classification
sum = Utils.dotProduct(CLRWithMMB.m_hdpThetaStars[k].getModel(), doc.getSparse(), 0);
if (MTCLRWithMMB.m_supWeights != null && CLRWithDP.m_q != 0)
sum += CLRWithDP.m_q * Utils.dotProduct(MTCLRWithMMB.m_supWeights, doc.getSparse(), 0);
// to maintain numerical precision, compute the expectation in log space as well
if (k == 0)
prob = probs[k] + Math.log(Utils.logistic(sum));
else
prob = Utils.logSum(prob, probs[k] + Math.log(Utils.logistic(sum)));
}
} else {
double[] As;
for (k = 0; k < probs.length; k++) {
As = CLRWithMMB.m_hdpThetaStars[k].getModel();
// Bias term: w_s0*a0+b0.
sum = As[0] * CLinAdaptWithMMB.m_supWeights[0] + As[m_dim];
for (_SparseFeature fv : doc.getSparse()) {
n = fv.getIndex() + 1;
m = m_featureGroupMap[n];
sum += (As[m] * CLinAdaptWithMMB.m_supWeights[n] + As[m_dim + m]) * fv.getValue();
}
// to maintain numerical precision, compute the expectation in log space as well
if (k == 0)
prob = probs[k] + Math.log(Utils.logistic(sum));
else
prob = Utils.logSum(prob, probs[k] + Math.log(Utils.logistic(sum)));
}
}
// accumulate the prediction results during sampling procedure
doc.m_pCount++;
// >0.5?1:0;
doc.m_prob += Math.exp(prob);
return prob;
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class ModelAdaptation method constructReverseNeighborhood.
protected int[] constructReverseNeighborhood() {
// total number of adaptation instances
int adaptSize = 0;
// construct the reverse link
CoAdaptStruct ui, uj;
for (int i = 0; i < m_userList.size(); i++) {
ui = (CoAdaptStruct) (m_userList.get(i));
for (_RankItem nit : ui.getNeighbors()) {
// uj is a neighbor of ui
uj = (CoAdaptStruct) (m_userList.get(nit.m_index));
uj.addReverseNeighbor(i, nit.m_value);
}
adaptSize += ui.getAdaptationSize();
}
// construct the order of online updating
ArrayList<_RankItem> userorder = new ArrayList<_RankItem>();
for (int i = 0; i < m_userList.size(); i++) {
ui = (CoAdaptStruct) (m_userList.get(i));
for (_Review r : ui.getReviews()) {
// reviews in each user is already ordered by time
if (r.getType() == rType.ADAPTATION) {
// to be in ascending order
userorder.add(new _RankItem(i, r.getTimeStamp()));
}
}
}
Collections.sort(userorder);
int[] userOrder = new int[adaptSize];
for (int i = 0; i < adaptSize; i++) userOrder[i] = userorder.get(i).m_index;
return userOrder;
}
Aggregations