Search in sources :

Example 11 with Classifier.supervised.modelAdaptation._AdaptStruct

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

the class asyncRegLR method train.

// this is online training in each individual user
@Override
public double train() {
    double gNorm, gNormOld = Double.MAX_VALUE;
    ;
    int predL, trueL;
    _Review doc;
    _PerformanceStat perfStat;
    initLBFGS();
    init();
    for (_AdaptStruct user : m_userList) {
        while (user.hasNextAdaptationIns()) {
            // test the latest model before model adaptation
            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();
            if (m_displayLv == 1) {
                if (gNorm < gNormOld)
                    System.out.print("o");
                else
                    System.out.print("x");
            }
            // gradient descent
            gradientDescent(user, m_initStepSize, m_g);
            gNormOld = gNorm;
        }
        if (m_displayLv > 0)
            System.out.println();
    }
    setPersonalizedModel();
    // we do not evaluate function value
    return 0;
}
Also used : structures._Review(structures._Review) Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) structures._PerformanceStat(structures._PerformanceStat)

Example 12 with Classifier.supervised.modelAdaptation._AdaptStruct

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

the class IndividualSVM method loadUsers.

@Override
public void loadUsers(ArrayList<_User> userList) {
    m_userList = new ArrayList<_AdaptStruct>();
    for (_User user : userList) // new _AdaptStruct(user, Integer.valueOf(user.getUserID()))
    m_userList.add(new _AdaptStruct(user));
    m_pWeights = new double[m_featureSize + 1];
}
Also used : Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) structures._User(structures._User)

Example 13 with Classifier.supervised.modelAdaptation._AdaptStruct

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

the class IndividualSVM method train.

@Override
public double train() {
    init();
    // Transfer all user reviews to instances recognized by SVM, indexed by users.
    int trainSize = 0, validUserIndex = 0;
    ArrayList<Feature[]> fvs = new ArrayList<Feature[]>();
    ArrayList<Double> ys = new ArrayList<Double>();
    // Two for loop to access the reviews, indexed by users.
    ArrayList<_Review> reviews;
    for (_AdaptStruct user : m_supFlag ? m_supUserList : m_userList) {
        trainSize = 0;
        reviews = user.getReviews();
        boolean validUser = false;
        for (_Review r : reviews) {
            if (r.getType() == rType.ADAPTATION) {
                // we will only use the adaptation data for this purpose
                fvs.add(createLibLinearFV(r, validUserIndex));
                ys.add(new Double(r.getYLabel()));
                trainSize++;
                validUser = true;
            }
        }
        if (validUser)
            validUserIndex++;
        // Train individual model for each user.
        Problem libProblem = new Problem();
        libProblem.l = trainSize;
        libProblem.x = new Feature[trainSize][];
        libProblem.y = new double[trainSize];
        for (int i = 0; i < trainSize; i++) {
            libProblem.x[i] = fvs.get(i);
            libProblem.y[i] = ys.get(i);
        }
        if (m_bias) {
            // including bias term; global model + user models
            libProblem.n = m_featureSize + 1;
            // bias term in liblinear.
            libProblem.bias = 1;
        } else {
            libProblem.n = m_featureSize;
            // no bias term in liblinear.
            libProblem.bias = -1;
        }
        m_libModel = Linear.train(libProblem, new Parameter(m_solverType, m_C, SVM.EPS));
        // Set users in the same cluster.
        if (m_supFlag)
            setPersonalizedModelInCluster(user.getUser().getClusterIndex());
        else
            setPersonalizedModel(user);
    }
    return 0;
}
Also used : structures._Review(structures._Review) Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) ArrayList(java.util.ArrayList) Parameter(Classifier.supervised.liblinear.Parameter) Problem(Classifier.supervised.liblinear.Problem) Feature(Classifier.supervised.liblinear.Feature) structures._SparseFeature(structures._SparseFeature)

Example 14 with Classifier.supervised.modelAdaptation._AdaptStruct

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

the class CoLinAdapt method constructUserList.

void constructUserList(ArrayList<_User> userList) {
    int vSize = 2 * m_dim;
    // step 1: create space
    m_userList = new ArrayList<_AdaptStruct>();
    for (int i = 0; i < userList.size(); i++) {
        _User user = userList.get(i);
        m_userList.add(new _CoLinAdaptStruct(user, m_dim, i, m_topK));
    }
    m_pWeights = new double[m_gWeights.length];
    // huge space consumption
    _CoLinAdaptStruct.sharedA = new double[getVSize()];
    // step 2: copy each user's A to shared A in _CoLinAdaptStruct
    _CoLinAdaptStruct user;
    for (int i = 0; i < m_userList.size(); i++) {
        user = (_CoLinAdaptStruct) m_userList.get(i);
        System.arraycopy(user.m_A, 0, _CoLinAdaptStruct.sharedA, vSize * i, vSize);
    }
}
Also used : Classifier.supervised.modelAdaptation._AdaptStruct(Classifier.supervised.modelAdaptation._AdaptStruct) structures._User(structures._User)

Example 15 with Classifier.supervised.modelAdaptation._AdaptStruct

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

the class CLRWithDP method loadUsers.

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

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