Search in sources :

Example 36 with structures._Review

use of structures._Review in project IR_Base by Linda-sunshine.

the class asyncCoRegLR method train.

// this is online training in each individual user
@Override
public double train() {
    double gNorm, gNormOld = Double.MAX_VALUE;
    int updateCount = 0;
    int predL, trueL;
    _Review doc;
    _PerformanceStat perfStat;
    _CoRegLRAdaptStruct user;
    initLBFGS();
    init();
    for (int t = 0; t < m_userOrder.length; t++) {
        user = (_CoRegLRAdaptStruct) m_userList.get(m_userOrder[t]);
        if (user.hasNextAdaptationIns()) {
            // test the latest model
            if (m_testmode != TestMode.TM_batch && (doc = user.getLatestTestIns()) != null) {
                perfStat = user.getPerfStat();
                predL = predict(doc, user);
                trueL = doc.getYLabel();
                perfStat.addOnePredResult(predL, trueL);
            }
            // in batch mode we will not accumulate the performance during adaptation
            // prepare to adapt: initialize gradient
            Arrays.fill(m_g, 0);
            calculateGradients(user);
            gNorm = gradientTest(user);
            if (m_displayLv == 1) {
                if (gNorm < gNormOld)
                    System.out.print("o");
                else
                    System.out.print("x");
            }
            // gradient descent
            gradientDescent(user, m_initStepSize, 1.0);
            gNormOld = gNorm;
            if (m_displayLv > 0 && ++updateCount % 100 == 0)
                System.out.println();
        }
    }
    if (m_displayLv > 0)
        System.out.println();
    setPersonalizedModel();
    // we do not evaluate function value
    return 0;
}
Also used : structures._Review(structures._Review) structures._PerformanceStat(structures._PerformanceStat)

Example 37 with structures._Review

use of structures._Review in project IR_Base by Linda-sunshine.

the class asyncMTRegLR method train.

@Override
public double train() {
    double gNorm, gNormOld = Double.MAX_VALUE;
    int predL, trueL;
    _Review doc;
    _AdaptStruct user;
    _PerformanceStat perfStat;
    double val;
    initLBFGS();
    init();
    try {
        m_writer = new PrintWriter(new File(String.format("train_online_MTRegLR.txt")));
        for (int i = 0; i < m_userList.size(); i++) {
            user = m_userList.get(i);
            while (user.hasNextAdaptationIns()) {
                // test the latest model before model adaptation
                if (m_testmode != TestMode.TM_batch && (doc = user.getLatestTestIns()) != null) {
                    perfStat = user.getPerfStat();
                    val = logit(doc.getSparse(), user);
                    predL = predict(doc, user);
                    trueL = doc.getYLabel();
                    perfStat.addOnePredResult(predL, trueL);
                    m_writer.format("%s\t%d\t%.4f\t%d\t%d\n", user.getUserID(), doc.getID(), val, predL, trueL);
                }
                // in batch mode we will not accumulate the performance during adaptation
                gradientDescent(user, m_initStepSize, 1.0);
                // test the gradient only when we want to debug
                if (m_displayLv > 0) {
                    gNorm = gradientTest();
                    if (m_displayLv == 1) {
                        if (gNorm < gNormOld)
                            System.out.print("o");
                        else
                            System.out.print("x");
                    }
                    gNormOld = gNorm;
                }
            }
            m_writer.flush();
            if (m_displayLv == 1)
                System.out.println();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    setPersonalizedModel();
    // we do not evaluate function value
    return 0;
}
Also used : structures._Review(structures._Review) Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) IOException(java.io.IOException) File(java.io.File) structures._PerformanceStat(structures._PerformanceStat) PrintWriter(java.io.PrintWriter)

Example 38 with structures._Review

use of structures._Review in project IR_Base by Linda-sunshine.

the class MultiThreadedLMAnalyzer method hasCoPurchase.

// Check if two users have the co-purchase
protected boolean hasCoPurchase(_User ui, _User uj) {
    int count = 0;
    HashSet<String> item_i = new HashSet<String>();
    for (_Review r : ui.getReviews()) {
        item_i.add(r.getItemID());
    }
    for (_Review r : uj.getReviews()) {
        if (item_i.contains(r.getItemID())) {
            count++;
            if (count == 1)
                return true;
        }
    }
    return false;
}
Also used : structures._Review(structures._Review) HashSet(java.util.HashSet)

Example 39 with structures._Review

use of structures._Review in project IR_Base by Linda-sunshine.

the class MultiThreadedUserAnalyzer method separateUsers.

// assign some of the users for testing only.
public void separateUsers(int k) {
    int count = 0;
    double light = 0, medium = 0;
    while (count < k) {
        if (m_users.get(count).getReviewSize() <= 10)
            light++;
        else if (m_users.get(count).getReviewSize() <= 50)
            medium++;
        for (_Review r : m_users.get(count++).getReviews()) {
            if (r.getType() == rType.ADAPTATION) {
                m_adaptSize--;
                r.setType(rType.TEST);
                m_testSize++;
                if (r.getYLabel() == 1) {
                    m_pCount[1]--;
                    m_pCount[2]++;
                }
            }
        }
    }
    System.out.print(String.format("[Prob Info]Light: %.4f, medium: %.4f, heavy: %.4f\n", light / k, medium / k, (k - light - medium) / k));
}
Also used : structures._Review(structures._Review)

Example 40 with structures._Review

use of structures._Review in project IR_Base by Linda-sunshine.

the class MultiThreadedUserAnalyzer method loadUser.

// Load one file as a user here.
private void loadUser(String filename, int core) {
    try {
        File file = new File(filename);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        String line;
        // UserId is contained in the filename.
        String userID = extractUserID(file.getName());
        // Skip the first line since it is user name.
        reader.readLine();
        String productID, source, category = "";
        ArrayList<_Review> reviews = new ArrayList<_Review>();
        _Review review;
        int ylabel;
        long timestamp = 0;
        while ((line = reader.readLine()) != null) {
            productID = line;
            synchronized (m_allocReviewLock) {
                if (map.containsKey(productID))
                    map.put(productID, map.get(productID) + 1);
                else
                    map.put(productID, 1);
            }
            // review content
            source = reader.readLine();
            // review category
            category = reader.readLine();
            ylabel = Integer.valueOf(reader.readLine());
            timestamp = Long.valueOf(reader.readLine());
            // Construct the new review.
            if (ylabel != 3) {
                ylabel = (ylabel >= 4) ? 1 : 0;
                review = new _Review(m_corpus.getCollection().size(), source, ylabel, userID, productID, category, timestamp);
                if (AnalyzeDoc(review, core)) {
                    // Create the sparse vector for the review.
                    reviews.add(review);
                }
            }
        }
        if (reviews.size() > 1) {
            // at least one for adaptation and one for testing
            synchronized (m_allocReviewLock) {
                allocateReviews(reviews);
                // create new user from the file.
                m_users.add(new _User(userID, m_classNo, reviews));
            }
        } else if (reviews.size() == 1) {
            // added by Lin, for those users with fewer than 2 reviews, ignore them.
            review = reviews.get(0);
            synchronized (m_rollbackLock) {
                rollBack(Utils.revertSpVct(review.getSparse()), review.getYLabel());
            }
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) structures._Review(structures._Review) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) structures._User(structures._User) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

structures._Review (structures._Review)44 structures._SparseFeature (structures._SparseFeature)24 structures._HDPThetaStar (structures._HDPThetaStar)9 ArrayList (java.util.ArrayList)8 Feature (Classifier.supervised.liblinear.Feature)6 Classifier.supervised.modelAdaptation._AdaptStruct (Classifier.supervised.modelAdaptation._AdaptStruct)6 structures._PerformanceStat (structures._PerformanceStat)6 IOException (java.io.IOException)5 File (java.io.File)4 structures._User (structures._User)4 FeatureNode (Classifier.supervised.liblinear.FeatureNode)3 Parameter (Classifier.supervised.liblinear.Parameter)3 Problem (Classifier.supervised.liblinear.Problem)3 structures._RankItem (structures._RankItem)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 PrintWriter (java.io.PrintWriter)2 MyPriorityQueue (structures.MyPriorityQueue)2 SolverType (Classifier.supervised.liblinear.SolverType)1