use of structures._HDPThetaStar in project IR_Base by Linda-sunshine.
the class MTCLinAdaptWithHDP method setPersonalizedModel.
@Override
protected void setPersonalizedModel() {
double[] prob;
_HDPAdaptStruct user;
Collection<_HDPThetaStar> thetas;
setClusterModels();
for (_AdaptStruct u : m_userList) {
// we set each user's personalized weights based on the review's cluster assignment
user = (_HDPAdaptStruct) u;
thetas = user.getHDPTheta4Rvw();
prob = new double[user.getHDPTheta4Rvw().size()];
int count = 0;
double sum = 0;
for (_HDPThetaStar theta : thetas) {
double clusterAssignment = getClusterAssignment(user, theta);
prob[count++] = clusterAssignment;
sum += clusterAssignment;
}
// normalize the probability for each cluster
for (int i = 0; i < prob.length; i++) {
prob[i] /= sum;
}
// construct the personalized weights by weighted cluster models
double[] pWeights = new double[m_gWeights.length];
count = 0;
for (_HDPThetaStar theta : thetas) {
Utils.add2Array(pWeights, theta.getWeights(), prob[count++]);
}
user.setPersonalizedModel(pWeights);
}
}
use of structures._HDPThetaStar 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._HDPThetaStar 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._HDPThetaStar in project IR_Base by Linda-sunshine.
the class CLRWithMMB method accumulateDecomposedLikelihoodEMMB.
// traverse all the clusters to get the decomposed likelihood
// 0: m*log(rho*zBz); 1: n*log(1-rho*zBz); 2:n*log(rho(1-zBz))
protected double[] accumulateDecomposedLikelihoodEMMB() {
double[] likelihoodE = new double[4];
_Connection connection;
int e_0, e_1;
double logRho = Math.log(m_rho), log_zBz = 0, zBz = 0;
_HDPThetaStar theta_g, theta_h;
for (int g = 0; g < m_kBar; g++) {
theta_g = m_hdpThetaStars[g];
for (int h = g; h < m_kBar; h++) {
theta_h = m_hdpThetaStars[h];
if (!theta_g.hasConnection(theta_h))
continue;
connection = theta_g.getConnection(theta_h);
e_1 = connection.getEdge()[1];
e_0 = connection.getEdge()[0];
log_zBz = Math.log(m_abcd[0] + e_1) - Math.log(m_abcd[0] + m_abcd[1] + e_0 + e_1);
zBz = (m_abcd[0] + e_1) / (m_abcd[0] + m_abcd[1] + e_0 + e_1);
likelihoodE[0] += e_1 * (logRho + log_zBz);
likelihoodE[1] += e_0 * (Math.log(1 - m_rho * zBz));
likelihoodE[2] += e_0 * (logRho + Math.log(1 - zBz));
// likelihoodE: m*log(rho*(a+e_1))/(a+b+e_0+e_1))+n*log(rho*(b+e_0))/(a+b+e_0+e_1))
// likelihoodE += (e_0+e_1)*Math.log(m_rho)+e_1*Math.log(m_abcd[0]+e_1)+
// e_0*Math.log(m_abcd[1]+e_0)-(e_0+e_1)*Math.log(m_abcd[0]+m_abcd[1]+e_0+e_1);
// likelihoodE += e_1*Math.log(m_abcd[0]+e_1)+e_0*Math.log(m_abcd[1]+e_0)
// -(e_0+e_1)*Math.log(m_abcd[0]+m_abcd[1]+e_0+e_1);
}
}
return likelihoodE;
}
use of structures._HDPThetaStar in project IR_Base by Linda-sunshine.
the class CLRWithMMB method checkEdges.
// check if the sum(m_MNL) == sum(edges of all clusters)
protected void checkEdges() {
int mmb_0 = 0, mmb_1 = 0;
_HDPThetaStar theta;
for (int i = 0; i < m_kBar; i++) {
theta = m_hdpThetaStars[i];
mmb_0 += theta.getEdgeSize(0);
mmb_1 += theta.getEdgeSize(1);
}
if (mmb_0 != m_MNL[0])
System.out.println("Zero edges sampled from mmb is not correct!");
if (mmb_1 != m_MNL[1])
System.out.println("One edges sampled from mmb is not correct!");
}
Aggregations