use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class MTCLinAdaptWithMMB method saveUserMembership.
// save the user mixture membership into a file
public void saveUserMembership(String clusterdir, String filename) {
PrintWriter writer;
File dir = new File(clusterdir);
if (!dir.exists())
dir.mkdirs();
try {
writer = new PrintWriter(new File(clusterdir + "/UserMembership.txt"));
for (_AdaptStruct u : m_userList) {
_MMBAdaptStruct user = (_MMBAdaptStruct) u;
writer.write(String.format("%s\n", u.getUserID()));
// write the clusters with edges first
for (_HDPThetaStar theta : user.getHDPTheta4Edge()) {
writer.write(String.format("(%d, %d, %d)\t", theta.getIndex(), user.getHDPThetaMemSize(theta), user.getHDPThetaEdgeSize(theta)));
}
// write the clusters with members then
for (_HDPThetaStar theta : user.getHDPTheta4Rvw()) {
if (!user.getHDPTheta4Edge().contains(theta))
writer.write(String.format("(%d, %d, %d)\t", theta.getIndex(), user.getHDPThetaMemSize(theta), user.getHDPThetaEdgeSize(theta)));
}
writer.write("\n");
}
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithHDP method calculateClusterProbPerUser.
@Override
protected // Indeed, it is for per review, for inheritance we don't change the function name.
void calculateClusterProbPerUser() {
double prob, logSum;
double[] probs;
if (m_newCluster)
probs = new double[m_kBar + 1];
else
probs = new double[m_kBar];
_HDPAdaptStruct user;
_HDPThetaStar curTheta;
// sample a new cluster parameter first.
if (m_newCluster) {
// to make it consistent since we will only use one auxiliary variable
m_hdpThetaStars[m_kBar].setGamma(m_gamma_e);
m_G0.sampling(m_hdpThetaStars[m_kBar].getModel());
}
for (int i = 0; i < m_userList.size(); i++) {
user = (_HDPAdaptStruct) m_userList.get(i);
if (user.getTestSize() == 0)
continue;
for (_Review r : user.getReviews()) {
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;
}
logSum = Utils.logSumOfExponentials(probs);
for (int k = 0; k < probs.length; k++) probs[k] -= logSum;
// posterior in log space
r.setClusterPosterior(probs);
}
}
}
use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithHDP method printTopWords.
void printTopWords(_HDPThetaStar cluster) {
MyPriorityQueue<_RankItem> wordRanker = new MyPriorityQueue<_RankItem>(30);
double[] lmStat = cluster.getLMStat();
double[] phi = cluster.getModel();
int[] tfs = m_tf_count[cluster.getIndex()];
double tf;
// features with positive/negative weights (skip the bias term)
System.out.format("Cluster %d (%d)\n[positive]: ", cluster.getIndex(), cluster.getMemSize());
for (int i = 1; i < phi.length; i++) {
tf = tfs[i - 1] == 0 ? 0.1 : tfs[i - 1];
// top positive words with expected polarity
wordRanker.add(new _RankItem(i, phi[i] * tf));
}
for (_RankItem it : wordRanker) System.out.format("%s:%.3f\t", m_features[it.m_index], phi[it.m_index]);
// features with negative weights
wordRanker.clear();
System.out.format("\n[negative]: ");
for (int i = 1; i < phi.length; i++) {
tf = tfs[i - 1] == 0 ? 0.1 : tfs[i - 1];
// top negative words
wordRanker.add(new _RankItem(i, -phi[i] * tf));
}
for (_RankItem it : wordRanker) System.out.format("%s:%.3f\t", m_features[it.m_index], phi[it.m_index]);
// features with highest frequency
wordRanker.clear();
System.out.format("\n[popular]: ");
for (int i = 0; i < lmStat.length; i++) // top positive words with expected polarity
wordRanker.add(new _RankItem(i, lmStat[i]));
for (_RankItem it : wordRanker) System.out.format("%s:%.1f\t", m_lmFeatures.get(it.m_index), lmStat[it.m_index]);
System.out.println();
}
use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithMMB method loadUsers.
@Override
public void loadUsers(ArrayList<_User> userList) {
m_userList = new ArrayList<_AdaptStruct>();
for (_User user : userList) m_userList.add(new _MMBAdaptStruct(user));
m_pWeights = new double[m_gWeights.length];
m_indicator = new _HDPThetaStar[m_userList.size()][m_userList.size()];
}
use of structures._HDPThetaStar._Connection in project IR_Base by Linda-sunshine.
the class CLRWithMMB method calculateMixture4TrainUser.
// calculate the mixture for train user based on review assignment and edge assignment
public void calculateMixture4TrainUser(_MMBAdaptStruct user) {
double sum = 0;
double[] probs = new double[m_kBar];
_HDPThetaStar theta;
// The set of clusters for review and edge could be different, just iterate over kBar
for (int k = 0; k < m_kBar; k++) {
theta = m_hdpThetaStars[k];
probs[k] = user.getHDPThetaMemSize(theta) + user.getHDPThetaEdgeSize(theta);
sum += probs[k];
}
for (int k = 0; k < m_kBar; k++) {
probs[k] /= sum;
}
user.setMixture(probs);
}
Aggregations