use of structures._Review in project IR_Base by Linda-sunshine.
the class MTCLinAdaptWithHDP method gradientByFunc.
@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight, double[] g) {
_Review r = (_Review) review;
_HDPThetaStar theta = r.getHDPThetaStar();
// feature index
int n, k, s;
int cIndex = theta.getIndex();
if (cIndex < 0 || cIndex >= m_kBar)
System.err.println("Error,cannot find the theta star!");
int offset = m_dim * 2 * cIndex, offsetSup = m_dim * 2 * m_kBar;
double[] Au = theta.getModel();
double delta = (review.getYLabel() - logit(review.getSparse(), r)) * weight;
// Bias term for individual user.
// a[0] = ws0*x0; x0=1
g[offset] -= delta * getSupWeights(0);
// b[0]
g[offset + m_dim] -= delta;
// Bias term for super user.
// a_s[0] = a_i0*w_g0*x_d0
g[offsetSup] -= delta * Au[0] * m_gWeights[0];
// b_s[0] = a_i0*x_d0
g[offsetSup + m_dimSup] -= delta * Au[0];
// Traverse all the feature dimension to calculate the gradient for both individual users and super user.
for (_SparseFeature fv : review.getSparse()) {
n = fv.getIndex() + 1;
k = m_featureGroupMap[n];
// w_si*x_di
g[offset + k] -= delta * getSupWeights(n) * fv.getValue();
// x_di
g[offset + m_dim + k] -= delta * fv.getValue();
s = m_featureGroupMap4SupUsr[n];
// a_i*w_gi*x_di
g[offsetSup + s] -= delta * Au[k] * m_gWeights[n] * fv.getValue();
// a_i*x_di
g[offsetSup + m_dimSup + s] -= delta * Au[k] * fv.getValue();
}
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class MTCLinAdaptWithHDPMultipleE method gradientByFunc.
@Override
protected void gradientByFunc(_AdaptStruct u, _Doc review, double weight, double[] g) {
int index = -1;
double confidence = 1;
_Review r = (_Review) review;
_HDPThetaStar oldTheta = r.getHDPThetaStar();
HashMap<_HDPThetaStar, Integer> thetaCountMap = r.getThetaCountMap();
for (_HDPThetaStar theta : thetaCountMap.keySet()) {
index = findHDPThetaStar(theta);
// some of the cluster may disappear, ignore them.
if (index >= m_kBar || index < 0)
continue;
r.setHDPThetaStar(theta);
confidence = thetaCountMap.get(theta);
// confidence plays the role of weight here, how many times the review shows in the cluster.
super.gradientByFunc(u, review, confidence, g);
}
r.setHDPThetaStar(oldTheta);
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class _HDPAdaptStruct 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(CLRWithHDP.m_hdpThetaStars[k].getModel(), doc.getSparse(), 0);
if (MTCLRWithHDP.m_supWeights != null && MTCLRWithHDP.m_q != 0)
sum += CLRWithDP.m_q * Utils.dotProduct(MTCLRWithHDP.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 = CLRWithHDP.m_hdpThetaStars[k].getModel();
// Bias term: w_s0*a0+b0.
sum = As[0] * CLinAdaptWithHDP.m_supWeights[0] + As[m_dim];
for (_SparseFeature fv : doc.getSparse()) {
n = fv.getIndex() + 1;
m = m_featureGroupMap[n];
sum += (As[m] * CLinAdaptWithHDP.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 _HDPAdaptStruct method evaluateG.
// Evaluate the performance of the global part.
public double evaluateG(_Doc doc) {
_Review r = (_Review) doc;
double prob = 0, sum = 0;
double[] probs = r.getCluPosterior();
int n, k;
for (k = 0; k < probs.length; k++) {
// As = CLRWithHDP.m_hdpThetaStars[k].getModel();
// Bias term: w_s0*a0+b0.
sum = CLinAdaptWithHDP.m_supWeights[0];
for (_SparseFeature fv : doc.getSparse()) {
n = fv.getIndex() + 1;
sum += CLinAdaptWithHDP.m_supWeights[n] * 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_g++;
// >0.5?1:0;
doc.m_prob_g += Math.exp(prob);
return prob;
}
use of structures._Review in project IR_Base by Linda-sunshine.
the class CLRWithMMB method calculateMixture4TestUser.
// calculate the mixture for test user based on review assignment
public void calculateMixture4TestUser(_MMBAdaptStruct user) {
int cIndex = 0;
double prob, logSum, sum = 0;
double[] probs = new double[m_kBar];
_HDPThetaStar curTheta;
// calculate the cluster assignment for each review first
for (_Review r : user.getReviews()) {
// suppose all reviews are test review in this setting
if (r.getType() != rType.TEST)
continue;
for (int k = 0; k < probs.length; k++) {
curTheta = m_hdpThetaStars[k];
r.setHDPThetaStar(curTheta);
prob = calcLogLikelihoodX(r) + Math.log(calcGroupPopularity(user, k, curTheta.getGamma()));
probs[k] = prob;
}
// normalize the prob
logSum = Utils.logSumOfExponentials(probs);
for (int k = 0; k < probs.length; k++) probs[k] -= logSum;
// take the cluster that has maximum prob as the review's cluster assignment
curTheta = m_hdpThetaStars[Utils.argmax(probs)];
r.setHDPThetaStar(curTheta);
// update the cluster assignment for the user
user.incHDPThetaStarMemSize(r.getHDPThetaStar(), 1);
}
// calculate the mixture: get the review assignment and normalize it
Arrays.fill(probs, 0);
// calculate the sum first
for (_HDPThetaStar theta : user.getHDPTheta4Rvw()) {
sum += user.getHDPThetaMemSize(theta);
}
// calculate the prob for each dim
for (_HDPThetaStar theta : user.getHDPTheta4Rvw()) {
cIndex = theta.getIndex();
probs[cIndex] = user.getHDPThetaMemSize(theta) / sum;
}
user.setMixture(probs);
}
Aggregations