Search in sources :

Example 1 with Classifier.supervised.modelAdaptation._AdaptStruct

use of Classifier.supervised.modelAdaptation._AdaptStruct 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);
    }
}
Also used : Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) structures._HDPThetaStar(structures._HDPThetaStar)

Example 2 with Classifier.supervised.modelAdaptation._AdaptStruct

use of Classifier.supervised.modelAdaptation._AdaptStruct in project IR_Base by Linda-sunshine.

the class CLRWithMMB method calculateFrdStat.

// calculate the average friend number of training users, testing users.
// it only applies to the two set of training and testing users.
public void calculateFrdStat() {
    Set<String> trainUsers = new HashSet<String>();
    Set<String> testUsers = new HashSet<String>();
    for (_AdaptStruct u : m_userList) {
        if (u.getTestSize() == 0) {
            trainUsers.add(u.getUserID());
        } else {
            testUsers.add(u.getUserID());
        }
    }
    double trainSum = 0, testSum = 0;
    for (_AdaptStruct u : m_userList) {
        // training users
        if (u.getTestSize() == 0) {
            for (String f : u.getUser().getFriends()) {
                if (trainUsers.contains(f))
                    trainSum++;
            }
        } else {
            for (String f : u.getUser().getFriends()) {
                if (trainUsers.contains(f) || testUsers.contains(f)) {
                    testSum++;
                }
            }
        }
    }
    System.out.println(String.format("[Stat]Avg friends for training users is %.2f; avg friends for testing users is %.2f.\n", trainSum / trainUsers.size(), testSum / testUsers.size()));
}
Also used : Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) HashSet(java.util.HashSet)

Example 3 with Classifier.supervised.modelAdaptation._AdaptStruct

use of Classifier.supervised.modelAdaptation._AdaptStruct in project IR_Base by Linda-sunshine.

the class CLRWithMMB method trainTraceMultipleE.

public double trainTraceMultipleE(String data, int iter, int multipleE) {
    m_numberOfIterations = iter;
    m_thinning = 1;
    System.out.print(String.format("[Info]Joint Sampling for all zero edges: %b\n", m_jointAll));
    System.out.print(toString());
    double delta = 0, lastLikelihood = 0, curLikelihood = 0;
    double likelihoodX = 0, likelihoodY = 0;
    int count = 0, ecount = 0;
    double likelihoodE = 0;
    // clear user performance, init cluster assignment, assign each review to one cluster
    init();
    initThetaStars_Edges_Joint();
    sanityCheck();
    // Burn in period for doc.
    while (count++ < m_burnIn) {
        calculate_E_step();
        calculate_M_step();
    }
    try {
        String traceFile = String.format("%s_iter_%d_burnin_%d_thin_%d_%b_%d.txt", data, iter, m_burnIn, m_thinning, m_jointAll, System.currentTimeMillis());
        PrintWriter writer = new PrintWriter(new File(traceFile));
        // EM iteration.
        for (int i = 0; i < m_numberOfIterations; i++) {
            // Cluster assignment, thinning to reduce auto-correlation.
            while (ecount++ < multipleE) {
                calculate_E_step();
                assignClusterIndex();
                sampleGamma();
            }
            likelihoodY = estPhi();
            ecount = 0;
            // accumulate the likelihood
            likelihoodX = accumulateLikelihoodX();
            likelihoodE = accumulateLikelihoodEMMB();
            likelihoodE += (m_MNL[2] / 2) * Math.log(1 - m_rho);
            curLikelihood = likelihoodY + likelihoodX + likelihoodE;
            delta = (lastLikelihood - curLikelihood) / curLikelihood;
            // evaluate the model
            if (i % m_thinning == 0) {
                evaluateModel();
                test();
                for (_AdaptStruct u : m_userList) u.getPerfStat().clear();
            }
            writer.write(String.format("%.5f\t%.5f\t%.5f\t%.5f\t%d\t%.5f\t%.5f\n", likelihoodY, likelihoodX, likelihoodE, delta, m_kBar, m_perf[0], m_perf[1]));
            System.out.print(String.format("\n[Info]Step %d: likelihood: %.4f, Delta_likelihood: %.3f\n", i, curLikelihood, delta));
            if (Math.abs(delta) < m_converge)
                break;
            lastLikelihood = curLikelihood;
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // we do not want to miss the last sample?!
    evaluateModel();
    return curLikelihood;
}
Also used : Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) IOException(java.io.IOException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 4 with Classifier.supervised.modelAdaptation._AdaptStruct

use of Classifier.supervised.modelAdaptation._AdaptStruct in project IR_Base by Linda-sunshine.

the class CLinAdaptWithMMB method loadUsers.

@Override
public void loadUsers(ArrayList<_User> userList) {
    m_userList = new ArrayList<_AdaptStruct>();
    // Init each user.
    for (_User user : userList) {
        m_userList.add(new _MMBAdaptStruct(user, m_dim));
    }
    m_pWeights = new double[m_gWeights.length];
    m_indicator = new _HDPThetaStar[m_userList.size()][m_userList.size()];
}
Also used : Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) structures._User(structures._User)

Example 5 with Classifier.supervised.modelAdaptation._AdaptStruct

use of Classifier.supervised.modelAdaptation._AdaptStruct 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();
    }
}
Also used : Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) structures._HDPThetaStar(structures._HDPThetaStar) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

Classifier.supervised.modelAdaptation._AdaptStruct (Classifier.supervised.modelAdaptation._AdaptStruct)34 structures._User (structures._User)15 File (java.io.File)6 PrintWriter (java.io.PrintWriter)6 structures._Review (structures._Review)6 IOException (java.io.IOException)5 ExceptionWithIflag (LBFGS.LBFGS.ExceptionWithIflag)3 structures._SparseFeature (structures._SparseFeature)3 Feature (Classifier.supervised.liblinear.Feature)2 Parameter (Classifier.supervised.liblinear.Parameter)2 Problem (Classifier.supervised.liblinear.Problem)2 ArrayList (java.util.ArrayList)2 structures._HDPThetaStar (structures._HDPThetaStar)2 structures._PerformanceStat (structures._PerformanceStat)2 FileNotFoundException (java.io.FileNotFoundException)1 HashSet (java.util.HashSet)1 structures._thetaStar (structures._thetaStar)1